1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-08 19:00:33 +00:00

git rid of boost::format

This commit is contained in:
Lu Wang 2012-09-08 00:38:41 +08:00
parent ba00a92a71
commit ff9b079a4f
10 changed files with 78 additions and 58 deletions

View File

@ -50,7 +50,7 @@ Special thanks to Arthur Titeica for the [AUR Package](https://aur.archlinux.org
* CMake * CMake
* compilers support C++11 * compilers support C++11
* libpoppler with xpdf header >= 0.20.2 (compile with --enable-xpdf-headers) * libpoppler with xpdf header >= 0.20.2 (compile with --enable-xpdf-headers)
* boost c++ library (format, program options, gil, filesystem, serialization, system(which is actually required by filesystem)) * boost c++ library (program options, gil, filesystem, serialization, system(which is actually required by filesystem))
* fontforge (with header files) * fontforge (with header files)
#### Compiling #### Compiling

View File

@ -14,7 +14,6 @@
#include <sstream> #include <sstream>
#include <cstdint> #include <cstdint>
#include <boost/format.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <OutputDev.h> #include <OutputDev.h>
@ -336,6 +335,9 @@ class HTMLRenderer : public OutputDev
char ** cur_mapping2; char ** cur_mapping2;
FontPreprocessor font_preprocessor; FontPreprocessor font_preprocessor;
// for string formatting
string_formatter str_fmt;
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
// styles & resources // styles & resources
//////////////////////////////////////////////////// ////////////////////////////////////////////////////

View File

@ -75,11 +75,7 @@ void HTMLRenderer::LineBuffer::flush(void)
// TODO: class for height ? // TODO: class for height ?
ostream & out = renderer->html_fout; ostream & out = renderer->html_fout;
out << format("<div style=\"left:%1%px;bottom:%2%px;height:%3%px;\" class=\"l t%|4$x|\">") out << "<div style=\"left:" << x << "px;bottom:" << y << "px;height:" << max_ascent << "px;\" class=\"l t" << tm_id << "\">";
% x % y
% max_ascent
% tm_id
;
auto cur_state_iter = states.begin(); auto cur_state_iter = states.begin();
auto cur_offset_iter = offsets.begin(); auto cur_offset_iter = offsets.begin();
@ -142,7 +138,7 @@ void HTMLRenderer::LineBuffer::flush(void)
auto * p = stack.back(); auto * p = stack.back();
double threshold = p->draw_font_size * (p->ascent - p->descent) * (renderer->param->space_threshold); double threshold = p->draw_font_size * (p->ascent - p->descent) * (renderer->param->space_threshold);
out << format("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > (threshold - EPS) ? " " : ""); out << "<span class=\"_ _" << wid << "\">" << (target > (threshold - EPS) ? " " : "") << "</span>";
dx = target - w; dx = target - w;
@ -203,7 +199,8 @@ void HTMLRenderer::LineBuffer::State::begin (ostream & out, const State * prev_s
out << ' '; out << ' ';
} }
out << format("%1%%|2$x|") % format_str[i] % ids[i]; // out should has set hex
out << format_str[i] << ids[i];
} }
if(first) if(first)

View File

