Skip to content

Commit

Permalink
remove scripts, link to fontforge directly
Browse files Browse the repository at this point in the history
  • Loading branch information
coolwanglu committed Sep 3, 2012
1 parent 879d854 commit 4543fc9
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 106 deletions.
15 changes: 12 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ find_package(Boost REQUIRED COMPONENTS program_options filesystem system)
include_directories(${Boost_INCLUDE_DIRS})
link_directories ( ${Boost_LIBRARY_DIRS} )
include_directories(${CMAKE_SOURCE_DIR}/src)
pkg_check_modules(FONTFORGE REQUIRED fontforge)
include_directories(${FONTFORGE_INCLUDE_DIRS})
link_directories ( ${FONTFORGE_LIBRARY_DIRS} )

set(PDF2HTMLEX_VERSION "0.2")
set(ARCHIVE_NAME pdf2htmlex-${PDF2HTMLEX_VERSION})
Expand All @@ -17,10 +20,14 @@ add_custom_target(dist
| bzip2 > ${CMAKE_BINARY_DIR}/${ARCHIVE_NAME}.tar.bz2
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wunused-function")
#set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -O2")
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -ggdb")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wunused-function")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb")

configure_file (${CMAKE_SOURCE_DIR}/src/config.h.in ${CMAKE_SOURCE_DIR}/src/config.h)

Expand All @@ -34,14 +41,16 @@ add_executable(pdf2htmlEX
src/HTMLRenderer/text.cc
src/HTMLRenderer/image.cc
src/HTMLRenderer/namespace.h
src/ff/ff.h
src/ff/ff.c
src/BackgroundRenderer.h
src/BackgroundRenderer.cc
src/Consts.h
src/Consts.cc
src/util.h
src/config.h)

target_link_libraries(pdf2htmlEX poppler boost_program_options boost_filesystem boost_system)
target_link_libraries(pdf2htmlEX poppler boost_program_options boost_filesystem boost_system fontforge python2.7)

install (TARGETS pdf2htmlEX DESTINATION bin)
file (GLOB datafiles share/*)
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake - pkgconfig - fontforge - python

link fontforge instead of script

option to break ligatures
Expand Down
31 changes: 0 additions & 31 deletions share/unify.pe

This file was deleted.

3 changes: 0 additions & 3 deletions src/HTMLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ class HTMLRenderer : public OutputDev
static const std::string NECK_HTML_FILENAME;
static const std::string TAIL_HTML_FILENAME;
static const std::string CSS_FILENAME;
static const std::string UNIFY_SCRIPT_FILENAME;
// for cross-platform purpose, use a "null" file instead of /dev/null
static const std::string NULL_FILENAME;
};

#endif /* HTMLRENDERER_H_ */
4 changes: 2 additions & 2 deletions src/HTMLRenderer/general.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "BackgroundRenderer.h"
#include "config.h"
#include "namespace.h"
#include "ff/ff.h"

using std::fixed;
using std::flush;
Expand All @@ -26,6 +27,7 @@ HTMLRenderer::HTMLRenderer(const Param * param)
,dest_dir(param->dest_dir)
,tmp_dir(param->tmp_dir)
{
ff_init();
}

HTMLRenderer::~HTMLRenderer()
Expand Down Expand Up @@ -242,5 +244,3 @@ 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::UNIFY_SCRIPT_FILENAME = "unify.pe";
const std::string HTMLRenderer::NULL_FILENAME = "null";
129 changes: 62 additions & 67 deletions src/HTMLRenderer/text.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* text.ccc
* text.cc
*
* Handling text & font, and relative stuffs
*
Expand All @@ -18,6 +18,7 @@
#include <CharCodeToUnicode.h>
#include <fofi/FoFiTrueType.h>

#include "ff/ff.h"
#include "HTMLRenderer.h"
#include "namespace.h"
#include "config.h"
Expand Down Expand Up @@ -162,11 +163,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &

string fn = (format("f%|1$x|") % info.id).str();

path script_path = tmp_dir / (fn + ".pe");
ofstream script_fout(script_path, ofstream::binary);
add_tmp_file(fn+".pe");

script_fout << format("Open(%1%, 1)") % filepath << endl;
ff_load_font(filepath.c_str());

int * code2GID = nullptr;
int code2GID_len = 0;
Expand Down Expand Up @@ -200,7 +197,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
maxcode = 0xff;
if((suffix == ".ttf") || (suffix == ".ttc") || (suffix == ".otf"))
{
script_fout << "Reencode(\"original\")" << endl;
ff_reencode("original", 0);
FoFiTrueType *fftt = nullptr;
if((fftt = FoFiTrueType::load((char*)filepath.c_str())) != nullptr)
{
Expand Down Expand Up @@ -246,8 +243,8 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
}
out << "] def" << endl;

script_fout << format("LoadEncodingFile(%1%)") % (tmp_dir / (fn+"_.encoding")) << endl;
script_fout << format("Reencode(\"%1%\")") % fn << endl;
ff_load_encoding((tmp_dir / (fn+"_.encoding")).c_str(), nullptr);
ff_reencode(fn.c_str(), 0);
}
}
else
Expand All @@ -256,7 +253,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &

