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
19 #include "GlobalParams.h"
22 //------------------------------------------------------------------------
24 TTFontEngine::TTFontEngine(Display *displayA, Visual *visualA, int depthA,
25 Colormap colormapA, GBool aaA):
26 SFontEngine(displayA, visualA, depthA, colormapA) {
27 static TT_Byte ttPalette[5] = {0, 1, 2, 3, 4};
30 if (TT_Init_FreeType(&engine)) {
35 if (TT_Set_Raster_Gray_Palette(engine, ttPalette)) {
42 TTFontEngine::~TTFontEngine() {
43 TT_Done_FreeType(engine);
46 //------------------------------------------------------------------------
48 TTFontFile::TTFontFile(TTFontEngine *engineA, char *fontFileName,
49 char **fontEnc, GBool pdfFontHasEncoding) {
50 TT_Face_Properties props;
51 TT_UShort unicodeCmap, macRomanCmap, msSymbolCmap;
52 TT_UShort platform, encoding, i;
58 if (TT_Open_Face(engine->engine, fontFileName, &face)) {
61 if (TT_Get_Face_Properties(face, &props)) {
65 // To match up with the Adobe-defined behaviour, we choose a cmap
67 // 1. If the PDF font has an encoding:
68 // 1a. If the TrueType font has a Microsoft Unicode cmap, use it,
69 // and use the Unicode indexes, not the char codes.
70 // 1b. If the TrueType font has a Macintosh Roman cmap, use it,
71 // and reverse map the char names through MacRomanEncoding to
73 // 2. If the PDF font does not have an encoding:
74 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
75 // and use char codes directly.
76 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
77 // and use (0xf000 + char code).
78 // 3. If none of these rules apply, use the first cmap and hope for
79 // the best (this shouldn't happen).
80 unicodeCmap = macRomanCmap = msSymbolCmap = 0xffff;
81 for (i = 0; i < props.num_CharMaps; ++i) {
82 if (!TT_Get_CharMap_ID(face, i, &platform, &encoding)) {
83 if (platform == 3 && encoding == 1) {
85 } else if (platform == 1 && encoding == 0) {
87 } else if (platform == 3 && encoding == 0) {
93 mode = ttFontModeCharCode;
95 if (pdfFontHasEncoding) {
96 if (unicodeCmap != 0xffff) {
98 mode = ttFontModeUnicode;
99 } else if (macRomanCmap != 0xffff) {
101 mode = ttFontModeCodeMap;
102 codeMap = (Guchar *)gmalloc(256 * sizeof(Guchar));
103 for (j = 0; j < 256; ++j) {
105 codeMap[j] = (Guchar)globalParams->getMacRomanCharCode(fontEnc[j]);
112 if (macRomanCmap != 0xffff) {
114 mode = ttFontModeCharCode;
115 } else if (msSymbolCmap != 0xffff) {
117 mode = ttFontModeCharCodeOffset;
118 charMapOffset = 0xf000;
121 TT_Get_CharMap(face, i, &charMap);
126 TTFontFile::TTFontFile(TTFontEngine *engineA, char *fontFileName,
127 Gushort *cidToGIDA, int cidToGIDLenA) {
131 cidToGID = cidToGIDA;
132 cidToGIDLen = cidToGIDLenA;
133 if (TT_Open_Face(engine->engine, fontFileName, &face)) {
136 mode = ttFontModeCIDToGIDMap;
140 TTFontFile::~TTFontFile() {
147 //------------------------------------------------------------------------
149 TTFont::TTFont(TTFontFile *fontFileA, double *m) {
150 TTFontEngine *engine;
151 TT_Face_Properties props;
152 TT_Instance_Metrics metrics;
158 fontFile = fontFileA;
159 engine = fontFile->engine;
160 if (TT_New_Instance(fontFile->face, &instance) ||
161 TT_Set_Instance_Resolutions(instance, 72, 72) ||
162 TT_Set_Instance_CharSize(instance, 1000 * 64) ||
163 TT_New_Glyph(fontFile->face, &glyph) ||
164 TT_Get_Face_Properties(fontFile->face, &props) ||
165 TT_Get_Instance_Metrics(instance, &metrics)) {
169 // transform the four corners of the font bounding box -- the min
170 // and max values form the bounding box of the transformed font
171 x = (int)((m[0] * props.header->xMin + m[2] * props.header->yMin) *
172 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
174 y = (int)((m[1] * props.header->xMin + m[3] * props.header->yMin) *
175 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
177 x = (int)((m[0] * props.header->xMin + m[2] * props.header->yMax) *
178 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
181 } else if (x > xMax) {
184 y = (int)((m[1] * props.header->xMin + m[3] * props.header->yMax) *
185 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
188 } else if (y > yMax) {
191 x = (int)((m[0] * props.header->xMax + m[2] * props.header->yMin) *
192 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
195 } else if (x > xMax) {
198 y = (int)((m[1] * props.header->xMax + m[3] * props.header->yMin) *
199 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
202 } else if (y > yMax) {
205 x = (int)((m[0] * props.header->xMax + m[2] * props.header->yMax) *
206 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
209 } else if (x > xMax) {
212 y = (int)((m[1] * props.header->xMax + m[3] * props.header->yMax) *
213 0.001 * metrics.x_ppem / props.header->Units_Per_EM);
216 } else if (y > yMax) {
221 ras.width = xMax - xMin + 1;
222 ras.rows = yMax - yMin + 1;
224 // set up the Raster_Map structure
226 ras.width = (ras.width + 3) & ~3;
227 ras.cols = ras.width;
229 ras.width = (ras.width + 7) & ~7;
230 ras.cols = ras.width >> 3;
232 ras.flow = TT_Flow_Down;
233 ras.size = ras.rows * ras.cols;
234 ras.bitmap = gmalloc(ras.size);
236 // set up the glyph pixmap cache
238 if (ras.size <= 256) {
240 } else if (ras.size <= 512) {
242 } else if (ras.size <= 1024) {
247 cache = (Guchar *)gmalloc(cacheSets * cacheAssoc * ras.size);
248 cacheTags = (TTFontCacheTag *)gmalloc(cacheSets * cacheAssoc *
249 sizeof(TTFontCacheTag));
250 for (i = 0; i < cacheSets * cacheAssoc; ++i) {
251 cacheTags[i].mru = i & (cacheAssoc - 1);
255 if (!(image = XCreateImage(engine->display, engine->visual, engine->depth,
256 ZPixmap, 0, NULL, ras.width, ras.rows, 8, 0))) {
259 image->data = (char *)gmalloc(ras.rows * image->bytes_per_line);
261 // compute the transform matrix
262 matrix.xx = (TT_Fixed)(m[0] * 65.536);
263 matrix.yx = (TT_Fixed)(m[1] * 65.536);
264 matrix.xy = (TT_Fixed)(m[2] * 65.536);
265 matrix.yy = (TT_Fixed)(m[3] * 65.536);
275 XDestroyImage(image);
277 TT_Done_Glyph(glyph);
278 TT_Done_Instance(instance);
281 GBool TTFont::drawChar(Drawable d, int w, int h, GC gc,
282 int x, int y, int r, int g, int b,
283 CharCode c, Unicode u) {
284 TTFontEngine *engine;
291 int x0, y0, x1, y1, w0, h0;
293 engine = fontFile->engine;
295 // compute: (x0,y0) = position in destination drawable
296 // (x1,y1) = position in glyph image
297 // (w0,h0) = size of image transfer
299 y0 = y - (ras.rows - yOffset);
328 XGetSubImage(engine->display, d, x0, y0, w0, h0, (1 << engine->depth) - 1,
329 ZPixmap, image, x1, y1);
331 // generate the glyph pixmap
332 if (!getGlyphPixmap(c, u)) {
338 // compute the colors
339 xcolor.pixel = XGetPixel(image, x1 + w0/2, y1 + h0/2);
340 XQueryColor(engine->display, engine->colormap, &xcolor);
344 colors[1] = engine->findColor((r + 3*bgR) / 4,
347 colors[2] = engine->findColor((r + bgR) / 2,
350 colors[3] = engine->findColor((3*r + bgR) / 4,
353 colors[4] = engine->findColor(r, g, b);
355 // stuff the glyph pixmap into the X image
356 p = (TT_Byte *)ras.bitmap;
357 for (yy = 0; yy < ras.rows; ++yy) {
358 for (xx = 0; xx < ras.width; ++xx) {
364 XPutPixel(image, xx, yy, colors[pix]);
372 colors[1] = engine->findColor(r, g, b);
374 // stuff the glyph bitmap into the X image
375 p = (TT_Byte *)ras.bitmap;
376 for (yy = 0; yy < ras.rows; ++yy) {
377 for (xx = 0; xx < ras.width; xx += 8) {
379 for (xx1 = xx; xx1 < xx + 8 && xx1 < ras.width; ++xx1) {
381 XPutPixel(image, xx1, yy, colors[1]);
391 XPutImage(engine->display, d, gc, image, x1, y1, x0, y0, w0, h0);
396 GBool TTFont::getGlyphPixmap(CharCode c, Unicode u) {
402 i = (c & (cacheSets - 1)) * cacheAssoc;
403 for (j = 0; j < cacheAssoc; ++j) {
404 if ((cacheTags[i+j].mru & 0x8000) && cacheTags[i+j].code == c) {
405 memcpy(ras.bitmap, cache + (i+j) * ras.size, ras.size);
406 for (k = 0; k < cacheAssoc; ++k) {
408 (cacheTags[i+k].mru & 0x7fff) < (cacheTags[i+j].mru & 0x7fff)) {
409 ++cacheTags[i+k].mru;
412 cacheTags[i+j].mru = 0x8000;
417 // generate the glyph pixmap or bitmap
418 idx = 0; // make gcc happy
419 switch (fontFile->mode) {
420 case ttFontModeUnicode:
421 idx = TT_Char_Index(fontFile->charMap, (TT_UShort)u);
423 case ttFontModeCharCode:
424 idx = TT_Char_Index(fontFile->charMap, (TT_UShort)c);
426 case ttFontModeCharCodeOffset:
427 idx = TT_Char_Index(fontFile->charMap,
428 (TT_UShort)(c + fontFile->charMapOffset));
430 case ttFontModeCodeMap:
432 idx = TT_Char_Index(fontFile->charMap,
433 (TT_UShort)(fontFile->codeMap[c] & 0xff));
438 case ttFontModeCIDToGIDMap:
439 if (fontFile->cidToGIDLen) {
440 if ((int)c < fontFile->cidToGIDLen) {
441 idx = (TT_UShort)fontFile->cidToGID[c];
450 if (TT_Load_Glyph(instance, glyph, idx, TTLOAD_DEFAULT) ||
451 TT_Get_Glyph_Outline(glyph, &outline)) {
454 TT_Transform_Outline(&outline, &matrix);
455 memset(ras.bitmap, 0, ras.size);
456 if (fontFile->engine->aa) {
457 if (TT_Get_Glyph_Pixmap(glyph, &ras, xOffset * 64, yOffset * 64)) {
461 if (TT_Get_Glyph_Bitmap(glyph, &ras, xOffset * 64, yOffset * 64)) {
466 // store glyph pixmap in cache
467 for (j = 0; j < cacheAssoc; ++j) {
468 if ((cacheTags[i+j].mru & 0x7fff) == cacheAssoc - 1) {
469 cacheTags[i+j].mru = 0x8000;
470 cacheTags[i+j].code = c;
471 memcpy(cache + (i+j) * ras.size, ras.bitmap, ras.size);
473 ++cacheTags[i+j].mru;
480 #endif // !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)