1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 17:48:38 +00:00

do not round the coordinates

This commit is contained in:
Lu Wang 2012-08-30 23:36:30 +08:00
parent 4b1aff0da5
commit e9b353c3de
7 changed files with 53 additions and 21 deletions

2
TODO
View File

@ -1,4 +1,4 @@
better solution for ascent/descent (cross-platform, embeded/not) check exact asc/dsc
option to embed local matched fonts for non-embedded ones option to embed local matched fonts for non-embedded ones

View File

@ -19,7 +19,7 @@
.l { .l {
position:absolute; position:absolute;
white-space:pre; white-space:pre;
font-size:0; font-size:1px;
transform-origin:0% 190%; transform-origin:0% 190%;
-ms-transform-origin:0% 100%; -ms-transform-origin:0% 100%;
-moz-transform-origin:0% 100%; -moz-transform-origin:0% 100%;

View File

@ -130,7 +130,7 @@ class HTMLRenderer : public OutputDev
// manage styles // manage styles
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
FontInfo install_font(GfxFont * font); FontInfo install_font(GfxFont * font);
void install_embedded_font(GfxFont * font, const std::string & suffix, long long fn_id, bool & use_tounicode); void install_embedded_font(GfxFont * font, const std::string & suffix, long long fn_id, FontInfo & info);
void install_base_font(GfxFont * font, GfxFontLoc * font_loc, long long fn_id); void install_base_font(GfxFont * font, GfxFontLoc * font_loc, long long fn_id);
void install_external_font (GfxFont * font, long long fn_id); void install_external_font (GfxFont * font, long long fn_id);
@ -149,7 +149,7 @@ class HTMLRenderer : public OutputDev
* remote font: to be retrieved from the web server * remote font: to be retrieved from the web server
* local font: to be substituted with a local (client side) font * local font: to be substituted with a local (client side) font
*/ */
void export_remote_font(long long fn_id, const std::string & suffix, const std::string & fontfileformat, GfxFont * font); void export_remote_font(const FontInfo & info, const std::string & suffix, const std::string & fontfileformat, GfxFont * font);
void export_remote_default_font(long long fn_id); void export_remote_default_font(long long fn_id);
void export_local_font(long long fn_id, GfxFont * font, const std::string & original_font_name, const std::string & cssfont); void export_local_font(long long fn_id, GfxFont * font, const std::string & original_font_name, const std::string & cssfont);

View File

@ -16,21 +16,22 @@
using boost::algorithm::ifind_first; using boost::algorithm::ifind_first;
void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, const string & fontfileformat, GfxFont * font) void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, const string & fontfileformat, GfxFont * font)
{ {
allcss_fout << format("@font-face{font-family:f%|1$x|;src:url(") % fn_id; allcss_fout << format("@font-face{font-family:f%|1$x|;src:url(") % info.id;
const std::string fn = (format("f%|1$x|%2%") % fn_id % suffix).str(); const std::string fn = (format("f%|1$x|") % info.id).str();
if(param->single_html) if(param->single_html)
{ {
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'"; allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / (fn+suffix), ifstream::binary)) << "'";
} }
else else
{ {
allcss_fout << fn; allcss_fout << (fn+suffix);
} }
allcss_fout << format(")format(\"%1%\");}.f%|2$x|{font-family:f%|2$x|;line-height:%3%;}") % fontfileformat % fn_id % (font->getAscent() - font->getDescent()) << endl; allcss_fout << format(")format(\"%1%\");}.f%|2$x|{font-family:f%|2$x|;line-height:%3%;}") % fontfileformat % info.id % (info.ascent - info.descent) << endl;
} }
static string general_font_family(GfxFont * font) static string general_font_family(GfxFont * font)

View File

