1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-09-28 17:21:29 +00:00

clean code

This commit is contained in:
Lu Wang 2012-08-14 17:50:16 +08:00
parent e050500328
commit 0182ea303b
9 changed files with 57 additions and 47 deletions

View File

@ -13,6 +13,7 @@
#include <map>
#include <vector>
#include <boost/format.hpp>
#include <boost/filesystem/fstream.hpp>
#include <OutputDev.h>
@ -103,7 +104,7 @@ 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 std::string & suffix, const std::string & format, GfxFont * font);
void export_remote_font(long long fn_id, const std::string & suffix, const std::string & fontfileformat, GfxFont * font);
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_font_size(long long fs_id, double font_size);
@ -126,7 +127,7 @@ class HTMLRenderer : public OutputDev
XRef * xref;
// page info
int pageNum ;
int pageNum;
double pageWidth ;
double pageHeight ;
@ -134,11 +135,11 @@ class HTMLRenderer : public OutputDev
////////////////////////////////////////////////////
// states
////////////////////////////////////////////////////
bool all_changed;
// if we have a pending opened line
bool line_opened;
// any state changed
bool all_changed;
// current position
double cur_tx, cur_ty; // real text position, in text coords
bool text_pos_changed;
@ -177,8 +178,6 @@ class HTMLRenderer : public OutputDev
// styles & resources
////////////////////////////////////////////////////
boost::filesystem::ofstream html_fout, allcss_fout, fontscript_fout;
std::unordered_map<long long, FontInfo> font_name_map;
std::map<double, long long> font_size_map;
std::map<double, long long> whitespace_map;
@ -188,6 +187,8 @@ class HTMLRenderer : public OutputDev
int image_count;
const Param * param;
boost::filesystem::path dest_dir, tmp_dir;
boost::filesystem::ofstream html_fout, allcss_fout, fontscript_fout;
};
#endif /* HTMLRENDERER_H_ */

View File

