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

clean code, solve the infinite-writing problem when tmp_dir == dest_dir

This commit is contained in:
Lu Wang 2012-08-15 15:29:35 +08:00
parent b21042021b
commit 63dd9b3e27
8 changed files with 44 additions and 19 deletions

View File

@ -30,6 +30,7 @@ Not supported yet
Dependency
----------------------------
* Recent version of GCC (no guarantee on other compilers)
* libpoppler with xpdf header >= 0.20.2
* boost c++ library (format, program options, gil, filesystem, serialization, system(which is actually required by filesystem))
* fontforge **Please use [the lastest version](https://github.com/fontforge/fontforge)**

View File

@ -10,7 +10,6 @@
using std::map;
using std::string;
const double EPS = 1e-6;
const double id_matrix[6] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};

View File

@ -11,7 +11,7 @@
#include <string>
#include <map>
extern const double EPS;
static constexpr double EPS = 1e-6;
extern const double id_matrix[6];
extern const std::map<std::string, std::string> BASE_14_FONT_CSS_FONT_MAP;

View File

@ -218,6 +218,12 @@ class HTMLRenderer : public OutputDev
boost::filesystem::path dest_dir, tmp_dir;
boost::filesystem::ofstream html_fout, allcss_fout;
std::set<std::string> tmp_files;
static const std::string HEAD_HTML_FILENAME;
static const std::string NECK_HTML_FILENAME;
static const std::string TAIL_HTML_FILENAME;
static const std::string CSS_FILENAME;
static const std::string NULL_FILENAME;
};
#endif /* HTMLRENDERER_H_ */

View File

@ -94,30 +94,34 @@ void HTMLRenderer::pre_process()
// we may output utf8 characters, so use binary
if(param->single_html)
{
html_fout.open(tmp_dir / param->output_filename, ofstream::binary);
allcss_fout.open(tmp_dir / "all.css", ofstream::binary);
// don't use output_file directly
// otherwise it'll be a disaster when tmp_dir == dest_dir
const string tmp_output_fn = param->output_filename + ".part";
html_fout.open(tmp_dir / tmp_output_fn, ofstream::binary);
allcss_fout.open(tmp_dir / CSS_FILENAME, ofstream::binary);
add_tmp_file(param->output_filename);
add_tmp_file("all.css");
add_tmp_file(tmp_output_fn);
add_tmp_file(CSS_FILENAME);
}
else
{
html_fout.open(dest_dir / param->output_filename, ofstream::binary);
allcss_fout.open(dest_dir / "all.css", ofstream::binary);
allcss_fout.open(dest_dir / CSS_FILENAME, ofstream::binary);
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
html_fout << "<link rel=\"stylesheet\" type=\"text/css\" href=\"all.css\"/>" << endl;
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "neck.html", ifstream::binary).rdbuf();
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / HEAD_HTML_FILENAME, ifstream::binary).rdbuf();
html_fout << "<link rel=\"stylesheet\" type=\"text/css\" href=\"" << CSS_FILENAME << "\"/>" << endl;
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / NECK_HTML_FILENAME, ifstream::binary).rdbuf();
}
allcss_fout << ifstream(PDF2HTMLEX_LIB_PATH / "base.css", ifstream::binary).rdbuf();
allcss_fout << ifstream(PDF2HTMLEX_LIB_PATH / CSS_FILENAME, ifstream::binary).rdbuf();
}
void HTMLRenderer::post_process()
{
if(!param->single_html)
{
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / TAIL_HTML_FILENAME, ifstream::binary).rdbuf();
}
html_fout.close();
@ -179,17 +183,17 @@ void HTMLRenderer::process_single_html()
{
ofstream out (dest_dir / param->output_filename, ofstream::binary);
out << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
out << ifstream(PDF2HTMLEX_LIB_PATH / HEAD_HTML_FILENAME , ifstream::binary).rdbuf();
out << "<style type=\"text/css\">" << endl;
out << ifstream(tmp_dir / "all.css", ifstream::binary).rdbuf();
out << ifstream(tmp_dir / CSS_FILENAME, ifstream::binary).rdbuf();
out << "</style>" << endl;
out << ifstream(PDF2HTMLEX_LIB_PATH / "neck.html", ifstream::binary).rdbuf();
out << ifstream(PDF2HTMLEX_LIB_PATH / NECK_HTML_FILENAME, ifstream::binary).rdbuf();
out << ifstream(tmp_dir / param->output_filename, ifstream::binary).rdbuf();
out << ifstream(tmp_dir / (param->output_filename + ".part"), ifstream::binary).rdbuf();
out << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
out << ifstream(PDF2HTMLEX_LIB_PATH / TAIL_HTML_FILENAME, ifstream::binary).rdbuf();
}
void HTMLRenderer::add_tmp_file(const string & fn)
@ -220,3 +224,9 @@ void HTMLRenderer::clean_tmp_files()
catch(const filesystem_error &)
{ }
}
const std::string HTMLRenderer::HEAD_HTML_FILENAME = "head.html";
const std::string HTMLRenderer::NECK_HTML_FILENAME = "neck.html";
const std::string HTMLRenderer::TAIL_HTML_FILENAME = "tail.html";
const std::string HTMLRenderer::CSS_FILENAME = "all.css";
const std::string HTMLRenderer::NULL_FILENAME = "null";

View File

@ -187,8 +187,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
if(param->single_html)
add_tmp_file(fn+".ttf");
// for cross-platform purpose, use a file instead of /dev/null
system((boost::format("fontforge -script %1% 2>%2%") % script_path % (tmp_dir / "null")).str().c_str());
system((boost::format("fontforge -script %1% 2>%2%") % script_path % (tmp_dir / NULL_FILENAME)).str().c_str());
add_tmp_file("null");
export_remote_font(fn_id, ".ttf", "truetype", font);

View File

@ -14,6 +14,7 @@
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <goo/GooString.h>
@ -169,6 +170,15 @@ int main(int argc, char **argv)
}
//prepare the directories
for(const auto & p : {param.dest_dir, param.tmp_dir})
{
if(equivalent(PDF2HTMLEX_LIB_PATH, p))
{
cerr << "The specified directory \"" << p << "\" is the library path for pdf2htmlEX. Please use another one." << endl;
return -1;
}
}
try
{
create_directories(param.dest_dir);