1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-03 08:38:39 +00:00

experimental: fix with of glphs accoring to the info in PDF

This commit is contained in:
Lu Wang 2012-09-20 13:24:16 +08:00
parent d17e340643
commit 1821a3dcfc
6 changed files with 83 additions and 13 deletions

View File

@ -45,6 +45,7 @@ HTMLRenderer::HTMLRenderer(const Param * param)
ffw_init(param->debug);
cur_mapping = new int32_t [0x10000];
cur_mapping2 = new char* [0x100];
width_list = new int [0x10000];
}
HTMLRenderer::~HTMLRenderer()
@ -53,6 +54,7 @@ HTMLRenderer::~HTMLRenderer()
clean_tmp_files();
delete [] cur_mapping;
delete [] cur_mapping2;
delete [] width_list;
}
static GBool annot_cb(Annot *, void *) {

View File

@ -11,6 +11,7 @@
#include <algorithm>
#include <unordered_set>
#include <cctype>
#include <cmath>
#include <CharCodeToUnicode.h>
#include <fofi/FoFiTrueType.h>
@ -24,6 +25,7 @@ namespace pdf2htmlEX {
using std::unordered_set;
using std::min;
using std::all_of;
using std::round;
string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
{
@ -165,6 +167,7 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
int maxcode = 0;
Gfx8BitFont * font_8bit = nullptr;
GfxCIDFont * font_cid = nullptr;
string suffix = get_suffix(filepath);
for(auto iter = suffix.begin(); iter != suffix.end(); ++iter)
@ -175,6 +178,13 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
const char * used_map = nullptr;
ffw_metric(&info.ascent, &info.descent, &info.em_size);
if(param->debug)
{
cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl;
}
if(!get_metric_only)
{
used_map = font_preprocessor.get_code_map(hash_ref(font->getID()));
@ -250,6 +260,7 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
}
else
{
font_cid = dynamic_cast<GfxCIDFont*>(font);
maxcode = 0xffff;
if(is_truetype_suffix(suffix))
@ -278,6 +289,8 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
* - For 8bit nonTruetype fonts:
* Try to calculate the correct Unicode value from the glyph names, unless param->always_apply_tounicode is set
*
*
* Also fill in the width_list, and set widths accordingly
*/
@ -286,18 +299,27 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
bool name_conflict_warned = false;
auto ctu = font->getToUnicode();
memset(cur_mapping, 0, 0x10000 * sizeof(int32_t));
memset(cur_mapping, -1, 0x10000 * sizeof(*cur_mapping));
memset(width_list, -1, 0x1000 * sizeof(*width_list));
if(code2GID)
maxcode = min(maxcode, code2GID_len - 1);
bool is_truetype = is_truetype_suffix(suffix);
int max_key = maxcode;
/*
* Traverse all possible codes
*/
for(int i = 0; i <= maxcode; ++i)
{
if(!used_map[i])
continue;
if(is_truetype_suffix(suffix) && (font_8bit != nullptr) && (font_8bit->getCharName(i) == nullptr))
/*
* Skip glyphs without names (only for non-ttf fonts)
*/
if(!is_truetype && (font_8bit != nullptr)
&& (font_8bit->getCharName(i) == nullptr))
{
continue;
}
@ -338,9 +360,22 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
cerr << "Warning: encoding confliction detected in font: " << hex << info.id << dec << endl;
}
}
if(font_8bit)
{
width_list[k] = (int)round(font_8bit->getWidth(i) * info.em_size);
}
else
{
char buf[2];
buf[0] = (i >> 8) & 0xff;
buf[1] = (i & 0xff);
width_list[k] = (int)round(font_cid->getWidth(buf, 2) * info.em_size);
}
}
ffw_reencode_raw(cur_mapping, max_key + 1, 1);
ffw_set_widths(width_list, max_key + 1);
if(ctu)
ctu->decRefCnt();
@ -357,21 +392,15 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
// TODO: see if we can get the values without save/load
/*
auto fn = str_fmt("%s/f%llx_.ttf", param->tmp_dir.c_str(), info.id);
add_tmp_file((char*)fn);
ffw_save((char*)fn);
ffw_close();
ffw_load_font((char*)fn);
*/
}
ffw_metric(&info.ascent, &info.descent);
if(param->debug)
{
cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl;
}
{
auto fn = str_fmt("%s/f%llx%s",
(param->single_html ? param->tmp_dir : param->dest_dir).c_str(),

View File

@ -18,6 +18,11 @@
#include "ffw.h"
static inline int min(int a, int b)
{
return (a<b)?a:b;
}
static FontViewBase * cur_fv = NULL;
static Encoding * original_enc = NULL;
static Encoding * enc_head = NULL;
@ -209,13 +214,17 @@ void ffw_close(void)
cur_fv = NULL;
}
void ffw_metric(double * ascent, double * descent)
void ffw_metric(double * ascent, double * descent, int * em_size)
{
DBounds bb;
SplineFont * sf = cur_fv->sf;
DBounds bb;
SplineFontFindBounds(sf, &bb);
struct pfminfo * info = &sf->pfminfo;
*em_size = sf->ascent + sf->descent;
/*
//debug
printf("bb %lf %lf\n", bb.maxy, bb.miny);
@ -267,3 +276,29 @@ void ffw_metric(double * ascent, double * descent)
sf->changed = true;
}
/*
* TODO:bitmap, reference have not been considered in this function
*/
void ffw_set_widths(int * width_list, int mapping_len)
{
SplineFont * sf = cur_fv->sf;
EncMap * map = cur_fv->map;
int i;
int imax = min(mapping_len, map->enccount);
for(i = 0; i < imax; ++i)
{
// TODO why need this
// when width_list[i] == -1, the code itself should be unused.
// but might be reference within ttf etc
if(width_list[i] == -1) continue;
int j = map->map[i];
if(j == -1) continue;
SplineChar * sc = sf->glyphs[j];
if(sc == NULL) continue;
sc->width = width_list[i];
}
}

View File

@ -357,6 +357,7 @@ class HTMLRenderer : public OutputDev
// for font reencoding
int32_t * cur_mapping;
char ** cur_mapping2;
int * width_list;
FontPreprocessor font_preprocessor;
// for string formatting

View File

@ -31,7 +31,9 @@ void ffw_save(const char * filename);
void ffw_close(void);
// fix metrics and get them
void ffw_metric(double * ascent, double * descent);
void ffw_metric(double * ascent, double * descent, int * em_size);
void ffw_set_widths(int * width_list, int mapping_len);
#ifdef __cplusplus
}

View File

@ -109,6 +109,7 @@ class FontInfo
public:
long long id;
bool use_tounicode;
int em_size;
double ascent, descent;
bool has_space; // whether space is included in the font
};