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

new option: remove unused glyphs

This commit is contained in:
Lu Wang 2012-10-05 23:38:17 +08:00
parent baa375f8ec
commit 091b623872
15 changed files with 4997 additions and 26 deletions

4
TODO
View File

@ -1,3 +1,5 @@
remove unused glyphs
don't dump image when there is nothing don't dump image when there is nothing
Integrate splash/cairo Integrate splash/cairo
@ -8,7 +10,7 @@ about glyph width:
- IE - IE
- stretching ratio might not be correct.. letter 'f' - stretching ratio might not be correct.. letter 'f'
draw lines with CSS draw non-orthogonal lines with CSS
create a glyph for ' ', if there is not in a font create a glyph for ' ', if there is not in a font

6
debian/changelog vendored
View File

@ -1,3 +1,9 @@
pdf2htmlex (0.5-1~git201210051800rbaa37-0ubuntu1) precise; urgency=low
* Regularly re-packing.
-- WANG Lu <coolwanglu@gmail.com> Fri, 05 Oct 2012 18:00:48 +0800
pdf2htmlex (0.5-1~git201209270317r6fa14-0ubuntu1) precise; urgency=low pdf2htmlex (0.5-1~git201209270317r6fa14-0ubuntu1) precise; urgency=low
* Regularly re-packing. * Regularly re-packing.

View File

@ -123,10 +123,8 @@ If set to 1, glyphs narrower than described in PDF will be strecth; otherwise sp
.B --squeeze_wide_glyph <0|1> (Default: 0) .B --squeeze_wide_glyph <0|1> (Default: 0)
If set to 1, glyphs wider than described in PDF will be squeezed; otherwise it will be truncated. If set to 1, glyphs wider than described in PDF will be squeezed; otherwise it will be truncated.
.TP .TP
.B --css-filename <filename> (Default: <none>) .B --remove-unused-glyph <0|1> (Default: 0)
Specify the filename of the generated css file, if not embedded. [Experimental] If set to 1, remove unused glyphs in embedded fonts in order to reduce the file size.
If it's empty, the file name will be determined automatically.
.TP .TP
.B --font-suffix <suffix> (Default: .ttf), --font-format <format> (Default: truetype) .B --font-suffix <suffix> (Default: .ttf), --font-format <format> (Default: truetype)
Specify the suffix and format of fonts extracted from the PDF file. They should be consistent. Specify the suffix and format of fonts extracted from the PDF file. They should be consistent.
@ -136,6 +134,11 @@ If specified, the tool will be called in order to enhanced hinting for fonts, th
The tool will be called as '<tool> <in.suffix> <out.suffix>', where suffix will be the same as specified for --font-suffix. The tool will be called as '<tool> <in.suffix> <out.suffix>', where suffix will be the same as specified for --font-suffix.
.TP .TP
.B --css-filename <filename> (Default: <none>)
Specify the filename of the generated css file, if not embedded.
If it's empty, the file name will be determined automatically.
.TP
.B --debug <0|1> (Default: 0) .B --debug <0|1> (Default: 0)
Show debug information. Show debug information.
.TP .TP

View File

