commit fd54d8a81d36e60d5dd7f2ce2895ce3be318ba8c Author: Jeff Muizelaar Date: Sun Sep 21 23:55:16 2008 -0400 rescaled fonts diff --git a/poppler/CairoFontEngine.cc b/poppler/CairoFontEngine.cc index 8d5ad72..31e7116 100644 --- a/poppler/CairoFontEngine.cc +++ b/poppler/CairoFontEngine.cc @@ -418,7 +418,7 @@ CairoFont *CairoFont::create(GfxFont *gfxFont, XRef *xref, FT_Library lib, GBool return new CairoFont(ref, font_face, face, codeToGID, codeToGIDLen, - substitute); + substitute, gfxFont); err2: /* hmm? */ @@ -426,10 +426,47 @@ CairoFont *CairoFont::create(GfxFont *gfxFont, XRef *xref, FT_Library lib, GBool return NULL; } +extern "C" { +#include "cairo-font-rescale.h" +} + +static cairo_font_face_t * +get_rescaled_font_face (cairo_font_face_t *substitute_font, + Gfx8BitFont *gfxFont, Gushort *codeToGID) +{ + int i; + double *widths; + int count; + int num_glyphs = 255; + unsigned long min_index, max_index; + cairo_font_face_t *ret; + + max_index = 255; + min_index = 0; + + count = max_index - min_index + 1; + widths = new double[count]; + /* record all of the widths */ + for (i=0; igetWidth(i); + } + + ret = create_rescaled_font (substitute_font, min_index, count, widths, codeToGID); + delete [] widths; + return ret; +} + + CairoFont::CairoFont(Ref ref, cairo_font_face_t *cairo_font_face, FT_Face face, - Gushort *codeToGID, int codeToGIDLen, GBool substitute) : ref(ref), cairo_font_face(cairo_font_face), + Gushort *codeToGID, int codeToGIDLen, GBool substitute, GfxFont *gfxFont) : ref(ref), cairo_font_face(cairo_font_face), face(face), codeToGID(codeToGID), - codeToGIDLen(codeToGIDLen), substitute(substitute) { } + codeToGIDLen(codeToGIDLen), substitute(substitute), rescaled(gFalse) +{ + if (isSubstitute() && !gfxFont->isCIDFont()) { + this->rescaled = gTrue; + this->cairo_font_face = get_rescaled_font_face(cairo_font_face, (Gfx8BitFont *)gfxFont, codeToGID); + } +} CairoFont::~CairoFont() { cairo_font_face_destroy (cairo_font_face); @@ -451,7 +488,7 @@ CairoFont::getGlyph(CharCode code, Unicode *u, int uLen) { FT_UInt gid; - if (codeToGID && code < codeToGIDLen) { + if (!rescaled && codeToGID && code < codeToGIDLen) { gid = (FT_UInt)codeToGID[code]; } else { gid = (FT_UInt)code; @@ -465,6 +502,7 @@ CairoFont::getSubstitutionCorrection(GfxFont *gfxFont) double w1, w2,w3; CharCode code; char *name; + return 1.0; // for substituted fonts: adjust the font matrix -- compare the // width of 'm' in the original font and the substituted font diff --git a/poppler/CairoFontEngine.h b/poppler/CairoFontEngine.h index 5ecdcdf..fa1dbf1 100644 --- a/poppler/CairoFontEngine.h +++ b/poppler/CairoFontEngine.h @@ -49,7 +49,7 @@ public: GBool isSubstitute() { return substitute; } private: CairoFont(Ref ref, cairo_font_face_t *cairo_font_face, FT_Face face, - Gushort *codeToGID, int codeToGIDLen, GBool substitute); + Gushort *codeToGID, int codeToGIDLen, GBool substitute, GfxFont *gfxFont); Ref ref; cairo_font_face_t *cairo_font_face; FT_Face face; @@ -58,6 +58,7 @@ private: int codeToGIDLen; GBool substitute; + GBool rescaled; }; //------------------------------------------------------------------------ diff --git a/poppler/Makefile.am b/poppler/Makefile.am index db9fd74..b4cc1a6 100644 --- a/poppler/Makefile.am +++ b/poppler/Makefile.am @@ -44,6 +44,7 @@ cairo_libs = \ $(CAIRO_LIBS) libpoppler_cairo_la_SOURCES = \ + cairo-font-rescale.c \ CairoFontEngine.cc \ CairoFontEngine.h \ CairoOutputDev.cc \ diff --git a/poppler/cairo-font-rescale.c b/poppler/cairo-font-rescale.c new file mode 100644 index 0000000..aa936ea --- /dev/null +++ b/poppler/cairo-font-rescale.c @@ -0,0 +1,218 @@ +/* + * Copyright © 2008 Jeff Muizelaar + * + * 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 + * Jeff Muizelaar not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Jeff Muizelaar makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * JEFF MUIZELAAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL JEFF MUIZELAAR 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. + * + * Contributor(s): + * Jeff Muizelaar + * Kristian Høgsberg + * Behdad Esfahbod + */ + +#include +#include +#include +#include +static const cairo_user_data_key_t rescale_font_closure_key; + +struct rescaled_font { + cairo_font_face_t *substitute_font; + cairo_scaled_font_t *measuring_font; + unsigned long glyph_count; + unsigned long start; + unsigned long code_to_glyph[256]; + double *desired_width; + double *rescale_factor; +}; + +static cairo_status_t +test_scaled_font_render_glyph (cairo_scaled_font_t *scaled_font, + unsigned long glyph, + cairo_t *cr, + cairo_text_extents_t *metrics) +{ + cairo_font_face_t *user_font; + struct rescaled_font *r; + cairo_glyph_t cairo_glyph; + + user_font = cairo_scaled_font_get_font_face (scaled_font); + r = cairo_font_face_get_user_data (user_font, &rescale_font_closure_key); + + cairo_glyph.index = r->code_to_glyph[glyph]; + cairo_glyph.x = 0; + cairo_glyph.y = 0; + + cairo_set_font_face (cr, r->substitute_font); + + if (glyph - r->start < r->glyph_count) { + cairo_matrix_t matrix; + + if (isnan (r->rescale_factor[glyph - r->start])) { + double desired_width; + double actual_width; + cairo_text_extents_t extents; + + /* measure the glyph and compute the necessary rescaling factor */ + cairo_scaled_font_glyph_extents (r->measuring_font, + &cairo_glyph, 1, + &extents); + + desired_width = r->desired_width[glyph - r->start]; + actual_width = extents.x_advance; + + r->rescale_factor[glyph - r->start] = desired_width / actual_width; + } + + /* scale the font so that the glyph width matches the desired width */ + cairo_get_font_matrix (cr, &matrix); + cairo_matrix_scale (&matrix, r->rescale_factor[glyph - r->start], 1.); + cairo_set_font_matrix (cr, &matrix); + } + + cairo_show_glyphs (cr, &cairo_glyph, 1); + cairo_glyph_extents (cr, &cairo_glyph, 1, metrics); + + return CAIRO_STATUS_SUCCESS; +} + +static void +unichar_to_utf8 (uint32_t ucs4, char utf8[7]) +{ + int i, charlen, first; + + if (ucs4 < 0x80) { + first = 0; + charlen = 1; + } else if (ucs4 < 0x800) { + first = 0xc0; + charlen = 2; + } else if (ucs4 < 0x10000) { + first = 0xe0; + charlen = 3; + } else if (ucs4 < 0x200000) { + first = 0xf0; + charlen = 4; + } else if (ucs4 < 0x4000000) { + first = 0xf8; + charlen = 5; + } else { + first = 0xfc; + charlen = 6; + } + + for (i = charlen - 1; i > 0; --i) { + utf8[i] = (ucs4 & 0x3f) | 0x80; + ucs4 >>= 6; + } + utf8[0] = ucs4 | first; + utf8[charlen] = '\0'; +} + +static cairo_status_t +test_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font, + unsigned long unicode, + unsigned long *glyph_index) +{ + cairo_font_face_t *user_font; + struct rescaled_font *r; + int num_glyphs; + cairo_glyph_t *glyphs = NULL; + cairo_status_t status; + char utf8[7]; + + user_font = cairo_scaled_font_get_font_face (scaled_font); + + unichar_to_utf8 (unicode, utf8); + r = cairo_font_face_get_user_data (user_font, &rescale_font_closure_key); + status = cairo_scaled_font_text_to_glyphs (r->measuring_font, 0, 0, + utf8, -1, + &glyphs, &num_glyphs, + NULL, NULL, NULL); + if (status) + return status; + + *glyph_index = glyphs[0].index; + + cairo_glyph_free (glyphs); + return CAIRO_STATUS_SUCCESS; +} + +static void rescale_font_closure_destroy (void *data) +{ + struct rescaled_font *r = data; + + cairo_scaled_font_destroy (r->measuring_font); + free (r->desired_width); + free (r->rescale_factor); + free (r); +} + +cairo_font_face_t * +create_rescaled_font (cairo_font_face_t *substitute_font, + int glyph_start, + int glyph_count, + double *desired_width, + unsigned short *code_to_glyph) +{ + cairo_font_face_t *user_font_face; + struct rescaled_font *r; + cairo_font_options_t *options; + cairo_matrix_t m; + unsigned long i; + + user_font_face = cairo_user_font_face_create (); + cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph); + cairo_user_font_face_set_unicode_to_glyph_func (user_font_face, test_scaled_font_unicode_to_glyph); + + r = malloc (sizeof (struct rescaled_font)); + r->substitute_font = substitute_font; + + /* we don't want any hinting when doing the measuring */ + 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_matrix_init_identity (&m); + + r->measuring_font = cairo_scaled_font_create (r->substitute_font, + &m, &m, + options); + cairo_font_options_destroy (options); + + + r->start = glyph_start; + r->glyph_count = glyph_count; + r->desired_width = calloc (sizeof (double), r->glyph_count); + r->rescale_factor = calloc (sizeof (double), r->glyph_count); + + for (i = 0; i < r->glyph_count; i++) { + r->desired_width[i] = desired_width[i]; + //XXX: the condition shouldn't be needed + r->code_to_glyph[i] = code_to_glyph ? code_to_glyph[i] : i; + /* use NaN to specify unset */ + r->rescale_factor[i] = strtod ("NaN", NULL); + } + + cairo_font_face_set_user_data (user_font_face, &rescale_font_closure_key, + r, rescale_font_closure_destroy); + + return user_font_face; +} + diff --git a/poppler/cairo-font-rescale.h b/poppler/cairo-font-rescale.h new file mode 100644 index 0000000..53f3e3f --- /dev/null +++ b/poppler/cairo-font-rescale.h @@ -0,0 +1,30 @@ +/* + * Copyright © 2008 Jeff Muizelaar + * + * 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 + * Jeff Muizelaar not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Jeff Muizelaar makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * JEFF MUIZELAAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL JEFF MUIZELAAR 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. + * + */ + +cairo_font_face_t * +create_rescaled_font (cairo_font_face_t *substitute_font, + int glyph_start, + int glyph_count, + double *desired_width, + unsigned short *code_to_glyph);