int pdf_fontcidtogid(pdf_fontdesc *fontdesc, int cid) { if (fontdesc->font->ftface) return ftcidtogid(fontdesc, cid); return cid; }
static int ftwidth(pdf_font *font, int cid) { int e; cid = ftcidtogid(font, cid); e = FT_Load_Glyph(font->ftface, cid, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM); if (e) return 0; return ((FT_Face)font->ftface)->glyph->advance.x; }
static int ftwidth(pdf_fontdesc *fontdesc, int cid) { int gid, fterr; gid = ftcidtogid(fontdesc, cid); fterr = FT_Load_Glyph(fontdesc->font->ftface, gid, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM); if (fterr) { fz_warn("freetype load glyph (gid %d): %s", gid, ft_errorstring(fterr)); return 0; } return ((FT_Face)fontdesc->font->ftface)->glyph->advance.x; }
static fz_error * ftrender(fz_glyph *glyph, fz_font *fzfont, int cid, fz_matrix trm) { pdf_font *font = (pdf_font*)fzfont; FT_Face face = font->ftface; FT_Matrix m; FT_Vector v; FT_Error fterr; float scale; int gid; int x, y; int hint = font->hint; gid = ftcidtogid(font, cid); if (font->substitute && fzfont->wmode == 0) { fz_hmtx subw; int realw; FT_Set_Char_Size(face, 1000, 1000, 72, 72); fterr = FT_Load_Glyph(font->ftface, gid, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM); if (fterr) return fz_throw("freetype failed to load glyph: %s", ft_errstr(fterr)); realw = ((FT_Face)font->ftface)->glyph->advance.x; subw = fz_gethmtx(fzfont, cid); if (realw) scale = (float) subw.w / realw; else scale = 1.0; trm = fz_concat(fz_scale(scale, 1.0), trm); } glyph->w = 0; glyph->h = 0; glyph->x = 0; glyph->y = 0; glyph->samples = nil; /* freetype mutilates complex glyphs if they are loaded * with FT_Set_Char_Size 1.0. it rounds the coordinates * before applying transformation. to get more precision in * freetype, we shift part of the scale in the matrix * into FT_Set_Char_Size instead */ #ifdef HINT hint = 1; #endif if (hint) { scale = fz_matrixexpansion(trm); m.xx = trm.a * 65536 / scale; m.yx = trm.b * 65536 / scale; m.xy = trm.c * 65536 / scale; m.yy = trm.d * 65536 / scale; v.x = 0; v.y = 0; FT_Set_Char_Size(face, 64 * scale, 64 * scale, 72, 72); FT_Set_Transform(face, &m, &v); fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP); if (fterr) fz_warn("freetype load glyph: %s", ft_errstr(fterr)); } else { m.xx = trm.a * 64; /* should be 65536 */ m.yx = trm.b * 64; m.xy = trm.c * 64; m.yy = trm.d * 64; v.x = trm.e * 64; v.y = trm.f * 64; FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */ FT_Set_Transform(face, &m, &v); fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING); if (fterr) fz_warn("freetype load glyph: %s", ft_errstr(fterr)); } fterr = FT_Render_Glyph(face->glyph, ft_render_mode_normal); if (fterr) fz_warn("freetype render glyph: %s", ft_errstr(fterr)); glyph->w = face->glyph->bitmap.width; glyph->h = face->glyph->bitmap.rows; glyph->x = face->glyph->bitmap_left; glyph->y = face->glyph->bitmap_top - glyph->h; glyph->samples = face->glyph->bitmap.buffer; for (y = 0; y < glyph->h / 2; y++) { for (x = 0; x < glyph->w; x++) { unsigned char a = glyph->samples[y * glyph->w + x ]; unsigned char b = glyph->samples[(glyph->h - y - 1) * glyph->w + x]; glyph->samples[y * glyph->w + x ] = b; glyph->samples[(glyph->h - y - 1) * glyph->w + x] = a; } } return fz_okay; }