@ -0,0 +1,804 @@
//========================================================================
//
// CairoFontEngine.cc
//
// Copyright 2003 Glyph & Cog, LLC
// Copyright 2004 Red Hat, Inc
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005-2007 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2005 Martin Kretzschmar <martink@gnome.org>
// Copyright (C) 2005, 2009, 2012 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2006, 2007, 2010, 2011 Carlos Garcia Campos <carlosgc@gnome.org>
// Copyright (C) 2007 Koji Otani <sho@bbr.jp>
// Copyright (C) 2008, 2009 Chris Wilson <chris@chris-wilson.co.uk>
// Copyright (C) 2008, 2012 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2009 Darren Kenny <darren.kenny@sun.com>
// Copyright (C) 2010 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
// Copyright (C) 2010 Jan Kümmel <jan+freedesktop@snorc.org>
// Copyright (C) 2012 Hib Eris <hib@hiberis.nl>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#include "config.h"
#include <string.h>
#include "CairoFontEngine.h"
#include "CairoOutputDev.h"
#include "GlobalParams.h"
#include <fofi/FoFiTrueType.h>
#include <fofi/FoFiType1C.h>
#include "goo/gfile.h"
#include "Error.h"
#include "XRef.h"
#include "Gfx.h"
#include "Page.h"
#if HAVE_FCNTL_H && HAVE_SYS_MMAN_H && HAVE_SYS_STAT_H
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define CAN_CHECK_OPEN_FACES 1
#endif
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
//------------------------------------------------------------------------
// CairoFont
//------------------------------------------------------------------------
CairoFont::CairoFont(Ref ref,
cairo_font_face_t *cairo_font_face,
int *codeToGID,
Guint codeToGIDLen,
GBool substitute,
GBool printing) : ref(ref),
cairo_font_face(cairo_font_face),
codeToGID(codeToGID),
codeToGIDLen(codeToGIDLen),
substitute(substitute),
printing(printing) { }
CairoFont::~CairoFont() {
cairo_font_face_destroy (cairo_font_face);
gfree(codeToGID);
}
GBool
CairoFont::matches(Ref &other, GBool printingA) {
return (other.num == ref.num && other.gen == ref.gen);
}
cairo_font_face_t *
CairoFont::getFontFace(void) {
return cairo_font_face;
}
unsigned long
CairoFont::getGlyph(CharCode code,
Unicode *u, int uLen) {
FT_UInt gid;
if (codeToGID && code < codeToGIDLen) {
gid = (FT_UInt)codeToGID[code];
} else {
gid = (FT_UInt)code;
}
return gid;
}
double
CairoFont::getSubstitutionCorrection(GfxFont *gfxFont)
{
double w1, w2;
CharCode code;
char *name;
// for substituted fonts: adjust the font matrix -- compare the
// width of 'm' in the original font and the substituted font
if (isSubstitute() && !gfxFont->isCIDFont()) {
for (code = 0; code < 256; ++code) {
if ((name = ((Gfx8BitFont *)gfxFont)->getCharName(code)) &&
name[0] == 'm' && name[1] == '\0') {
break;
}
}
if (code < 256) {
w1 = ((Gfx8BitFont *)gfxFont)->getWidth(code);
{
cairo_matrix_t m;
cairo_matrix_init_identity(&m);
cairo_font_options_t *options = cairo_font_options_create();
cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
cairo_scaled_font_t *scaled_font = cairo_scaled_font_create(cairo_font_face, &m, &m, options);
cairo_text_extents_t extents;
cairo_scaled_font_text_extents(scaled_font, "m", &extents);
cairo_scaled_font_destroy(scaled_font);
cairo_font_options_destroy(options);
w2 = extents.x_advance;
}
if (!gfxFont->isSymbolic()) {
// if real font is substantially narrower than substituted
// font, reduce the font size accordingly
if (w1 > 0.01 && w1 < 0.9 * w2) {
w1 /= w2;
return w1;
}
}
}
}
return 1.0;
}
//------------------------------------------------------------------------
// CairoFreeTypeFont
//------------------------------------------------------------------------
static cairo_user_data_key_t _ft_cairo_key;
static void
_ft_done_face_uncached (void *closure)
{
FT_Face face = (FT_Face) closure;
FT_Done_Face (face);
}
static GBool
_ft_new_face_uncached (FT_Library lib,
const char *filename,
char *font_data,
int font_data_len,
FT_Face *face_out,
cairo_font_face_t **font_face_out)
{
FT_Face face;
cairo_font_face_t *font_face;
if (font_data == NULL) {
if (FT_New_Face (lib, filename, 0, &face))
return gFalse;
} else {
if (FT_New_Memory_Face (lib, (unsigned char *)font_data, font_data_len, 0, &face))
return gFalse;
}
font_face = cairo_ft_font_face_create_for_ft_face (face,
FT_LOAD_NO_HINTING |
FT_LOAD_NO_BITMAP);
if (cairo_font_face_set_user_data (font_face,
&_ft_cairo_key,
face,
_ft_done_face_uncached))
{
_ft_done_face_uncached (face);
cairo_font_face_destroy (font_face);
return gFalse;
}
*face_out = face;
*font_face_out = font_face;
return gTrue;
}
#if CAN_CHECK_OPEN_FACES
static struct _ft_face_data {
struct _ft_face_data *prev, *next, **head;
int fd;
unsigned long hash;
size_t size;
unsigned char *bytes;
FT_Library lib;
FT_Face face;
cairo_font_face_t *font_face;
} *_ft_open_faces;
static unsigned long
_djb_hash (const unsigned char *bytes, size_t len)
{
unsigned long hash = 5381;
while (len--) {
unsigned char c = *bytes++;
hash *= 33;
hash ^= c;
}
return hash;
}
static GBool
_ft_face_data_equal (struct _ft_face_data *a, struct _ft_face_data *b)
{
if (a->lib != b->lib)
return gFalse;
if (a->size != b->size)
return gFalse;
if (a->hash != b->hash)
return gFalse;
return memcmp (a->bytes, b->bytes, a->size) == 0;
}
static void
_ft_done_face (void *closure)
{
struct _ft_face_data *data = (struct _ft_face_data *) closure;
if (data->next)
data->next->prev = data->prev;
if (data->prev)
data->prev->next = data->next;
else
_ft_open_faces = data->next;
#if defined(__SUNPRO_CC) && defined(__sun) && defined(__SVR4)
munmap ((char*)data->bytes, data->size);
#else
munmap (data->bytes, data->size);
#endif
close (data->fd);
FT_Done_Face (data->face);
gfree (data);
}
static GBool
_ft_new_face (FT_Library lib,
const char *filename,
char *font_data,
int font_data_len,
FT_Face *face_out,
cairo_font_face_t **font_face_out)
{
struct _ft_face_data *l;
struct stat st;
struct _ft_face_data tmpl;
tmpl.fd = -1;
if (font_data == NULL) {
/* if we fail to mmap the file, just pass it to FreeType instead */
tmpl.fd = open (filename, O_RDONLY);
if (tmpl.fd == -1)
return _ft_new_face_uncached (lib, filename, font_data, font_data_len, face_out, font_face_out);
if (fstat (tmpl.fd, &st) == -1) {
close (tmpl.fd);
return _ft_new_face_uncached (lib, filename, font_data, font_data_len, face_out, font_face_out);
}
tmpl.bytes = (unsigned char *) mmap (NULL, st.st_size,
PROT_READ, MAP_PRIVATE,
tmpl.fd, 0);
if (tmpl.bytes == MAP_FAILED) {
close (tmpl.fd);
return _ft_new_face_uncached (lib, filename, font_data, font_data_len, face_out, font_face_out);
}
tmpl.size = st.st_size;
} else {
tmpl.bytes = (unsigned char*) font_data;
tmpl.size = font_data_len;
}
/* check to see if this is a duplicate of any of the currently open fonts */
tmpl.lib = lib;
tmpl.hash = _djb_hash (tmpl.bytes, tmpl.size);
for (l = _ft_open_faces; l; l = l->next) {
if (_ft_face_data_equal (l, &tmpl)) {
if (tmpl.fd != -1) {
#if defined(__SUNPRO_CC) && defined(__sun) && defined(__SVR4)
munmap ((char*)tmpl.bytes, tmpl.size);
#else
munmap (tmpl.bytes, tmpl.size);
#endif
close (tmpl.fd);
}
*face_out = l->face;
*font_face_out = cairo_font_face_reference (l->font_face);
return gTrue;
}
}
/* not a dup, open and insert into list */
if (FT_New_Memory_Face (lib,
(FT_Byte *) tmpl.bytes, tmpl.size,
0, &tmpl.face))
{
if (tmpl.fd != -1) {
#if defined(__SUNPRO_CC) && defined(__sun) && defined(__SVR4)
munmap ((char*)tmpl.bytes, tmpl.size);
#else
munmap (tmpl.bytes, tmpl.size);
#endif
close (tmpl.fd);
}
return gFalse;
}
l = (struct _ft_face_data *) gmallocn (1, sizeof (struct _ft_face_data));
*l = tmpl;
l->prev = NULL;
l->next = _ft_open_faces;
if (_ft_open_faces)
_ft_open_faces->prev = l;
_ft_open_faces = l;
l->font_face = cairo_ft_font_face_create_for_ft_face (tmpl.face,
FT_LOAD_NO_HINTING |
FT_LOAD_NO_BITMAP);
if (cairo_font_face_set_user_data (l->font_face,
&_ft_cairo_key,
l,
_ft_done_face))
{
cairo_font_face_destroy (l->font_face);
_ft_done_face (l);
return gFalse;
}
*face_out = l->face;
*font_face_out = l->font_face;
return gTrue;
}
#else
#define _ft_new_face _ft_new_face_uncached
#endif
CairoFreeTypeFont::CairoFreeTypeFont(Ref ref,
cairo_font_face_t *cairo_font_face,
int *codeToGID,
Guint codeToGIDLen,
GBool substitute) : CairoFont(ref,
cairo_font_face,
codeToGID,
codeToGIDLen,
substitute,
gTrue) { }
CairoFreeTypeFont::~CairoFreeTypeFont() { }
CairoFreeTypeFont *CairoFreeTypeFont::create(GfxFont *gfxFont, XRef *xref,
FT_Library lib, GBool useCIDs) {
Object refObj, strObj;
GooString *fileName;
char *fileNameC;
char *font_data;
int font_data_len;
int i, n;
GfxFontType fontType;
GfxFontLoc *fontLoc;
char **enc;
char *name;
FoFiTrueType *ff;
FoFiType1C *ff1c;
Ref ref;
FT_Face face;
cairo_font_face_t *font_face;
int *codeToGID;
Guint codeToGIDLen;
codeToGID = NULL;
codeToGIDLen = 0;
font_data = NULL;
font_data_len = 0;
fileName = NULL;
fileNameC = NULL;
GBool substitute = gFalse;
ref = *gfxFont->getID();
fontType = gfxFont->getType();
if (!(fontLoc = gfxFont->locateFont(xref, gFalse))) {
error(errSyntaxError, -1, "Couldn't find a font for '{0:s}'",
gfxFont->getName() ? gfxFont->getName()->getCString()
: "(unnamed)");
goto err2;
}
// embedded font
if (fontLoc->locType == gfxFontLocEmbedded) {
font_data = gfxFont->readEmbFontFile(xref, &font_data_len);
if (NULL == font_data)
goto err2;
// external font
} else { // gfxFontLocExternal
fileName = fontLoc->path;
fontType = fontLoc->fontType;
substitute = gTrue;
}
if (fileName != NULL) {
fileNameC = fileName->getCString();
}
switch (fontType) {
case fontType1:
case fontType1C:
case fontType1COT:
if (! _ft_new_face (lib, fileNameC, font_data, font_data_len, &face, &font_face)) {
error(errSyntaxError, -1, "could not create type1 face");
goto err2;
}
enc = ((Gfx8BitFont *)gfxFont)->getEncoding();
codeToGID = (int *)gmallocn(256, sizeof(int));
codeToGIDLen = 256;
for (i = 0; i < 256; ++i) {
codeToGID[i] = 0;
if ((name = enc[i])) {
codeToGID[i] = FT_Get_Name_Index(face, name);
}
}
break;
case fontCIDType2:
case fontCIDType2OT:
codeToGID = NULL;
n = 0;
if (((GfxCIDFont *)gfxFont)->getCIDToGID()) {
n = ((GfxCIDFont *)gfxFont)->getCIDToGIDLen();
if (n) {
codeToGID = (int *)gmallocn(n, sizeof(int));
memcpy(codeToGID, ((GfxCIDFont *)gfxFont)->getCIDToGID(),
n * sizeof(int));
}
} else {
if (font_data != NULL) {
ff = FoFiTrueType::make(font_data, font_data_len);
} else {
ff = FoFiTrueType::load(fileNameC);
}
if (! ff)
goto err2;
codeToGID = ((GfxCIDFont *)gfxFont)->getCodeToGIDMap(ff, &n);
delete ff;
}
codeToGIDLen = n;
/* Fall through */
case fontTrueType:
if (font_data != NULL) {
ff = FoFiTrueType::make(font_data, font_data_len);
} else {
ff = FoFiTrueType::load(fileNameC);
}
if (! ff) {
error(errSyntaxError, -1, "failed to load truetype font\n");
goto err2;
}
/* This might be set already for the CIDType2 case */
if (fontType == fontTrueType) {
codeToGID = ((Gfx8BitFont *)gfxFont)->getCodeToGIDMap(ff);
codeToGIDLen = 256;
}
delete ff;
if (! _ft_new_face (lib, fileNameC, font_data, font_data_len, &face, &font_face)) {
error(errSyntaxError, -1, "could not create truetype face\n");
goto err2;
}
break;
case fontCIDType0:
case fontCIDType0C:
codeToGID = NULL;
codeToGIDLen = 0;
if (!useCIDs)
{
if (font_data != NULL) {
ff1c = FoFiType1C::make(font_data, font_data_len);
} else {
ff1c = FoFiType1C::load(fileNameC);
}
if (ff1c) {
codeToGID = ff1c->getCIDToGIDMap((int *)&codeToGIDLen);
delete ff1c;
}
}
if (! _ft_new_face (lib, fileNameC, font_data, font_data_len, &face, &font_face)) {
gfree(codeToGID);
codeToGID = NULL;
error(errSyntaxError, -1, "could not create cid face\n");
goto err2;
}
break;
default:
fprintf (stderr, "font type %d not handled\n", (int)fontType);
goto err2;
break;
}
delete fontLoc;
return new CairoFreeTypeFont(ref,
font_face,
codeToGID, codeToGIDLen,
substitute);
err2:
/* hmm? */
delete fontLoc;
fprintf (stderr, "some font thing failed\n");
return NULL;
}
//------------------------------------------------------------------------
// CairoType3Font
//------------------------------------------------------------------------
static const cairo_user_data_key_t type3_font_key = {0};
typedef struct _type3_font_info {
GfxFont *font;
PDFDoc *doc;
CairoFontEngine *fontEngine;
GBool printing;
} type3_font_info_t;
static void
_free_type3_font_info(void *closure)
{
type3_font_info_t *info = (type3_font_info_t *) closure;
info->font->decRefCnt();
free (info);
}
static cairo_status_t
_init_type3_glyph (cairo_scaled_font_t *scaled_font,
cairo_t *cr,
cairo_font_extents_t *extents)
{
type3_font_info_t *info;
GfxFont *font;
double *mat;
info = (type3_font_info_t *)
cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
&type3_font_key);
font = info->font;
mat = font->getFontBBox();
extents->ascent = mat[3]; /* y2 */
extents->descent = -mat[3]; /* -y1 */
extents->height = extents->ascent + extents->descent;
extents->max_x_advance = mat[2] - mat[1]; /* x2 - x1 */
extents->max_y_advance = 0;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_render_type3_glyph (cairo_scaled_font_t *scaled_font,
unsigned long glyph,
cairo_t *cr,
cairo_text_extents_t *metrics)
{
Dict *charProcs;
Object charProc;
CairoOutputDev *output_dev;
cairo_matrix_t matrix, invert_y_axis;
double *mat;
double wx, wy;
PDFRectangle box;
type3_font_info_t *info;
GfxFont *font;
Dict *resDict;
Gfx *gfx;
info = (type3_font_info_t *)
cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
&type3_font_key);
font = info->font;
resDict = ((Gfx8BitFont *)font)->getResources();
charProcs = ((Gfx8BitFont *)(info->font))->getCharProcs();
if (!charProcs)
return CAIRO_STATUS_USER_FONT_ERROR;
if ((int)glyph >= charProcs->getLength())
return CAIRO_STATUS_USER_FONT_ERROR;
mat = font->getFontMatrix();
matrix.xx = mat[0];
matrix.yx = mat[1];
matrix.xy = mat[2];
matrix.yy = mat[3];
matrix.x0 = mat[4];
matrix.y0 = mat[5];
cairo_matrix_init_scale (&invert_y_axis, 1, -1);
cairo_matrix_multiply (&matrix, &matrix, &invert_y_axis);
cairo_transform (cr, &matrix);
output_dev = new CairoOutputDev();
output_dev->setCairo(cr);
output_dev->setPrinting(info->printing);
mat = font->getFontBBox();
box.x1 = mat[0];
box.y1 = mat[1];
box.x2 = mat[2];
box.y2 = mat[3];
gfx = new Gfx(info->doc, output_dev, resDict, &box, NULL);
output_dev->startDoc(info->doc, info->fontEngine);
output_dev->startPage (1, gfx->getState());
output_dev->setInType3Char(gTrue);
gfx->display(charProcs->getVal(glyph, &charProc));
output_dev->getType3GlyphWidth (&wx, &wy);
cairo_matrix_transform_distance (&matrix, &wx, &wy);
metrics->x_advance = wx;
metrics->y_advance = wy;
if (output_dev->hasType3GlyphBBox()) {
double *bbox = output_dev->getType3GlyphBBox();
cairo_matrix_transform_point (&matrix, &bbox[0], &bbox[1]);
cairo_matrix_transform_point (&matrix, &bbox[2], &bbox[3]);
metrics->x_bearing = bbox[0];
metrics->y_bearing = bbox[1];
metrics->width = bbox[2] - bbox[0];
metrics->height = bbox[3] - bbox[1];
}
delete gfx;
delete output_dev;
charProc.free();
return CAIRO_STATUS_SUCCESS;
}
CairoType3Font *CairoType3Font::create(GfxFont *gfxFont, PDFDoc *doc,
CairoFontEngine *fontEngine,
GBool printing) {
Object refObj, strObj;
type3_font_info_t *info;
cairo_font_face_t *font_face;
Ref ref;
int *codeToGID;
Guint codeToGIDLen;
int i, j;
char **enc;
Dict *charProcs;
char *name;
charProcs = ((Gfx8BitFont *)gfxFont)->getCharProcs();
info = (type3_font_info_t *) malloc(sizeof(*info));
ref = *gfxFont->getID();
font_face = cairo_user_font_face_create();
cairo_user_font_face_set_init_func (font_face, _init_type3_glyph);
cairo_user_font_face_set_render_glyph_func (font_face, _render_type3_glyph);
gfxFont->incRefCnt();
info->font = gfxFont;
info->doc = doc;
info->fontEngine = fontEngine;
info->printing = printing;
cairo_font_face_set_user_data (font_face, &type3_font_key, (void *) info, _free_type3_font_info);
enc = ((Gfx8BitFont *)gfxFont)->getEncoding();
codeToGID = (int *)gmallocn(256, sizeof(int));
codeToGIDLen = 256;
for (i = 0; i < 256; ++i) {
codeToGID[i] = 0;
if (charProcs && (name = enc[i])) {
for (j = 0; j < charProcs->getLength(); j++) {
if (strcmp(name, charProcs->getKey(j)) == 0) {
codeToGID[i] = j;
}
}
}
}
return new CairoType3Font(ref, doc, font_face, codeToGID, codeToGIDLen, printing);
}
CairoType3Font::CairoType3Font(Ref ref,
PDFDoc *doc,
cairo_font_face_t *cairo_font_face,
int *codeToGID,
Guint codeToGIDLen,
GBool printing) : CairoFont(ref,
cairo_font_face,
codeToGID,
codeToGIDLen,
gFalse,
printing),
doc(doc) { }
CairoType3Font::~CairoType3Font() { }
GBool
CairoType3Font::matches(Ref &other, GBool printingA) {
return (other.num == ref.num && other.gen == ref.gen && printing == printingA);
}
//------------------------------------------------------------------------
// CairoFontEngine
//------------------------------------------------------------------------
CairoFontEngine::CairoFontEngine(FT_Library libA) {
int i;
lib = libA;
for (i = 0; i < cairoFontCacheSize; ++i) {
fontCache[i] = NULL;
}
FT_Int major, minor, patch;
// as of FT 2.1.8, CID fonts are indexed by CID instead of GID
FT_Library_Version(lib, &major, &minor, &patch);
useCIDs = major > 2 ||
(major == 2 && (minor > 1 || (minor == 1 && patch > 7)));
}
CairoFontEngine::~CairoFontEngine() {
int i;
for (i = 0; i < cairoFontCacheSize; ++i) {
if (fontCache[i])
delete fontCache[i];
}
}
CairoFont *
CairoFontEngine::getFont(GfxFont *gfxFont, PDFDoc *doc, GBool printing) {
int i, j;
Ref ref;
CairoFont *font;
GfxFontType fontType;
ref = *gfxFont->getID();
for (i = 0; i < cairoFontCacheSize; ++i) {
font = fontCache[i];
if (font && font->matches(ref, printing)) {
for (j = i; j > 0; --j) {
fontCache[j] = fontCache[j-1];
}
fontCache[0] = font;
return font;
}
}
fontType = gfxFont->getType();
if (fontType == fontType3)
font = CairoType3Font::create (gfxFont, doc, this, printing);
else
font = CairoFreeTypeFont::create (gfxFont, doc->getXRef(), lib, useCIDs);
//XXX: if font is null should we still insert it into the cache?
if (fontCache[cairoFontCacheSize - 1]) {
delete fontCache[cairoFontCacheSize - 1];
}
for (j = cairoFontCacheSize - 1; j > 0; --j) {
fontCache[j] = fontCache[j-1];
}
fontCache[0] = font;
return font;
}

