forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf2htmlEX.js
More file actions
330 lines (285 loc) · 8.82 KB
/
Copy pathpdf2htmlEX.js
File metadata and controls
330 lines (285 loc) · 8.82 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* vim modeline copied from pdf.js */
/*
* pdf2htmlEX.js
*
* handles UI events/actions/effects
* Copyright 2012 Lu Wang <coolwanglu@gmail.com>
*/
var pdf2htmlEX = (function(){
var pdf2htmlEX = new Object();
var EPS = 1e-6;
var invert = function(ctm) {
var det = ctm[0] * ctm[3] - ctm[1] * ctm[2];
var ictm = new Array();
ictm[0] = ctm[3] / det;
ictm[1] = -ctm[1] / det;
ictm[2] = -ctm[2] / det;
ictm[3] = ctm[0] / det;
ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) / det;
ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) / det;
return ictm;
};
var transform = function(ctm, pos) {
return [ctm[0] * pos[0] + ctm[2] * pos[1] + ctm[4]
,ctm[1] * pos[0] + ctm[3] * pos[1] + ctm[5]];
};
var Page = function(page, container) {
if(page == undefined) return;
this.p = $(page);
this.n = parseInt(this.p.attr('data-page-no'), 16);
this.b = $('.b', this.p);
/*
* scale ratios
*
* default_r : the first one
* set_r : last set
* cur_r : currently using
*/
this.default_r = this.set_r = this.cur_r = this.p.height() / this.b.height();
this.data = JSON.parse($($('.j', this.p)[0]).attr('data-data'));
this.ctm = this.data.ctm;
this.ictm = invert(this.ctm);
this.container = container;
};
$.extend(Page.prototype, {
hide : function(){
this.b.hide();
},
show : function(){
if(Math.abs(this.set_r - this.cur_r) > EPS) {
this.cur_r = this.set_r;
this.b.css('transform', 'scale('+this.cur_r.toFixed(3)+')');
}
this.b.show();
},
rescale : function(ratio, is_relative) {
if(ratio == 0) {
this.set_r = this.default_r;
} else if (is_relative) {
this.set_r *= ratio;
} else {
this.set_r = ratio;
}
/* wait for redraw */
this.hide();
this.p.height(this.b.height() * this.set_r);
this.p.width(this.b.width() * this.set_r);
},
is_visible : function() {
var off = this.position();
return !((off[1] > this.height()) || (off[1] + this.container.height() < 0));
},
/* return the coordinate of the top-left corner of container
* in our cooridnate system
*/
position : function () {
var off = this.p.offset();
var off_c = this.container.offset();
return [off_c.left-off.left, off_c.top-off.top];
},
height : function() {
return this.p.height();
}
});
pdf2htmlEX.Viewer = function(container_id, outline_id) {
this.container_id = container_id;
this.outline_id = outline_id;
this.init_before_loading_content();
var _ = this;
$(function(){_.init_after_loading_content();});
};
$.extend(pdf2htmlEX.Viewer.prototype, {
/* Constants */
render_timeout : 130,
scale_step : 0.9,
init_before_loading_content : function() {
/*hide all pages before loading, will reveal only visible ones later */
this.pre_hide_pages();
},
init_after_loading_content : function() {
this.outline = $('#'+this.outline_id);
this.container = $('#'+this.container_id);
// need a better design
if(this.outline.children().length > 0) {
this.outline.addClass('opened');
}
var new_pages = new Array();
var pl= $('.p', this.container);
/* don't use for(..in..) */
for(var i = 0, l = pl.length; i < l; ++i) {
var p = new Page(pl[i], this.container);
new_pages[p.n] = p;
}
this.pages = new_pages;
var _ = this;
this.container.scroll(function(){ _.schedule_render(); });
//this.zoom_fixer();
// used by outline/annot_link etc
// note that one is for the class 'a' and the other is for the tag 'a'
this.container.on('click', '.a', this, this.link_handler);
this.outline.on('click', 'a', this, this.link_handler);
this.render();
},
pre_hide_pages : function() {
/* pages might have not been loaded yet, so add a CSS rule */
var s = '.b{display:none;}';
var n = document.createElement('style');
n.type = 'text/css';
if (n.styleSheet) {
n.styleSheet.cssText = s;
} else {
n.appendChild(document.createTextNode(s));
}
document.getElementsByTagName('head')[0].appendChild(n);
},
hide_pages : function() {
var pl = this.pages;
for(var i in pl)
pl[i].hide();
},
render : function () {
/* hide (positional) invisible pages */
var pl = this.pages;
for(var i in pl) {
var p = pl[i];
if(p.is_visible()){
p.show();
} else {
p.hide();
}
}
},
schedule_render : function() {
if(this.render_timer)
clearTimeout(this.render_timer);
var _ = this;
this.render_timer = setTimeout(function () {
_.render();
}, this.render_timeout);
},
zoom_fixer : function () {
/*
* When user try to zoom in/out using ctrl + +/- or mouse wheel
* handle this and prevent the default behaviours
*
* Code credit to PDF.js
*/
var _ = this;
// Firefox specific event, so that we can prevent browser from zooming
$(window).on('DOMMouseScroll', function(e) {
if (e.ctrlKey) {
e.preventDefault();
_.rescale(Math.pow(_.scale_step, e.detail), true);
}
});
$(window).on('keydown', function keydown(e) {
if (e.ctrlKey || e.metaKey) {
switch (e.keyCode) {
case 61: // FF/Mac '='
case 107: // FF '+' and '='
case 187: // Chrome '+'
_.rescale(1.0 / _.scale_step, true);
break;
case 173: // FF/Mac '-'
case 109: // FF '-'
case 189: // Chrome '-'
_.rescale(_.scale_step, true);
break;
case 48: // '0'
_.rescale(0, false);
break;
default:
return;
}
e.preventDefault();
}
});
},
rescale : function (ratio, is_relative) {
var pl = this.pages;
for(var i in pl) {
pl[i].rescale(ratio, is_relative);
}
this.schedule_render();
},
get_containing_page : function(obj) {
/* get the page obj containing obj */
var p = obj.closest('.p')[0];
return p && this.pages[(new Page(p)).n];
},
link_handler : function (e) {
var _ = e.data;
var t = $(e.currentTarget);
var cur_pos = [0,0];
// cur_page might be undefined, e.g. from Outline
var cur_page = _.get_containing_page(t);
if(cur_page != undefined)
{
cur_pos = cur_page.position();
//get the coordinates in default user system
cur_pos = transform(cur_page.ictm, [cur_pos[0], cur_page.height()-cur_pos[1]]);
}
var detail_str = t.attr('data-dest-detail');
if(detail_str == undefined) return;
var ok = false;
var detail = JSON.parse(detail_str);
var target_page = _.pages[detail[0]];
if(target_page == undefined) return;
var pos = [0,0];
var upside_down = true;
// TODO: zoom
// TODO: BBox
switch(detail[1]) {
case 'XYZ':
pos = [(detail[2] == null) ? cur_pos[0] : detail[2]
,(detail[3] == null) ? cur_pos[1] : detail[3]];
ok = true;
break;
case 'Fit':
case 'FitB':
pos = [0,0];
ok = true;
break;
case 'FitH':
case 'FitBH':
pos = [0, (detail[2] == null) ? cur_pos[1] : detail[2]]
ok = true;
break;
case 'FitV':
case 'FitBV':
pos = [(detail[2] == null) ? cur_pos[0] : detail[2], 0];
ok = true;
break;
case 'FitR':
/* locate the top-left corner of the rectangle */
pos = [detail[2], detail[5]];
upside_down = false;
ok = true;
break;
default:
ok = false;
break;
}
if(ok) {
pos = transform(target_page.ctm, pos);
if(upside_down) {
pos[1] = target_page.height() - pos[1];
}
_.scroll_to(detail[0], pos);
e.preventDefault();
}
},
/* pos=[x,y], where (0,0) is the top-left corner */
scroll_to : function(pageno, pos) {
var target_page = this.pages[pageno];
if(target_page == undefined) return;
var cur_target_pos = target_page.position();
this.container.scrollLeft(this.container.scrollLeft()-cur_target_pos[0]+pos[0]);
this.container.scrollTop(this.container.scrollTop()-cur_target_pos[1]+pos[1]);
},
__last_member__ : 'no comma' /*,*/
});
return pdf2htmlEX;
})();