1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-08-25 12:57:40 +00:00
pdf2htmlEX/src/HTMLRenderer/install.cc

316 lines
8.9 KiB
C++
Raw Normal View History

2012-08-14 06:35:55 +00:00
/*
* install.cc
*
* maintaining all known styles
*
* by WangLu
* 2012.08.14
*/
#include <iostream>
#include <cmath>
#include <algorithm>
2012-08-14 06:35:55 +00:00
#include <GlobalParams.h>
2012-08-31 09:06:19 +00:00
#include "Param.h"
2012-08-14 06:35:55 +00:00
#include "HTMLRenderer.h"
2012-11-29 09:28:05 +00:00
#include "util/namespace.h"
2012-11-29 10:16:05 +00:00
#include "util/math.h"
2012-11-29 10:28:07 +00:00
#include "util/misc.h"
2012-08-26 15:56:38 +00:00
2012-09-12 15:26:14 +00:00
namespace pdf2htmlEX {
2012-08-31 15:14:05 +00:00
using std::abs;
2012-11-29 10:28:07 +00:00
using std::cerr;
using std::endl;
2012-08-31 15:14:05 +00:00
2012-09-04 15:33:15 +00:00
const FontInfo * HTMLRenderer::install_font(GfxFont * font)
2012-08-14 06:35:55 +00:00
{
assert(sizeof(long long) == 2*sizeof(int));
2012-09-06 16:58:23 +00:00
long long fn_id = (font == nullptr) ? 0 : hash_ref(font->getID());
2012-08-14 06:35:55 +00:00
auto iter = font_name_map.find(fn_id);
if(iter != font_name_map.end())
2012-09-04 15:33:15 +00:00
return &(iter->second);
2012-08-14 06:35:55 +00:00
long long new_fn_id = font_name_map.size();
auto cur_info_iter = font_name_map.insert(make_pair(fn_id, FontInfo({new_fn_id, true}))).first;
2012-08-14 06:35:55 +00:00
if(font == nullptr)
{
export_remote_default_font(new_fn_id);
2012-09-04 15:33:15 +00:00
return &(cur_info_iter->second);
2012-08-14 06:35:55 +00:00
}
cur_info_iter->second.ascent = font->getAscent();
cur_info_iter->second.descent = font->getDescent();
2012-08-14 06:35:55 +00:00
if(param->debug)
{
2012-09-07 16:38:41 +00:00
cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << "f" << hex << new_fn_id << dec << endl;
2012-08-14 06:35:55 +00:00
}
if(font->getType() == fontType3) {
2012-08-14 09:13:29 +00:00
cerr << "Type 3 fonts are unsupported and will be rendered as Image" << endl;
2012-08-14 06:35:55 +00:00
export_remote_default_font(new_fn_id);
2012-09-04 15:33:15 +00:00
return &(cur_info_iter->second);
2012-08-14 06:35:55 +00:00
}
if(font->getWMode()) {
2012-08-14 09:13:29 +00:00
cerr << "Writing mode is unsupported and will be rendered as Image" << endl;
2012-08-14 06:35:55 +00:00
export_remote_default_font(new_fn_id);
2012-09-04 15:33:15 +00:00
return &(cur_info_iter->second);
2012-08-14 06:35:55 +00:00
}
auto * font_loc = font->locateFont(xref, gTrue);
if(font_loc != nullptr)
{
switch(font_loc -> locType)
{
case gfxFontLocEmbedded:
install_embedded_font(font, cur_info_iter->second);
2012-08-14 06:35:55 +00:00
break;
case gfxFontLocExternal:
install_external_font(font, cur_info_iter->second);
2012-08-14 06:35:55 +00:00
break;
case gfxFontLocResident:
install_base_font(font, font_loc, cur_info_iter->second);
2012-08-14 06:35:55 +00:00
break;
default:
2012-08-14 09:13:29 +00:00
cerr << "TODO: other font loc" << endl;
2012-08-14 06:35:55 +00:00
export_remote_default_font(new_fn_id);
break;
}
delete font_loc;
}
else
{
export_remote_default_font(new_fn_id);
}
2012-09-04 15:33:15 +00:00
return &(cur_info_iter->second);
2012-08-14 06:35:55 +00:00
}
void HTMLRenderer::install_embedded_font(GfxFont * font, FontInfo & info)
2012-08-14 06:35:55 +00:00
{
auto path = dump_embedded_font(font, info.id);
2012-09-09 17:27:32 +00:00
if(path != "")
2012-08-26 15:56:38 +00:00
{
embed_font(path, font, info);
export_remote_font(info, param->font_suffix, param->font_format, font);
2012-08-26 15:56:38 +00:00
}
else
{
export_remote_default_font(info.id);
}
}
void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, FontInfo & info)
{
string psname(font_loc->path->getCString());
string basename = psname.substr(0, psname.find('-'));
GfxFontLoc * localfontloc = font->locateFont(xref, gFalse);
if(param->embed_base_font)
2012-08-30 15:36:30 +00:00
{
if(localfontloc != nullptr)
{
embed_font(localfontloc->path->getCString(), font, info);
export_remote_font(info, param->font_suffix, param->font_format, font);
delete localfontloc;
return;
}
else
{
2012-09-07 16:38:41 +00:00
cerr << "Cannot embed base font: f" << hex << info.id << dec << ' ' << psname << endl;
// fallback to exporting by name
}
2012-08-30 15:36:30 +00:00
}
2012-08-14 06:35:55 +00:00
string cssfont;
auto iter = BASE_14_FONT_CSS_FONT_MAP.find(basename);
if(iter == BASE_14_FONT_CSS_FONT_MAP.end())
{
2012-08-14 09:13:29 +00:00
cerr << "PS Font: " << basename << " not found in the base 14 font map" << endl;
2012-08-14 06:35:55 +00:00
cssfont = "";
}
else
cssfont = iter->second;
// still try to get an idea of read ascent/descent
if(localfontloc != nullptr)
{
// fill in ascent/descent only, do not embed
2012-09-09 16:21:46 +00:00
embed_font(string(localfontloc->path->getCString()), font, info, true);
delete localfontloc;
}
else
{
info.ascent = font->getAscent();
info.descent = font->getDescent();
}
export_local_font(info, font, psname, cssfont);
2012-08-14 06:35:55 +00:00
}
void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info)
2012-08-14 06:35:55 +00:00
{
2012-08-14 09:13:29 +00:00
string fontname(font->getName()->getCString());
2012-08-14 06:35:55 +00:00
// resolve bad encodings in GB
auto iter = GB_ENCODED_FONT_NAME_MAP.find(fontname);
if(iter != GB_ENCODED_FONT_NAME_MAP.end())
{
fontname = iter->second;
2012-08-14 09:13:29 +00:00
cerr << "Warning: workaround for font names in bad encodings." << endl;
2012-08-14 06:35:55 +00:00
}
2012-08-31 09:06:19 +00:00
GfxFontLoc * localfontloc = font->locateFont(xref, gFalse);
2012-08-31 09:06:19 +00:00
if(param->embed_external_font)
2012-08-31 09:06:19 +00:00
{
if(localfontloc != nullptr)
{
2012-09-09 16:21:46 +00:00
embed_font(string(localfontloc->path->getCString()), font, info);
export_remote_font(info, param->font_suffix, param->font_format, font);
delete localfontloc;
return;
}
else
{
2012-09-07 16:38:41 +00:00
cerr << "Cannot embed external font: f" << hex << info.id << dec << ' ' << fontname << endl;
// fallback to exporting by name
}
}
// still try to get an idea of read ascent/descent
if(localfontloc != nullptr)
{
// fill in ascent/descent only, do not embed
2012-09-09 16:21:46 +00:00
embed_font(string(localfontloc->path->getCString()), font, info, true);
delete localfontloc;
}
else
{
info.ascent = font->getAscent();
info.descent = font->getDescent();
2012-08-31 09:06:19 +00:00
}
2012-08-14 06:35:55 +00:00
export_local_font(info, font, fontname, "");
2012-08-14 06:35:55 +00:00
}
long long HTMLRenderer::install_font_size(double font_size)
{
auto iter = font_size_map.lower_bound(font_size - EPS);
2012-11-29 10:16:05 +00:00
if((iter != font_size_map.end()) && (equal(iter->first, font_size)))
2012-08-14 06:35:55 +00:00
return iter->second;
long long new_fs_id = font_size_map.size();
2012-08-14 09:13:29 +00:00
font_size_map.insert(make_pair(font_size, new_fs_id));
2012-08-14 06:35:55 +00:00
export_font_size(new_fs_id, font_size);
return new_fs_id;
}
long long HTMLRenderer::install_transform_matrix(const double * tm)
{
2012-10-02 18:19:40 +00:00
Matrix m;
memcpy(m.m, tm, sizeof(m.m));
2012-08-14 06:35:55 +00:00
auto iter = transform_matrix_map.lower_bound(m);
2012-11-29 10:16:05 +00:00
if((iter != transform_matrix_map.end()) && (tm_equal(m.m, iter->first.m, 4)))
2012-08-14 06:35:55 +00:00
return iter->second;
long long new_tm_id = transform_matrix_map.size();
2012-08-14 09:13:29 +00:00
transform_matrix_map.insert(make_pair(m, new_tm_id));
2012-08-14 06:35:55 +00:00
export_transform_matrix(new_tm_id, tm);
return new_tm_id;
}
long long HTMLRenderer::install_letter_space(double letter_space)
{
auto iter = letter_space_map.lower_bound(letter_space - EPS);
2012-11-29 10:16:05 +00:00
if((iter != letter_space_map.end()) && (equal(iter->first, letter_space)))
return iter->second;
long long new_ls_id = letter_space_map.size();
letter_space_map.insert(make_pair(letter_space, new_ls_id));
export_letter_space(new_ls_id, letter_space);
return new_ls_id;
}
long long HTMLRenderer::install_word_space(double word_space)
{
auto iter = word_space_map.lower_bound(word_space - EPS);
2012-11-29 10:16:05 +00:00
if((iter != word_space_map.end()) && (equal(iter->first, word_space)))
return iter->second;
long long new_ws_id = word_space_map.size();
word_space_map.insert(make_pair(word_space, new_ws_id));
export_word_space(new_ws_id, word_space);
return new_ws_id;
}
2012-08-14 06:35:55 +00:00
long long HTMLRenderer::install_color(const GfxRGB * rgb)
{
2012-08-14 09:13:29 +00:00
const GfxRGB & c = *rgb;
auto iter = color_map.find(c);
if(iter != color_map.end())
2012-08-14 06:35:55 +00:00
return iter->second;
long long new_color_id = color_map.size();
2012-08-14 09:13:29 +00:00
color_map.insert(make_pair(c, new_color_id));
2012-08-14 06:35:55 +00:00
export_color(new_color_id, rgb);
return new_color_id;
}
long long HTMLRenderer::install_whitespace(double ws_width, double & actual_width)
{
// ws_width is already mulitpled by draw_scale
auto iter = whitespace_map.lower_bound(ws_width - param->h_eps);
if((iter != whitespace_map.end()) && (abs(iter->first - ws_width) < param->h_eps))
{
actual_width = iter->first;
return iter->second;
}
actual_width = ws_width;
long long new_ws_id = whitespace_map.size();
whitespace_map.insert(make_pair(ws_width, new_ws_id));
export_whitespace(new_ws_id, ws_width);
return new_ws_id;
}
2012-08-24 17:40:43 +00:00
long long HTMLRenderer::install_rise(double rise)
{
auto iter = rise_map.lower_bound(rise - param->v_eps);
if((iter != rise_map.end()) && (abs(iter->first - rise) < param->v_eps))
{
return iter->second;
}
long long new_rise_id = rise_map.size();
rise_map.insert(make_pair(rise, new_rise_id));
export_rise(new_rise_id, rise);
return new_rise_id;
}
2012-09-12 15:26:14 +00:00
2012-09-16 07:53:41 +00:00
long long HTMLRenderer::install_height(double height)
{
auto iter = height_map.lower_bound(height - EPS);
if((iter != height_map.end()) && (abs(iter->first - height) < EPS))
{
return iter->second;
}
long long new_height_id = height_map.size();
height_map.insert(make_pair(height, new_height_id));
export_height(new_height_id, height);
return new_height_id;
}
2012-09-12 15:26:14 +00:00
} // namespace pdf2htmlEX