View File

@ -0,0 +1,124 @@
//========================================================================
//
// CairoFontEngine.h
//
// Copyright 2003 Glyph & Cog, LLC
// Copyright 2004 Red Hat, Inc
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2005 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2006, 2007 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2006, 2010 Carlos Garcia Campos <carlosgc@gnome.org>
// Copyright (C) 2008 Adrian Johnson <ajohnson@redneon.com>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#ifndef CAIROFONTENGINE_H
#define CAIROFONTENGINE_H
#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif
#include "goo/gtypes.h"
#include <cairo-ft.h>
#include "GfxFont.h"
#include "PDFDoc.h"
class CairoFontEngine;
class CairoFont {
public:
CairoFont(Ref ref,
cairo_font_face_t *face,
int *codeToGID,
Guint codeToGIDLen,
GBool substitute,
GBool printing);
virtual ~CairoFont();
virtual GBool matches(Ref &other, GBool printing);
cairo_font_face_t *getFontFace(void);
unsigned long getGlyph(CharCode code, Unicode *u, int uLen);
double getSubstitutionCorrection(GfxFont *gfxFont);
GBool isSubstitute() { return substitute; }
protected:
Ref ref;
cairo_font_face_t *cairo_font_face;
int *codeToGID;
Guint codeToGIDLen;
GBool substitute;
GBool printing;
};
//------------------------------------------------------------------------
class CairoFreeTypeFont : public CairoFont {
public:
static CairoFreeTypeFont *create(GfxFont *gfxFont, XRef *xref, FT_Library lib, GBool useCIDs);
virtual ~CairoFreeTypeFont();
private:
CairoFreeTypeFont(Ref ref, cairo_font_face_t *cairo_font_face,
int *codeToGID, Guint codeToGIDLen, GBool substitute);
};
//------------------------------------------------------------------------
class CairoType3Font : public CairoFont {
public:
static CairoType3Font *create(GfxFont *gfxFont, PDFDoc *doc,
CairoFontEngine *fontEngine,
GBool printing);
virtual ~CairoType3Font();
virtual GBool matches(Ref &other, GBool printing);
private:
CairoType3Font(Ref ref, PDFDoc *doc,
cairo_font_face_t *cairo_font_face,
int *codeToGID, Guint codeToGIDLen,
GBool printing);
PDFDoc *doc;
};
//------------------------------------------------------------------------
#define cairoFontCacheSize 64
//------------------------------------------------------------------------
// CairoFontEngine
//------------------------------------------------------------------------
class CairoFontEngine {
public:
// Create a font engine.
CairoFontEngine(FT_Library libA);
~CairoFontEngine();
CairoFont *getFont(GfxFont *gfxFont, PDFDoc *doc, GBool printing);
private:
CairoFont *fontCache[cairoFontCacheSize];
FT_Library lib;
GBool useCIDs;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,498 @@
//========================================================================
//
// CairoOutputDev.h
//
// Copyright 2003 Glyph & Cog, LLC
// Copyright 2004 Red Hat, INC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005-2008 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2005 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
// Copyright (C) 2006-2011 Carlos Garcia Campos <carlosgc@gnome.org>
// Copyright (C) 2008, 2009, 2011, 2012 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2008 Michael Vrable <mvrable@cs.ucsd.edu>
// Copyright (C) 2010-2012 Thomas Freitag <Thomas.Freitag@alfa.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#ifndef CAIROOUTPUTDEV_H
#define CAIROOUTPUTDEV_H
#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif
#include "goo/gtypes.h"
#include <cairo-ft.h>
#include "OutputDev.h"
#include "TextOutputDev.h"
#include "GfxState.h"
class PDFDoc;
class GfxState;
class GfxPath;
class Gfx8BitFont;
struct GfxRGB;
class CairoFontEngine;
class CairoFont;
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// CairoImage
//------------------------------------------------------------------------
class CairoImage {
public:
// Constructor.
CairoImage (double x1, double y1, double x2, double y2);
// Destructor.
~CairoImage ();
// Set the image cairo surface
void setImage (cairo_surface_t *image);
// Get the image cairo surface
cairo_surface_t *getImage () const { return image; }
// Get the image rectangle
void getRect (double *xa1, double *ya1, double *xa2, double *ya2)
{ *xa1 = x1; *ya1 = y1; *xa2 = x2; *ya2 = y2; }
private:
cairo_surface_t *image; // image cairo surface
double x1, y1; // upper left corner
double x2, y2; // lower right corner
};
//------------------------------------------------------------------------
// CairoOutputDev
//------------------------------------------------------------------------
class CairoOutputDev: public OutputDev {
public:
// Constructor.
CairoOutputDev();
// Destructor.
virtual ~CairoOutputDev();
//----- get info about output device
// Does this device use upside-down coordinates?
// (Upside-down means (0,0) is the top left corner of the page.)
virtual GBool upsideDown() { return gTrue; }
// Does this device use drawChar() or drawString()?
virtual GBool useDrawChar() { return gTrue; }
// Does this device use tilingPatternFill()? If this returns false,
// tiling pattern fills will be reduced to a series of other drawing
// operations.
virtual GBool useTilingPatternFill() { return gTrue; }
// Does this device use functionShadedFill(), axialShadedFill(), and
// radialShadedFill()? If this returns false, these shaded fills
// will be reduced to a series of other drawing operations.
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
virtual GBool useShadedFills(int type) { return type <= 7; }
#else
virtual GBool useShadedFills(int type) { return type < 4; }
#endif
// Does this device use FillColorStop()?
virtual GBool useFillColorStop() { return gTrue; }
// Does this device use beginType3Char/endType3Char? Otherwise,
// text in Type 3 fonts will be drawn with drawChar/drawString.
virtual GBool interpretType3Chars() { return gFalse; }
//----- initialization and control
// Start a page.
virtual void startPage(int pageNum, GfxState *state);
// End a page.
virtual void endPage();
//----- save/restore graphics state
virtual void saveState(GfxState *state);
virtual void restoreState(GfxState *state);
//----- update graphics state
virtual void updateAll(GfxState *state);
virtual void setDefaultCTM(double *ctm);
virtual void updateCTM(GfxState *state, double m11, double m12,
double m21, double m22, double m31, double m32);
virtual void updateLineDash(GfxState *state);
virtual void updateFlatness(GfxState *state);
virtual void updateLineJoin(GfxState *state);
virtual void updateLineCap(GfxState *state);
virtual void updateMiterLimit(GfxState *state);
virtual void updateLineWidth(GfxState *state);
virtual void updateFillColor(GfxState *state);
virtual void updateStrokeColor(GfxState *state);
virtual void updateFillOpacity(GfxState *state);
virtual void updateStrokeOpacity(GfxState *state);
virtual void updateFillColorStop(GfxState *state, double offset);
virtual void updateBlendMode(GfxState *state);
//----- update text state
virtual void updateFont(GfxState *state);
//----- path painting
virtual void stroke(GfxState *state);
virtual void fill(GfxState *state);
virtual void eoFill(GfxState *state);
virtual void clipToStrokePath(GfxState *state);
virtual GBool tilingPatternFill(GfxState *state, Gfx *gfx, Catalog *cat, Object *str,
double *pmat, int paintType, int tilingType, Dict *resDict,
double *mat, double *bbox,
int x0, int y0, int x1, int y1,
double xStep, double yStep);
virtual GBool axialShadedFill(GfxState *state, GfxAxialShading *shading, double tMin, double tMax);
virtual GBool axialShadedSupportExtend(GfxState *state, GfxAxialShading *shading);
virtual GBool radialShadedFill(GfxState *state, GfxRadialShading *shading, double sMin, double sMax);
virtual GBool radialShadedSupportExtend(GfxState *state, GfxRadialShading *shading);
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
virtual GBool gouraudTriangleShadedFill(GfxState *state, GfxGouraudTriangleShading *shading);
virtual GBool patchMeshShadedFill(GfxState *state, GfxPatchMeshShading *shading);
#endif
//----- path clipping
virtual void clip(GfxState *state);
virtual void eoClip(GfxState *state);
//----- text drawing
void beginString(GfxState *state, GooString *s);
void endString(GfxState *state);
void drawChar(GfxState *state, double x, double y,
double dx, double dy,
double originX, double originY,
CharCode code, int nBytes, Unicode *u, int uLen);
void beginActualText(GfxState *state, GooString *text);
void endActualText(GfxState *state);
virtual GBool beginType3Char(GfxState *state, double x, double y,
double dx, double dy,
CharCode code, Unicode *u, int uLen);
virtual void endType3Char(GfxState *state);
virtual void beginTextObject(GfxState *state);
virtual GBool deviceHasTextClip(GfxState *state) { return textClipPath; }
virtual void endTextObject(GfxState *state);
//----- image drawing
virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
int width, int height, GBool invert, GBool interpolate,
GBool inlineImg);
virtual void setSoftMaskFromImageMask(GfxState *state,
Object *ref, Stream *str,
int width, int height, GBool invert,
GBool inlineImg, double *baseMatrix);
virtual void unsetSoftMaskFromImageMask(GfxState *state, double *baseMatrix);
void drawImageMaskPrescaled(GfxState *state, Object *ref, Stream *str,
int width, int height, GBool invert, GBool interpolate,
GBool inlineImg);
void drawImageMaskRegular(GfxState *state, Object *ref, Stream *str,
int width, int height, GBool invert, GBool interpolate,
GBool inlineImg);
virtual void drawImage(GfxState *state, Object *ref, Stream *str,
int width, int height, GfxImageColorMap *colorMap,
GBool interpolate, int *maskColors, GBool inlineImg);
virtual void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
GBool interpolate,
Stream *maskStr,
int maskWidth, int maskHeight,
GfxImageColorMap *maskColorMap,
GBool maskInterpolate);
virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
GBool interpolate,
Stream *maskStr,
int maskWidth, int maskHeight,
GBool maskInvert, GBool maskInterpolate);
//----- transparency groups and soft masks
virtual void beginTransparencyGroup(GfxState * /*state*/, double * /*bbox*/,
GfxColorSpace * /*blendingColorSpace*/,
GBool /*isolated*/, GBool /*knockout*/,
GBool /*forSoftMask*/);
virtual void endTransparencyGroup(GfxState * /*state*/);
void popTransparencyGroup();
virtual void paintTransparencyGroup(GfxState * /*state*/, double * /*bbox*/);
virtual void setSoftMask(GfxState * /*state*/, double * /*bbox*/, GBool /*alpha*/,
Function * /*transferFunc*/, GfxColor * /*backdropColor*/);
virtual void clearSoftMask(GfxState * /*state*/);
//----- Type 3 font operators
virtual void type3D0(GfxState *state, double wx, double wy);
virtual void type3D1(GfxState *state, double wx, double wy,
double llx, double lly, double urx, double ury);
//----- special access
// Called to indicate that a new PDF document has been loaded.
void startDoc(PDFDoc *docA, CairoFontEngine *fontEngine = NULL);
GBool isReverseVideo() { return gFalse; }
void setCairo (cairo_t *cr);
void setTextPage (TextPage *text);
void setPrinting (GBool printing) { this->printing = printing; needFontUpdate = gTrue; }
void setInType3Char(GBool inType3Char) { this->inType3Char = inType3Char; }
void getType3GlyphWidth (double *wx, double *wy) { *wx = t3_glyph_wx; *wy = t3_glyph_wy; }
GBool hasType3GlyphBBox () { return t3_glyph_has_bbox; }
double *getType3GlyphBBox () { return t3_glyph_bbox; }
protected:
void doPath(cairo_t *cairo, GfxState *state, GfxPath *path);
cairo_surface_t *downscaleSurface(cairo_surface_t *orig_surface);
void getScaledSize(int orig_width, int orig_height,
int *scaledWidth, int *scaledHeight);
cairo_filter_t getFilterForSurface(cairo_surface_t *image,
GBool interpolate);
GBool getStreamData (Stream *str, char **buffer, int *length);
void setMimeData(Stream *str, Object *ref, cairo_surface_t *image);
void fillToStrokePathClip(GfxState *state);
void alignStrokeCoords(GfxSubpath *subpath, int i, double *x, double *y);
GfxRGB fill_color, stroke_color;
cairo_pattern_t *fill_pattern, *stroke_pattern;
double fill_opacity;
double stroke_opacity;
GBool stroke_adjust;
GBool adjusted_stroke_width;
GBool align_stroke_coords;
CairoFont *currentFont;
struct StrokePathClip {
GfxPath *path;
cairo_matrix_t ctm;
double line_width;
double *dashes;
int dash_count;
double dash_offset;
cairo_line_cap_t cap;
cairo_line_join_t join;
double miter;
} *strokePathClip;
PDFDoc *doc; // the current document
static FT_Library ft_lib;
static GBool ft_lib_initialized;
CairoFontEngine *fontEngine;
GBool fontEngine_owner;
cairo_t *cairo;
cairo_matrix_t orig_matrix;
GBool needFontUpdate; // set when the font needs to be updated
GBool printing;
GBool use_show_text_glyphs;
cairo_surface_t *surface;
cairo_glyph_t *glyphs;
int glyphCount;
cairo_text_cluster_t *clusters;
int clusterCount;
char *utf8;
int utf8Count;
int utf8Max;
cairo_path_t *textClipPath;
GBool inType3Char; // inside a Type 3 CharProc
double t3_glyph_wx, t3_glyph_wy;
GBool t3_glyph_has_bbox;
double t3_glyph_bbox[4];
GBool prescaleImages;
TextPage *text; // text for the current page
ActualText *actualText;
cairo_pattern_t *group;
cairo_pattern_t *shape;
cairo_pattern_t *mask;
cairo_matrix_t mask_matrix;
cairo_surface_t *cairo_shape_surface;
cairo_t *cairo_shape;
int knockoutCount;
struct ColorSpaceStack {
GBool knockout;
GfxColorSpace *cs;
cairo_matrix_t group_matrix;
struct ColorSpaceStack *next;
} * groupColorSpaceStack;
struct MaskStack {
cairo_pattern_t *mask;
cairo_matrix_t mask_matrix;
struct MaskStack *next;
} *maskStack;
};
//------------------------------------------------------------------------
// CairoImageOutputDev
//------------------------------------------------------------------------
//XXX: this should ideally not inherit from CairoOutputDev but use it instead perhaps
class CairoImageOutputDev: public CairoOutputDev {
public:
// Constructor.
CairoImageOutputDev();
// Destructor.
virtual ~CairoImageOutputDev();
//----- get info about output device
// Does this device use upside-down coordinates?
// (Upside-down means (0,0) is the top left corner of the page.)
virtual GBool upsideDown() { return gTrue; }
// Does this device use drawChar() or drawString()?
virtual GBool useDrawChar() { return gFalse; }
// Does this device use tilingPatternFill()? If this returns false,
// tiling pattern fills will be reduced to a series of other drawing
// operations.
virtual GBool useTilingPatternFill() { return gTrue; }
// Does this device use functionShadedFill(), axialShadedFill(), and
// radialShadedFill()? If this returns false, these shaded fills
// will be reduced to a series of other drawing operations.
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 11, 2)
virtual GBool useShadedFills(int type) { return type <= 7; }
#else
virtual GBool useShadedFills(int type) { return type < 4; }
#endif
// Does this device use FillColorStop()?
virtual GBool useFillColorStop() { return gFalse; }
// Does this device use beginType3Char/endType3Char? Otherwise,
// text in Type 3 fonts will be drawn with drawChar/drawString.
virtual GBool interpretType3Chars() { return gFalse; }
// Does this device need non-text content?
virtual GBool needNonText() { return gTrue; }
//----- save/restore graphics state
virtual void saveState(GfxState *state) { }
virtual void restoreState(GfxState *state) { }
//----- update graphics state
virtual void updateAll(GfxState *state) { }
virtual void setDefaultCTM(double *ctm) { }
virtual void updateCTM(GfxState *state, double m11, double m12,
double m21, double m22, double m31, double m32) { }
virtual void updateLineDash(GfxState *state) { }
virtual void updateFlatness(GfxState *state) { }
virtual void updateLineJoin(GfxState *state) { }
virtual void updateLineCap(GfxState *state) { }
virtual void updateMiterLimit(GfxState *state) { }
virtual void updateLineWidth(GfxState *state) { }
virtual void updateFillColor(GfxState *state) { }
virtual void updateStrokeColor(GfxState *state) { }
virtual void updateFillOpacity(GfxState *state) { }
virtual void updateStrokeOpacity(GfxState *state) { }
virtual void updateBlendMode(GfxState *state) { }
//----- update text state
virtual void updateFont(GfxState *state) { }
//----- path painting
virtual void stroke(GfxState *state) { }
virtual void fill(GfxState *state) { }
virtual void eoFill(GfxState *state) { }
virtual void clipToStrokePath(GfxState *state) { }
virtual GBool tilingPatternFill(GfxState *state, Gfx *gfx, Catalog *cat, Object *str,
double *pmat, int paintType, int tilingType, Dict *resDict,
double *mat, double *bbox,
int x0, int y0, int x1, int y1,
double xStep, double yStep) { return gTrue; }
virtual GBool axialShadedFill(GfxState *state,
GfxAxialShading *shading,
double tMin, double tMax) { return gTrue; }
virtual GBool radialShadedFill(GfxState *state,
GfxRadialShading *shading,
double sMin, double sMax) { return gTrue; }
//----- path clipping
virtual void clip(GfxState *state) { }
virtual void eoClip(GfxState *state) { }
//----- image drawing
virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
int width, int height, GBool invert,
GBool interpolate, GBool inlineImg);
virtual void drawImage(GfxState *state, Object *ref, Stream *str,
int width, int height, GfxImageColorMap *colorMap,
GBool interpolate, int *maskColors, GBool inlineImg);
virtual void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
GBool interpolate,
Stream *maskStr,
int maskWidth, int maskHeight,
GfxImageColorMap *maskColorMap,
GBool maskInterpolate);
virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
GBool interpolate,
Stream *maskStr,
int maskWidth, int maskHeight,
GBool maskInvert, GBool maskInterpolate);
//----- transparency groups and soft masks
virtual void beginTransparencyGroup(GfxState * /*state*/, double * /*bbox*/,
GfxColorSpace * /*blendingColorSpace*/,
GBool /*isolated*/, GBool /*knockout*/,
GBool /*forSoftMask*/) {}
virtual void endTransparencyGroup(GfxState * /*state*/) {}
virtual void paintTransparencyGroup(GfxState * /*state*/, double * /*bbox*/) {}
virtual void setSoftMask(GfxState * /*state*/, double * /*bbox*/, GBool /*alpha*/,
Function * /*transferFunc*/, GfxColor * /*backdropColor*/) {}
virtual void clearSoftMask(GfxState * /*state*/) {}
//----- Image list
// By default images are not rendred
void setImageDrawDecideCbk(GBool (*cbk)(int img_id, void *data),
void *data) { imgDrawCbk = cbk; imgDrawCbkData = data; }
// Iterate through list of images.
int getNumImages() const { return numImages; }
CairoImage *getImage(int i) const { return images[i]; }
private:
void saveImage(CairoImage *image);
CairoImage **images;
int numImages;
int size;
GBool (*imgDrawCbk)(int img_id, void *data);
void *imgDrawCbkData;
};
#endif

