1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 09:38:40 +00:00
This commit is contained in:
Lu Wang 2012-09-23 13:04:29 +08:00
parent 9ed1931caa
commit b6ac8eb8f0
2 changed files with 140 additions and 142 deletions

View File

@ -10,168 +10,166 @@
* Copyright 2012 Lu Wang <coolwanglu@gmail.com> * Copyright 2012 Lu Wang <coolwanglu@gmail.com>
*/ */
var pdf2htmlEX = (function(){ var pdf2htmlEX = {
return { pages : [],
pages : [], page_boxes : [],
page_boxes : [], container : null,
container : null, selective_render_timer : null,
selective_render_timer : null, default_scale_ratio : 1.0,
default_scale_ratio : 1.0, cur_scale_ratio : 1.0,
cur_scale_ratio : 1.0,
/* Constants */ /* Constants */
render_timeout : 200, render_timeout : 200,
scale_step : 0.9, scale_step : 0.9,
init_before_loading_content : function() { init_before_loading_content : function() {
/*hide all pages before loading, will reveal only visible ones later */ /*hide all pages before loading, will reveal only visible ones later */
this.pre_hide_pages(); this.pre_hide_pages();
}, },
init_after_loading_content : function() { init_after_loading_content : function() {
this.pages = $(".p"); this.pages = $(".p");
this.page_boxes = $(".b"); this.page_boxes = $(".b");
this.container = $("#pdf-main"); this.container = $("#pdf-main");
if(this.pages.length > 0) if(this.pages.length > 0)
{ {
this.default_scale_ratio = this.cur_scale_ratio this.default_scale_ratio = this.cur_scale_ratio
= $(this.pages[0]).height() / $(this.page_boxes[0]).height(); = $(this.pages[0]).height() / $(this.page_boxes[0]).height();
} }
this.selective_render(); this.selective_render();
var _ = this; var _ = this;
this.container.scroll(function(){ _.schedule_render(); }); this.container.scroll(function(){ _.schedule_render(); });
this.zoom_fixer(); this.zoom_fixer();
}, },
init : function() { init : function() {
this.init_before_loading_content(); this.init_before_loading_content();
var _ = this; var _ = this;
$(document).ready(function(){_.init_after_loading_content();}); $(document).ready(function(){_.init_after_loading_content();});
return this; return this;
}, },
pre_hide_pages : function() { pre_hide_pages : function() {
/* pages might have not been loaded yet, so add a CSS rule */ /* pages might have not been loaded yet, so add a CSS rule */
var s = '.b{display:none;}'; var s = '.b{display:none;}';
var n = document.createElement("style"); var n = document.createElement("style");
n.type = "text/css"; n.type = "text/css";
if (n.styleSheet) { if (n.styleSheet) {
n.styleSheet.cssText = s; n.styleSheet.cssText = s;
} else { } else {
n.appendChild(document.createTextNode(s)); n.appendChild(document.createTextNode(s));
} }
document.getElementsByTagName("head")[0].appendChild(n); document.getElementsByTagName("head")[0].appendChild(n);
}, },
hide_pages : function() { hide_pages : function() {
for(var i = 0, l = this.page_boxes.length; i < l; ++i) for(var i = 0, l = this.page_boxes.length; i < l; ++i)
{ {
$(this.page_boxes[i]).hide(); $(this.page_boxes[i]).hide();
} }
}, },
selective_render : function () { selective_render : function () {
/* hide (positional) invisible pages */ /* hide (positional) invisible pages */
var l = this.pages.length; var l = this.pages.length;
var ch = this.container.height(); var ch = this.container.height();
var i; var i;
for(i = 0; i < l; ++i) { for(i = 0; i < l; ++i) {
var cur_page = $(this.pages[i]); var cur_page = $(this.pages[i]);
if(cur_page.offset().top + cur_page.height() >= 0) break; if(cur_page.offset().top + cur_page.height() >= 0) break;
if(i > 0) $(this.page_boxes[i-1]).hide(); if(i > 0) $(this.page_boxes[i-1]).hide();
} }
if((i > 0) && (i < l)) $(this.page_boxes[i-1]).show(); if((i > 0) && (i < l)) $(this.page_boxes[i-1]).show();
for(; i < l; ++i) { for(; i < l; ++i) {
$(this.page_boxes[i]).show(); $(this.page_boxes[i]).show();
if($(this.pages[i]).offset().top > ch) break; if($(this.pages[i]).offset().top > ch) break;
} }
for(++i; i < l; ++i) { for(++i; i < l; ++i) {
$(this.page_boxes[i]).hide(); $(this.page_boxes[i]).hide();
} }
}, },
schedule_render : function() { schedule_render : function() {
if(this.selective_render_timer) if(this.selective_render_timer)
clearTimeout(this.selective_render_timer); clearTimeout(this.selective_render_timer);
var _ = this; var _ = this;
this.selective_render_timer = setTimeout(function () { this.selective_render_timer = setTimeout(function () {
_.selective_render(); _.selective_render();
}, this.render_timeout); }, this.render_timeout);
}, },
zoom_fixer : function () { zoom_fixer : function () {
/* /*
* When user try to zoom in/out using ctrl + +/- or mouse wheel * When user try to zoom in/out using ctrl + +/- or mouse wheel
* handle this and prevent the default behaviours * handle this and prevent the default behaviours
* *
* Code credit to PDF.js * Code credit to PDF.js
*/ */
var _ = this; var _ = this;
// Firefox specific event, so that we can prevent browser from zooming // Firefox specific event, so that we can prevent browser from zooming
$(window).on('DOMMouseScroll', function(e) { $(window).on('DOMMouseScroll', function(e) {
if (e.ctrlKey) { 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(_.default_scale_ratio, false);
break;
default:
return;
}
}
e.preventDefault(); e.preventDefault();
}); _.rescale(Math.pow(_.scale_step, e.detail), true);
},
rescale : function (ratio, is_relative) {
console.log('RESCALE');
if(is_relative)
ratio *= this.cur_scale_ratio;
this.cur_scale_ratio = ratio;
this.hide_pages();
for(var i = 0, l = this.page_boxes.length; i < l; ++i)
{
var p = $(this.pages[i]);
var pb = $(this.page_boxes[i]);
p.height(pb.height() * ratio);
p.width(pb.width() * ratio);
pb.css('transform', 'scale('+ratio.toFixed(3)+')');
} }
});
this.schedule_render(); $(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(_.default_scale_ratio, false);
break;
default:
return;
}
}
e.preventDefault();
});
},
__last_member__ : 'no comma' /*,*/ rescale : function (ratio, is_relative) {
}.init(); console.log('RESCALE');
})();
if(is_relative)
ratio *= this.cur_scale_ratio;
this.cur_scale_ratio = ratio;
this.hide_pages();
for(var i = 0, l = this.page_boxes.length; i < l; ++i)
{
var p = $(this.pages[i]);
var pb = $(this.page_boxes[i]);
p.height(pb.height() * ratio);
p.width(pb.width() * ratio);
pb.css('transform', 'scale('+ratio.toFixed(3)+')');
}
this.schedule_render();
},
__last_member__ : 'no comma' /*,*/
}.init();