@ -7,8 +7,9 @@
* 2012.08.14 * 2012.08.14
*/ */
#include <sstream>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include "HTMLRenderer.h" #include "HTMLRenderer.h"
#include "namespace.h" #include "namespace.h"
@ -18,20 +19,21 @@ using boost::algorithm::ifind_first;
void HTMLRenderer::export_remote_font(const FontInfo & info, 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(") % info.id; allcss_fout << "@font-face{font-family:f" << info.id << ";src:url(";
const std::string fn = (format("f%|1$x|") % info.id).str(); const char * fn = str_fmt("f%llx%s", info.id, suffix.c_str());
if(param->single_html) if(param->single_html)
{ {
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / (fn+suffix), ifstream::binary)) << "'"; allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'";
} }
else else
{ {
allcss_fout << (fn+suffix); allcss_fout << fn;
} }
allcss_fout << format(")format(\"%1%\");}.f%|2$x|{font-family:f%|2$x|;line-height:%3%;}") % fontfileformat % info.id % (info.ascent - info.descent) << endl; allcss_fout << ")format(\"" << fontfileformat << "\");}.f" << info.id << "{font-family:f" << info.id << ";line-height:" << (info.ascent - info.descent) << ";}" << endl;
} }
static string general_font_family(GfxFont * font) static string general_font_family(GfxFont * font)
@ -47,12 +49,12 @@ 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 // 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) void HTMLRenderer::export_remote_default_font(long long fn_id)
{ {
allcss_fout << format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id << endl; allcss_fout << ".f" << fn_id << "{font-family:sans-serif;color:transparent;visibility:hidden;}" << endl;
} }
void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, const string & original_font_name, const string & cssfont) void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, const string & original_font_name, const string & cssfont)
{ {
allcss_fout << format(".f%|1$x|{") % info.id; allcss_fout << ".f" << info.id << "{";
allcss_fout << "font-family:" << ((cssfont == "") ? (original_font_name + "," + general_font_family(font)) : cssfont) << ";"; allcss_fout << "font-family:" << ((cssfont == "") ? (original_font_name + "," + general_font_family(font)) : cssfont) << ";";
if(font->isBold() || ifind_first(original_font_name, "bold")) if(font->isBold() || ifind_first(original_font_name, "bold"))
@ -70,12 +72,12 @@ void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, cons
void HTMLRenderer::export_font_size (long long fs_id, double font_size) void HTMLRenderer::export_font_size (long long fs_id, double font_size)
{ {
allcss_fout << format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size << endl; allcss_fout << ".s" << fs_id << "{font-size:" << font_size << "px;}" << endl;
} }
void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm) void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
{ {
allcss_fout << format(".t%|1$x|{") % tm_id; allcss_fout << ".t" << tm_id << "{";
// always ignore tm[4] and tm[5] because // always ignore tm[4] and tm[5] because
// we have already shifted the origin // we have already shifted the origin
@ -91,43 +93,37 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
<< tm[3] << ','; << tm[3] << ',';
allcss_fout << "0,0);"; allcss_fout << "0,0);";
/*
if(prefix == "-moz-")
allcss_fout << format("%1%px,%2%px);") % tm[4] % -tm[5];
else
allcss_fout << format("%1%,%2%);") % tm[4] % -tm[5];
*/
} }
allcss_fout << "}" << endl; allcss_fout << "}" << endl;
} }
void HTMLRenderer::export_letter_space (long long ls_id, double letter_space) void HTMLRenderer::export_letter_space (long long ls_id, double letter_space)
{ {
allcss_fout << format(".l%|1$x|{letter-spacing:%2%px;}") % ls_id % letter_space << endl; allcss_fout << ".l" << ls_id << "{letter-spacing:" << letter_space << "px;}" << endl;
} }
void HTMLRenderer::export_word_space (long long ws_id, double word_space) void HTMLRenderer::export_word_space (long long ws_id, double word_space)
{ {
allcss_fout << format(".w%|1$x|{word-spacing:%2%px;}") % ws_id % word_space << endl; allcss_fout << ".w" << ws_id << "{word-spacing:" << word_space << "px;}" << endl;
} }
void HTMLRenderer::export_color (long long color_id, const GfxRGB * rgb) void HTMLRenderer::export_color (long long color_id, const GfxRGB * rgb)
{ {
allcss_fout << format(".c%|1$x|{color:rgb(%2%,%3%,%4%);}") allcss_fout << ".c" << color_id << "{color:rgb("
% color_id % (int)colToByte(rgb->r) % (int)colToByte(rgb->g) % (int)colToByte(rgb->b) << dec << (int)colToByte(rgb->r) << "," << (int)colToByte(rgb->g) << "," << (int)colToByte(rgb->b) << ");}" << hex
<< endl; << endl;
} }
void HTMLRenderer::export_whitespace (long long ws_id, double ws_width) void HTMLRenderer::export_whitespace (long long ws_id, double ws_width)
{ {
if(ws_width > 0) if(ws_width > 0)
allcss_fout << format("._%|1$x|{display:inline-block;width:%2%px;}") % ws_id % ws_width << endl; allcss_fout << "._" << ws_id << "{display:inline-block;width:" << ws_width << "px;}" << endl;
else else
allcss_fout << format("._%|1$x|{display:inline;margin-left:%2%px;}") % ws_id % ws_width << endl; allcss_fout << "._" << ws_id << "{display:inline;margin-left:" << ws_width << "px;}" << endl;
} }
void HTMLRenderer::export_rise (long long rise_id, double rise) void HTMLRenderer::export_rise (long long rise_id, double rise)
{ {
allcss_fout << format(".r%|1$x|{top:%2%px;}") % rise_id % (-rise) << endl; allcss_fout << ".r" << rise_id << "{top:" << (-rise) << "px;}" << endl;
} }

