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

clean code: namespace

This commit is contained in:
Lu Wang 2012-08-14 17:13:29 +08:00
parent fd9a9ab783
commit 04eecc6cd3
12 changed files with 106 additions and 88 deletions

View File

@ -29,6 +29,7 @@ add_executable(pdf2htmlEX
src/HTMLRenderer/export.cc
src/HTMLRenderer/text.cc
src/HTMLRenderer/image.cc
src/HTMLRenderer/namespace.h
src/BackgroundRenderer.h
src/BackgroundRenderer.cc
src/Consts.h

View File

@ -7,11 +7,14 @@
#include "Consts.h"
using std::map;
using std::string;
const double EPS = 1e-6;
const std::string TMP_DIR="/tmp/pdf2htmlEX";
const string TMP_DIR="/tmp/pdf2htmlEX";
const std::map<std::string, std::string> BASE_14_FONT_CSS_FONT_MAP({
const map<string, string> BASE_14_FONT_CSS_FONT_MAP({
{ "Courier", "Courier,monospace" },
{ "Helvetica", "Helvetica,Arial,\"Nimbus Sans L\",sans-serif" },
{ "Times", "Times,\"Time New Roman\",\"Nimbus Roman No9 L\",serif" },
@ -21,7 +24,7 @@ const std::map<std::string, std::string> BASE_14_FONT_CSS_FONT_MAP({
const double id_matrix[6] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
const std::map<std::string, std::string> GB_ENCODED_FONT_NAME_MAP({
const map<string, string> GB_ENCODED_FONT_NAME_MAP({
{"\xCB\xCE\xCC\xE5", "SimSun"},
{"\xBA\xDA\xCC\xE5", "SimHei"},
{"\xBF\xAC\xCC\xE5_GB2312", "SimKai"},

View File

@ -7,9 +7,9 @@
#ifndef CONSTS_H__
#define CONSTS_H__
#include <string>
#include <map>
#include <string>
extern const double EPS;

View File

@ -9,11 +9,12 @@
#define HTMLRENDERER_H_
#include <algorithm>
#include <fstream>
#include <unordered_map>
#include <map>
#include <vector>
#include <boost/filesystem/fstream.hpp>
#include <OutputDev.h>
#include <GfxState.h>
#include <Stream.h>
@ -28,8 +29,6 @@
#include "Param.h"
#include "util.h"
using namespace std;
class HTMLRenderer : public OutputDev
{
public:
@ -115,9 +114,9 @@ class HTMLRenderer : public OutputDev
* remote font: to be retrieved from the web server
* local font: to be substituted with a local (client side) font
*/
void export_remote_font(long long fn_id, const string & suffix, const string & format, GfxFont * font);
void export_remote_font(long long fn_id, const std::string & suffix, const std::string & format, GfxFont * font);
void export_remote_default_font(long long fn_id);
void export_local_font(long long fn_id, GfxFont * font, const string & original_font_name, const string & cssfont);
void export_local_font(long long fn_id, GfxFont * font, const std::string & original_font_name, const std::string & cssfont);
void export_font_size(long long fs_id, double font_size);
void export_whitespace(long long ws_id, double ws_width);
void export_transform_matrix(long long tm_id, const double * tm);
@ -175,15 +174,15 @@ class HTMLRenderer : public OutputDev
// the position of next char, in text coords
double draw_tx, draw_ty;
ofstream html_fout, allcss_fout, fontscript_fout;
std::ofstream html_fout, allcss_fout, fontscript_fout;
class FontInfo{
public:
long long fn_id;
};
unordered_map<long long, FontInfo> font_name_map;
map<double, long long> font_size_map;
map<double, long long> whitespace_map;
std::unordered_map<long long, FontInfo> font_name_map;
std::map<double, long long> font_size_map;
std::map<double, long long> whitespace_map;
// transform matrix
class TM{
@ -206,36 +205,9 @@ class HTMLRenderer : public OutputDev
double _[6];
};
map<TM, long long> transform_matrix_map;
std::map<TM, long long> transform_matrix_map;
class Color{
public:
Color() {}
Color(const GfxRGB * rgb) {
_[0] = rgb->r;
_[1] = rgb->g;
_[2] = rgb->b;
}
bool operator < (const Color & c) const {
for(int i = 0; i < 3; ++i)
{
if(_[i] < c._[i])
return true;
if(_[i] > c._[i])
return false;
}
return false;
}
bool operator == (const Color & c) const {
for(int i = 0; i < 3; ++i)
if(_[i] != c._[i])
return false;
return true;
}
int _[3];
};
map<Color, long long> color_map;
std::map<GfxRGB, long long> color_map;
int image_count;

View File

@ -12,6 +12,8 @@
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
using std::string;
using std::endl;
/*
* CSS classes
@ -40,7 +42,7 @@ void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, co
double d = font->getDescent();
double r = _is_positive(a-d) ? (a/(a-d)) : 1.0;
for(const std::string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
for(const string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
{
allcss_fout << prefix << "transform-origin:0% " << (r*100.0) << "%;";
}
@ -50,7 +52,7 @@ void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, co
allcss_fout << "}" << endl;
}
static std::string general_font_family(GfxFont * font)
static string general_font_family(GfxFont * font)
{
if(font -> isFixedWidth())
return "monospace";
@ -83,7 +85,7 @@ void HTMLRenderer::export_local_font(long long fn_id, GfxFont * font, const stri
double d = font->getDescent();
double r = _is_positive(a-d) ? (a/(a-d)) : 1.0;
for(const std::string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
for(const string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
{
allcss_fout << prefix << "transform-origin:0% " << (r*100.0) << "%;";
}
@ -115,7 +117,7 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
}
else
{
for(const std::string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
for(const string & prefix : {"", "-ms-", "-moz-", "-webkit-", "-o-"})
{
// PDF use a different coordinate system from Web
allcss_fout << prefix << "transform:matrix("

View File

@ -19,6 +19,7 @@
#include "HTMLRenderer.h"
#include "BackgroundRenderer.h"
#include "config.h"
#include "namespace.h"
HTMLRenderer::HTMLRenderer(const Param * param)
:line_opened(false)
@ -44,7 +45,7 @@ HTMLRenderer::~HTMLRenderer()
void HTMLRenderer::process(PDFDoc *doc)
{
std::cerr << "Processing Text: ";
cerr << "Processing Text: ";
write_html_head();
xref = doc->getXRef();
for(int i = param->first_page; i <= param->last_page ; ++i)
@ -53,16 +54,16 @@ void HTMLRenderer::process(PDFDoc *doc)
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
std::cerr << ".";
std::cerr.flush();
cerr << ".";
cerr.flush();
}
write_html_tail();
std::cerr << std::endl;
cerr << endl;
if(param->process_nontext)
{
// Render non-text objects as image
std::cerr << "Processing Others: ";
cerr << "Processing Others: ";
// copied from poppler
SplashColor color;
color[0] = color[1] = color[2] = 255;
@ -77,22 +78,22 @@ void HTMLRenderer::process(PDFDoc *doc)
nullptr, nullptr, nullptr, nullptr);
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)(boost::format("p%|1$x|.png")%i).str().c_str(), param->h_dpi2, param->v_dpi2);
std::cerr << ".";
std::cerr.flush();
cerr << ".";
cerr.flush();
}
delete bg_renderer;
std::cerr << std::endl;
cerr << endl;
}
}
void HTMLRenderer::write_html_head()
{
html_fout << boost::filesystem::ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
}
void HTMLRenderer::write_html_tail()
{
html_fout << boost::filesystem::ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
}
void HTMLRenderer::startPage(int pageNum, GfxState *state)

View File

@ -14,7 +14,7 @@ const int *int_p_NULL = nullptr;
#include <boost/gil/extension/io/png_dynamic_io.hpp>
#include "HTMLRenderer.h"
#include "namespace.h"
void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int width, int height, GfxImageColorMap * colorMap, GBool interpolate, int *maskColors, GBool inlineImg)
{

View File

@ -14,6 +14,7 @@
#include <CharCodeToUnicode.h>
#include "HTMLRenderer.h"
#include "namespace.h"
long long HTMLRenderer::install_font(GfxFont * font)
{
@ -27,7 +28,7 @@ long long HTMLRenderer::install_font(GfxFont * font)
long long new_fn_id = font_name_map.size();
font_name_map.insert(std::make_pair(fn_id, FontInfo({new_fn_id})));
font_name_map.insert(make_pair(fn_id, FontInfo({new_fn_id})));
if(font == nullptr)
{
@ -37,16 +38,16 @@ long long HTMLRenderer::install_font(GfxFont * font)
if(param->debug)
{
std::cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << boost::format("f%|1$x|")%new_fn_id << std::endl;
cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << boost::format("f%|1$x|")%new_fn_id << endl;
}
if(font->getType() == fontType3) {
std::cerr << "Type 3 fonts are unsupported and will be rendered as Image" << std::endl;
cerr << "Type 3 fonts are unsupported and will be rendered as Image" << endl;
export_remote_default_font(new_fn_id);
return new_fn_id;
}
if(font->getWMode()) {
std::cerr << "Writing mode is unsupported and will be rendered as Image" << std::endl;
cerr << "Writing mode is unsupported and will be rendered as Image" << endl;
export_remote_default_font(new_fn_id);
return new_fn_id;
}
@ -58,7 +59,7 @@ long long HTMLRenderer::install_font(GfxFont * font)
{
case gfxFontLocEmbedded:
{
std::string suffix = dump_embedded_font(font, new_fn_id);
string suffix = dump_embedded_font(font, new_fn_id);
if(suffix != "")
{
install_embedded_font(font, suffix, new_fn_id);
@ -76,7 +77,7 @@ long long HTMLRenderer::install_font(GfxFont * font)
install_base_font(font, font_loc, new_fn_id);
break;
default:
std::cerr << "TODO: other font loc" << std::endl;
cerr << "TODO: other font loc" << endl;
export_remote_default_font(new_fn_id);
break;
}
@ -93,7 +94,7 @@ long long HTMLRenderer::install_font(GfxFont * font)
// TODO
// add a new function and move to text.cc
void HTMLRenderer::install_embedded_font(GfxFont * font, const std::string & suffix, long long fn_id)
void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix, long long fn_id)
{
// TODO Should use standard way to handle CID fonts
/*
@ -116,7 +117,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const std::string & suf
* generate an encoding file and let fontforge handle it.
*/
std::string fn = (boost::format("f%|1$x|") % fn_id).str();
string fn = (boost::format("f%|1$x|") % fn_id).str();
fontscript_fout << boost::format("Open(\"%1%/%2%%3%\",1)") % TMP_DIR % fn % suffix << endl;
@ -183,13 +184,13 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const std::string & suf
void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, long long fn_id)
{
std::string psname(font_loc->path->getCString());
string psname(font_loc->path->getCString());
string basename = psname.substr(0, psname.find('-'));
string cssfont;
auto iter = BASE_14_FONT_CSS_FONT_MAP.find(basename);
if(iter == BASE_14_FONT_CSS_FONT_MAP.end())
{
std::cerr << "PS Font: " << basename << " not found in the base 14 font map" << std::endl;
cerr << "PS Font: " << basename << " not found in the base 14 font map" << endl;
cssfont = "";
}
else
@ -200,14 +201,14 @@ void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, long
void HTMLRenderer::install_external_font( GfxFont * font, long long fn_id)
{
std::string fontname(font->getName()->getCString());
string fontname(font->getName()->getCString());
// 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;
std::cerr << "Warning: workaround for font names in bad encodings." << std::endl;
cerr << "Warning: workaround for font names in bad encodings." << endl;
}
export_local_font(fn_id, font, fontname, "");
@ -220,7 +221,7 @@ long long HTMLRenderer::install_font_size(double font_size)
return iter->second;
long long new_fs_id = font_size_map.size();
font_size_map.insert(std::make_pair(font_size, new_fs_id));
font_size_map.insert(make_pair(font_size, new_fs_id));
export_font_size(new_fs_id, font_size);
return new_fs_id;
}
@ -228,7 +229,7 @@ long long HTMLRenderer::install_font_size(double font_size)
long long HTMLRenderer::install_whitespace(double ws_width, double & actual_width)
{
auto iter = whitespace_map.lower_bound(ws_width - param->h_eps);
if((iter != whitespace_map.end()) && (std::abs(iter->first - 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;
@ -236,7 +237,7 @@ long long HTMLRenderer::install_whitespace(double ws_width, double & actual_widt
actual_width = ws_width;
long long new_ws_id = whitespace_map.size();
whitespace_map.insert(std::make_pair(ws_width, new_ws_id));
whitespace_map.insert(make_pair(ws_width, new_ws_id));
export_whitespace(new_ws_id, ws_width);
return new_ws_id;
}
@ -249,20 +250,20 @@ long long HTMLRenderer::install_transform_matrix(const double * tm)
return iter->second;
long long new_tm_id = transform_matrix_map.size();
transform_matrix_map.insert(std::make_pair(m, new_tm_id));
transform_matrix_map.insert(make_pair(m, new_tm_id));
export_transform_matrix(new_tm_id, tm);
return new_tm_id;
}
long long HTMLRenderer::install_color(const GfxRGB * rgb)
{
Color c(rgb);
const GfxRGB & c = *rgb;
auto iter = color_map.lower_bound(c);
if((iter != color_map.end()) && (c == (iter->first)))
return iter->second;
long long new_color_id = color_map.size();
color_map.insert(std::make_pair(c, new_color_id));
color_map.insert(make_pair(c, new_color_id));
export_color(new_color_id, rgb);
return new_color_id;
}

View File

@ -0,0 +1,23 @@
/*
* namespace.h
*
* specifying common used namespace
*
* by WangLu
*/
#ifndef NAMESPACE_H__
#define NAMESPACE_H__
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::make_pair;
using boost::filesystem::ifstream;
using boost::filesystem::ofstream;
using boost::filesystem::path;
#endif // NAMESPACE_H__

View File

@ -9,6 +9,7 @@
#include "HTMLRenderer.h"
#include "namespace.h"
void HTMLRenderer::check_state_change(GfxState * state)
{
@ -16,7 +17,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
if(all_changed || text_pos_changed)
{
if(!(std::abs(cur_ty - draw_ty) * draw_scale < param->v_eps))
if(!(abs(cur_ty - draw_ty) * draw_scale < param->v_eps))
{
close_line = true;
draw_ty = cur_ty;
@ -80,7 +81,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
double new_draw_ctm[6];
memcpy(new_draw_ctm, cur_ctm, sizeof(new_draw_ctm));
draw_scale = std::sqrt(new_draw_ctm[2] * new_draw_ctm[2] + new_draw_ctm[3] * new_draw_ctm[3]);
draw_scale = sqrt(new_draw_ctm[2] * new_draw_ctm[2] + new_draw_ctm[3] * new_draw_ctm[3]);
double new_draw_font_size = cur_font_size;
if(_is_positive(draw_scale))

View File

@ -12,8 +12,9 @@
#include <boost/format.hpp>
#include "HTMLRenderer.h"
#include "namespace.h"
std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
{
// mupdf consulted
@ -21,7 +22,7 @@ std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
Object obj, obj1, obj2;
Dict * dict = nullptr;
std::string suffix, subtype;
string suffix, subtype;
char buf[1024];
int len;
@ -35,7 +36,7 @@ std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
if(!font_obj.isDict())
{
std::cerr << "Font object is not a dictionary" << std::endl;
cerr << "Font object is not a dictionary" << endl;
goto err;
}
@ -44,12 +45,12 @@ std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
{
if(font_obj2.arrayGetLength() == 0)
{
std::cerr << "Warning: empty DescendantFonts array" << std::endl;
cerr << "Warning: empty DescendantFonts array" << endl;
}
else
{
if(font_obj2.arrayGetLength() > 1)
std::cerr << "TODO: multiple entries in DescendantFonts array" << std::endl;
cerr << "TODO: multiple entries in DescendantFonts array" << endl;
if(font_obj2.arrayGet(0, &obj2)->isDict())
{
@ -60,7 +61,7 @@ std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
if(!dict->lookup("FontDescriptor", &fontdesc_obj)->isDict())
{
std::cerr << "Cannot find FontDescriptor " << std::endl;
cerr << "Cannot find FontDescriptor " << endl;
goto err;
}
@ -81,13 +82,13 @@ std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
}
else
{
std::cerr << "Unknown subtype: " << subtype << std::endl;
cerr << "Unknown subtype: " << subtype << endl;
goto err;
}
}
else
{
std::cerr << "Invalid subtype in font descriptor" << std::endl;
cerr << "Invalid subtype in font descriptor" << endl;
goto err;
}
}
@ -101,13 +102,13 @@ std::string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
}
else
{
std::cerr << "Cannot find FontFile for dump" << std::endl;
cerr << "Cannot find FontFile for dump" << endl;
goto err;
}
if(suffix == "")
{
std::cerr << "Font type unrecognized" << std::endl;
cerr << "Font type unrecognized" << endl;
goto err;
}
@ -245,7 +246,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
if(!(_equal(ox, 0) && _equal(oy, 0)))
{
std::cerr << "TODO: non-zero origins" << std::endl;
cerr << "TODO: non-zero origins" << endl;
}
if(uLen == 0)

View File

@ -65,6 +65,19 @@ static inline void outputUnicodes(std::ostream & out, const Unicode * u, int uLe
}
}
static inline bool operator < (const GfxRGB & rgb1, const GfxRGB & rgb2)
{
if(rgb1.r < rgb2.r) return true;
if(rgb1.r > rgb2.r) return false;
if(rgb1.g < rgb2.g) return true;
if(rgb1.g > rgb2.g) return false;
return (rgb1.b < rgb2.b);
}
static inline bool operator == (const GfxRGB & rgb1, const GfxRGB & rgb2)
{
return ((rgb1.r == rgb2.r) && (rgb1.g == rgb2.g) && (rgb1.b == rgb1.b));
}
#endif //UTIL_H__