1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 04:50:09 +00:00

clean pdf2htmlEX.js

This commit is contained in:
Lu Wang 2013-09-30 15:50:15 +08:00
parent 23c4c4d4fe
commit 22cb315b99
2 changed files with 37 additions and 18 deletions

8
TODO
View File

@ -1,5 +1,7 @@
show hint for ttfautohint show hint for ttfautohint
open issue for text accuracy open issue for text accuracy
compress js
move pdf2htmlEX.Viewer's contstants outside
- dots issue - dots issue
- AdobeXML*.pdf - AdobeXML*.pdf
@ -7,9 +9,6 @@ open issue for text accuracy
- cjkmix2*.pdf - cjkmix2*.pdf
- space issue - space issue
- python简明*.pdf - python简明*.pdf
- smarter space-as-offset
- show hints about possible useful parameters
- optimization levels
- spinning icon for dynamic page loading - spinning icon for dynamic page loading
- fix negative word-spacing for IE - fix negative word-spacing for IE
@ -33,4 +32,7 @@ Not enough motivation/Lazy
- separate classes for annotations (such that we don't have to hide all css drawings for printing) - separate classes for annotations (such that we don't have to hide all css drawings for printing)
- Widget Annoataion - Widget Annoataion
- handle large negative letter space - handle large negative letter space
- optimization levels
- smarter space-as-offset
- show hints about possible useful parameters

View File

