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

use real asc/dec of the extracted font; cache line for multiple font/size

This commit is contained in:
Lu Wang 2012-08-31 00:25:05 +08:00
parent e9b353c3de
commit f661aa5d8a
4 changed files with 45 additions and 25 deletions

View File

@ -11,6 +11,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <set> #include <set>
#include <sstream>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
@ -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) // 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; 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 // styles & resources
//////////////////////////////////////////////////// ////////////////////////////////////////////////////

View File

@ -40,10 +40,12 @@ FontInfo HTMLRenderer::install_font(GfxFont * font)
return cur_info_iter->second; return cur_info_iter->second;
} }
cur_info_iter->second.ascent = font->getAscent();
cur_info_iter->second.descent = font->getDescent();
if(param->debug) if(param->debug)
{ {
cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << format("f%|1$x|")%new_fn_id << endl; 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) { if(font->getType() == fontType3) {
@ -251,6 +253,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
add_tmp_file(fn+".ttf"); add_tmp_file(fn+".ttf");
script_fout << format("Generate(%1%)") % dest << endl; script_fout << format("Generate(%1%)") % dest << endl;
script_fout << "Close()" << endl;
script_fout << format("Open(%1%, 1)") % dest << endl; script_fout << format("Open(%1%, 1)") % dest << endl;
for(const string & s1 : {"Win", "Typo", "HHead"}) 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 << "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) 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; 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; int WinAsc, WinDes, TypoAsc, TypoDes, HHeadAsc, HHeadDes;
if(ifstream(tmp_dir / (fn+".info")) >> WinAsc >> WinDes >> TypoAsc >> TypoDes >> HHeadAsc >> HHeadDes) if(ifstream(tmp_dir / (fn+".info")) >> WinAsc >> WinDes >> TypoAsc >> TypoDes >> HHeadAsc >> HHeadDes)
{ {
double em = TypoAsc - TypoDes; int em = TypoAsc - TypoDes;
info.ascent = ((double)HHeadAsc) / em; if(em != 0)
info.descent = ((double)HHeadDes) / em; {
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(); cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl;
info.descent = font->getDescent();
} }
export_remote_font(info, ".ttf", "truetype", font); export_remote_font(info, ".ttf", "truetype", font);

View File

@ -338,7 +338,7 @@ void HTMLRenderer::prepare_line(GfxState * state)
// don't close a pending span here, keep the styling // don't close a pending span here, keep the styling
double w; double w;
auto wid = install_whitespace(target, w); auto wid = install_whitespace(target, w);
html_fout << format("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > 0 ? " " : ""); line_buf << format("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > 0 ? " " : "");
draw_tx += w / draw_scale; draw_tx += w / draw_scale;
} }
} }
@ -348,18 +348,9 @@ void HTMLRenderer::prepare_line(GfxState * state)
// have to open a new tag // have to open a new tag
if (new_line_status == LineStatus::DIV) if (new_line_status == LineStatus::DIV)
{ {
// TODO: recheck descent/ascent state->transform(state->getCurX(), state->getCurY(), &line_x, &line_y);
double x,y; // in user space line_tm_id = cur_tm_id;
state->transform(state->getCurX(), state->getCurY(), &x, &y); line_height = cur_font_info.ascent * draw_font_size;
// TODO class for height
html_fout << format("<div style=\"left:%1%px;bottom:%2%px;height:%4%px;line-height:%5%px;\" class=\"l t%|3$x|\">")
% x
% y
% cur_tm_id
% (cur_font_info.ascent * draw_font_size)
% (2 * cur_font_info.ascent * draw_font_size)
;
//resync position //resync position
draw_ty = cur_ty; draw_ty = cur_ty;
@ -374,8 +365,9 @@ void HTMLRenderer::prepare_line(GfxState * state)
assert(false && "Bad value of new_line_status"); assert(false && "Bad value of new_line_status");
} }
html_fout << format("<span class=\"f%|1$x| s%|2$x| c%|3$x| l%|4$x| w%|5$x| r%|6$x|\">") line_buf << format("<span class=\"f%|1$x| s%|2$x| c%|3$x| l%|4$x| w%|5$x| r%|6$x|\">")
% cur_font_info.id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id % cur_rise_id; % 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; line_status = LineStatus::SPAN;
} }
@ -385,6 +377,17 @@ void HTMLRenderer::close_line()
if(line_status == LineStatus::NONE) if(line_status == LineStatus::NONE)
return; return;
// TODO class for height
html_fout << format("<div style=\"left:%1%px;bottom:%2%px;height:%4%px;line-height:%5%px;\" class=\"l t%|3$x|\">")
% 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) if(line_status == LineStatus::SPAN)
html_fout << "</span>"; html_fout << "</span>";
else else
@ -392,4 +395,5 @@ void HTMLRenderer::close_line()
html_fout << "</div>"; html_fout << "</div>";
line_status = LineStatus::NONE; line_status = LineStatus::NONE;
} }

View File

@ -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)); 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; dx += dx1;
dy += dy1; dy += dy1;