From 810fb00de730ab8bb409d594665309de6cfa262f Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Sun, 12 Aug 2012 20:36:56 +0800 Subject: [PATCH 1/4] working on images --- src/Consts.cc | 31 ++++++++++-------- src/HTMLRenderer.cc | 78 ++++++++++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 46 deletions(-) diff --git a/src/Consts.cc b/src/Consts.cc index 07abc62..c690cc2 100644 --- a/src/Consts.cc +++ b/src/Consts.cc @@ -30,8 +30,8 @@ const std::string HTML_HEAD = "\n\ overflow:hidden;\ display:none;\ }\ -.p > .l {\ - position:absolute; \ +.p > .l {\ + position:absolute;\ white-space:pre;\ }\ .l > .w {\ @@ -44,22 +44,25 @@ const std::string HTML_HEAD = "\n\ ::-moz-selection{\ background: rgba(168,209,255,0.5);\ }\ +.p > .i {\ + position:absolute;\ +}\ \ \
"; diff --git a/src/HTMLRenderer.cc b/src/HTMLRenderer.cc index a4e90bc..55371b3 100644 --- a/src/HTMLRenderer.cc +++ b/src/HTMLRenderer.cc @@ -39,6 +39,7 @@ const int *int_p_NULL = nullptr; * p - Page * l - Line * w - White space + * i - Image * * * Reusable CSS classes @@ -69,14 +70,12 @@ HTMLRenderer::HTMLRenderer(const Param * param) black.r = black.g = black.b = 0; install_color(&black); - html_fout << HTML_HEAD; - html_fout << endl; + html_fout << HTML_HEAD << endl; } HTMLRenderer::~HTMLRenderer() { - html_fout << HTML_TAIL; - html_fout << endl; + html_fout << HTML_TAIL << endl; } void HTMLRenderer::process(PDFDoc *doc) @@ -133,8 +132,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) html_fout << boost::format("background-image:url(p%|3$x|.png);background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;") % pageWidth % pageHeight % pageNum; - html_fout << "\">"; - html_fout << endl; + html_fout << "\">" << endl; cur_fn_id = cur_fs_id = cur_tm_id = cur_color_id = 0; cur_tx = cur_ty = 0; @@ -154,17 +152,14 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) void HTMLRenderer::endPage() { close_cur_line(); // close page - html_fout << "
"; - html_fout << endl; + html_fout << "" << endl; } void HTMLRenderer::close_cur_line() { if(line_opened) { - html_fout << ""; - html_fout << endl; - + html_fout << "" << endl; line_opened = false; } } @@ -375,7 +370,10 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int width, int height, GfxImageColorMap * colorMap, GBool interpolate, int *maskColors, GBool inlineImg) { - boost::gil::rgb16_image_t img(width, height); + if(maskColors) + return; + + boost::gil::rgb8_image_t img(width, height); auto imgview = view(img); auto loc = imgview.xy_at(0,0); @@ -390,7 +388,7 @@ void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int w GfxRGB rgb; colorMap->getRGB(p, &rgb); - *loc = boost::gil::rgb16_pixel_t(rgb.r, rgb.g, rgb.b); + *loc = boost::gil::rgb8_pixel_t(colToByte(rgb.r), colToByte(rgb.g), colToByte(rgb.b)); p += colorMap->getNumPixelComps(); @@ -401,10 +399,17 @@ void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int w } boost::gil::png_write_view((boost::format("i%|1$x|.png")%image_count).str(), imgview); - + img_stream->close(); delete img_stream; + close_cur_line(); + + double * ctm = state->getCTM(); + ctm[4] = ctm[5] = 0.0; + html_fout << boost::format("") % image_count % install_transform_matrix(ctm) % state->getCurX() % state->getCurY() % width % height << endl; + + ++ image_count; } @@ -607,6 +612,25 @@ err: void HTMLRenderer::install_embedded_font(GfxFont * font, const std::string & suffix, long long fn_id) { // TODO Should use standard way to handle CID fonts + /* + * How it works: + * + * 1.dump the font file directly from the font descriptor and put the glyphs into the correct slots + * + * for nonCID + * nothing need to do + * + * for CID + nonTrueType + * Flatten the font + * + * for CID Truetype + * Just use glyph order, and later we'll map GID (instead of char code) to Unicode + * + * + * 2. map charcode (or GID for CID truetype) to Unicode + * + * generate an encoding file and let fontforge handle it. + */ std::string fn = (boost::format("f%|1$x|") % fn_id).str(); @@ -617,13 +641,11 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const std::string & suf if(ctu) { // TODO: ctu could be CID2Unicode for CID fonts - int maxcode = 0; if(!font->isCIDFont()) { maxcode = 0xff; - //TODO read code2GID for TrueType } else { @@ -776,16 +798,13 @@ void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, co allcss_fout << "line-height:" << (a-d) << ";"; - allcss_fout << "}"; - - allcss_fout << endl; + allcss_fout << "}" << endl; } // TODO: this function is called when some font is unable to process, may use the name there as a hint void HTMLRenderer::export_remote_default_font(long long fn_id) { - allcss_fout << boost::format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id; - allcss_fout << endl; + allcss_fout << boost::format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id << endl; } void HTMLRenderer::export_local_font(long long fn_id, GfxFont * font, const string & original_font_name, const string & cssfont) @@ -812,9 +831,7 @@ void HTMLRenderer::export_local_font(long long fn_id, GfxFont * font, const stri allcss_fout << "line-height:" << (a-d) << ";"; - allcss_fout << "}"; - - allcss_fout << endl; + allcss_fout << "}" << endl; } std::string HTMLRenderer::general_font_family(GfxFont * font) @@ -829,14 +846,12 @@ std::string HTMLRenderer::general_font_family(GfxFont * font) void HTMLRenderer::export_font_size (long long fs_id, double font_size) { - allcss_fout << boost::format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size; - allcss_fout << endl; + allcss_fout << boost::format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size << endl; } void HTMLRenderer::export_whitespace (long long ws_id, double ws_width) { - allcss_fout << boost::format(".w%|1$x|{width:%2%px;}") % ws_id % ws_width; - allcss_fout << endl; + allcss_fout << boost::format(".w%|1$x|{width:%2%px;}") % ws_id % ws_width << endl; } void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm) @@ -865,15 +880,14 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm) allcss_fout << boost::format("%1%,%2%);") % tm[4] % -tm[5]; } } - allcss_fout << "}"; - allcss_fout << endl; + allcss_fout << "}" << endl; } void HTMLRenderer::export_color(long long color_id, const GfxRGB * rgb) { allcss_fout << boost::format(".c%|1$x|{color:rgb(%2%,%3%,%4%);}") - % color_id % (int)colToByte(rgb->r) % (int)colToByte(rgb->g) % (int)colToByte(rgb->b); - allcss_fout << endl; + % color_id % (int)colToByte(rgb->r) % (int)colToByte(rgb->g) % (int)colToByte(rgb->b) + << endl; } void HTMLRenderer::check_state_change(GfxState * state) From aba9aa9cddbdeb926b58663686b82fed37429a4c Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 13 Aug 2012 13:28:05 +0800 Subject: [PATCH 2/4] update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d30c740..546e6ee 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Dependency ---------------------------- * libpoppler with xpdf header >= 0.20.2 * boost c++ library (format, program options, gil) -* fontforge **Please use [my modified version](http://github.com/coolwanglu/fontforge) before the bugs are fixed** +* fontforge **Please use [the lastest version](https://github.com/fontforge/fontforge)** HOW TO COMPILE ---------------------------- From cc921507846aa7ab207419eff89db463c96789e4 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 13 Aug 2012 19:06:41 +0800 Subject: [PATCH 3/4] .. --- src/Consts.cc | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Consts.cc b/src/Consts.cc index c690cc2..b4e3785 100644 --- a/src/Consts.cc +++ b/src/Consts.cc @@ -51,18 +51,18 @@ const std::string HTML_HEAD = "\n\ \
"; From 5e4677c1e8272a627a9d3a37c993a27f34a9dafc Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 13 Aug 2012 19:13:25 +0800 Subject: [PATCH 4/4] solved a dpi bug --- src/HTMLRenderer.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/HTMLRenderer.cc b/src/HTMLRenderer.cc index 55371b3..ba7bb81 100644 --- a/src/HTMLRenderer.cc +++ b/src/HTMLRenderer.cc @@ -957,14 +957,17 @@ void HTMLRenderer::check_state_change(GfxState * state) if(need_rescale_font) { - draw_scale = std::sqrt(cur_ctm[2] * cur_ctm[2] + cur_ctm[3] * cur_ctm[3]); + double new_draw_ctm[6]; + memcpy(new_draw_ctm, cur_ctm, sizeof(new_draw_ctm)); + + draw_scale = std::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(draw_scale)) { new_draw_font_size *= draw_scale; for(int i = 0; i < 4; ++i) - cur_ctm[i] /= draw_scale; + new_draw_ctm[i] /= draw_scale; } else { @@ -977,9 +980,9 @@ void HTMLRenderer::check_state_change(GfxState * state) cur_fs_id = install_font_size(draw_font_size); close_line = true; } - if(!(_tm_equal(cur_ctm, draw_ctm))) + if(!(_tm_equal(new_draw_ctm, draw_ctm))) { - memcpy(draw_ctm, cur_ctm, sizeof(draw_ctm)); + memcpy(draw_ctm, new_draw_ctm, sizeof(draw_ctm)); cur_tm_id = install_transform_matrix(draw_ctm); close_line = true; }