@ -7,13 +7,13 @@
* 2012.08.14
*/
#include "HTMLRenderer.h"
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
using std::string;
using std::endl;
#include "HTMLRenderer.h"
#include "namespace.h"
using boost::algorithm::ifind_first;
/*
* CSS classes
@ -34,9 +34,9 @@ using std::endl;
*
*/
void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, const string & format, GfxFont * font)
void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix, const string & fontfileformat, GfxFont * font)
{
allcss_fout << boost::format("@font-face{font-family:f%|1$x|;src:url(f%|1$x|%2%)format(\"%3%\");}.f%|1$x|{font-family:f%|1$x|;") % fn_id % suffix % format;
allcss_fout << format("@font-face{font-family:f%|1$x|;src:url(f%|1$x|%2%)format(\"%3%\");}.f%|1$x|{font-family:f%|1$x|;") % fn_id % suffix % fontfileformat;
double a = font->getAscent();
double d = font->getDescent();
@ -65,18 +65,18 @@ static string general_font_family(GfxFont * font)
// TODO: this function is called when some font is unable to process, may use the name there as a hint
void HTMLRenderer::export_remote_default_font(long long fn_id)
{
allcss_fout << boost::format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id << endl;
allcss_fout << format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id << endl;
}
void HTMLRenderer::export_local_font(long long fn_id, GfxFont * font, const string & original_font_name, const string & cssfont)
{
allcss_fout << boost::format(".f%|1$x|{") % fn_id;
allcss_fout << format(".f%|1$x|{") % fn_id;
allcss_fout << "font-family:" << ((cssfont == "") ? (original_font_name + "," + general_font_family(font)) : cssfont) << ";";
if(font->isBold())
allcss_fout << "font-weight:bold;";
if(boost::algorithm::ifind_first(original_font_name, "oblique"))
if(ifind_first(original_font_name, "oblique"))
allcss_fout << "font-style:oblique;";
else if(font->isItalic())
allcss_fout << "font-style:italic;";
@ -98,17 +98,17 @@ void HTMLRenderer::export_local_font(long long fn_id, GfxFont * font, const stri
void HTMLRenderer::export_font_size (long long fs_id, double font_size)
{
allcss_fout << boost::format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size << endl;
allcss_fout << format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size << endl;
}
void HTMLRenderer::export_whitespace (long long ws_id, double ws_width)
{
allcss_fout << boost::format(".w%|1$x|{width:%2%px;}") % ws_id % ws_width << endl;
allcss_fout << format(".w%|1$x|{width:%2%px;}") % ws_id % ws_width << endl;
}
void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
{
allcss_fout << boost::format(".t%|1$x|{") % tm_id;
allcss_fout << format(".t%|1$x|{") % tm_id;
// TODO: recognize common matices
if(_tm_equal(tm, id_matrix))
@ -127,9 +127,9 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
<< tm[3] << ',';
if(prefix == "-moz-")
allcss_fout << boost::format("%1%px,%2%px);") % tm[4] % -tm[5];
allcss_fout << format("%1%px,%2%px);") % tm[4] % -tm[5];
else
allcss_fout << boost::format("%1%,%2%);") % tm[4] % -tm[5];
allcss_fout << format("%1%,%2%);") % tm[4] % -tm[5];
}
}
allcss_fout << "}" << endl;
@ -137,7 +137,7 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
void HTMLRenderer::export_color(long long color_id, const GfxRGB * rgb)
{
allcss_fout << boost::format(".c%|1$x|{color:rgb(%2%,%3%,%4%);}")
allcss_fout << format(".c%|1$x|{color:rgb(%2%,%3%,%4%);}")
% color_id % (int)colToByte(rgb->r) % (int)colToByte(rgb->g) % (int)colToByte(rgb->b)
<< endl;
}

View File

@ -23,11 +23,13 @@
HTMLRenderer::HTMLRenderer(const Param * param)
:line_opened(false)
,html_fout(param->output_filename.c_str(), ofstream::binary)
,allcss_fout("all.css")
,fontscript_fout(TMP_DIR+"/convert.pe")
,image_count(0)
,param(param)
,dest_dir(param->dest_dir)
,tmp_dir(TMP_DIR)
,html_fout(dest_dir / param->output_filename, ofstream::binary) // we may output utf8 characters, so use binary
,allcss_fout(dest_dir / "all.css", ofstream::binary)
,fontscript_fout(tmp_dir / "convert.pe", ofstream::binary)
{
// install default font & size
install_font(nullptr);
@ -76,7 +78,7 @@ void HTMLRenderer::process(PDFDoc *doc)
doc->displayPage(bg_renderer, i, param->h_dpi2, param->v_dpi2,
0, true, false, false,
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);
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)(format("p%|1$x|.png")%i).str().c_str(), param->h_dpi2, param->v_dpi2);
cerr << ".";
cerr.flush();
@ -104,9 +106,9 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
assert(!line_opened);
html_fout << boost::format("<div id=\"page-%3%\" class=\"p\" style=\"width:%1%px;height:%2%px;") % pageWidth % pageHeight % pageNum;
html_fout << format("<div id=\"page-%3%\" class=\"p\" style=\"width:%1%px;height:%2%px;") % pageWidth % pageHeight % pageNum;
html_fout << boost::format("background-image:url(p%|3$x|.png);background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;") % pageWidth % pageHeight % pageNum;
html_fout << format("background-image:url(p%|3$x|.png);background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;") % pageWidth % pageHeight % pageNum;
html_fout << "\">" << endl;

View File

@ -16,12 +16,14 @@ const int *int_p_NULL = nullptr;
#include "HTMLRenderer.h"
#include "namespace.h"
using namespace boost::gil;
void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int width, int height, GfxImageColorMap * colorMap, GBool interpolate, int *maskColors, GBool inlineImg)
{
if(maskColors)
return;
boost::gil::rgb8_image_t img(width, height);
rgb8_image_t img(width, height);
auto imgview = view(img);
auto loc = imgview.xy_at(0,0);
@ -36,7 +38,7 @@ void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int w
GfxRGB rgb;
colorMap->getRGB(p, &rgb);
*loc = boost::gil::rgb8_pixel_t(colToByte(rgb.r), colToByte(rgb.g), colToByte(rgb.b));
*loc = rgb8_pixel_t(colToByte(rgb.r), colToByte(rgb.g), colToByte(rgb.b));
p += colorMap->getNumPixelComps();
@ -46,7 +48,7 @@ void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int w
loc = imgview.xy_at(0, i+1);
}
boost::gil::png_write_view((boost::format("i%|1$x|.png")%image_count).str(), imgview);
png_write_view((format("i%|1$x|.png")%image_count).str(), imgview);
img_stream->close();
delete img_stream;
@ -55,7 +57,7 @@ void HTMLRenderer::drawImage(GfxState * state, Object * ref, Stream * str, int w
double * ctm = state->getCTM();
ctm[4] = ctm[5] = 0.0;
html_fout << boost::format("<img class=\"i t%2%\" style=\"left:%3%px;bottom:%4%px;width:%5%px;height:%6%px;\" src=\"i%|1$x|.png\" />") % image_count % install_transform_matrix(ctm) % state->getCurX() % state->getCurY() % width % height << endl;
html_fout << format("<img class=\"i t%2%\" style=\"left:%3%px;bottom:%4%px;width:%5%px;height:%6%px;\" src=\"i%|1$x|.png\" />") % image_count % install_transform_matrix(ctm) % state->getCurX() % state->getCurY() % width % height << endl;
++ image_count;

View File

@ -38,7 +38,7 @@ long long HTMLRenderer::install_font(GfxFont * font)
if(param->debug)
{
cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << boost::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;
}
if(font->getType() == fontType3) {
@ -117,9 +117,9 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
* generate an encoding file and let fontforge handle it.
*/
string fn = (boost::format("f%|1$x|") % fn_id).str();
string fn = (format("f%|1$x|") % fn_id).str();
fontscript_fout << boost::format("Open(\"%1%/%2%%3%\",1)") % TMP_DIR % fn % suffix << endl;
fontscript_fout << format("Open(\"%1%\", 1)") % (tmp_dir / (fn + suffix)) << endl;
auto ctu = font->getToUnicode();
int * code2GID = nullptr;
@ -141,7 +141,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
}
else
{
fontscript_fout << boost::format("Reencode(\"original\")") << endl;
fontscript_fout << format("Reencode(\"original\")") << endl;
int len;
// code2GID has been stored for embedded CID fonts
code2GID = dynamic_cast<GfxCIDFont*>(font)->getCodeToGIDMap(nullptr, &len);
@ -150,7 +150,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
if(maxcode > 0)
{
ofstream map_fout((boost::format("%1%/%2%.encoding") % TMP_DIR % fn).str().c_str());
ofstream map_fout(tmp_dir / (fn + ".encoding"));
int cnt = 0;
for(int i = 0; i <= maxcode; ++i)
{
@ -160,24 +160,24 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
if(n > 0)
{
++cnt;
map_fout << boost::format("0x%|1$X|") % (code2GID ? code2GID[i] : i);
map_fout << format("0x%|1$X|") % (code2GID ? code2GID[i] : i);
for(int j = 0; j < n; ++j)
map_fout << boost::format(" 0x%|1$X|") % u[j];
map_fout << boost::format(" # 0x%|1$X|") % i << endl;
map_fout << format(" 0x%|1$X|") % u[j];
map_fout << format(" # 0x%|1$X|") % i << endl;
}
}
if(cnt > 0)
{
fontscript_fout << boost::format("LoadEncodingFile(\"%1%/%2%.encoding\", \"%2%\")") % TMP_DIR % fn << endl;
fontscript_fout << boost::format("Reencode(\"%1%\", 1)") % fn << endl;
fontscript_fout << format("LoadEncodingFile(\"%1%\", \"%2%\")") % (tmp_dir / (fn+".encoding")) % fn << endl;
fontscript_fout << format("Reencode(\"%1%\", 1)") % fn << endl;
}
}
ctu->decRefCnt();
}
fontscript_fout << boost::format("Generate(\"%1%.ttf\")") % fn << endl;
fontscript_fout << format("Generate(\"%1%.ttf\")") % (dest_dir / fn) << endl;
export_remote_font(fn_id, ".ttf", "truetype", font);
}

