2012-09-22 14:47:44 +00:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2013-02-05 17:05:58 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-09-22 14:47:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pdf2htmlEX.js
|
|
|
|
*
|
|
|
|
* handles UI events/actions/effects
|
2012-09-25 13:56:48 +00:00
|
|
|
|
2012-09-22 14:47:44 +00:00
|
|
|
* Copyright 2012 Lu Wang <coolwanglu@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
var pdf2htmlEX = (function(){
|
2013-02-27 18:11:34 +00:00
|
|
|
var CSS_CLASS_NAMES = {
|
|
|
|
page_frame : '@CSS_PAGE_FRAME_CN@',
|
|
|
|
page_decoration : '@CSS_PAGE_DECORATION_CN@',
|
|
|
|
page_content_box : '@CSS_PAGE_CONTENT_BOX_CN@',
|
|
|
|
page_data : '@CSS_PAGE_DATA_CN@',
|
2013-03-01 06:02:11 +00:00
|
|
|
background_image : '@CSS_BACKGROUND_IMAGE_CN@',
|
2013-02-27 18:52:00 +00:00
|
|
|
link : '@CSS_LINK_CN@',
|
2013-02-27 18:11:34 +00:00
|
|
|
__dummy__ : 'no comma'
|
|
|
|
};
|
|
|
|
|
2012-09-25 16:27:58 +00:00
|
|
|
var pdf2htmlEX = new Object();
|
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
var EPS = 1e-6;
|
2012-09-25 11:29:59 +00:00
|
|
|
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]];
|
|
|
|
};
|
2012-09-25 13:56:48 +00:00
|
|
|
var Page = function(page, container) {
|
2013-01-28 15:58:00 +00:00
|
|
|
if(page == undefined) return;
|
2012-09-25 11:29:59 +00:00
|
|
|
|
|
|
|
this.p = $(page);
|
|
|
|
this.n = parseInt(this.p.attr('data-page-no'), 16);
|
2013-02-27 18:17:33 +00:00
|
|
|
this.b = $('.'+CSS_CLASS_NAMES['page_content_box'], this.p);
|
2012-09-25 11:29:59 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
/*
|
|
|
|
* scale ratios
|
|
|
|
*
|
|
|
|
* default_r : the first one
|
|
|
|
* set_r : last set
|
|
|
|
* cur_r : currently using
|
|
|
|
*/
|
2012-09-25 11:29:59 +00:00
|
|
|
this.default_r = this.set_r = this.cur_r = this.p.height() / this.b.height();
|
|
|
|
|
2013-02-27 18:11:34 +00:00
|
|
|
this.data = JSON.parse($($('.'+CSS_CLASS_NAMES['page_data'], this.p)[0]).attr('data-data'));
|
2012-09-25 11:29:59 +00:00
|
|
|
|
|
|
|
this.ctm = this.data.ctm;
|
|
|
|
this.ictm = invert(this.ctm);
|
2012-09-25 13:56:48 +00:00
|
|
|
this.container = container;
|
2012-09-23 09:26:12 +00:00
|
|
|
};
|
2012-09-25 16:27:58 +00:00
|
|
|
$.extend(Page.prototype, {
|
2013-02-06 11:52:34 +00:00
|
|
|
/* hide & show are for contents, the page frame is still there */
|
2012-09-25 16:27:58 +00:00
|
|
|
hide : function(){
|
2013-02-05 17:05:58 +00:00
|
|
|
this.b.removeClass('opened');
|
2012-09-25 16:27:58 +00:00
|
|
|
},
|
|
|
|
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)+')');
|
|
|
|
}
|
2013-02-05 17:05:58 +00:00
|
|
|
this.b.addClass('opened');
|
2012-09-25 16:27:58 +00:00
|
|
|
},
|
|
|
|
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;
|
|
|
|
}
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-25 16:27:58 +00:00
|
|
|
/* wait for redraw */
|
|
|
|
this.hide();
|
2012-09-25 13:56:48 +00:00
|
|
|
|
2012-09-25 16:27:58 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2013-01-28 14:11:42 +00:00
|
|
|
pdf2htmlEX.Viewer = function(container_id, outline_id) {
|
2012-09-25 14:40:21 +00:00
|
|
|
this.container_id = container_id;
|
2013-01-28 14:11:42 +00:00
|
|
|
this.outline_id = outline_id;
|
2012-09-25 14:40:21 +00:00
|
|
|
this.init_before_loading_content();
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-25 14:40:21 +00:00
|
|
|
var _ = this;
|
|
|
|
$(function(){_.init_after_loading_content();});
|
|
|
|
};
|
|
|
|
|
2012-09-25 16:27:58 +00:00
|
|
|
$.extend(pdf2htmlEX.Viewer.prototype, {
|
2012-09-23 09:26:12 +00:00
|
|
|
/* Constants */
|
2012-09-25 16:09:15 +00:00
|
|
|
render_timeout : 130,
|
2012-09-23 09:26:12 +00:00
|
|
|
scale_step : 0.9,
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
init_before_loading_content : function() {
|
|
|
|
/*hide all pages before loading, will reveal only visible ones later */
|
|
|
|
this.pre_hide_pages();
|
|
|
|
},
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
init_after_loading_content : function() {
|
2013-01-28 16:38:07 +00:00
|
|
|
this.outline = $('#'+this.outline_id);
|
2012-09-25 14:24:36 +00:00
|
|
|
this.container = $('#'+this.container_id);
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2013-03-01 06:02:11 +00:00
|
|
|
// Open the outline if nonempty
|
2013-01-29 14:23:59 +00:00
|
|
|
if(this.outline.children().length > 0) {
|
|
|
|
this.outline.addClass('opened');
|
2013-01-28 14:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 06:02:11 +00:00
|
|
|
// collect pages
|
2012-09-23 09:26:12 +00:00
|
|
|
var new_pages = new Array();
|
2013-02-27 18:11:34 +00:00
|
|
|
var pl= $('.'+CSS_CLASS_NAMES['page_frame'], this.container);
|
2012-09-25 11:29:59 +00:00
|
|
|
/* don't use for(..in..) */
|
2012-09-23 09:26:12 +00:00
|
|
|
for(var i = 0, l = pl.length; i < l; ++i) {
|
2012-09-25 13:56:48 +00:00
|
|
|
var p = new Page(pl[i], this.container);
|
2012-09-25 11:29:59 +00:00
|
|
|
new_pages[p.n] = p;
|
2012-09-23 09:26:12 +00:00
|
|
|
}
|
|
|
|
this.pages = new_pages;
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2013-03-01 06:02:11 +00:00
|
|
|
// register schedule rendering
|
2012-09-23 09:26:12 +00:00
|
|
|
var _ = this;
|
|
|
|
this.container.scroll(function(){ _.schedule_render(); });
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
//this.zoom_fixer();
|
2012-09-25 11:29:59 +00:00
|
|
|
|
2013-03-01 06:02:11 +00:00
|
|
|
// handle links
|
2013-02-27 18:52:00 +00:00
|
|
|
this.container.add(this.outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler);
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2013-03-01 06:02:11 +00:00
|
|
|
// disable background image draging
|
|
|
|
$('.'+CSS_CLASS_NAMES['background_image'], this.container).on('dragstart', function(e){return false;});
|
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
pre_hide_pages : function() {
|
|
|
|
/* pages might have not been loaded yet, so add a CSS rule */
|
2013-02-27 18:11:34 +00:00
|
|
|
var s = '@media screen{.'+CSS_CLASS_NAMES['page_content_box']+'{display:none;}}';
|
2012-09-25 11:29:59 +00:00
|
|
|
var n = document.createElement('style');
|
|
|
|
n.type = 'text/css';
|
2012-09-23 09:26:12 +00:00
|
|
|
if (n.styleSheet) {
|
|
|
|
n.styleSheet.cssText = s;
|
|
|
|
} else {
|
|
|
|
n.appendChild(document.createTextNode(s));
|
|
|
|
}
|
2012-09-25 11:29:59 +00:00
|
|
|
document.getElementsByTagName('head')[0].appendChild(n);
|
2012-09-23 09:26:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hide_pages : function() {
|
|
|
|
var pl = this.pages;
|
2012-09-25 11:29:59 +00:00
|
|
|
for(var i in pl)
|
2012-09-23 09:26:12 +00:00
|
|
|
pl[i].hide();
|
|
|
|
},
|
|
|
|
|
|
|
|
render : function () {
|
|
|
|
/* hide (positional) invisible pages */
|
|
|
|
var pl = this.pages;
|
2012-09-25 11:29:59 +00:00
|
|
|
for(var i in pl) {
|
2012-09-23 09:26:12 +00:00
|
|
|
var p = pl[i];
|
2012-09-25 13:56:48 +00:00
|
|
|
if(p.is_visible()){
|
2012-09-25 11:29:59 +00:00
|
|
|
p.show();
|
2012-09-25 13:56:48 +00:00
|
|
|
} else {
|
2012-09-25 11:29:59 +00:00
|
|
|
p.hide();
|
2012-09-25 13:56:48 +00:00
|
|
|
}
|
2012-09-22 14:47:44 +00:00
|
|
|
}
|
2012-09-23 09:26:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-09-24 12:20:34 +00:00
|
|
|
e.preventDefault();
|
2012-09-22 16:17:30 +00:00
|
|
|
}
|
2012-09-23 09:26:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
rescale : function (ratio, is_relative) {
|
|
|
|
var pl = this.pages;
|
2012-09-25 11:29:59 +00:00
|
|
|
for(var i in pl) {
|
2012-09-23 09:26:12 +00:00
|
|
|
pl[i].rescale(ratio, is_relative);
|
2012-09-23 05:04:29 +00:00
|
|
|
}
|
2012-09-22 16:17:30 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
this.schedule_render();
|
|
|
|
},
|
2012-09-22 16:17:30 +00:00
|
|
|
|
2012-09-25 11:29:59 +00:00
|
|
|
get_containing_page : function(obj) {
|
|
|
|
/* get the page obj containing obj */
|
2013-02-27 18:11:34 +00:00
|
|
|
var p = obj.closest('.'+CSS_CLASS_NAMES['page_frame'])[0];
|
2013-01-28 16:38:07 +00:00
|
|
|
return p && this.pages[(new Page(p)).n];
|
2012-09-25 11:29:59 +00:00
|
|
|
},
|
|
|
|
|
2013-01-28 13:01:02 +00:00
|
|
|
link_handler : function (e) {
|
2013-02-27 18:52:00 +00:00
|
|
|
console.log('here');
|
2012-09-25 11:29:59 +00:00
|
|
|
var _ = e.data;
|
|
|
|
var t = $(e.currentTarget);
|
2012-09-25 13:56:48 +00:00
|
|
|
|
2013-01-28 15:58:00 +00:00
|
|
|
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]]);
|
|
|
|
}
|
2012-09-25 11:29:59 +00:00
|
|
|
|
|
|
|
var detail_str = t.attr('data-dest-detail');
|
|
|
|
if(detail_str == undefined) return;
|
|
|
|
|
|
|
|
var ok = false;
|
2012-09-26 08:00:55 +00:00
|
|
|
var detail = JSON.parse(detail_str);
|
2012-09-25 11:29:59 +00:00
|
|
|
|
2012-09-26 08:00:55 +00:00
|
|
|
var target_page = _.pages[detail[0]];
|
|
|
|
if(target_page == undefined) return;
|
2012-09-25 13:56:48 +00:00
|
|
|
|
2012-09-26 08:00:55 +00:00
|
|
|
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;
|
2012-09-25 11:29:59 +00:00
|
|
|
default:
|
2012-09-26 08:00:55 +00:00
|
|
|
ok = false;
|
2012-09-25 11:29:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-26 08:00:55 +00:00
|
|
|
if(ok) {
|
|
|
|
pos = transform(target_page.ctm, pos);
|
|
|
|
if(upside_down) {
|
|
|
|
pos[1] = target_page.height() - pos[1];
|
|
|
|
}
|
|
|
|
_.scroll_to(detail[0], pos);
|
2012-09-25 11:29:59 +00:00
|
|
|
e.preventDefault();
|
2012-09-26 08:00:55 +00:00
|
|
|
}
|
2012-09-25 14:24:36 +00:00
|
|
|
},
|
|
|
|
|
2012-09-26 08:00:55 +00:00
|
|
|
/* pos=[x,y], where (0,0) is the top-left corner */
|
2012-09-25 14:24:36 +00:00
|
|
|
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]);
|
2012-09-26 08:00:55 +00:00
|
|
|
this.container.scrollTop(this.container.scrollTop()-cur_target_pos[1]+pos[1]);
|
2012-09-25 14:24:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
__last_member__ : 'no comma' /*,*/
|
2012-09-25 14:40:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return pdf2htmlEX;
|
2012-09-23 09:26:12 +00:00
|
|
|
})();
|