-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.html
308 lines (252 loc) · 12.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="book.css" />
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 1024px) and (orientation:portrait)" href="portrait.css" />
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 1024px) and (orientation: landscape)" href="landscape.css" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=930, user-scalable=no" />
<script type="text/javascript" src="js/inflate.js"></script>
<script type="text/javascript" src="js/sax.js"></script>
<script type="text/javascript" src="js/zip.js"></script>
<script type="text/javascript" src="js/epub.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/imageSize.js"></script>
<script type="text/javascript" src="js/paginator.js"></script>
<script type="text/javascript" src="js/section.js"></script>
<script type="text/javascript" src="js/pageHandler.js"></script>
<!-- Hyphenation. -->
<script src="js/Hyphenator.js" type="text/javascript"></script>
<script src="js/patterns/en.js" type="text/javascript"></script>
<script src="js/swipe.js"></script>
<script src="js/pageRenderer.js"></script>
<script type="text/javascript">
function loadTheBook () {
var epuburl = document.getElementById('epuburl').value;
loadBook(epuburl);
}
function setPageSize(pageRatio) {
if (!pageRatio) pageRatio = Math.sqrt(Math.E);
var width = (window.innerWidth - 20) / 2;
var height = window.innerHeight - 20;
var pageHeight = height,
pageWidth = width;
if (height / pageRatio > width) {
// We'll need to constrain the height.
pageHeight = width * pageRatio;
/* } else if (height / pageRatio > width * 1.5) {
// We have way more height than width. Go to a one-page view.
pageHeight = width * pageRatio; */
} else {
// Constrain the width, 2-page.
pageWidth = height / pageRatio;
}
console.log("Width/Height: " + width + "/" + height + ", pageWidth/pageHeight: " + pageWidth + "/" + pageHeight);
var container = document.getElementById('container');
container.style.width = pageWidth * 2;
container.style.height = pageHeight;
var background = document.getElementById('backgroundDiv');
background.style.width = pageWidth * 2;
background.style.height = pageHeight;
var canvas = document.getElementById('background');
canvas.width = pageWidth * 2;
canvas.height = pageHeight;
var pages = document.getElementsByClassName('page');
for (var i = 0, l = pages.length; i < l; i++) {
pages[i].style.width = pageWidth - Math.log(22) * 8;
pages[i].style.height = pageHeight - Math.log(22) * 10;
pages[i].style.marginTop = '0px';
var header = document.getElementsByClassName('header')[0];
var headerHeight = 24 * 2; // hard coded for now, until we properly size the header...
var contents = document.getElementsByClassName('content');
contents[0].style.height = pageHeight - (Math.log(22) * 10) - headerHeight;
}
// This needs tweaking, because it should actually be done once we know
// which font will be used as the body font for the book. This is a reasonable
// approximation, though, and will do for now.
var sizerElement = document.createElement('p');
// Rough statistical relevancy of letters [in english]. This will give us a more-accurate approximation of average line length.
var letters = "aaaaaaaabbcccddddeeeeeeeeeeeeeffgghhhhhhiiiiiiijkllllmmnnnnnnnooooooooppqrrrrrrsssssstttttttttuuuvwxyyz";
sizerElement.textContent = letters;
sizerElement.style.left = '0';
sizerElement.style.top = '0';
sizerElement.style.position = 'absolute';
sizerElement.style.margin = '0';
sizerElement.style.padding = '0';
document.body.appendChild(sizerElement);
// Our ideal line width is 66 characters.
console.log('offsetwidth: ' + sizerElement.offsetWidth);
console.log('letters.length: ' + letters.length);
var idealLineWidth = (sizerElement.offsetWidth / letters.length) * 66;
var measuredFontSize = document.defaultView.getComputedStyle(sizerElement, null).getPropertyValue('font-size').toString().replace('px', '');
console.log('measured font size: ' + measuredFontSize);
// now that we have that, remove the sizer element.
document.body.removeChild(sizerElement);
var pageSize = pageWidth - Math.log(22) * 8
console.log('content width: ' + pageSize);
// multiply the actual fontSize by our sizing ratio
var pixelFontSize = (measuredFontSize * pageSize / idealLineWidth) + 'px';
console.log('pixelFontSize: ' + pixelFontSize);
// and apply that to the document body. All other sizes should be in ems.
document.body.style.fontSize = pixelFontSize;
}
var loadBook = function (epub) {
setPageSize();
selector.style.display = 'none';
container.style.display = 'block';
var epubUrl;
if (epub.substr(0, 4) == 'http') {
epubUrl = epub;
} else {
epubUrl = "epubs/" + epub;
}
ePub.open(epubUrl, function (book) {
var te = document.getElementById('book_title');
te.textContent = book.title;
var ae = document.getElementById('author_heading');
ae.textContent = book.author;
var lc = document.getElementById('leftcontent'),
rc = document.getElementById('rightcontent'),
ln = document.getElementsByClassName('left pagenum')[0],
rn = document.getElementsByClassName('right pagenum')[0];
var pages;
if (window.orientation == 0 || window.orientation == 180) {
pages = [lc];
} else {
pages = [lc, rc];
}
pageHandler = new PageHandler(book, pages, [ln, rn]);
var contents = document.getElementById('contents');
for (var i = 0, l = book.toc.length; i < l; i++) {
// Sometimes navpoints aren't all covered, or they are done so in weird ways.
// try to be liberal about things.
if (book.toc[i] === undefined) continue;
var chapter = document.createElement('a');
var secName = book.toc[i].fileName;
chapter.setAttribute('href', '#section=' + secName);
chapter.textContent = book.toc[i].title;
chapter.onclick = function (secName) {
return function () {
pageHandler.goToSection(secName);
pageHandler.display();
contents.style.display = 'none';
}
}(secName);
contents.appendChild(chapter);
}
pageHandler.display();
swipe(null, pageHandler);
function handleArrowKeys(evt) {
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt) {
switch (evt.keyCode) {
case 37:
pageHandler.prevPage();
break;
case 39:
pageHandler.nextPage();
break;
case 67:
document.getElementById('contents').style.display = 'block';
break;
}
}
}
document.onkeyup = handleArrowKeys;
// Set up an orientation handler
window.onorientationchange = function () {
if (window.orientation == 0 || window.orientation == 180) {
pageHandler.setPages([lc]);
} else {
pageHandler.setPages([lc,rc]);
}
pageHandler.display();
}
});
}
</script>
</head>
<body>
<div id="spinner">
<img src="loader.gif" alt="Loading" />
</div>
<div id="contents" style="display: none;">
</div>
<div id="selector">
<div style="text-align: center; font-family: Helvetica, sans; margin-top: 20px; background-color: rgba(255,255,255,0.3); line-height: 1.5em;">
<span style="color: #0000dd">r</span><span style="color: #4444ee">ePub</span><span style="color: #0000dd">lish</span> — Pure-JavaScript ePub Reader
</div>
<div id="selectorContainer">
<div class="selection" onclick="loadBook('piedpiper.epub');">
<img src="covers/Pied_piper.jpg" alt="The Pied Piper of Hamelin"/>
<br/>The Pied Piper of Hamelin</div>
<div class="selection" onclick="loadBook('warofworlds.epub');">
<img src="covers/WarOfWorlds.jpg" alt="The War of the Worlds" />
<br/>The War of the Worlds</div>
<div class="selection" onclick="loadBook('flatland.epub');">
<img src="covers/Flatland.jpg" alt="Flatland"/>
<br/>Flatland</div>
<div class="selection" onclick="loadBook('gulliverstravels.epub');">
<img src="covers/Gulliver.jpg" alt="Gulliver's Travels"/>
<br/>Gulliver's Travels</div>
<div class="selection" onclick="loadBook('portraitoftheartist.epub');">
<img src="covers/Joyce.jpg" alt="Portrait of the Artist as a Young Man"/>
<br/>Portrait of the Artist<br/> as a Young Man</div>
<div class="selection" onclick="loadBook('wizardofoz.epub');">
<img src="covers/WizardOfOz.jpg" alt="The Wonderful Wizard of Oz"/>
<br/>The Wonderful Wizard of Oz</div>
<div class="selection" onclick="loadBook('shelleypoetry.epub');">
<img src="covers/Shelley.jpg" alt="Shelley's Poetry"/>
<br/>Shelley's Poetry</div>
<div class="selection" onclick="loadBook('fanniefarmer.epub');">
<img src="covers/Fannie.jpg" alt="The Fannie Farmer Cookbook" />
<br/>The Fannie Farmer Cookbook</div>
<div class="selection" onclick="loadBook('warandpeace.epub');">
<img src="covers/warandpeace.jpg" alt="War and Peace" />
<br/>War and Peace</div>
<div class="selection" onclick="loadBook('dracula.epub');">
<img src="covers/Dracula.jpg" alt="Dracula" />
<br/>Dracula</div>
<div class="selection" onclick="loadBook('faunaboreali.epub');">
<img src="covers/Fauna.jpg" alt="Fauna Boreali" />
<br/>Fauna Boreali</div>
<div class="selection" onclick="loadBook('huckleberry.epub');">
<img src="covers/Huck.jpg" alt="Huckleberry Finn" />
<br/>The Adventures of Huckleberry Finn</div>
</div>
<div style="text-align: center; font-family: Helvetica, sans; margin-top: -5px; background-color: rgba(255,255,255,0.3); line-height: 1.5em;">
<a href="http://github.com/blaine/rePublish">Get the Code</a>
</div>
</div>
<div id="backgroundDiv">
<canvas id="background" width="952" height="686"></canvas>
</div>
<div id="container">
<div id="leftpage" class="page">
<div class="left header">
<span class="left pagenum"></span> ·
<h1 id="book_title"></h1>
</div>
<div id="leftcontent" class="left content"></div>
<div class="left footer">
<span class="left pagenum"></span> ·
<h1 id="chapter_heading"></h1>
</div>
</div>
<div id="rightpage" class="page landscape">
<div class="right header">
<h1 id="author_heading"></h1> ·
<span class="right pagenum"></span>
</div>
<div id="rightcontent" class="right content"></div>
<div class="right footer">
<h1></h1>·
<span class="right pagenum"></span>
</div>
</div>
</div>
<!-- <div>
<input id='epuburl' type="text" />
<input id='epuburlgo' type='button' value='load' onclick='loadTheBook();' />
</div> -->
</body>
</html>