View File

@ -18,6 +18,7 @@ using std::make_pair;
using boost::filesystem::ifstream;
using boost::filesystem::ofstream;
using boost::filesystem::path;
using boost::format;
#endif // NAMESPACE_H__

View File

@ -113,7 +113,7 @@ string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
}
obj.streamReset();
outf.open((boost::format("%1%/f%|2$x|%3%")%TMP_DIR%fn_id%suffix).str().c_str(), ofstream::binary);
outf.open(tmp_dir / (format("f%|1$x|%2%")%fn_id%suffix).str(), ofstream::binary);
while((len = obj.streamGetChars(1024, (Guchar*)buf)) > 0)
{
outf.write(buf, len);
@ -162,7 +162,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
{
double w;
auto wid = install_whitespace(target, w);
html_fout << boost::format("<span class=\"w w%|1$x|\"> </span>") % wid;
html_fout << format("<span class=\"w w%|1$x|\"> </span>") % wid;
draw_tx += w / draw_scale;
}
}
@ -179,11 +179,11 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
// classes
html_fout << "<div class=\"l "
<< boost::format("f%|1$x| s%|2$x| c%|3$x|") % cur_fn_id % cur_fs_id % cur_color_id;
<< format("f%|1$x| s%|2$x| c%|3$x|") % cur_fn_id % cur_fs_id % cur_color_id;
// "t0" is the id_matrix
if(cur_tm_id != 0)
html_fout << boost::format(" t%|1$x|") % cur_tm_id;
html_fout << format(" t%|1$x|") % cur_tm_id;
{
double x,y; // in user space
@ -211,7 +211,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
html_fout << "\"";
double x,y;
state->transform(state->getCurX(), state->getCurY(), &x, &y);
html_fout << boost::format("data-lx=\"%5%\" data-ly=\"%6%\" data-drawscale=\"%4%\" data-x=\"%1%\" data-y=\"%2%\" data-hs=\"%3%")
html_fout << format("data-lx=\"%5%\" data-ly=\"%6%\" data-drawscale=\"%4%\" data-x=\"%1%\" data-y=\"%2%\" data-hs=\"%3%")
%x%y%(state->getHorizScaling())%draw_scale%state->getLineX()%state->getLineY();
#endif
}

View File

@ -15,6 +15,9 @@ struct Param
{
std::string owner_password, user_password;
std::string input_filename, output_filename;
std::string dest_dir;
int first_page, last_page;
double h_dpi, v_dpi;

View File

@ -121,6 +121,7 @@ po::variables_map parse_options (int argc, char **argv)
("metadata,m", "show the document meta data in JSON")
("owner-password,o", po::value<string>(&param.owner_password)->default_value(""), "owner password (for encrypted files)")
("user-password,u", po::value<string>(&param.user_password)->default_value(""), "user password (for encrypted files)")
("dest-dir", po::value<string>(&param.dest_dir)->default_value("."), "destination directory")
("hdpi", po::value<double>(&param.h_dpi)->default_value(72.0), "horizontal DPI for text")
("vdpi", po::value<double>(&param.v_dpi)->default_value(72.0), "vertical DPI for text")
("hdpi2", po::value<double>(&param.h_dpi2)->default_value(144.0), "horizontal DPI for non-text")