1 //========================================================================
5 // Copyright 2001-2003 Glyph & Cog, LLC
7 //========================================================================
11 #if FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
13 #ifdef USE_GCC_PRAGMAS
14 #pragma implementation
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,
52 GBool pdfFontIsSymbolic) {
54 int unicodeCmap, macRomanCmap, msSymbolCmap;
63 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
67 if (!strcmp(face->driver->root.clazz->module_name, "type1") ||
68 !strcmp(face->driver->root.clazz->module_name, "cff")) {
70 mode = ftFontModeCodeMapDirect;
71 codeMap = (Guint *)gmalloc(256 * sizeof(Guint));
72 for (i = 0; i < 256; ++i) {
74 if ((name = fontEnc[i])) {
75 codeMap[i] = FT_Get_Name_Index(face, name);
81 // To match up with the Adobe-defined behaviour, we choose a cmap
83 // 1. If the PDF font has an encoding:
84 // 1a. If the TrueType font has a Microsoft Unicode cmap, use it,
85 // and use the Unicode indexes, not the char codes.
86 // 1b. If the PDF font is symbolic and the TrueType font has a
87 // Microsoft Symbol cmap, use it, and use (0xf000 + char code).
88 // 1c. If the TrueType font has a Macintosh Roman cmap, use it,
89 // and reverse map the char names through MacRomanEncoding to
91 // 2. If the PDF font does not have an encoding:
92 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
93 // and use char codes directly.
94 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
95 // and use (0xf000 + char code).
96 // 3. If none of these rules apply, use the first cmap and hope for
97 // the best (this shouldn't happen).
98 unicodeCmap = macRomanCmap = msSymbolCmap = 0xffff;
99 for (i = 0; i < face->num_charmaps; ++i) {
100 if ((face->charmaps[i]->platform_id == 3 &&
101 face->charmaps[i]->encoding_id == 1) ||
102 face->charmaps[i]->platform_id == 0) {
104 } else if (face->charmaps[i]->platform_id == 1 &&
105 face->charmaps[i]->encoding_id == 0) {
107 } else if (face->charmaps[i]->platform_id == 3 &&
108 face->charmaps[i]->encoding_id == 0) {
113 mode = ftFontModeCharCode;
115 if (pdfFontHasEncoding) {
116 if (unicodeCmap != 0xffff) {
118 mode = ftFontModeUnicode;
119 } else if (pdfFontIsSymbolic && msSymbolCmap != 0xffff) {
121 mode = ftFontModeCharCodeOffset;
122 charMapOffset = 0xf000;
123 } else if (macRomanCmap != 0xffff) {
125 mode = ftFontModeCodeMap;
126 codeMap = (Guint *)gmalloc(256 * sizeof(Guint));
127 for (j = 0; j < 256; ++j) {
129 codeMap[j] = globalParams->getMacRomanCharCode(fontEnc[j]);
136 if (macRomanCmap != 0xffff) {
138 mode = ftFontModeCharCode;
139 } else if (msSymbolCmap != 0xffff) {
141 mode = ftFontModeCharCodeOffset;
142 charMapOffset = 0xf000;
145 if (FT_Set_Charmap(face, face->charmaps[i])) {
153 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName,
154 Gushort *cidToGIDA, int cidToGIDLenA, GBool embedded) {
163 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
166 cidToGIDLen = cidToGIDLenA;
167 cidToGID = (Gushort *)gmalloc(cidToGIDLen * sizeof(Gushort));
168 memcpy(cidToGID, cidToGIDA, cidToGIDLen * sizeof(Gushort));
169 if (!strcmp(face->driver->root.clazz->module_name, "t1cid")) {
170 mode = ftFontModeCID;
171 } else if (!strcmp(face->driver->root.clazz->module_name, "cff")) {
172 mode = ftFontModeCFFCharset;
173 } else if (embedded) {
174 mode = ftFontModeCIDToGIDMap;
176 mode = ftFontModeUnicode;
177 for (i = 0; i < face->num_charmaps; ++i) {
178 if ((face->charmaps[i]->platform_id == 3 &&
179 face->charmaps[i]->encoding_id == 1) ||
180 face->charmaps[i]->platform_id == 0) {
184 if (i == face->num_charmaps) {
187 FT_Set_Charmap(face, face->charmaps[i]);
192 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName,
202 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
205 if (!strcmp(face->driver->root.clazz->module_name, "t1cid")) {
206 mode = ftFontModeCID;
207 } else if (embedded) {
208 mode = ftFontModeCFFCharset;
210 mode = ftFontModeUnicode;
211 for (i = 0; i < face->num_charmaps; ++i) {
212 if ((face->charmaps[i]->platform_id == 3 &&
213 face->charmaps[i]->encoding_id == 1) ||
214 face->charmaps[i]->platform_id == 0) {
218 if (i == face->num_charmaps) {
221 FT_Set_Charmap(face, face->charmaps[i]);
226 FTFontFile::~FTFontFile() {
238 //------------------------------------------------------------------------
240 FTFont::FTFont(FTFontFile *fontFileA, double *m) {
241 FTFontEngine *engine;
249 fontFile = fontFileA;
250 engine = fontFile->engine;
251 face = fontFile->face;
252 if (FT_New_Size(face, &sizeObj)) {
255 face->size = sizeObj;
256 size = sqrt(m[2]*m[2] + m[3]*m[3]);
257 if (FT_Set_Pixel_Sizes(face, 0, (int)size)) {
261 div = face->bbox.xMax > 20000 ? 65536 : 1;
263 // transform the four corners of the font bounding box -- the min
264 // and max values form the bounding box of the transformed font
265 x = (int)((m[0] * face->bbox.xMin + m[2] * face->bbox.yMin) /
266 (div * face->units_per_EM));
268 y = (int)((m[1] * face->bbox.xMin + m[3] * face->bbox.yMin) /
269 (div * face->units_per_EM));
271 x = (int)((m[0] * face->bbox.xMin + m[2] * face->bbox.yMax) /
272 (div * face->units_per_EM));
275 } else if (x > xMax) {
278 y = (int)((m[1] * face->bbox.xMin + m[3] * face->bbox.yMax) /
279 (div * face->units_per_EM));
282 } else if (y > yMax) {
285 x = (int)((m[0] * face->bbox.xMax + m[2] * face->bbox.yMin) /
286 (div * face->units_per_EM));
289 } else if (x > xMax) {
292 y = (int)((m[1] * face->bbox.xMax + m[3] * face->bbox.yMin) /
293 (div * face->units_per_EM));
296 } else if (y > yMax) {
299 x = (int)((m[0] * face->bbox.xMax + m[2] * face->bbox.yMax) /
300 (div * face->units_per_EM));
303 } else if (x > xMax) {
306 y = (int)((m[1] * face->bbox.xMax + m[3] * face->bbox.yMax) /
307 (div * face->units_per_EM));
310 } else if (y > yMax) {
313 // This is a kludge: some buggy PDF generators embed fonts with
314 // zero bounding boxes.
321 yMax = (int)(1.2 * size);
323 // this should be (max - min + 1), but we add some padding to
324 // deal with rounding errors, bogus bboxes, etc.
325 glyphW = xMax - xMin + 3;
326 glyphW += glyphW >> 1;
327 glyphH = yMax - yMin + 3;
328 glyphH += glyphH >> 1;
330 glyphSize = glyphW * glyphH;
332 glyphSize = ((glyphW + 7) >> 3) * glyphH;
335 // set up the glyph pixmap cache
337 if (glyphSize <= 256) {
339 } else if (glyphSize <= 512) {
341 } else if (glyphSize <= 1024) {
346 cache = (Guchar *)gmalloc(cacheSets * cacheAssoc * glyphSize);
347 cacheTags = (FTFontCacheTag *)gmalloc(cacheSets * cacheAssoc *
348 sizeof(FTFontCacheTag));
349 for (i = 0; i < cacheSets * cacheAssoc; ++i) {
350 cacheTags[i].mru = i & (cacheAssoc - 1);
354 if (!(image = XCreateImage(engine->display, engine->visual, engine->depth,
355 ZPixmap, 0, NULL, glyphW, glyphH, 8, 0))) {
358 image->data = (char *)gmalloc(glyphH * image->bytes_per_line);
360 // compute the transform matrix
361 matrix.xx = (FT_Fixed)((m[0] / size) * 65536);
362 matrix.yx = (FT_Fixed)((m[1] / size) * 65536);
363 matrix.xy = (FT_Fixed)((m[2] / size) * 65536);
364 matrix.yy = (FT_Fixed)((m[3] / size) * 65536);
374 XDestroyImage(image);
377 GBool FTFont::drawChar(Drawable d, int w, int h, GC gc,
378 int x, int y, int r, int g, int b,
379 CharCode c, Unicode u) {
380 FTFontEngine *engine;
388 int xOffset, yOffset, x0, y0, x1, y1, gw, gh, w0, h0;
391 engine = fontFile->engine;
393 // no Unicode index for this char - don't draw anything
394 if (fontFile->mode == ftFontModeUnicode && u == 0) {
398 // generate the glyph pixmap
399 if (!(bitmap = getGlyphPixmap(c, u, &xOffset, &yOffset, &gw, &gh,
404 // compute: (x0,y0) = position in destination drawable
405 // (x1,y1) = position in glyph image
406 // (w0,h0) = size of image transfer
436 // getGlyphPixmap may have returned a larger-than-cache-entry
437 // bitmap, in which case we need to allocate a temporary XImage here
439 if (!(img = XCreateImage(engine->display, engine->visual, engine->depth,
440 ZPixmap, 0, NULL, gw, gh, 8, 0))) {
443 img->data = (char *)gmalloc(gh * img->bytes_per_line);
449 XGetSubImage(engine->display, d, x0, y0, w0, h0, (1 << engine->depth) - 1,
450 ZPixmap, img, x1, y1);
454 // compute the colors
455 xcolor.pixel = XGetPixel(img, x1 + w0/2, y1 + h0/2);
456 XQueryColor(engine->display, engine->colormap, &xcolor);
460 colors[1] = engine->findColor((r + 3*bgR) / 4,
463 colors[2] = engine->findColor((r + bgR) / 2,
466 colors[3] = engine->findColor((3*r + bgR) / 4,
469 colors[4] = engine->findColor(r, g, b);
471 // stuff the glyph pixmap into the X image
473 for (yy = 0; yy < gh; ++yy) {
474 for (xx = 0; xx < gw; ++xx) {
476 // this is a heuristic which seems to produce decent
477 // results -- the linear mapping would be:
478 // pix = (pix * 5) / 256;
479 pix = ((pix + 10) * 5) / 256;
484 XPutPixel(img, xx, yy, colors[pix]);
492 colors[1] = engine->findColor(r, g, b);
494 // stuff the glyph bitmap into the X image
496 for (yy = 0; yy < gh; ++yy) {
497 for (xx = 0; xx < gw; xx += 8) {
499 for (xx1 = xx; xx1 < xx + 8 && xx1 < gw; ++xx1) {
501 XPutPixel(img, xx1, yy, colors[1]);
511 XPutImage(engine->display, d, gc, img, x1, y1, x0, y0, w0, h0);
525 Guchar *FTFont::getGlyphPixmap(CharCode c, Unicode u,
526 int *x, int *y, int *w, int *h,
535 i = (c & (cacheSets - 1)) * cacheAssoc;
536 for (j = 0; j < cacheAssoc; ++j) {
537 if ((cacheTags[i+j].mru & 0x8000) && cacheTags[i+j].code == c) {
538 *x = cacheTags[i+j].x;
539 *y = cacheTags[i+j].y;
540 *w = cacheTags[i+j].w;
541 *h = cacheTags[i+j].h;
542 for (k = 0; k < cacheAssoc; ++k) {
544 (cacheTags[i+k].mru & 0x7fff) < (cacheTags[i+j].mru & 0x7fff)) {
545 ++cacheTags[i+k].mru;
548 cacheTags[i+j].mru = 0x8000;
549 *tempBitmap = gFalse;
550 return cache + (i+j) * glyphSize;
554 // generate the glyph pixmap or bitmap
555 fontFile->face->size = sizeObj;
556 FT_Set_Transform(fontFile->face, &matrix, NULL);
557 slot = fontFile->face->glyph;
558 idx = getGlyphIndex(c, u);
559 // if we have the FT2 bytecode interpreter, autohinting won't be used
560 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
561 if (FT_Load_Glyph(fontFile->face, idx, FT_LOAD_DEFAULT)) {
565 // FT2's autohinting doesn't always work very well (especially with
566 // font subsets), so turn it off if anti-aliasing is enabled; if
567 // anti-aliasing is disabled, this seems to be a tossup - some fonts
568 // look better with hinting, some without, so leave hinting on
569 if (FT_Load_Glyph(fontFile->face, idx,
570 fontFile->engine->aa ? FT_LOAD_NO_HINTING
571 : FT_LOAD_DEFAULT)) {
575 if (FT_Render_Glyph(slot,
576 fontFile->engine->aa ? ft_render_mode_normal :
577 ft_render_mode_mono)) {
581 // copy the glyph into the cache or a temporary bitmap
582 *x = -slot->bitmap_left;
583 *y = slot->bitmap_top;
584 *w = slot->bitmap.width;
585 *h = slot->bitmap.rows;
586 if (fontFile->engine->aa) {
589 rowSize = (*w + 7) >> 3;
591 if (*w > glyphW || *h > glyphH) {
592 // the glyph doesn't fit in the bounding box -- return a
593 // temporary, uncached bitmap (this shouldn't happen but some
594 // fonts have incorrect bboxes)
595 ret = (Guchar *)gmalloc(*h * rowSize);
598 // store glyph pixmap in cache
599 ret = NULL; // make gcc happy
600 for (j = 0; j < cacheAssoc; ++j) {
601 if ((cacheTags[i+j].mru & 0x7fff) == cacheAssoc - 1) {
602 cacheTags[i+j].mru = 0x8000;
603 cacheTags[i+j].code = c;
604 cacheTags[i+j].x = *x;
605 cacheTags[i+j].y = *y;
606 cacheTags[i+j].w = *w;
607 cacheTags[i+j].h = *h;
608 ret = cache + (i+j) * glyphSize;
610 ++cacheTags[i+j].mru;
613 *tempBitmap = gFalse;
615 for (k = 0, p = ret, q = slot->bitmap.buffer;
616 k < slot->bitmap.rows;
617 ++k, p += rowSize, q += slot->bitmap.pitch) {
618 memcpy(p, q, rowSize);
623 GBool FTFont::getCharPath(CharCode c, Unicode u, GfxState *state) {
624 static FT_Outline_Funcs outlineFuncs = {
635 fontFile->face->size = sizeObj;
636 FT_Set_Transform(fontFile->face, &matrix, NULL);
637 slot = fontFile->face->glyph;
638 idx = getGlyphIndex(c, u);
639 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
640 if (FT_Load_Glyph(fontFile->face, idx, FT_LOAD_DEFAULT)) {
644 // FT2's autohinting doesn't always work very well (especially with
645 // font subsets), so turn it off if anti-aliasing is enabled; if
646 // anti-aliasing is disabled, this seems to be a tossup - some fonts
647 // look better with hinting, some without, so leave hinting on
648 if (FT_Load_Glyph(fontFile->face, idx,
649 fontFile->engine->aa ? FT_LOAD_NO_HINTING
650 : FT_LOAD_DEFAULT)) {
654 if (FT_Get_Glyph(slot, &glyph)) {
657 FT_Outline_Decompose(&((FT_OutlineGlyph)glyph)->outline,
658 &outlineFuncs, state);
662 int FTFont::charPathMoveTo(FT_Vector *pt, void *state) {
663 ((GfxState *)state)->moveTo(pt->x / 64.0, -pt->y / 64.0);
667 int FTFont::charPathLineTo(FT_Vector *pt, void *state) {
668 ((GfxState *)state)->lineTo(pt->x / 64.0, -pt->y / 64.0);
672 int FTFont::charPathConicTo(FT_Vector *ctrl, FT_Vector *pt, void *state) {
673 double x0, y0, x1, y1, x2, y2, x3, y3, xc, yc;
675 x0 = ((GfxState *)state)->getCurX();
676 y0 = ((GfxState *)state)->getCurY();
678 yc = -ctrl->y / 64.0;
682 // A second-order Bezier curve is defined by two endpoints, p0 and
683 // p3, and one control point, pc:
685 // p(t) = (1-t)^2*p0 + t*(1-t)*pc + t^2*p3
687 // A third-order Bezier curve is defined by the same two endpoints,
688 // p0 and p3, and two control points, p1 and p2:
690 // p(t) = (1-t)^3*p0 + 3t*(1-t)^2*p1 + 3t^2*(1-t)*p2 + t^3*p3
692 // Applying some algebra, we can convert a second-order curve to a
693 // third-order curve:
695 // p1 = (1/3) * (p0 + 2pc)
696 // p2 = (1/3) * (2pc + p3)
698 x1 = (1.0 / 3.0) * (x0 + 2 * xc);
699 y1 = (1.0 / 3.0) * (y0 + 2 * yc);
700 x2 = (1.0 / 3.0) * (2 * xc + x3);
701 y2 = (1.0 / 3.0) * (2 * yc + y3);
703 ((GfxState *)state)->curveTo(x1, y1, x2, y2, x3, y3);
707 int FTFont::charPathCubicTo(FT_Vector *ctrl1, FT_Vector *ctrl2,
708 FT_Vector *pt, void *state) {
709 ((GfxState *)state)->curveTo(ctrl1->x / 64.0, -ctrl1->y / 64.0,
710 ctrl2->x / 64.0, -ctrl2->y / 64.0,
711 pt->x / 64.0, -pt->y / 64.0);
715 FT_UInt FTFont::getGlyphIndex(CharCode c, Unicode u) {
719 idx = 0; // make gcc happy
720 switch (fontFile->mode) {
721 case ftFontModeUnicode:
722 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)u);
724 case ftFontModeCharCode:
725 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)c);
727 case ftFontModeCharCodeOffset:
728 if ((idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)c)) == 0) {
729 idx = FT_Get_Char_Index(fontFile->face,
730 (FT_ULong)(c + fontFile->charMapOffset));
733 case ftFontModeCodeMap:
735 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)fontFile->codeMap[c]);
740 case ftFontModeCodeMapDirect:
742 idx = (FT_UInt)fontFile->codeMap[c];
747 case ftFontModeCIDToGIDMap:
748 if (fontFile->cidToGIDLen) {
749 if ((int)c < fontFile->cidToGIDLen) {
750 idx = (FT_UInt)fontFile->cidToGID[c];
758 case ftFontModeCFFCharset:
759 #if 1 //~ cff cid->gid map
761 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 0
762 CFF_Font *cff = (CFF_Font *)((TT_Face)fontFile->face)->extra.data;
764 CFF_Font cff = (CFF_Font)((TT_Face)fontFile->face)->extra.data;
767 for (j = 0; j < (int)cff->num_glyphs; ++j) {
768 if (cff->charset.sids[j] == c) {
783 #endif // FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)