View File

@ -0,0 +1,366 @@
/* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */
/*
* Copyright © 2009 Mozilla Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Mozilla Corporation not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Mozilla Corporation makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
* SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
* Author: Jeff Muizelaar, Mozilla Corp.
*/
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2012 Hib Eris <hib@hiberis.nl>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
/* This implements a box filter that supports non-integer box sizes */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>
#include "goo/gmem.h"
#include "goo/gtypes_p.h"
#include "CairoRescaleBox.h"
/* we work in fixed point where 1. == 1 << 24 */
#define FIXED_SHIFT 24
static void downsample_row_box_filter (
int start, int width,
uint32_t *src, uint32_t *dest,
int coverage[], int pixel_coverage)
{
/* we need an array of the pixel contribution of each destination pixel on the boundaries.
* we invert the value to get the value on the other size of the box */
/*
value = a * contribution * 1/box_size
value += a * 1/box_size
value += a * 1/box_size
value += a * 1/box_size
value += a * (1 - contribution) * 1/box_size
a * (1/box_size - contribution * 1/box_size)
box size is constant
value = a * contribtion_a * 1/box_size + b * contribution_b * 1/box_size
contribution_b = (1 - contribution_a)
= (1 - contribution_a_next)
*/
/* box size = ceil(src_width/dest_width) */
int x = 0;
/* skip to start */
/* XXX: it might be possible to do this directly instead of iteratively, however
* the iterative solution is simple */
while (x < start)
{
int box = 1 << FIXED_SHIFT;
int start_coverage = coverage[x];
box -= start_coverage;
src++;
while (box >= pixel_coverage)
{
src++;
box -= pixel_coverage;
}
x++;
}
while (x < start + width)
{
uint32_t a = 0;
uint32_t r = 0;
uint32_t g = 0;
uint32_t b = 0;
int box = 1 << FIXED_SHIFT;
int start_coverage = coverage[x];
a = ((*src >> 24) & 0xff) * start_coverage;
r = ((*src >> 16) & 0xff) * start_coverage;
g = ((*src >> 8) & 0xff) * start_coverage;
b = ((*src >> 0) & 0xff) * start_coverage;
src++;
x++;
box -= start_coverage;
while (box >= pixel_coverage)
{
a += ((*src >> 24) & 0xff) * pixel_coverage;
r += ((*src >> 16) & 0xff) * pixel_coverage;
g += ((*src >> 8) & 0xff) * pixel_coverage;
b += ((*src >> 0) & 0xff) * pixel_coverage;
src++;
box -= pixel_coverage;
}
/* multiply by whatever is leftover
* this ensures that we don't bias down.
* i.e. start_coverage + n*pixel_coverage + box == 1 << 24 */
if (box > 0)
{
a += ((*src >> 24) & 0xff) * box;
r += ((*src >> 16) & 0xff) * box;
g += ((*src >> 8) & 0xff) * box;
b += ((*src >> 0) & 0xff) * box;
}
a >>= FIXED_SHIFT;
r >>= FIXED_SHIFT;
g >>= FIXED_SHIFT;
b >>= FIXED_SHIFT;
*dest = (a << 24) | (r << 16) | (g << 8) | b;
dest++;
}
}
static void downsample_columns_box_filter (
int n,
int start_coverage,
int pixel_coverage,
uint32_t *src, uint32_t *dest)
{
int stride = n;
while (n--) {
uint32_t a = 0;
uint32_t r = 0;
uint32_t g = 0;
uint32_t b = 0;
uint32_t *column_src = src;
int box = 1 << FIXED_SHIFT;
a = ((*column_src >> 24) & 0xff) * start_coverage;
r = ((*column_src >> 16) & 0xff) * start_coverage;
g = ((*column_src >> 8) & 0xff) * start_coverage;
b = ((*column_src >> 0) & 0xff) * start_coverage;
column_src += stride;
box -= start_coverage;
while (box >= pixel_coverage)
{
a += ((*column_src >> 24) & 0xff) * pixel_coverage;
r += ((*column_src >> 16) & 0xff) * pixel_coverage;
g += ((*column_src >> 8) & 0xff) * pixel_coverage;
b += ((*column_src >> 0) & 0xff) * pixel_coverage;
column_src += stride;
box -= pixel_coverage;
}
if (box > 0) {
a += ((*column_src >> 24) & 0xff) * box;
r += ((*column_src >> 16) & 0xff) * box;
g += ((*column_src >> 8) & 0xff) * box;
b += ((*column_src >> 0) & 0xff) * box;
}
a >>= FIXED_SHIFT;
r >>= FIXED_SHIFT;
g >>= FIXED_SHIFT;
b >>= FIXED_SHIFT;
*dest = (a << 24) | (r << 16) | (g << 8) | b;
dest++;
src++;
}
}
static int compute_coverage (int coverage[], int src_length, int dest_length)
{
int i;
/* num = src_length/dest_length
total = sum(pixel) / num
pixel * 1/num == pixel * dest_length / src_length
*/
/* the average contribution of each source pixel */
int ratio = ((1 << 24)*(long long int)dest_length)/src_length;
/* because ((1 << 24)*(long long int)dest_length) won't always be divisible by src_length
* we'll need someplace to put the other bits.
*
* We want to ensure a + n*ratio < 1<<24
*
* 1<<24
* */
double scale = (double)src_length/dest_length;
/* for each destination pixel compute the coverage of the left most pixel included in the box */
/* I have a proof of this, which this margin is too narrow to contain */
for (i=0; i<dest_length; i++)
{
float left_side = i*scale;
float right_side = (i+1)*scale;
float right_fract = right_side - floor (right_side);
float left_fract = ceil (left_side) - left_side;
int overage;
/* find out how many source pixels will be used to fill the box */
int count = floor (right_side) - ceil (left_side);
/* what's the maximum value this expression can become?
floor((i+1)*scale) - ceil(i*scale)
(i+1)*scale - i*scale == scale
since floor((i+1)*scale) <= (i+1)*scale
and ceil(i*scale) >= i*scale
floor((i+1)*scale) - ceil(i*scale) <= scale
further since: floor((i+1)*scale) - ceil(i*scale) is an integer
therefore:
floor((i+1)*scale) - ceil(i*scale) <= floor(scale)
*/
if (left_fract == 0.)
count--;
/* compute how much the right-most pixel contributes */
overage = ratio*(right_fract);
/* the remainder is the the amount that the left-most pixel
* contributes */
coverage[i] = (1<<24) - (count * ratio + overage);
}
return ratio;
}
GBool downscale_box_filter(uint32_t *orig, int orig_stride, unsigned orig_width, unsigned orig_height,
signed scaled_width, signed scaled_height,
uint16_t start_column, uint16_t start_row,
uint16_t width, uint16_t height,
uint32_t *dest, int dst_stride)
{
int pixel_coverage_x, pixel_coverage_y;
int dest_y;
int src_y = 0;
uint32_t *scanline = orig;
int *x_coverage = NULL;
int *y_coverage = NULL;
uint32_t *temp_buf = NULL;
GBool retval = gFalse;
x_coverage = (int *)gmallocn3 (orig_width, 1, sizeof(int));
y_coverage = (int *)gmallocn3 (orig_height, 1, sizeof(int));
/* we need to allocate enough room for ceil(src_height/dest_height)+1
Example:
src_height = 140
dest_height = 50
src_height/dest_height = 2.8
|-------------| 2.8 pixels
|----|----|----|----| 4 pixels
need to sample 3 pixels
|-------------| 2.8 pixels
|----|----|----|----| 4 pixels
need to sample 4 pixels
*/
temp_buf = (uint32_t *)gmallocn3 ((orig_height + scaled_height-1)/scaled_height+1, scaled_width, sizeof(uint32_t));
if (!x_coverage || !y_coverage || !scanline || !temp_buf)
goto cleanup;
pixel_coverage_x = compute_coverage (x_coverage, orig_width, scaled_width);
pixel_coverage_y = compute_coverage (y_coverage, orig_height, scaled_height);
assert (width + start_column <= scaled_width);
/* skip the rows at the beginning */
for (dest_y = 0; dest_y < start_row; dest_y++)
{
int box = 1 << FIXED_SHIFT;
int start_coverage_y = y_coverage[dest_y];
box -= start_coverage_y;
src_y++;
while (box >= pixel_coverage_y)
{
box -= pixel_coverage_y;
src_y++;
}
}
for (; dest_y < start_row + height; dest_y++)
{
int columns = 0;
int box = 1 << FIXED_SHIFT;
int start_coverage_y = y_coverage[dest_y];
scanline = orig + src_y * orig_stride / 4;
downsample_row_box_filter (start_column, width, scanline, temp_buf + width * columns, x_coverage, pixel_coverage_x);
columns++;
src_y++;
box -= start_coverage_y;
while (box >= pixel_coverage_y)
{
scanline = orig + src_y * orig_stride / 4;
downsample_row_box_filter (start_column, width, scanline, temp_buf + width * columns, x_coverage, pixel_coverage_x);
columns++;
src_y++;
box -= pixel_coverage_y;
}
/* downsample any leftovers */
if (box > 0)
{
scanline = orig + src_y * orig_stride / 4;
downsample_row_box_filter (start_column, width, scanline, temp_buf + width * columns, x_coverage, pixel_coverage_x);
columns++;
}
/* now scale the rows we just downsampled in the y direction */
downsample_columns_box_filter (width, start_coverage_y, pixel_coverage_y, temp_buf, dest);
dest += dst_stride / 4;
// assert(width*columns <= ((orig_height + scaled_height-1)/scaled_height+1) * width);
}
// assert (src_y<=orig_height);
retval = gTrue;
cleanup:
free (x_coverage);
free (y_coverage);
free (temp_buf);
return retval;
}