View File

@ -73,7 +73,7 @@ void parse_options (int argc, char **argv)
.add("heps", &param.h_eps, 1.0, "max tolerated horizontal offset (in pixels)") .add("heps", &param.h_eps, 1.0, "max tolerated horizontal offset (in pixels)")
.add("veps", &param.v_eps, 1.0, "max tolerated vertical offset (in pixels)") .add("veps", &param.v_eps, 1.0, "max tolerated vertical offset (in pixels)")
.add("space-threshold", &param.space_threshold, (1.0/8), "distance no thiner than (threshold * em) will be considered as a space character") .add("space-threshold", &param.space_threshold, (1.0/8), "distance no thiner than (threshold * em) will be considered as a space character")
.add("font-size-multiplier", &param.font_size_multiplier, 8.0, "setting a value greater than 1 would increase the rendering accuracy") .add("font-size-multiplier", &param.font_size_multiplier, 4.0, "setting a value greater than 1 would increase the rendering accuracy")
.add("auto-hint", &param.auto_hint, 0, "[Experimental] Whether to generate hints for fonts") .add("auto-hint", &param.auto_hint, 0, "[Experimental] Whether to generate hints for fonts")
.add("tounicode", &param.tounicode, 0, "Specify how to deal with ToUnicode map, 0 for auto, 1 for forced, -1 for disabled") .add("tounicode", &param.tounicode, 0, "Specify how to deal with ToUnicode map, 0 for auto, 1 for forced, -1 for disabled")
.add("space-as-offset", &param.space_as_offset, 0, "treat space characters as offsets") .add("space-as-offset", &param.space_as_offset, 0, "treat space characters as offsets")