diff --git a/src/HTMLRenderer.h b/src/HTMLRenderer.h index f789df2..23aae8e 100644 --- a/src/HTMLRenderer.h +++ b/src/HTMLRenderer.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -260,6 +261,13 @@ class HTMLRenderer : public OutputDev // also keep in mind that they are not the final position, as they will be transform by CTM (also true for cur_tx/ty) double draw_tx, draw_ty; + // some metrics have to be determined after all elements in the lines have been seen + // TODO: add a class for these + double line_x, line_y; + long long line_tm_id; + double line_height; + std::stringstream line_buf; + //////////////////////////////////////////////////// // styles & resources //////////////////////////////////////////////////// diff --git a/src/HTMLRenderer/install.cc b/src/HTMLRenderer/install.cc index a983065..04f966b 100644 --- a/src/HTMLRenderer/install.cc +++ b/src/HTMLRenderer/install.cc @@ -40,10 +40,12 @@ FontInfo HTMLRenderer::install_font(GfxFont * font) return cur_info_iter->second; } + cur_info_iter->second.ascent = font->getAscent(); + cur_info_iter->second.descent = font->getDescent(); + if(param->debug) { cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << format("f%|1$x|")%new_fn_id << endl; - cerr << "Ascent: " << (font->getAscent()) << " Descent: " << (font->getDescent()) << endl; } if(font->getType() == fontType3) { @@ -251,6 +253,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix, add_tmp_file(fn+".ttf"); script_fout << format("Generate(%1%)") % dest << endl; + script_fout << "Close()" << endl; script_fout << format("Open(%1%, 1)") % dest << endl; for(const string & s1 : {"Win", "Typo", "HHead"}) @@ -260,9 +263,6 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix, script_fout << "Print(GetOS2Value(\"" << s1 << s2 << "\"))" << endl; } } - script_fout << "SetOS2Value(\"TypoLineGap\", 0)" << endl; - script_fout << "SetOS2Value(\"HHeadLineGap\", 0)" << endl; - script_fout << format("Generate(%1%)") % dest << endl; if(system((boost::format("fontforge -script %1% 1>%2% 2>%3%") % script_path % (tmp_dir / (fn+".info")) % (tmp_dir / NULL_FILENAME)).str().c_str()) != 0) cerr << "Warning: fontforge failed." << endl; @@ -274,14 +274,22 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix, int WinAsc, WinDes, TypoAsc, TypoDes, HHeadAsc, HHeadDes; if(ifstream(tmp_dir / (fn+".info")) >> WinAsc >> WinDes >> TypoAsc >> TypoDes >> HHeadAsc >> HHeadDes) { - double em = TypoAsc - TypoDes; - info.ascent = ((double)HHeadAsc) / em; - info.descent = ((double)HHeadDes) / em; + int em = TypoAsc - TypoDes; + if(em != 0) + { + info.ascent = ((double)HHeadAsc) / em; + info.descent = ((double)HHeadDes) / em; + } + else + { + info.ascent = 0; + info.descent = 0; + } } - else + + if(param->debug) { - info.ascent = font->getAscent(); - info.descent = font->getDescent(); + cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl; } export_remote_font(info, ".ttf", "truetype", font); diff --git a/src/HTMLRenderer/state.cc b/src/HTMLRenderer/state.cc index 5581c9b..ca119f3 100644 --- a/src/HTMLRenderer/state.cc +++ b/src/HTMLRenderer/state.cc @@ -338,7 +338,7 @@ void HTMLRenderer::prepare_line(GfxState * state) // don't close a pending span here, keep the styling double w; auto wid = install_whitespace(target, w); - html_fout << format("%2%") % wid % (target > 0 ? " " : ""); + line_buf << format("%2%") % wid % (target > 0 ? " " : ""); draw_tx += w / draw_scale; } } @@ -348,18 +348,9 @@ void HTMLRenderer::prepare_line(GfxState * state) // have to open a new tag if (new_line_status == LineStatus::DIV) { - // TODO: recheck descent/ascent - double x,y; // in user space - state->transform(state->getCurX(), state->getCurY(), &x, &y); - - // TODO class for height - html_fout << format("
") - % x - % y - % cur_tm_id - % (cur_font_info.ascent * draw_font_size) - % (2 * cur_font_info.ascent * draw_font_size) - ; + state->transform(state->getCurX(), state->getCurY(), &line_x, &line_y); + line_tm_id = cur_tm_id; + line_height = cur_font_info.ascent * draw_font_size; //resync position draw_ty = cur_ty; @@ -374,8 +365,9 @@ void HTMLRenderer::prepare_line(GfxState * state) assert(false && "Bad value of new_line_status"); } - html_fout << format("") + line_buf << format("") % cur_font_info.id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id % cur_rise_id; + line_height = max(line_height, cur_font_info.ascent * draw_font_size); line_status = LineStatus::SPAN; } @@ -385,6 +377,17 @@ void HTMLRenderer::close_line() if(line_status == LineStatus::NONE) return; + // TODO class for height + html_fout << format("
") + % line_x + % (line_y + 1) + % line_tm_id + % line_height + % (line_height * 2) + ; + html_fout << line_buf.rdbuf(); + line_buf.str(""); + if(line_status == LineStatus::SPAN) html_fout << ""; else @@ -392,4 +395,5 @@ void HTMLRenderer::close_line() html_fout << "
"; line_status = LineStatus::NONE; + } diff --git a/src/HTMLRenderer/text.cc b/src/HTMLRenderer/text.cc index 4e05162..6759765 100644 --- a/src/HTMLRenderer/text.cc +++ b/src/HTMLRenderer/text.cc @@ -205,7 +205,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) } Unicode uu = (cur_font_info.use_tounicode ? check_unicode(u, uLen, code, font) : unicode_from_font(code, font)); - outputUnicodes(html_fout, &uu, 1); + outputUnicodes(line_buf, &uu, 1); dx += dx1; dy += dy1;