View File

@ -92,7 +92,7 @@ void HTMLRenderer::process(PDFDoc *doc)
0, true, false, false, 0, true, false, false,
nullptr, nullptr, &annot_cb, nullptr); nullptr, nullptr, &annot_cb, nullptr);
string fn = (format("p%|1$x|.png")%i).str(); const char * fn = str_fmt("p%x.png", i);
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi, param->v_dpi); bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi, param->v_dpi);
if(param->single_html) if(param->single_html)
add_tmp_file(fn); add_tmp_file(fn);
@ -137,8 +137,8 @@ void HTMLRenderer::pre_process()
html_fout << ifstream(PDF2HTMLEX_DATA_PATH / NECK_HTML_FILENAME, ifstream::binary).rdbuf(); html_fout << ifstream(PDF2HTMLEX_DATA_PATH / NECK_HTML_FILENAME, ifstream::binary).rdbuf();
} }
html_fout << fixed; html_fout << fixed << hex;
allcss_fout << fixed; allcss_fout << fixed << hex;
allcss_fout << ifstream(PDF2HTMLEX_DATA_PATH / CSS_FILENAME, ifstream::binary).rdbuf(); allcss_fout << ifstream(PDF2HTMLEX_DATA_PATH / CSS_FILENAME, ifstream::binary).rdbuf();
} }
@ -167,11 +167,11 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
assert((!line_opened) && "Open line in startPage detected!"); assert((!line_opened) && "Open line in startPage detected!");
html_fout << format("<div id=\"p%|1$x|\" class=\"p\" style=\"width:%2%px;height:%3%px;") % pageNum % pageWidth % pageHeight; html_fout << "<div id=\"p" << pageNum << "\" class=\"p\" style=\"width:" << pageWidth << "px;height:" << pageHeight << "px;";
html_fout << "background-image:url("; html_fout << "background-image:url(";
const std::string fn = (format("p%|1$x|.png") % pageNum).str(); const char * fn = str_fmt("p%x.png", pageNum);
if(param->single_html) if(param->single_html)
{ {
auto path = tmp_dir / fn; auto path = tmp_dir / fn;
@ -182,7 +182,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
html_fout << fn; html_fout << fn;
} }
html_fout << format(");background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;\">") % pageWidth % pageHeight; html_fout << ");background-position:0 0;background-size:" << pageWidth << "px " << pageHeight << "px;background-repeat:no-repeat;\">";
draw_scale = 1.0; draw_scale = 1.0;

View File

@ -8,7 +8,6 @@
*/ */
#if 0 #if 0
#include <boost/format.hpp>
// for gil bug // for gil bug
const int *int_p_NULL = nullptr; const int *int_p_NULL = nullptr;
#include <boost/gil/gil_all.hpp> #include <boost/gil/gil_all.hpp>

View File

@ -11,8 +11,6 @@
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
#include <boost/format.hpp>
#include "Param.h" #include "Param.h"
#include "HTMLRenderer.h" #include "HTMLRenderer.h"
@ -46,7 +44,7 @@ const FontInfo * HTMLRenderer::install_font(GfxFont * font)
if(param->debug) if(param->debug)
{ {
cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << format("f%|1$x|")%new_fn_id << endl; cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << "f" << hex << new_fn_id << dec << endl;
} }
if(font->getType() == fontType3) { if(font->getType() == fontType3) {
@ -120,7 +118,7 @@ void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, Font
} }
else else
{ {
cerr << format("Cannot embed base font: f%|1$x| %2%") % info.id % psname << endl; cerr << "Cannot embed base font: f" << hex << info.id << dec << ' ' << psname << endl;
// fallback to exporting by name // fallback to exporting by name
} }
@ -178,7 +176,7 @@ void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info)
} }
else else
{ {
cerr << format("Cannot embed external font: f%|1$x| %2%") % info.id % fontname << endl; cerr << "Cannot embed external font: f" << hex << info.id << dec << ' ' << fontname << endl;
// fallback to exporting by name // fallback to exporting by name
} }
} }

View File

@ -9,6 +9,8 @@
#ifndef NAMESPACE_H__ #ifndef NAMESPACE_H__
#define NAMESPACE_H__ #define NAMESPACE_H__
using std::hex;
using std::dec;
using std::string; using std::string;
using std::cout; using std::cout;
using std::cerr; using std::cerr;
@ -18,7 +20,6 @@ using std::make_pair;
using boost::filesystem::ifstream; using boost::filesystem::ifstream;
using boost::filesystem::ofstream; using boost::filesystem::ofstream;
using boost::filesystem::path; using boost::filesystem::path;
using boost::format;
#endif // NAMESPACE_H__ #endif // NAMESPACE_H__

