1 //========================================================================
5 // Copyright 2001-2002 Glyph & Cog, LLC
7 //========================================================================
10 #pragma implementation
15 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
20 #include "freetype/ftoutln.h"
21 #include "freetype/internal/ftobjs.h"
22 #if 1 //~ cff cid->gid map
23 #include "freetype/internal/cfftypes.h"
24 #include "freetype/internal/tttypes.h"
26 #include "GlobalParams.h"
30 //------------------------------------------------------------------------
32 FTFontEngine::FTFontEngine(Display *displayA, Visual *visualA, int depthA,
33 Colormap colormapA, GBool aaA):
34 SFontEngine(displayA, visualA, depthA, colormapA) {
37 if (FT_Init_FreeType(&lib)) {
44 FTFontEngine::~FTFontEngine() {
45 FT_Done_FreeType(lib);
48 //------------------------------------------------------------------------
50 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName,
51 char **fontEnc, GBool pdfFontHasEncoding) {
53 int unicodeCmap, macRomanCmap, msSymbolCmap;
59 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
63 if (!strcmp(face->driver->root.clazz->module_name, "type1") ||
64 !strcmp(face->driver->root.clazz->module_name, "cff")) {
66 mode = ftFontModeCodeMapDirect;
67 codeMap = (Guint *)gmalloc(256 * sizeof(Guint));
68 for (i = 0; i < 256; ++i) {
70 if ((name = fontEnc[i])) {
71 codeMap[i] = FT_Get_Name_Index(face, name);
77 // To match up with the Adobe-defined behaviour, we choose a cmap
79 // 1. If the PDF font has an encoding:
80 // 1a. If the TrueType font has a Microsoft Unicode cmap, use it,
81 // and use the Unicode indexes, not the char codes.
82 // 1b. If the TrueType font has a Macintosh Roman cmap, use it,
83 // and reverse map the char names through MacRomanEncoding to
85 // 2. If the PDF font does not have an encoding:
86 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
87 // and use char codes directly.
88 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
89 // and use (0xf000 + char code).
90 // 3. If none of these rules apply, use the first cmap and hope for
91 // the best (this shouldn't happen).
92 unicodeCmap = macRomanCmap = msSymbolCmap = 0xffff;
93 for (i = 0; i < face->num_charmaps; ++i) {
94 if (face->charmaps[i]->platform_id == 3 &&
95 face->charmaps[i]->encoding_id == 1) {
97 } else if (face->charmaps[i]->platform_id == 1 &&
98 face->charmaps[i]->encoding_id == 0) {
100 } else if (face->charmaps[i]->platform_id == 3 &&
101 face->charmaps[i]->encoding_id == 0) {
106 mode = ftFontModeCharCode;
108 if (pdfFontHasEncoding) {
109 if (unicodeCmap != 0xffff) {
111 mode = ftFontModeUnicode;
112 } else if (macRomanCmap != 0xffff) {
114 mode = ftFontModeCodeMap;
115 codeMap = (Guint *)gmalloc(256 * sizeof(Guint));
116 for (j = 0; j < 256; ++j) {
118 codeMap[j] = globalParams->getMacRomanCharCode(fontEnc[j]);
125 if (macRomanCmap != 0xffff) {
127 mode = ftFontModeCharCode;
128 } else if (msSymbolCmap != 0xffff) {
130 mode = ftFontModeCharCodeOffset;
131 charMapOffset = 0xf000;
134 if (FT_Set_Charmap(face, face->charmaps[i])) {
142 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName,
143 Gushort *cidToGIDA, int cidToGIDLenA) {
147 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
150 cidToGID = cidToGIDA;
151 cidToGIDLen = cidToGIDLenA;
152 mode = ftFontModeCIDToGIDMap;
156 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName) {
160 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
165 mode = ftFontModeCFFCharset;
169 FTFontFile::~FTFontFile() {
178 //------------------------------------------------------------------------
180 FTFont::FTFont(FTFontFile *fontFileA, double *m) {
181 FTFontEngine *engine;
189 fontFile = fontFileA;
190 engine = fontFile->engine;
191 face = fontFile->face;
192 if (FT_New_Size(face, &sizeObj)) {
195 face->size = sizeObj;
196 size = sqrt(m[2]*m[2] + m[3]*m[3]);
197 if (FT_Set_Pixel_Sizes(face, 0, (int)size)) {
201 div = face->bbox.xMax > 20000 ? 65536 : 1;
203 // transform the four corners of the font bounding box -- the min
204 // and max values form the bounding box of the transformed font
205 x = (int)((m[0] * face->bbox.xMin + m[2] * face->bbox.yMin) /
206 (div * face->units_per_EM));
208 y = (int)((m[1] * face->bbox.xMin + m[3] * face->bbox.yMin) /
209 (div * face->units_per_EM));
211 x = (int)((m[0] * face->bbox.xMin + m[2] * face->bbox.yMax) /
212 (div * face->units_per_EM));
215 } else if (x > xMax) {
218 y = (int)((m[1] * face->bbox.xMin + m[3] * face->bbox.yMax) /
219 (div * face->units_per_EM));
222 } else if (y > yMax) {
225 x = (int)((m[0] * face->bbox.xMax + m[2] * face->bbox.yMin) /
226 (div * face->units_per_EM));
229 } else if (x > xMax) {
232 y = (int)((m[1] * face->bbox.xMax + m[3] * face->bbox.yMin) /
233 (div * face->units_per_EM));
236 } else if (y > yMax) {
239 x = (int)((m[0] * face->bbox.xMax + m[2] * face->bbox.yMax) /
240 (div * face->units_per_EM));
243 } else if (x > xMax) {
246 y = (int)((m[1] * face->bbox.xMax + m[3] * face->bbox.yMax) /
247 (div * face->units_per_EM));
250 } else if (y > yMax) {
253 // This is a kludge: some buggy PDF generators embed fonts with
254 // zero bounding boxes.
261 yMax = (int)(1.2 * size);
263 // this should be (max - min + 1), but we add some padding to
264 // deal with rounding errors
265 glyphW = xMax - xMin + 3;
266 glyphH = yMax - yMin + 3;
267 // another kludge: some CJK TT fonts have bogus bboxes, so add more
269 if (face->num_glyphs > 1000) {
270 glyphW += glyphW >> 1;
271 glyphH += glyphH >> 1;
274 glyphSize = glyphW * glyphH;
276 glyphSize = ((glyphW + 7) >> 3) * glyphH;
279 // set up the glyph pixmap cache
281 if (glyphSize <= 256) {
283 } else if (glyphSize <= 512) {
285 } else if (glyphSize <= 1024) {
290 cache = (Guchar *)gmalloc(cacheSets * cacheAssoc * glyphSize);
291 cacheTags = (FTFontCacheTag *)gmalloc(cacheSets * cacheAssoc *
292 sizeof(FTFontCacheTag));
293 for (i = 0; i < cacheSets * cacheAssoc; ++i) {
294 cacheTags[i].mru = i & (cacheAssoc - 1);
298 if (!(image = XCreateImage(engine->display, engine->visual, engine->depth,
299 ZPixmap, 0, NULL, glyphW, glyphH, 8, 0))) {
302 image->data = (char *)gmalloc(glyphH * image->bytes_per_line);
304 // compute the transform matrix
305 matrix.xx = (FT_Fixed)((m[0] / size) * 65536);
306 matrix.yx = (FT_Fixed)((m[1] / size) * 65536);
307 matrix.xy = (FT_Fixed)((m[2] / size) * 65536);
308 matrix.yy = (FT_Fixed)((m[3] / size) * 65536);
318 XDestroyImage(image);
321 GBool FTFont::drawChar(Drawable d, int w, int h, GC gc,
322 int x, int y, int r, int g, int b,
323 CharCode c, Unicode u) {
324 FTFontEngine *engine;
330 int xOffset, yOffset, x0, y0, x1, y1, gw, gh, w0, h0;
333 engine = fontFile->engine;
335 // no Unicode index for this char - don't draw anything
336 if (fontFile->mode == ftFontModeUnicode && u == 0) {
340 // generate the glyph pixmap
341 if (!(p = getGlyphPixmap(c, u, &xOffset, &yOffset, &gw, &gh))) {
345 // compute: (x0,y0) = position in destination drawable
346 // (x1,y1) = position in glyph image
347 // (w0,h0) = size of image transfer
378 XGetSubImage(engine->display, d, x0, y0, w0, h0, (1 << engine->depth) - 1,
379 ZPixmap, image, x1, y1);
383 // compute the colors
384 xcolor.pixel = XGetPixel(image, x1 + w0/2, y1 + h0/2);
385 XQueryColor(engine->display, engine->colormap, &xcolor);
389 colors[1] = engine->findColor((r + 3*bgR) / 4,
392 colors[2] = engine->findColor((r + bgR) / 2,
395 colors[3] = engine->findColor((3*r + bgR) / 4,
398 colors[4] = engine->findColor(r, g, b);
400 // stuff the glyph pixmap into the X image
401 for (yy = 0; yy < gh; ++yy) {
402 for (xx = 0; xx < gw; ++xx) {
404 // this is a heuristic which seems to produce decent
405 // results -- the linear mapping would be:
406 // pix = (pix * 5) / 256;
407 pix = ((pix + 10) * 5) / 256;
412 XPutPixel(image, xx, yy, colors[pix]);
420 colors[1] = engine->findColor(r, g, b);
422 // stuff the glyph bitmap into the X image
423 for (yy = 0; yy < gh; ++yy) {
424 for (xx = 0; xx < gw; xx += 8) {
426 for (xx1 = xx; xx1 < xx + 8 && xx1 < gw; ++xx1) {
428 XPutPixel(image, xx1, yy, colors[1]);
438 XPutImage(engine->display, d, gc, image, x1, y1, x0, y0, w0, h0);
443 Guchar *FTFont::getGlyphPixmap(CharCode c, Unicode u,
444 int *x, int *y, int *w, int *h) {
452 i = (c & (cacheSets - 1)) * cacheAssoc;
453 for (j = 0; j < cacheAssoc; ++j) {
454 if ((cacheTags[i+j].mru & 0x8000) && cacheTags[i+j].code == c) {
455 *x = cacheTags[i+j].x;
456 *y = cacheTags[i+j].y;
457 *w = cacheTags[i+j].w;
458 *h = cacheTags[i+j].h;
459 for (k = 0; k < cacheAssoc; ++k) {
461 (cacheTags[i+k].mru & 0x7fff) < (cacheTags[i+j].mru & 0x7fff)) {
462 ++cacheTags[i+k].mru;
465 cacheTags[i+j].mru = 0x8000;
466 return cache + (i+j) * glyphSize;
470 // generate the glyph pixmap or bitmap
471 fontFile->face->size = sizeObj;
472 FT_Set_Transform(fontFile->face, &matrix, NULL);
473 slot = fontFile->face->glyph;
474 idx = getGlyphIndex(c, u);
475 // if we have the FT2 bytecode interpreter, autohinting won't be used
476 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
477 if (FT_Load_Glyph(fontFile->face, idx, FT_LOAD_DEFAULT)) {
481 // FT2's autohinting doesn't always work very well (especially with
482 // font subsets), so turn it off if anti-aliasing is enabled; if
483 // anti-aliasing is disabled, this seems to be a tossup - some fonts
484 // look better with hinting, some without, so leave hinting on
485 if (FT_Load_Glyph(fontFile->face, idx,
486 fontFile->engine->aa ? FT_LOAD_NO_HINTING
487 : FT_LOAD_DEFAULT)) {
491 if (FT_Render_Glyph(slot,
492 fontFile->engine->aa ? ft_render_mode_normal :
493 ft_render_mode_mono)) {
496 *x = -slot->bitmap_left;
497 *y = slot->bitmap_top;
498 *w = slot->bitmap.width;
499 *h = slot->bitmap.rows;
500 if (*w > glyphW || *h > glyphH) {
502 fprintf(stderr, "Weird FreeType glyph size: %d > %d or %d > %d\n",
503 *w, glyphW, *h, glyphH);
508 // store glyph pixmap in cache
510 for (j = 0; j < cacheAssoc; ++j) {
511 if ((cacheTags[i+j].mru & 0x7fff) == cacheAssoc - 1) {
512 cacheTags[i+j].mru = 0x8000;
513 cacheTags[i+j].code = c;
514 cacheTags[i+j].x = *x;
515 cacheTags[i+j].y = *y;
516 cacheTags[i+j].w = *w;
517 cacheTags[i+j].h = *h;
518 if (fontFile->engine->aa) {
521 gSize = ((*w + 7) >> 3) * *h;
523 ret = cache + (i+j) * glyphSize;
524 memcpy(ret, slot->bitmap.buffer, gSize);
526 ++cacheTags[i+j].mru;
532 GBool FTFont::getCharPath(CharCode c, Unicode u, GfxState *state) {
533 static FT_Outline_Funcs outlineFuncs = {
544 fontFile->face->size = sizeObj;
545 FT_Set_Transform(fontFile->face, &matrix, NULL);
546 slot = fontFile->face->glyph;
547 idx = getGlyphIndex(c, u);
548 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
549 if (FT_Load_Glyph(fontFile->face, idx, FT_LOAD_DEFAULT)) {
553 // FT2's autohinting doesn't always work very well (especially with
554 // font subsets), so turn it off if anti-aliasing is enabled; if
555 // anti-aliasing is disabled, this seems to be a tossup - some fonts
556 // look better with hinting, some without, so leave hinting on
557 if (FT_Load_Glyph(fontFile->face, idx,
558 fontFile->engine->aa ? FT_LOAD_NO_HINTING
559 : FT_LOAD_DEFAULT)) {
563 if (FT_Get_Glyph(slot, &glyph)) {
566 FT_Outline_Decompose(&((FT_OutlineGlyph)glyph)->outline,
567 &outlineFuncs, state);
571 int FTFont::charPathMoveTo(FT_Vector *pt, void *state) {
572 ((GfxState *)state)->moveTo(pt->x / 64.0, -pt->y / 64.0);
576 int FTFont::charPathLineTo(FT_Vector *pt, void *state) {
577 ((GfxState *)state)->lineTo(pt->x / 64.0, -pt->y / 64.0);
581 int FTFont::charPathConicTo(FT_Vector *ctrl, FT_Vector *pt, void *state) {
582 double x0, y0, x1, y1, x2, y2, x3, y3, xc, yc;
584 x0 = ((GfxState *)state)->getCurX();
585 y0 = ((GfxState *)state)->getCurY();
587 yc = -ctrl->y / 64.0;
591 // A second-order Bezier curve is defined by two endpoints, p0 and
592 // p3, and one control point, pc:
594 // p(t) = (1-t)^2*p0 + t*(1-t)*pc + t^2*p3
596 // A third-order Bezier curve is defined by the same two endpoints,
597 // p0 and p3, and two control points, p1 and p2:
599 // p(t) = (1-t)^3*p0 + 3t*(1-t)^2*p1 + 3t^2*(1-t)*p2 + t^3*p3
601 // Applying some algebra, we can convert a second-order curve to a
602 // third-order curve:
604 // p1 = (1/3) * (p0 + 2pc)
605 // p2 = (1/3) * (2pc + p3)
607 x1 = (1.0 / 3.0) * (x0 + 2 * xc);
608 y1 = (1.0 / 3.0) * (y0 + 2 * yc);
609 x2 = (1.0 / 3.0) * (2 * xc + x3);
610 y2 = (1.0 / 3.0) * (2 * yc + y3);
612 ((GfxState *)state)->curveTo(x1, y1, x2, y2, x3, y3);
616 int FTFont::charPathCubicTo(FT_Vector *ctrl1, FT_Vector *ctrl2,
617 FT_Vector *pt, void *state) {
618 ((GfxState *)state)->curveTo(ctrl1->x / 64.0, -ctrl1->y / 64.0,
619 ctrl2->x / 64.0, -ctrl2->y / 64.0,
620 pt->x / 64.0, -pt->y / 64.0);
624 FT_UInt FTFont::getGlyphIndex(CharCode c, Unicode u) {
628 idx = 0; // make gcc happy
629 switch (fontFile->mode) {
630 case ftFontModeUnicode:
631 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)u);
633 case ftFontModeCharCode:
634 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)c);
636 case ftFontModeCharCodeOffset:
637 idx = FT_Get_Char_Index(fontFile->face,
638 (FT_ULong)(c + fontFile->charMapOffset));
640 case ftFontModeCodeMap:
642 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)fontFile->codeMap[c]);
647 case ftFontModeCodeMapDirect:
649 idx = (FT_UInt)fontFile->codeMap[c];
654 case ftFontModeCIDToGIDMap:
655 if (fontFile->cidToGIDLen) {
656 if ((int)c < fontFile->cidToGIDLen) {
657 idx = (FT_UInt)fontFile->cidToGID[c];
665 case ftFontModeCFFCharset:
666 #if 1 //~ cff cid->gid map
667 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 0
668 CFF_Font *cff = (CFF_Font *)((TT_Face)fontFile->face)->extra.data;
670 CFF_Font cff = (CFF_Font)((TT_Face)fontFile->face)->extra.data;
673 for (j = 0; j < (int)cff->num_glyphs; ++j) {
674 if (cff->charset.sids[j] == c) {
685 #endif // FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)