View File

@ -0,0 +1,12 @@
#ifndef CAIRO_RESCALE_BOX_H
#define CAIRO_RESCALE_BOX_H
#include "goo/gtypes.h"
GBool downscale_box_filter(unsigned int *orig, int orig_stride, unsigned orig_width, unsigned orig_height,
signed scaled_width, signed scaled_height,
unsigned short int start_column, unsigned short int start_row,
unsigned short int width, unsigned short int height,
unsigned int *dest, int dst_stride);
#endif /* CAIRO_RESCALE_BOX_H */

View File

@ -3,8 +3,7 @@
* *
* Handling text & font, and relative stuffs * Handling text & font, and relative stuffs
* *
* by WangLu * Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
* 2012.08.14
*/ */
#include <iostream> #include <iostream>
@ -167,6 +166,14 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
} }
ffw_load_font(filepath.c_str()); ffw_load_font(filepath.c_str());
if(param->debug)
{
auto fn = str_fmt("%s/__raw_font_%lld%s", param->tmp_dir.c_str(), info.id, param->font_suffix.c_str());
add_tmp_file((char*)fn);
ffw_save((char*)fn);
}
int * code2GID = nullptr; int * code2GID = nullptr;
int code2GID_len = 0; int code2GID_len = 0;
int maxcode = 0; int maxcode = 0;
@ -398,7 +405,10 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
} }
} }
ffw_set_widths(width_list, max_key + 1, param->stretch_narrow_glyph, param->squeeze_wide_glyph); //debug
cerr << info.id << endl;
ffw_set_widths(width_list, max_key + 1, param->stretch_narrow_glyph, param->squeeze_wide_glyph, param->remove_unused_glyph);
ffw_reencode_raw(cur_mapping, max_key + 1, 1); ffw_reencode_raw(cur_mapping, max_key + 1, 1);
if(ctu) if(ctu)
@ -418,13 +428,6 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo
ffw_save(cur_tmp_fn.c_str()); ffw_save(cur_tmp_fn.c_str());
if(param->debug)
{
auto fn = str_fmt("%s/__raw_font_%lld%s", param->tmp_dir.c_str(), info.id, param->font_suffix.c_str());
add_tmp_file((char*)fn);
ffw_save((char*)fn);
}
ffw_close(); ffw_close();
/* /*

View File

@ -296,7 +296,9 @@ void ffw_metric(double * ascent, double * descent)
/* /*
* TODO:bitmap, reference have not been considered in this function * TODO:bitmap, reference have not been considered in this function
*/ */
void ffw_set_widths(int * width_list, int mapping_len, int stretch_narrow, int squeeze_wide) void ffw_set_widths(int * width_list, int mapping_len,
int stretch_narrow, int squeeze_wide,
int remove_unused)
{ {
/* /*
* Disabled, because it causes crashing * Disabled, because it causes crashing
@ -316,6 +318,11 @@ void ffw_set_widths(int * width_list, int mapping_len, int stretch_narrow, int s
printf("TODO: width vs bitmap\n"); printf("TODO: width vs bitmap\n");
} }
// debug
printf("enccount %d\n", (int)(cur_fv->map->enccount));
memset(cur_fv->selected, 0, cur_fv->map->enccount);
EncMap * map = cur_fv->map; EncMap * map = cur_fv->map;
int i; int i;
int imax = min(mapping_len, map->enccount); int imax = min(mapping_len, map->enccount);
@ -324,7 +331,11 @@ void ffw_set_widths(int * width_list, int mapping_len, int stretch_narrow, int s
/* /*
* Do mess with it if the glyphs is not used. * Do mess with it if the glyphs is not used.
*/ */
if(width_list[i] == -1) continue; if(width_list[i] == -1)
{
cur_fv->selected[i] = 1;
continue;
}
int j = map->map[i]; int j = map->map[i];
if(j == -1) continue; if(j == -1) continue;
@ -344,6 +355,12 @@ void ffw_set_widths(int * width_list, int mapping_len, int stretch_narrow, int s
sc->width = width_list[i]; sc->width = width_list[i];
} }
for(; i < cur_fv->map->enccount; ++i)
cur_fv->selected[i] = 1;
if(remove_unused)
FVDetachAndRemoveGlyphs(cur_fv);
} }
void ffw_auto_hint(void) void ffw_auto_hint(void)