@ -19,7 +19,6 @@
#include "namespace.h" #include "namespace.h"
using std::all_of; using std::all_of;
using std::round;
FontInfo HTMLRenderer::install_font(GfxFont * font) FontInfo HTMLRenderer::install_font(GfxFont * font)
{ {
@ -68,7 +67,7 @@ FontInfo HTMLRenderer::install_font(GfxFont * font)
string suffix = dump_embedded_font(font, new_fn_id); string suffix = dump_embedded_font(font, new_fn_id);
if(suffix != "") if(suffix != "")
{ {
install_embedded_font(font, suffix, new_fn_id, cur_info_iter->second.use_tounicode); install_embedded_font(font, suffix, new_fn_id, cur_info_iter->second);
} }
else else
{ {
@ -99,7 +98,7 @@ FontInfo HTMLRenderer::install_font(GfxFont * font)
// TODO // TODO
// add a new function and move to text.cc // add a new function and move to text.cc
void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix, long long fn_id, bool & use_tounicode) void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix, long long fn_id, FontInfo & info)
{ {
string fn = (format("f%|1$x|") % fn_id).str(); string fn = (format("f%|1$x|") % fn_id).str();
@ -202,7 +201,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
* *
*/ */
use_tounicode = ((suffix == ".ttf") || (font->isCIDFont()) || (param->always_apply_tounicode)); info.use_tounicode = ((suffix == ".ttf") || (font->isCIDFont()) || (param->always_apply_tounicode));
auto ctu = font->getToUnicode(); auto ctu = font->getToUnicode();
@ -220,7 +219,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
Unicode u, *pu=&u; Unicode u, *pu=&u;
if(use_tounicode) if(info.use_tounicode)
{ {
int n = 0; int n = 0;
if(ctu) if(ctu)
@ -247,16 +246,45 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
if(ctu) if(ctu)
ctu->decRefCnt(); ctu->decRefCnt();
script_fout << format("Generate(%1%)") % ((param->single_html ? tmp_dir : dest_dir) / (fn+".ttf")) << endl; auto dest = ((param->single_html ? tmp_dir : dest_dir) / (fn+".ttf"));
if(param->single_html) if(param->single_html)
add_tmp_file(fn+".ttf"); add_tmp_file(fn+".ttf");
if(system((boost::format("fontforge -script %1% 2>%2%") % script_path % (tmp_dir / NULL_FILENAME)).str().c_str()) != 0) script_fout << format("Generate(%1%)") % dest << endl;
script_fout << format("Open(%1%, 1)") % dest << endl;
for(const string & s1 : {"Win", "Typo", "HHead"})
{
for(const string & s2 : {"Ascent", "Descent"})
{
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; cerr << "Warning: fontforge failed." << endl;
add_tmp_file(fn+".info");
add_tmp_file(NULL_FILENAME); add_tmp_file(NULL_FILENAME);
export_remote_font(fn_id, ".ttf", "truetype", font); // read metric
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;
}
else
{
info.ascent = font->getAscent();
info.descent = font->getDescent();
}
export_remote_font(info, ".ttf", "truetype", font);
} }
void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, long long fn_id) void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, long long fn_id)

View File

@ -354,11 +354,11 @@ void HTMLRenderer::prepare_line(GfxState * state)
// TODO class for height // 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|\">") html_fout << format("<div style=\"left:%1%px;bottom:%2%px;height:%4%px;line-height:%5%px;\" class=\"l t%|3$x|\">")
% x % x
% y % y
% cur_tm_id % cur_tm_id
% (state->getFont()->getAscent() * draw_font_size) % (cur_font_info.ascent * draw_font_size)
% (2*(state->getFont()->getAscent() * draw_font_size)) % (2 * cur_font_info.ascent * draw_font_size)
; ;
//resync position //resync position

View File

@ -14,6 +14,7 @@
#include <istream> #include <istream>
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>
#include <cmath>
#include <GfxState.h> #include <GfxState.h>
#include <GfxFont.h> #include <GfxFont.h>
@ -29,6 +30,7 @@ using std::noskipws;
using std::endl; using std::endl;
using std::flush; using std::flush;
using std::cerr; using std::cerr;
using std::floor;
// mute gcc warning of unused function // mute gcc warning of unused function
namespace namespace
@ -178,6 +180,7 @@ class FontInfo
public: public:
long long id; long long id;
bool use_tounicode; bool use_tounicode;
double ascent, descent;
}; };
// wrapper of the transform matrix double[6] // wrapper of the transform matrix double[6]