From a9bc242c1c669ca0d8348a498042ebb8e0adf62e Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 28 Jan 2013 21:01:02 +0800 Subject: [PATCH] process outline --- CMakeLists.txt | 1 + share/base.css | 10 ++++- share/pdf2htmlEX.js | 5 ++- src/HTMLRenderer/HTMLRenderer.h | 7 +++- src/HTMLRenderer/general.cc | 7 ++++ src/HTMLRenderer/link.cc | 34 ++++++++++------ src/HTMLRenderer/outline.cc | 72 +++++++++++++++++++++++++++++++++ src/util/unicode.h | 3 ++ 8 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 src/HTMLRenderer/outline.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index c97b1b1..0a6839d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,7 @@ add_executable(pdf2htmlEX src/HTMLRenderer/TextLineBuffer.h src/HTMLRenderer/TextLineBuffer.cc src/HTMLRenderer/link.cc + src/HTMLRenderer/outline.cc src/HTMLRenderer/state.cc src/HTMLRenderer/text.cc src/BackgroundRenderer/BackgroundRenderer.h diff --git a/share/base.css b/share/base.css index dec7559..9492e14 100644 --- a/share/base.css +++ b/share/base.css @@ -1,10 +1,18 @@ /* Base CSS */ /* Copyright 2012 Lu Wang */ -#pdf-main { /* PDF container */ +#pdf-outline { /* PDF Outline */ position:absolute; top:0; left:0; bottom:0; + width:200px; + overflow:auto; +} +#pdf-main { /* PDF container */ + position:absolute; + top:0; + left:200px; + bottom:0; right:0; overflow:auto; background-color:grey; diff --git a/share/pdf2htmlEX.js b/share/pdf2htmlEX.js index 59a1ee4..cd850d8 100644 --- a/share/pdf2htmlEX.js +++ b/share/pdf2htmlEX.js @@ -129,7 +129,8 @@ var pdf2htmlEX = (function(){ //this.zoom_fixer(); - this.container.on('click', '.a', this, this.annot_link_handler); + // used by outline/annot_link etc + this.container.on('click', '.a', this, this.link_handler); this.render(); }, @@ -231,7 +232,7 @@ var pdf2htmlEX = (function(){ return this.pages[(new Page(obj.closest('.p')[0])).n]; }, - annot_link_handler : function (e) { + link_handler : function (e) { var _ = e.data; var t = $(e.currentTarget); var cur_page = _.get_containing_page(t); diff --git a/src/HTMLRenderer/HTMLRenderer.h b/src/HTMLRenderer/HTMLRenderer.h index 087cae9..532eedb 100644 --- a/src/HTMLRenderer/HTMLRenderer.h +++ b/src/HTMLRenderer/HTMLRenderer.h @@ -212,13 +212,16 @@ class HTMLRenderer : public OutputDev void pre_process(PDFDoc * doc); void post_process(); + void process_outline(); + void process_outline_items(GooList * items); + void set_stream_flags (std::ostream & out); std::string dump_embedded_font (GfxFont * font, long long fn_id); void embed_font(const std::string & filepath, GfxFont * font, FontInfo & info, bool get_metric_only = false); - // convert a LinkDest to a string that our Javascript code can understand - std::string get_linkdest_str(int & pageno, LinkDest * dest); + // convert a LinkAction to a string that our Javascript code can understand + std::string get_linkaction_str(LinkAction *, std::string & detail); //////////////////////////////////////////////////// // manage styles diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index a81ed09..574e9b5 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -74,6 +74,9 @@ void HTMLRenderer::process(PDFDoc *doc) pre_process(doc); + /////////////////// + // Process pages + BackgroundRenderer * bg_renderer = nullptr; if(param->process_nontext) { @@ -120,6 +123,10 @@ void HTMLRenderer::process(PDFDoc *doc) cerr << "Working: " << page_count << "/" << page_count; cerr << endl; + //////////////////////// + // Process Outline + process_outline(); + post_process(); if(bg_renderer) diff --git a/src/HTMLRenderer/link.cc b/src/HTMLRenderer/link.cc index fb76998..ec35f26 100644 --- a/src/HTMLRenderer/link.cc +++ b/src/HTMLRenderer/link.cc @@ -29,14 +29,16 @@ using std::endl; /* * The detailed rectangle area of the link destination * Will be parsed and performed by Javascript + * The string will be put into a HTML attribute, surrounded by single quotes + * So pay attention to the characters used here */ -string HTMLRenderer::get_linkdest_str(int & pageno, LinkDest * dest) +static string get_linkdest_detail_str(LinkDest * dest, Catalog * catalog, int & pageno) { pageno = 0; if(dest->isPageRef()) { auto pageref = dest->getPageRef(); - pageno = cur_catalog->findPage(pageref.num, pageref.gen); + pageno = catalog->findPage(pageref.num, pageref.gen); } else { @@ -124,16 +126,11 @@ string HTMLRenderer::get_linkdest_str(int & pageno, LinkDest * dest) return sout.str(); } - -/* - * Based on pdftohtml from poppler - * TODO: CSS for link rectangles - * TODO: share rectangle draw with css-draw - */ -void HTMLRenderer::processLink(AnnotLink * al) + +string HTMLRenderer::get_linkaction_str(LinkAction * action, string & detail) { - std::string dest_str, dest_detail_str; - auto action = al->getAction(); + string dest_str; + detail = ""; if(action) { auto kind = action->getKind(); @@ -150,7 +147,7 @@ void HTMLRenderer::processLink(AnnotLink * al) if(dest) { int pageno = 0; - dest_detail_str = get_linkdest_str(pageno, dest); + detail = get_linkdest_detail_str(dest, cur_catalog, pageno); if(pageno > 0) { dest_str = (char*)str_fmt("#p%x", pageno); @@ -181,6 +178,19 @@ void HTMLRenderer::processLink(AnnotLink * al) } } + return dest_str; +} + +/* + * Based on pdftohtml from poppler + * TODO: CSS for link rectangles + * TODO: share rectangle draw with css-draw + */ +void HTMLRenderer::processLink(AnnotLink * al) +{ + string dest_detail_str; + string dest_str = get_linkaction_str(al->getAction(), dest_detail_str); + if(!dest_str.empty()) { f_pages.fs << " + +#include +#include + +#include "HTMLRenderer.h" +#include "util/namespace.h" +#include "util/unicode.h" + +namespace pdf2htmlEX { + +using std::ostream; + +void HTMLRenderer::process_outline_items(GooList * items) +{ + if((!items) || (items->getLength() == 0)) + return; + + f_outline.fs << ""; +} + +void HTMLRenderer::process_outline() +{ + Outline * outline = cur_doc->getOutline(); + if(!outline) + return; + + process_outline_items(outline->getItems()); +} + +}// namespace pdf2htmlEX diff --git a/src/util/unicode.h b/src/util/unicode.h index 9cc9dc6..6b527da 100644 --- a/src/util/unicode.h +++ b/src/util/unicode.h @@ -33,6 +33,9 @@ Unicode unicode_from_font (CharCode code, GfxFont * font); */ Unicode check_unicode(Unicode * u, int len, CharCode code, GfxFont * font); +/* + * Escape necessary characters, and map Unicode to UTF-8 + */ void outputUnicodes(std::ostream & out, const Unicode * u, int uLen);