View File

@ -37,22 +37,34 @@ struct Param
int decompose_ligature; int decompose_ligature;
// Advanced tweak // Advanced tweak
/*
* Position & Size
*/
double h_eps, v_eps; double h_eps, v_eps;
double space_threshold; double space_threshold;
double font_size_multiplier; double font_size_multiplier;
/*
* Font
*/
int auto_hint; int auto_hint;
int tounicode; int tounicode;
int space_as_offset; int space_as_offset;
int stretch_narrow_glyph; int stretch_narrow_glyph;
int squeeze_wide_glyph; int squeeze_wide_glyph;
int remove_unused_glyph;
std::string css_filename;
std::string font_suffix, font_format; std::string font_suffix, font_format;
std::string external_hint_tool; std::string external_hint_tool;
/*
* Output
*/
std::string css_filename;
/*
* Debug
*/
int debug; int debug;
int clean_tmp; int clean_tmp;

View File

@ -34,7 +34,9 @@ int ffw_get_em_size(void);
// fix metrics and get them // fix metrics and get them
void ffw_metric(double * ascent, double * descent); void ffw_metric(double * ascent, double * descent);
void ffw_set_widths(int * width_list, int mapping_len, int stretch_narrow, int squeeze_wide); void ffw_set_widths(int * width_list, int mapping_len,
int stretch_narrow, int squeeze_wide,
int remove_unused);
void ffw_auto_hint(void); void ffw_auto_hint(void);