View File

@ -11,7 +11,6 @@
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@ -127,11 +126,11 @@ path HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
obj.streamReset(); obj.streamReset();
string fn = (format("f%|1$x|")%fn_id).str(); const char * fn = str_fmt("f%x%s", fn_id, suffix.c_str());
ofstream outf; ofstream outf;
filepath = tmp_dir / (fn + suffix); filepath = tmp_dir / fn;
outf.open(filepath, ofstream::binary); outf.open(filepath, ofstream::binary);
add_tmp_file(fn+suffix); add_tmp_file(fn);
char buf[1024]; char buf[1024];
int len; int len;
@ -144,7 +143,7 @@ path HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
} }
catch(int) catch(int)
{ {
cerr << format("Someting wrong when trying to dump font %|1$x|") % fn_id << endl; cerr << "Someting wrong when trying to dump font " << hex << fn_id << dec << endl;
} }
obj2.free(); obj2.free();
@ -163,8 +162,6 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
string suffix = filepath.extension().string(); string suffix = filepath.extension().string();
to_lower(suffix); to_lower(suffix);
string fn = (format("f%|1$x|") % info.id).str();
ff_load_font(filepath.c_str()); ff_load_font(filepath.c_str());
int * code2GID = nullptr; int * code2GID = nullptr;
@ -241,7 +238,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
{ {
name_conflict_warned = true; name_conflict_warned = true;
//TODO: may be resolved using advanced font properties? //TODO: may be resolved using advanced font properties?
cerr << "Warning: encoding confliction detected in font: " << fn << endl; cerr << "Warning: encoding confliction detected in font: " << hex << info.id << dec << endl;
} }
} }
} }
@ -332,9 +329,10 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
} }
} }
auto dest = ((param->single_html ? tmp_dir : dest_dir) / (fn+(param->font_suffix))); const char * fn = str_fmt("f%x%s", info.id, param->font_suffix.c_str());
auto dest = ((param->single_html ? tmp_dir : dest_dir) / fn);
if(param->single_html) if(param->single_html)
add_tmp_file(fn+(param->font_suffix)); add_tmp_file(fn);
/* /*
* [Win|Typo|HHead][Ascent|Descent] * [Win|Typo|HHead][Ascent|Descent]
@ -344,9 +342,9 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
// Generate an intermediate ttf font in order to retrieve the metrics // Generate an intermediate ttf font in order to retrieve the metrics
// TODO: see if we can get the values without save/load // TODO: see if we can get the values without save/load
string tmp_fn = fn+"_.ttf"; fn = str_fmt("f%x_.ttf", info.id);
add_tmp_file(tmp_fn); add_tmp_file(fn);
auto tmp_path = tmp_dir / tmp_fn; auto tmp_path = tmp_dir / fn;
ff_save(tmp_path.c_str()); ff_save(tmp_path.c_str());
ff_close(); ff_close();
ff_load_font(tmp_path.c_str()); ff_load_font(tmp_path.c_str());

View File

@ -15,6 +15,7 @@
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <cstdarg>
#include <GfxState.h> #include <GfxState.h>
#include <GfxFont.h> #include <GfxFont.h>
@ -261,4 +262,32 @@ private:
static inline ostream & operator << (ostream & out, base64stream & bf) { return bf.dumpto(out); } static inline ostream & operator << (ostream & out, base64stream & bf) { return bf.dumpto(out); }
static inline ostream & operator << (ostream & out, base64stream && bf) { return bf.dumpto(out); } static inline ostream & operator << (ostream & out, base64stream && bf) { return bf.dumpto(out); }
class string_formatter
{
public:
string_formatter() { buf.reserve(64); }
/*
* Important:
* there is only one buffer, so new strings will replace old ones
*/
const char * operator () (const char * format, ...) {
va_list vlist;
va_start(vlist, format);
int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist);
va_end(vlist);
if((l+1) > (int)buf.capacity())
{
buf.reserve(l+1);
va_start(vlist, format);
l = vsnprintf(&buf.front(), buf.capacity(), format, vlist);
va_end(vlist);
}
if(l < 0) return nullptr;
assert(l < (int)buf.capacity());
return &buf.front();
}
private:
std::vector<char> buf;
};
#endif //UTIL_H__ #endif //UTIL_H__