11#if !FREETYPE2 && (HAVE_FREETYPE_FREETYPE_H || HAVE_FREETYPE_H)
19#include "GlobalParams.h"
24TTFontEngine::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)) {
42TTFontEngine::~TTFontEngine() {
43 TT_Done_FreeType(engine);
48TTFontFile::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)) {
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);
126TTFontFile::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;
140TTFontFile::~TTFontFile() {
149TTFont::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)) {
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;
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);
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);
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);
281GBool 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;
299 y0 = y - (ras.rows - yOffset);
328 XGetSubImage(engine->display, d, x0, y0, w0, h0, (1 << engine->depth) - 1,
329 ZPixmap, image, x1, y1);
332 if (!getGlyphPixmap(c, u)) {
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);
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);
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);
396GBool 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;
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)) {
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;