View File

@ -1,6 +1,6 @@
// pdftohtmlEX.cc // pdftohtmlEX.cc
// //
// Copyright (C) 2012 Lu Wang coolwanglu<at>gmail.com // Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -34,7 +34,7 @@ void show_usage_and_exit(const char * dummy = nullptr)
{ {
cerr << "pdftohtmlEX version " << PDF2HTMLEX_VERSION << endl; cerr << "pdftohtmlEX version " << PDF2HTMLEX_VERSION << endl;
cerr << endl; cerr << endl;
cerr << "Copyright 2012 Lu Wang (coolwanglu<at>gmail.com)" << endl; cerr << "Copyright 2012 Lu Wang <coolwanglu@gmail.com>" << endl;
cerr << endl; cerr << endl;
cerr << "Usage: pdf2htmlEX [Options] <input.pdf> [<output.html>]" << endl; cerr << "Usage: pdf2htmlEX [Options] <input.pdf> [<output.html>]" << endl;
cerr << endl; cerr << endl;
@ -83,11 +83,12 @@ void parse_options (int argc, char **argv)
.add("space-as-offset", &param.space_as_offset, 0, "treat space characters as offsets") .add("space-as-offset", &param.space_as_offset, 0, "treat space characters as offsets")
.add("stretch_narrow_glyph", &param.stretch_narrow_glyph, 0, "stretch narrow glyphs instead of padding space") .add("stretch_narrow_glyph", &param.stretch_narrow_glyph, 0, "stretch narrow glyphs instead of padding space")
.add("squeeze-wide-glyph", &param.squeeze_wide_glyph, 0, "squeeze wide glyphs instead of truncating") .add("squeeze-wide-glyph", &param.squeeze_wide_glyph, 0, "squeeze wide glyphs instead of truncating")
.add("remove-unused-glyph", &param.remove_unused_glyph, 0, "remove unused glyphs in embedded fonts")
.add("css-filename", &param.css_filename, "", "Specify the file name of the generated css file")
.add("font-suffix", &param.font_suffix, ".ttf", "suffix for extracted font files") .add("font-suffix", &param.font_suffix, ".ttf", "suffix for extracted font files")
.add("font-format", &param.font_format, "opentype", "format for extracted font files") .add("font-format", &param.font_format, "opentype", "format for extracted font files")
.add("external-hint-tool", &param.external_hint_tool, "", "external tool for hintting fonts.(overrides --auto-hint)") .add("external-hint-tool", &param.external_hint_tool, "", "external tool for hintting fonts.(overrides --auto-hint)")
.add("css-filename", &param.css_filename, "", "Specify the file name of the generated css file")
.add("debug", &param.debug, 0, "output debug information") .add("debug", &param.debug, 0, "output debug information")
.add("clean-tmp", &param.clean_tmp, 1, "clean temporary files after processing") .add("clean-tmp", &param.clean_tmp, 1, "clean temporary files after processing")

View File

@ -12,8 +12,8 @@ with open('out.html','w') as outf:
if not f.lower().endswith('.pdf'): if not f.lower().endswith('.pdf'):
continue continue
print f print f
#os.system('pdf2htmlEX --dest-dir html --auto-hint=1 --external-hint-tool="ttfautohint" "%s/%s"' % (DIR,f)) os.system('pdf2htmlEX --dest-dir html --auto-hint=1 --external-hint-tool="ttfautohint" --remove-unused-glyph 1 "%s/%s"' % (DIR,f))
os.system('pdf2htmlEX --dest-dir html --process-nontext 0 --css-draw 1 "%s/%s"' % (DIR,f)) #os.system('pdf2htmlEX --dest-dir html --process-nontext 0 --css-draw 1 "%s/%s"' % (DIR,f))
ff = f[:-3] ff = f[:-3]
outf.write('<a href="html/%shtml" target="pdf">%s</a><br/>' % (ff,ff)) outf.write('<a href="html/%shtml" target="pdf">%s</a><br/>' % (ff,ff))
outf.flush(); outf.flush();