1 //========================================================================
5 // Copyright 2001-2002 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) {
53 int unicodeCmap, macRomanCmap, msSymbolCmap;
62 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
66 if (!strcmp(face->driver->root.clazz->module_name, "type1") ||
67 !strcmp(face->driver->root.clazz->module_name, "cff")) {
69 mode = ftFontModeCodeMapDirect;
70 codeMap = (Guint *)gmalloc(256 * sizeof(Guint));
71 for (i = 0; i < 256; ++i) {
73 if ((name = fontEnc[i])) {
74 codeMap[i] = FT_Get_Name_Index(face, name);
80 // To match up with the Adobe-defined behaviour, we choose a cmap
82 // 1. If the PDF font has an encoding:
83 // 1a. If the TrueType font has a Microsoft Unicode cmap, use it,
84 // and use the Unicode indexes, not the char codes.
85 // 1b. If the TrueType font has a Macintosh Roman cmap, use it,
86 // and reverse map the char names through MacRomanEncoding to
88 // 2. If the PDF font does not have an encoding:
89 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
90 // and use char codes directly.
91 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
92 // and use (0xf000 + char code).
93 // 3. If none of these rules apply, use the first cmap and hope for
94 // the best (this shouldn't happen).
95 unicodeCmap = macRomanCmap = msSymbolCmap = 0xffff;
96 for (i = 0; i < face->num_charmaps; ++i) {
97 if (face->charmaps[i]->platform_id == 3 &&
98 face->charmaps[i]->encoding_id == 1) {
100 } else if (face->charmaps[i]->platform_id == 1 &&
101 face->charmaps[i]->encoding_id == 0) {
103 } else if (face->charmaps[i]->platform_id == 3 &&
104 face->charmaps[i]->encoding_id == 0) {
109 mode = ftFontModeCharCode;
111 if (pdfFontHasEncoding) {
112 if (unicodeCmap != 0xffff) {
114 mode = ftFontModeUnicode;
115 } else if (macRomanCmap != 0xffff) {
117 mode = ftFontModeCodeMap;
118 codeMap = (Guint *)gmalloc(256 * sizeof(Guint));
119 for (j = 0; j < 256; ++j) {
121 codeMap[j] = globalParams->getMacRomanCharCode(fontEnc[j]);
128 if (macRomanCmap != 0xffff) {
130 mode = ftFontModeCharCode;
131 } else if (msSymbolCmap != 0xffff) {
133 mode = ftFontModeCharCodeOffset;
134 charMapOffset = 0xf000;
137 if (FT_Set_Charmap(face, face->charmaps[i])) {
145 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName,
146 Gushort *cidToGIDA, int cidToGIDLenA) {
153 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
156 cidToGIDLen = cidToGIDLenA;
157 cidToGID = (Gushort *)gmalloc(cidToGIDLen * sizeof(Gushort));
158 memcpy(cidToGID, cidToGIDA, cidToGIDLen * sizeof(Gushort));
159 mode = ftFontModeCIDToGIDMap;
163 FTFontFile::FTFontFile(FTFontEngine *engineA, char *fontFileName) {
170 if (FT_New_Face(engine->lib, fontFileName, 0, &face)) {
173 if (!strcmp(face->driver->root.clazz->module_name, "t1cid")) {
174 mode = ftFontModeCID;
176 mode = ftFontModeCFFCharset;
181 FTFontFile::~FTFontFile() {
193 //------------------------------------------------------------------------
195 FTFont::FTFont(FTFontFile *fontFileA, double *m) {
196 FTFontEngine *engine;
204 fontFile = fontFileA;
205 engine = fontFile->engine;
206 face = fontFile->face;
207 if (FT_New_Size(face, &sizeObj)) {
210 face->size = sizeObj;
211 size = sqrt(m[2]*m[2] + m[3]*m[3]);
212 if (FT_Set_Pixel_Sizes(face, 0, (int)size)) {
216 div = face->bbox.xMax > 20000 ? 65536 : 1;
218 // transform the four corners of the font bounding box -- the min
219 // and max values form the bounding box of the transformed font
220 x = (int)((m[0] * face->bbox.xMin + m[2] * face->bbox.yMin) /
221 (div * face->units_per_EM));
223 y = (int)((m[1] * face->bbox.xMin + m[3] * face->bbox.yMin) /
224 (div * face->units_per_EM));
226 x = (int)((m[0] * face->bbox.xMin + m[2] * face->bbox.yMax) /
227 (div * face->units_per_EM));
230 } else if (x > xMax) {
233 y = (int)((m[1] * face->bbox.xMin + m[3] * face->bbox.yMax) /
234 (div * face->units_per_EM));
237 } else if (y > yMax) {
240 x = (int)((m[0] * face->bbox.xMax + m[2] * face->bbox.yMin) /
241 (div * face->units_per_EM));
244 } else if (x > xMax) {
247 y = (int)((m[1] * face->bbox.xMax + m[3] * face->bbox.yMin) /
248 (div * face->units_per_EM));
251 } else if (y > yMax) {
254 x = (int)((m[0] * face->bbox.xMax + m[2] * face->bbox.yMax) /
255 (div * face->units_per_EM));
258 } else if (x > xMax) {
261 y = (int)((m[1] * face->bbox.xMax + m[3] * face->bbox.yMax) /
262 (div * face->units_per_EM));
265 } else if (y > yMax) {
268 // This is a kludge: some buggy PDF generators embed fonts with
269 // zero bounding boxes.
276 yMax = (int)(1.2 * size);
278 // this should be (max - min + 1), but we add some padding to
279 // deal with rounding errors, bogus bboxes, etc.
280 glyphW = xMax - xMin + 3;
281 glyphW += glyphW >> 1;
282 glyphH = yMax - yMin + 3;
283 glyphH += glyphH >> 1;
285 glyphSize = glyphW * glyphH;
287 glyphSize = ((glyphW + 7) >> 3) * glyphH;
290 // set up the glyph pixmap cache
292 if (glyphSize <= 256) {
294 } else if (glyphSize <= 512) {
296 } else if (glyphSize <= 1024) {
301 cache = (Guchar *)gmalloc(cacheSets * cacheAssoc * glyphSize);
302 cacheTags = (FTFontCacheTag *)gmalloc(cacheSets * cacheAssoc *
303 sizeof(FTFontCacheTag));
304 for (i = 0; i < cacheSets * cacheAssoc; ++i) {
305 cacheTags[i].mru = i & (cacheAssoc - 1);
309 if (!(image = XCreateImage(engine->display, engine->visual, engine->depth,
310 ZPixmap, 0, NULL, glyphW, glyphH, 8, 0))) {
313 image->data = (char *)gmalloc(glyphH * image->bytes_per_line);
315 // compute the transform matrix
316 matrix.xx = (FT_Fixed)((m[0] / size) * 65536);
317 matrix.yx = (FT_Fixed)((m[1] / size) * 65536);
318 matrix.xy = (FT_Fixed)((m[2] / size) * 65536);
319 matrix.yy = (FT_Fixed)((m[3] / size) * 65536);
329 XDestroyImage(image);
332 GBool FTFont::drawChar(Drawable d, int w, int h, GC gc,
333 int x, int y, int r, int g, int b,
334 CharCode c, Unicode u) {
335 FTFontEngine *engine;
341 int xOffset, yOffset, x0, y0, x1, y1, gw, gh, w0, h0;
344 engine = fontFile->engine;
346 // no Unicode index for this char - don't draw anything
347 if (fontFile->mode == ftFontModeUnicode && u == 0) {
351 // generate the glyph pixmap
352 if (!(p = getGlyphPixmap(c, u, &xOffset, &yOffset, &gw, &gh))) {
356 // compute: (x0,y0) = position in destination drawable
357 // (x1,y1) = position in glyph image
358 // (w0,h0) = size of image transfer
389 XGetSubImage(engine->display, d, x0, y0, w0, h0, (1 << engine->depth) - 1,
390 ZPixmap, image, x1, y1);
394 // compute the colors
395 xcolor.pixel = XGetPixel(image, x1 + w0/2, y1 + h0/2);
396 XQueryColor(engine->display, engine->colormap, &xcolor);
400 colors[1] = engine->findColor((r + 3*bgR) / 4,
403 colors[2] = engine->findColor((r + bgR) / 2,
406 colors[3] = engine->findColor((3*r + bgR) / 4,
409 colors[4] = engine->findColor(r, g, b);
411 // stuff the glyph pixmap into the X image
412 for (yy = 0; yy < gh; ++yy) {
413 for (xx = 0; xx < gw; ++xx) {
415 // this is a heuristic which seems to produce decent
416 // results -- the linear mapping would be:
417 // pix = (pix * 5) / 256;
418 pix = ((pix + 10) * 5) / 256;
423 XPutPixel(image, xx, yy, colors[pix]);
431 colors[1] = engine->findColor(r, g, b);
433 // stuff the glyph bitmap into the X image
434 for (yy = 0; yy < gh; ++yy) {
435 for (xx = 0; xx < gw; xx += 8) {
437 for (xx1 = xx; xx1 < xx + 8 && xx1 < gw; ++xx1) {
439 XPutPixel(image, xx1, yy, colors[1]);
449 XPutImage(engine->display, d, gc, image, x1, y1, x0, y0, w0, h0);
454 Guchar *FTFont::getGlyphPixmap(CharCode c, Unicode u,
455 int *x, int *y, int *w, int *h) {
463 i = (c & (cacheSets - 1)) * cacheAssoc;
464 for (j = 0; j < cacheAssoc; ++j) {
465 if ((cacheTags[i+j].mru & 0x8000) && cacheTags[i+j].code == c) {
466 *x = cacheTags[i+j].x;
467 *y = cacheTags[i+j].y;
468 *w = cacheTags[i+j].w;
469 *h = cacheTags[i+j].h;
470 for (k = 0; k < cacheAssoc; ++k) {
472 (cacheTags[i+k].mru & 0x7fff) < (cacheTags[i+j].mru & 0x7fff)) {
473 ++cacheTags[i+k].mru;
476 cacheTags[i+j].mru = 0x8000;
477 return cache + (i+j) * glyphSize;
481 // generate the glyph pixmap or bitmap
482 fontFile->face->size = sizeObj;
483 FT_Set_Transform(fontFile->face, &matrix, NULL);
484 slot = fontFile->face->glyph;
485 idx = getGlyphIndex(c, u);
486 // if we have the FT2 bytecode interpreter, autohinting won't be used
487 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
488 if (FT_Load_Glyph(fontFile->face, idx, FT_LOAD_DEFAULT)) {
492 // FT2's autohinting doesn't always work very well (especially with
493 // font subsets), so turn it off if anti-aliasing is enabled; if
494 // anti-aliasing is disabled, this seems to be a tossup - some fonts
495 // look better with hinting, some without, so leave hinting on
496 if (FT_Load_Glyph(fontFile->face, idx,
497 fontFile->engine->aa ? FT_LOAD_NO_HINTING
498 : FT_LOAD_DEFAULT)) {
502 if (FT_Render_Glyph(slot,
503 fontFile->engine->aa ? ft_render_mode_normal :
504 ft_render_mode_mono)) {
507 *x = -slot->bitmap_left;
508 *y = slot->bitmap_top;
509 *w = slot->bitmap.width;
510 *h = slot->bitmap.rows;
511 if (*w > glyphW || *h > glyphH) {
513 fprintf(stderr, "Weird FreeType glyph size: %d > %d or %d > %d\n",
514 *w, glyphW, *h, glyphH);
519 // store glyph pixmap in cache
521 for (j = 0; j < cacheAssoc; ++j) {
522 if ((cacheTags[i+j].mru & 0x7fff) == cacheAssoc - 1) {
523 cacheTags[i+j].mru = 0x8000;
524 cacheTags[i+j].code = c;
525 cacheTags[i+j].x = *x;
526 cacheTags[i+j].y = *y;
527 cacheTags[i+j].w = *w;
528 cacheTags[i+j].h = *h;
529 if (fontFile->engine->aa) {
532 rowSize = (*w + 7) >> 3;
534 ret = cache + (i+j) * glyphSize;
535 for (k = 0, p = ret, q = slot->bitmap.buffer;
536 k < slot->bitmap.rows;
537 ++k, p += rowSize, q += slot->bitmap.pitch) {
538 memcpy(p, q, rowSize);
541 ++cacheTags[i+j].mru;
547 GBool FTFont::getCharPath(CharCode c, Unicode u, GfxState *state) {
548 static FT_Outline_Funcs outlineFuncs = {
559 fontFile->face->size = sizeObj;
560 FT_Set_Transform(fontFile->face, &matrix, NULL);
561 slot = fontFile->face->glyph;
562 idx = getGlyphIndex(c, u);
563 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
564 if (FT_Load_Glyph(fontFile->face, idx, FT_LOAD_DEFAULT)) {
568 // FT2's autohinting doesn't always work very well (especially with
569 // font subsets), so turn it off if anti-aliasing is enabled; if
570 // anti-aliasing is disabled, this seems to be a tossup - some fonts
571 // look better with hinting, some without, so leave hinting on
572 if (FT_Load_Glyph(fontFile->face, idx,
573 fontFile->engine->aa ? FT_LOAD_NO_HINTING
574 : FT_LOAD_DEFAULT)) {
578 if (FT_Get_Glyph(slot, &glyph)) {
581 FT_Outline_Decompose(&((FT_OutlineGlyph)glyph)->outline,
582 &outlineFuncs, state);
586 int FTFont::charPathMoveTo(FT_Vector *pt, void *state) {
587 ((GfxState *)state)->moveTo(pt->x / 64.0, -pt->y / 64.0);
591 int FTFont::charPathLineTo(FT_Vector *pt, void *state) {
592 ((GfxState *)state)->lineTo(pt->x / 64.0, -pt->y / 64.0);
596 int FTFont::charPathConicTo(FT_Vector *ctrl, FT_Vector *pt, void *state) {
597 double x0, y0, x1, y1, x2, y2, x3, y3, xc, yc;
599 x0 = ((GfxState *)state)->getCurX();
600 y0 = ((GfxState *)state)->getCurY();
602 yc = -ctrl->y / 64.0;
606 // A second-order Bezier curve is defined by two endpoints, p0 and
607 // p3, and one control point, pc:
609 // p(t) = (1-t)^2*p0 + t*(1-t)*pc + t^2*p3
611 // A third-order Bezier curve is defined by the same two endpoints,
612 // p0 and p3, and two control points, p1 and p2:
614 // p(t) = (1-t)^3*p0 + 3t*(1-t)^2*p1 + 3t^2*(1-t)*p2 + t^3*p3
616 // Applying some algebra, we can convert a second-order curve to a
617 // third-order curve:
619 // p1 = (1/3) * (p0 + 2pc)
620 // p2 = (1/3) * (2pc + p3)
622 x1 = (1.0 / 3.0) * (x0 + 2 * xc);
623 y1 = (1.0 / 3.0) * (y0 + 2 * yc);
624 x2 = (1.0 / 3.0) * (2 * xc + x3);
625 y2 = (1.0 / 3.0) * (2 * yc + y3);
627 ((GfxState *)state)->curveTo(x1, y1, x2, y2, x3, y3);
631 int FTFont::charPathCubicTo(FT_Vector *ctrl1, FT_Vector *ctrl2,
632 FT_Vector *pt, void *state) {
633 ((GfxState *)state)->curveTo(ctrl1->x / 64.0, -ctrl1->y / 64.0,
634 ctrl2->x / 64.0, -ctrl2->y / 64.0,
635 pt->x / 64.0, -pt->y / 64.0);
639 FT_UInt FTFont::getGlyphIndex(CharCode c, Unicode u) {
643 idx = 0; // make gcc happy
644 switch (fontFile->mode) {
645 case ftFontModeUnicode:
646 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)u);
648 case ftFontModeCharCode:
649 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)c);
651 case ftFontModeCharCodeOffset:
652 idx = FT_Get_Char_Index(fontFile->face,
653 (FT_ULong)(c + fontFile->charMapOffset));
655 case ftFontModeCodeMap:
657 idx = FT_Get_Char_Index(fontFile->face, (FT_ULong)fontFile->codeMap[c]);
662 case ftFontModeCodeMapDirect:
664 idx = (FT_UInt)fontFile->codeMap[c];
669 case ftFontModeCIDToGIDMap:
670 if (fontFile->cidToGIDLen) {
671 if ((int)c < fontFile->cidToGIDLen) {
672 idx = (FT_UInt)fontFile->cidToGID[c];
680 case ftFontModeCFFCharset:
681 #if 1 //~ cff cid->gid map
683 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 0
684 CFF_Font *cff = (CFF_Font *)((TT_Face)fontFile->face)->extra.data;
686 CFF_Font cff = (CFF_Font)((TT_Face)fontFile->face)->extra.data;
689 for (j = 0; j < (int)cff->num_glyphs; ++j) {
690 if (cff->charset.sids[j] == c) {
705 #endif // FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)