if(suffix == ".ttf")
{
script_fout << "Reencode(\"original\")" << endl;
ff_reencode("original", 0);

GfxCIDFont * _font = dynamic_cast<GfxCIDFont*>(font);

Expand All @@ -266,7 +263,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
}
else
{
script_fout << "CIDFlatten()" << endl;
ff_cidflatten();
}
}

Expand All @@ -280,46 +277,49 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
* - For 8bit nonTruetype fonts:
* Try to calculate the correct Unicode value from the glyph names, unless param->always_apply_tounicode is set
*
* TODO: build Encoding directly, without read/write files
*/

auto ctu = font->getToUnicode();

ofstream map_fout(tmp_dir / (fn + ".encoding"));
add_tmp_file(fn+".encoding");

int cnt = 0;
for(int i = 0; i <= maxcode; ++i)

{
if((suffix != ".ttf") && (font_8bit != nullptr) && (font_8bit->getCharName(i) == nullptr))
continue;
ofstream map_fout(tmp_dir / (fn + ".encoding"));
add_tmp_file(fn+".encoding");

++ cnt;
map_fout << format("0x%|1$X|") % ((code2GID && (i < code2GID_len))? code2GID[i] : i);
for(int i = 0; i <= maxcode; ++i)
{
if((suffix != ".ttf") && (font_8bit != nullptr) && (font_8bit->getCharName(i) == nullptr))
continue;

Unicode u, *pu=&u;
++ cnt;
map_fout << format("0x%|1$X|") % ((code2GID && (i < code2GID_len))? code2GID[i] : i);

if(info.use_tounicode)
{
int n = 0;
if(ctu)
n = ctu->mapToUnicode(i, &pu);
u = check_unicode(pu, n, i, font);
}
else
{
u = unicode_from_font(i, font);
}
Unicode u, *pu=&u;

map_fout << format(" 0x%|1$X|") % u;
map_fout << format(" # 0x%|1$X|") % i;
if(info.use_tounicode)
{
int n = 0;
if(ctu)
n = ctu->mapToUnicode(i, &pu);
u = check_unicode(pu, n, i, font);
}
else
{
u = unicode_from_font(i, font);
}

map_fout << endl;
map_fout << format(" 0x%|1$X|") % u;
map_fout << format(" # 0x%|1$X|") % i;

map_fout << endl;
}
}

if(cnt > 0)
{
script_fout << format("LoadEncodingFile(%1%, \"%2%\")") % (tmp_dir / (fn+".encoding")) % fn << endl;
script_fout << format("Reencode(\"%1%\", 1)") % fn << endl;
ff_load_encoding((tmp_dir / (fn+".encoding")).c_str(), fn.c_str());
ff_reencode(fn.c_str(), 1);
}

if(ctu)
Expand All @@ -336,42 +336,37 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
* Trying to unify them
*/
// Generate an intermediate ttf font in order to retrieve the metrics
add_tmp_file(fn + "_.ttf");
script_fout << format("Generate(%1%)") % (tmp_dir / (fn + "_.ttf")) << endl;
script_fout << "Close()" << endl;
script_fout << format("Open(%1%, 1)") % (tmp_dir / (fn + "_.ttf")) << endl;
script_fout << ifstream(PDF2HTMLEX_DATA_PATH / UNIFY_SCRIPT_FILENAME).rdbuf();
script_fout << format("Generate(%1%)") % dest << endl;
script_fout.close();

if(system((boost::format("fontforge -script %1% 1>%2% 2>%3%") % script_path % (tmp_dir / (fn+".info")) % (tmp_dir / NULL_FILENAME)).str().c_str()) != 0)
cerr << "Warning: fontforge failed." << endl;

add_tmp_file(fn+".info");
add_tmp_file(NULL_FILENAME);

// read metric
int em, ascent, descent;
if(ifstream(tmp_dir / (fn+".info")) >> em >> ascent >> descent)
// TODO: see if we can get the values without save/load

string tmp_fn = fn+"_.ttf";
add_tmp_file(tmp_fn);
auto tmp_path = tmp_dir / tmp_fn;
ff_save(tmp_path.c_str());
ff_close();
ff_load_font(tmp_path.c_str());

int em = ff_get_em_size();
int ascent = ff_get_max_ascent();
int descent = ff_get_max_descent();

ff_set_ascent(ascent);
ff_set_descent(descent);
ff_save(dest.c_str());
ff_close();

if(em != 0)
{
if(em != 0)
{
info.ascent = ((double)ascent) / em;
info.descent = -((double)descent) / em;
}
else
{
info.ascent = 0;
info.descent = 0;
}
info.ascent = ((double)ascent) / em;
info.descent = -((double)descent) / em;
}
else
{
cerr << "Warning: cannot read font info for " << fn << endl;
info.ascent = font->getAscent();
info.descent = font->getDescent();
info.ascent = 0;
info.descent = 0;
}

// read metric

if(param->debug)
{
cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl;
Expand Down
Loading

0 comments on commit 4543fc9

Please sign in to comment.