@ -8,6 +8,7 @@
'use strict'; 'use strict';
/* The namespace */
var pdf2htmlEX = (function(){ var pdf2htmlEX = (function(){
var CSS_CLASS_NAMES = { var CSS_CLASS_NAMES = {
page_frame : '@CSS_PAGE_FRAME_CN@', page_frame : '@CSS_PAGE_FRAME_CN@',
@ -18,11 +19,16 @@ var pdf2htmlEX = (function(){
link : '@CSS_LINK_CN@', link : '@CSS_LINK_CN@',
__dummy__ : 'no comma' __dummy__ : 'no comma'
}; };
// How many page shall we preload that are below the last visible page
var DEFAULT_PAGES_TO_PRELOAD = 3; var DEFAULT_PAGES_TO_PRELOAD = 3;
// Smooth zoom is enabled when pages shown are less then SMOOTH_ZOOM_THRESHOLD. Otherwise page content is hidden and redrawn after a delay (function schedule_render). // Smooth zoom is enabled when the number of pages shown is less then SMOOTH_ZOOM_THRESHOLD.
// Otherwise page content is hidden and redrawn after a delay (function schedule_render).
var SMOOTH_ZOOM_THRESHOLD = 4; // 0: disable smooth zoom optimizations (less CPU usage but flickering on zoom) var SMOOTH_ZOOM_THRESHOLD = 4; // 0: disable smooth zoom optimizations (less CPU usage but flickering on zoom)
var pdf2htmlEX = new Object(); var pdf2htmlEX = new Object();
var EPS = 1e-6; var EPS = 1e-6;
@ -53,7 +59,9 @@ var pdf2htmlEX = (function(){
this.$b = $('.'+CSS_CLASS_NAMES['page_content_box'], this.$p); this.$b = $('.'+CSS_CLASS_NAMES['page_content_box'], this.$p);
this.$d = this.$p.parent('.'+CSS_CLASS_NAMES['page_decoration']); this.$d = this.$p.parent('.'+CSS_CLASS_NAMES['page_decoration']);
this.h = this.$p.height(); // Need to make rescale work when page_content_box is not loaded, yet // page size
// Need to make rescale work when page_content_box is not loaded, yet
this.h = this.$p.height();
this.w = this.$p.width(); this.w = this.$p.width();
// if page is loaded // if page is loaded
@ -145,7 +153,7 @@ var pdf2htmlEX = (function(){
}; };
$.extend(pdf2htmlEX.Viewer.prototype, { $.extend(pdf2htmlEX.Viewer.prototype, {
/* Constants */ /* Constants: TODO: move outside */
render_timeout : 100, render_timeout : 100,
scale_step : 0.9, scale_step : 0.9,
scale : 1, scale : 1,
@ -172,7 +180,7 @@ var pdf2htmlEX = (function(){
var _ = this; var _ = this;
this.$container.scroll(function(){ _.schedule_render(); }); this.$container.scroll(function(){ _.schedule_render(); });
this.zoom_fixer(); //this.zoom_fixer();
// handle links // handle links
this.$container.add(this.$outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler); this.$container.add(this.$outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler);
@ -183,7 +191,7 @@ var pdf2htmlEX = (function(){
find_pages : function() { find_pages : function() {
var new_pages = new Array(); var new_pages = new Array();
var $pl= $('.'+CSS_CLASS_NAMES['page_frame'], this.$container); var $pl= $('.'+CSS_CLASS_NAMES['page_frame'], this.$container);
/* don't use for(..in..) */ /* don't use for(..in..) since $pl is more than an Array */
for(var i = 0, l = $pl.length; i < l; ++i) { for(var i = 0, l = $pl.length; i < l; ++i) {
var p = new Page($pl[i], this.$container); var p = new Page($pl[i], this.$container);
new_pages[p.n] = p; new_pages[p.n] = p;
@ -344,8 +352,11 @@ var pdf2htmlEX = (function(){
// Save offset of the active page // Save offset of the active page
var active_page = this.get_active_page(); var active_page = this.get_active_page();
if(!active_page) return;
var prev_offset = active_page.$p.offset(); var prev_offset = active_page.$p.offset();
var old_scale = this.scale; var old_scale = this.scale;
var pl = this.pages;
var prerendering_enabled = false; var prerendering_enabled = false;
if (SMOOTH_ZOOM_THRESHOLD > 0) { if (SMOOTH_ZOOM_THRESHOLD > 0) {
@ -353,11 +364,13 @@ var pdf2htmlEX = (function(){
// Find out which pages are visible // Find out which pages are visible
var min_visible, max_visible; var min_visible, max_visible;
min_visible = max_visible = active_page.n; min_visible = max_visible = active_page.n;
while (min_visible > 0 && this.pages[min_visible].is_visible()) { min_visible-- } while (pl[min_visible] && pl[min_visible].is_visible()) { --min_visible; }
while (max_visible < this.pages.length && this.pages[max_visible].is_visible()) { max_visible++ } ++ min_visible;
while (pl[max_visible] && pl[max_visible].is_visible()) { ++max_visible; }
-- max_visible;
// If less then the threshold, enable prerendering on selected pages // If less then the threshold, enable prerendering on selected pages
if (max_visible - min_visible - 2 < SMOOTH_ZOOM_THRESHOLD) if (max_visible - min_visible + 1 < SMOOTH_ZOOM_THRESHOLD)
prerendering_enabled = true; prerendering_enabled = true;
} }
@ -368,9 +381,8 @@ var pdf2htmlEX = (function(){
this.scale = ratio; this.scale = ratio;
// Rescale pages // Rescale pages
var pl = this.pages;
for(var i in pl) { for(var i in pl) {
if (prerendering_enabled && i > min_visible && i < max_visible) if (prerendering_enabled && i >= min_visible && i <= max_visible)
pl[i].rescale(this.scale, true); // Force immediate refresh pl[i].rescale(this.scale, true); // Force immediate refresh
else else
pl[i].rescale(this.scale); // Delayed refresh pl[i].rescale(this.scale); // Delayed refresh
@ -393,6 +405,7 @@ var pdf2htmlEX = (function(){
fit_width : function () { fit_width : function () {
var active_page = this.get_active_page(); var active_page = this.get_active_page();
if(!active_page) return;
this.rescale(this.$container.width() / active_page.w, false); this.rescale(this.$container.width() / active_page.w, false);
this.scroll_to(active_page.n, [0,0]); this.scroll_to(active_page.n, [0,0]);
@ -400,6 +413,7 @@ var pdf2htmlEX = (function(){
fit_height : function () { fit_height : function () {
var active_page = this.get_active_page(); var active_page = this.get_active_page();
if(!active_page) return;
this.rescale(this.$container.height() / active_page.h, false); this.rescale(this.$container.height() / active_page.h, false);
this.scroll_to(active_page.n, [0,0]); this.scroll_to(active_page.n, [0,0]);
@ -407,12 +421,15 @@ var pdf2htmlEX = (function(){
get_active_page : function () { get_active_page : function () {
// get page that are on the center of the view //TODO better on top?! // get page that are on the center of the view //TODO better on top?!
var y_center = $(this.$container).offset().top + this.$container.height() / 2; var y_center = this.$container.offset().top + this.$container.height() / 2;
for (var i=2; i<this.pages.length; i++) { var pl = this.pages;
if (this.pages[i].$p.offset().top > y_center) var last_page = -1;
return this.pages[i-1]; for (var i in pl) {
if (pl[i].$p.offset().top > y_center)
break;
last_page = i;
} }
return this.pages[i-1]; // Last page return pl[last_page];
}, },
get_containing_page : function(obj) { get_containing_page : function(obj) {