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

single js

This commit is contained in:
Lu Wang 2012-09-22 22:47:44 +08:00
parent ead9f06281
commit ca0b3e6cac
10 changed files with 140 additions and 94 deletions

View File

@ -136,6 +136,11 @@ add_executable(pdf2htmlEX
target_link_libraries(pdf2htmlEX ${PDF2HTMLEX_LIBS})
install (TARGETS pdf2htmlEX DESTINATION bin)
file (GLOB datafiles share/*)
file (GLOB datafiles
share/base.css
share/jquery.js
share/pdf2htmlEX.js
share/manifest
)
install (FILES ${datafiles} DESTINATION share/pdf2htmlEX)
install (FILES pdf2htmlEX.1 DESTINATION share/man/man1)

View File

@ -1,8 +1,5 @@
/* Base CSS */
/* by Wang Lu */
body {
position:relative;
}
#pdf-main {
position:absolute;
top:0;
@ -25,6 +22,11 @@ body {
background-color:white;
overflow:hidden;
display:block;
transform-origin:0% 0%;
-ms-transform-origin:0% 0%;
-moz-transform-origin:0% 0%;
-webkit-transform-origin:0% 0%;
-o-transform-origin:0% 0%;
}
.l {
position:absolute;

View File

@ -1,12 +0,0 @@
/* hide all pages */
(function(){
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);
})();

View File

@ -24,7 +24,7 @@
@base.css
$css
@jquery.js
@hide_pages.js
@pdf2htmlEX.js
"""
<title></title>
@ -37,11 +37,6 @@ $pages
"""
</div>
"""
@scroll.js
"""
</body>
</html>
"""

91
share/pdf2htmlEX.js Normal file
View File

@ -0,0 +1,91 @@
/* -*- 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 obj = {
pages : [],
page_boxes : [],
container : null,
selective_render_timer : null,
init_before_loading_content : function() {
/*hide all pages before loading, will reveal only visible ones later */
this.hide_pages();
},
init_after_loading_content : function() {
this.pages = $(".p");
this.page_boxes = $(".b");
this.container = $("#pdf-main");
this.selective_render();
var _ = this;
this.container.scroll(function(){
if(_.selective_render_timer)
clearTimeout(_.selective_render_timer);
_.selective_render_timer = setTimeout(function () {
_.selective_render();
}, 200);
});
},
init : function() {
this.init_before_loading_content();
var _ = this;
$(document).ready(function(){_.init_after_loading_content();});
},
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);
},
selective_render : function () {
/* hide (positional) invisible pages */
var l = this.pages.length;
var ch = this.container.height();
var i;
for(i = 0; i < l; ++i) {
var cur_page = $(this.pages[i]);
if(cur_page.offset().top + cur_page.height() >= 0) break;
if(i > 0) $(this.page_boxes[i-1]).hide();
}
if((i > 0) && (i < l)) $(this.page_boxes[i-1]).show();
for(; i < l; ++i) {
$(this.page_boxes[i]).show();
if($(this.pages[i]).offset().top > ch) break;
}
for(++i; i < l; ++i) {
$(this.page_boxes[i]).hide();
}
},
};
obj.init();
return obj;
})();

View File

@ -1,64 +0,0 @@
/*
* scroll.js
* render only necessary pages
*
* by Hongliang TIAN
* modifiedy by Lu WANG
*/
$(function() {
var $pages = $(".p");
var $page_boxes= $(".b");
var $main = $("#pdf-main");
var l = $pages.length;
function isPageVisible(i, H) {
var $p = $($pages[i]);
var t = $p.offset().top;
var b = t + $p.height();
return ( ! ( b < 0 || t >= H ) );
}
function setVisibilities(from, to, visible) {
for(var i = from; i <= to; ++i) {
var $pb = $($page_boxes[i]);
if(visible) $pb.show(); else $pb.hide();
}
}
// Selectively rendering of pages that are visible or will be visible
function selectiveRender() {
var first = 0, last = l - 1, H = $main.height();
// Find the first visible page
while(first < l && !isPageVisible(first, H))
first++;
// Find the last visible page
while(last >= 0 && !isPageVisible(last, H))
last--;
// Set invisible
setVisibilities(first > 0 ? first-1 : first,
last < l -1 ? last + 1 : last, true);
setVisibilities(0, first - 2, false);
setVisibilities(last + 2, l-1, false);
}
// Listen to scrolling events to render proper pages
var scrolled = false;
var last_scroll_time = Date.now();
$("#pdf-main").scroll(function() {
scrolled = true;
last_scroll_time = Date.now();
});
setInterval(function() {
// If scrolling pauses 200+ms
if (scrolled && (Date.now() - last_scroll_time> 100)) {
scrolled = false;
// Only render pages that are or will be visible
selectiveRender();
}
}, 100);
// Trigger the event
$("#pdf-main").scroll();
});

View File

@ -97,20 +97,19 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
// we have already shifted the origin
// TODO: recognize common matices
if(_tm_euqal(tm, id_matrix, 4))
if(_tm_equal(tm, id_matrix, 4))
{
auto prefixes = {"", "-ms-", "-moz-", "-webkit-", "-o-"};
for(auto iter = prefixes.begin(); iter != prefixes.end(); ++iter)
css_fout << prefix << "transform:none;";
css_fout << *iter << "transform:none;";
}
else
{
auto prefixes = {"", "-ms-", "-moz-", "-webkit-", "-o-"};
for(auto iter = prefixes.begin(); iter != prefixes.end(); ++iter)
{
const auto & prefix = *iter;
// PDF use a different coordinate system from Web
css_fout << prefix << "transform:matrix("
css_fout << *iter << "transform:matrix("
<< _round(tm[0]) << ','
<< _round(-tm[1]) << ','
<< _round(-tm[2]) << ','

View File

@ -9,6 +9,7 @@
#include <cstdio>
#include <ostream>
#include <cmath>
#include <splash/SplashBitmap.h>
#include <Link.h>
@ -24,6 +25,7 @@ namespace pdf2htmlEX {
using std::fixed;
using std::flush;
using std::ostream;
using std::max;
static void dummy(void *, enum ErrorCategory, int pos, char *)
{
@ -47,6 +49,12 @@ HTMLRenderer::HTMLRenderer(const Param * param)
cur_mapping = new int32_t [0x10000];
cur_mapping2 = new char* [0x100];
width_list = new int [0x10000];
/*
* determine scale factors
*/
scale_factor1 = max(param->zoom, param->font_size_multiplier);
scale_factor2 = (param->zoom) / scale_factor1;
}
HTMLRenderer::~HTMLRenderer()
@ -113,7 +121,7 @@ void HTMLRenderer::process(PDFDoc *doc)
}
}
doc->displayPage(this, i, param->zoom * DEFAULT_DPI, param->zoom * DEFAULT_DPI,
doc->displayPage(this, i, scale_factor1 * DEFAULT_DPI, scale_factor1 * DEFAULT_DPI,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
@ -147,8 +155,18 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
assert((!line_opened) && "Open line in startPage detected!");
html_fout
<< "<div id=\"p" << pageNum << "\" class=\"p\" style=\"width:" << pageWidth << "px;height:" << pageHeight << "px;\">"
<< "<div id=\"b" << pageNum << "\" class=\"b\" style=\"width:" << pageWidth << "px;height:" << pageHeight << "px;";
<< "<div id=\"p" << pageNum << "\" class=\"p\" style=\"width:"
<< (pageWidth * scale_factor2) << "px;height:"
<< (pageHeight * scale_factor2) << "px;\">"
<< "<div id=\"b" << pageNum << "\" class=\"b\" style=\"width:"
<< pageWidth << "px;height:"
<< pageHeight << "px;";
{
auto prefixes = {"", "-ms-", "-moz-", "-webkit-", "-o-"};
for(auto iter = prefixes.begin(); iter != prefixes.end(); ++iter)
html_fout << *iter << "transform:scale(" << scale_factor2 << ");";
}
if(param->process_nontext)
{

View File

@ -146,7 +146,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
double new_draw_ctm[6];
memcpy(new_draw_ctm, cur_ctm, sizeof(new_draw_ctm));
double new_draw_scale = (param->font_size_multiplier) / (param->zoom) * sqrt(new_draw_ctm[2] * new_draw_ctm[2] + new_draw_ctm[3] * new_draw_ctm[3]);
double new_draw_scale = sqrt(new_draw_ctm[2] * new_draw_ctm[2] + new_draw_ctm[3] * new_draw_ctm[3]);
double new_draw_font_size = cur_font_size;
if(_is_positive(new_draw_scale))

View File

@ -204,6 +204,18 @@ class HTMLRenderer : public OutputDev
double pageWidth ;
double pageHeight ;
/*
* The content of each page is first scaled with factor1 (>=1), then scale back with factor2(<=1)
*
* factor1 is use to multiplied with all metrics (height/width/font-size...), in order to improve accuracy
* factor2 is applied with css transform, and is exposed to Javascript
*
* factor1 & factor 2 are determined according to zoom and font-size-multiplier
*
*/
double scale_factor1;
double scale_factor2;
////////////////////////////////////////////////////
// states