gdft.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /********************************************/
  2. /* gd interface to freetype library */
  3. /* */
  4. /* John Ellson ellson@graphviz.org */
  5. /********************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #include "gd.h"
  11. #include "gdhelpers.h"
  12. #ifndef MSWIN32
  13. #include <unistd.h>
  14. #else
  15. #include <io.h>
  16. #ifndef R_OK
  17. # define R_OK 04 /* Needed in Windows */
  18. #endif
  19. #endif
  20. #ifdef WIN32
  21. #define access _access
  22. #ifndef R_OK
  23. #define R_OK 2
  24. #endif
  25. #endif
  26. /* number of antialised colors for indexed bitmaps */
  27. /* overwrite Windows GDI define in case of windows build */
  28. #ifdef NUMCOLORS
  29. #undef NUMCOLORS
  30. #endif
  31. #define NUMCOLORS 8
  32. char *
  33. gdImageStringTTF (gdImage * im, int *brect, int fg, char *fontlist,
  34. double ptsize, double angle, int x, int y, char *string)
  35. {
  36. /* 2.0.6: valid return */
  37. return gdImageStringFT (im, brect, fg, fontlist, ptsize, angle, x, y, string);
  38. }
  39. #ifndef HAVE_LIBFREETYPE
  40. char *
  41. gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist,
  42. double ptsize, double angle, int x, int y, char *string,
  43. gdFTStringExtraPtr strex)
  44. {
  45. return "libgd was not built with FreeType font support\n";
  46. }
  47. char *
  48. gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
  49. double ptsize, double angle, int x, int y, char *string)
  50. {
  51. return "libgd was not built with FreeType font support\n";
  52. }
  53. #else
  54. #include "gdcache.h"
  55. #include <ft2build.h>
  56. #include FT_FREETYPE_H
  57. #include FT_GLYPH_H
  58. /* number of fonts cached before least recently used is replaced */
  59. #define FONTCACHESIZE 6
  60. /* number of antialias color lookups cached */
  61. #define TWEENCOLORCACHESIZE 32
  62. /*
  63. * Line separation as a factor of font height.
  64. * No space between if LINESPACE = 1.00
  65. * Line separation will be rounded up to next pixel row.
  66. */
  67. #define LINESPACE 1.05
  68. /*
  69. * The character (space) used to separate alternate fonts in the
  70. * fontlist parameter to gdImageStringFT. 2.0.18: space was a oor choice for this.
  71. */
  72. #define LISTSEPARATOR ";"
  73. /*
  74. * DEFAULT_FONTPATH and PATHSEPARATOR are host type dependent and
  75. * are normally set by configure in config.h. These are just
  76. * some last resort values that might match some Un*x system
  77. * if building this version of gd separate from graphviz.
  78. */
  79. #ifndef DEFAULT_FONTPATH
  80. #if defined(__APPLE__) || (defined(__MWERKS__) && defined(macintosh))
  81. #define DEFAULT_FONTPATH "/usr/share/fonts/truetype:/System/Library/Fonts:/Library/Fonts"
  82. #else
  83. #define DEFAULT_FONTPATH "/usr/share/fonts/truetype"
  84. #endif
  85. #endif
  86. #ifndef PATHSEPARATOR
  87. #define PATHSEPARATOR ":"
  88. #endif
  89. #ifndef TRUE
  90. #define FALSE 0
  91. #define TRUE !FALSE
  92. #endif
  93. #ifndef MAX
  94. #define MAX(a,b) ((a)>(b)?(a):(b))
  95. #endif
  96. #ifndef MIN
  97. #define MIN(a,b) ((a)<(b)?(a):(b))
  98. #endif
  99. typedef struct
  100. {
  101. char *fontlist; /* key */
  102. FT_Library *library;
  103. FT_Face face;
  104. FT_Bool have_char_map_unicode, have_char_map_big5, have_char_map_sjis, have_char_map_apple_roman;
  105. gdCache_head_t *glyphCache;
  106. } font_t;
  107. typedef struct
  108. {
  109. char *fontlist; /* key */
  110. int preferred_map;
  111. FT_Library *library;
  112. } fontkey_t;
  113. typedef struct
  114. {
  115. int pixel; /* key */
  116. int bgcolor; /* key */
  117. int fgcolor; /* key *//* -ve means no antialias */
  118. gdImagePtr im; /* key */
  119. int tweencolor;
  120. } tweencolor_t;
  121. typedef struct
  122. {
  123. int pixel; /* key */
  124. int bgcolor; /* key */
  125. int fgcolor; /* key *//* -ve means no antialias */
  126. gdImagePtr im; /* key */
  127. } tweencolorkey_t;
  128. /********************************************************************
  129. * gdTcl_UtfToUniChar is borrowed from Tcl ...
  130. */
  131. /*
  132. * tclUtf.c --
  133. *
  134. * Routines for manipulating UTF-8 strings.
  135. *
  136. * Copyright (c) 1997-1998 Sun Microsystems, Inc.
  137. *
  138. * See the file "license.terms" for information on usage and redistribution
  139. * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  140. *
  141. * SCCS: @(#) tclUtf.c 1.25 98/01/28 18:02:43
  142. */
  143. /*
  144. *---------------------------------------------------------------------------
  145. *
  146. * gdTcl_UtfToUniChar --
  147. *
  148. * Extract the Tcl_UniChar represented by the UTF-8 string. Bad
  149. * UTF-8 sequences are converted to valid Tcl_UniChars and processing
  150. * continues. Equivalent to Plan 9 chartorune().
  151. *
  152. * The caller must ensure that the source buffer is long enough that
  153. * this routine does not run off the end and dereference non-existent
  154. * memory looking for trail bytes. If the source buffer is known to
  155. * be '\0' terminated, this cannot happen. Otherwise, the caller
  156. * should call Tcl_UtfCharComplete() before calling this routine to
  157. * ensure that enough bytes remain in the string.
  158. *
  159. * Results:
  160. * *chPtr is filled with the Tcl_UniChar, and the return value is the
  161. * number of bytes from the UTF-8 string that were consumed.
  162. *
  163. * Side effects:
  164. * None.
  165. *
  166. *---------------------------------------------------------------------------
  167. */
  168. #ifdef JISX0208
  169. #include "jisx0208.h"
  170. #endif
  171. extern int any2eucjp (char *, char *, unsigned int);
  172. /* Persistent font cache until explicitly cleared */
  173. /* Fonts can be used across multiple images */
  174. /* 2.0.16: thread safety (the font cache is shared) */
  175. gdMutexDeclare(gdFontCacheMutex);
  176. static gdCache_head_t *fontCache = NULL;
  177. static FT_Library library;
  178. #define Tcl_UniChar int
  179. #define TCL_UTF_MAX 3
  180. static int gdTcl_UtfToUniChar (char *str, Tcl_UniChar * chPtr)
  181. /* str is the UTF8 next character pointer */
  182. /* chPtr is the int for the result */
  183. {
  184. int byte;
  185. /* HTML4.0 entities in decimal form, e.g. &#197; */
  186. byte = *((unsigned char *) str);
  187. if (byte == '&') {
  188. int i, n = 0;
  189. byte = *((unsigned char *) (str + 1));
  190. if (byte == '#') {
  191. byte = *((unsigned char *) (str + 2));
  192. if (byte == 'x' || byte == 'X') {
  193. for (i = 3; i < 8; i++) {
  194. byte = *((unsigned char *) (str + i));
  195. if (byte >= 'A' && byte <= 'F')
  196. byte = byte - 'A' + 10;
  197. else if (byte >= 'a' && byte <= 'f')
  198. byte = byte - 'a' + 10;
  199. else if (byte >= '0' && byte <= '9')
  200. byte = byte - '0';
  201. else
  202. break;
  203. n = (n * 16) + byte;
  204. }
  205. } else {
  206. for (i = 2; i < 8; i++) {
  207. byte = *((unsigned char *) (str + i));
  208. if (byte >= '0' && byte <= '9') {
  209. n = (n * 10) + (byte - '0');
  210. } else {
  211. break;
  212. }
  213. }
  214. }
  215. if (byte == ';') {
  216. *chPtr = (Tcl_UniChar) n;
  217. return ++i;
  218. }
  219. }
  220. }
  221. /* Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones. */
  222. byte = *((unsigned char *) str);
  223. #ifdef JISX0208
  224. if (0xA1 <= byte && byte <= 0xFE) {
  225. int ku, ten;
  226. ku = (byte & 0x7F) - 0x20;
  227. ten = (str[1] & 0x7F) - 0x20;
  228. if ((ku < 1 || ku > 92) || (ten < 1 || ten > 94)) {
  229. *chPtr = (Tcl_UniChar) byte;
  230. return 1;
  231. }
  232. *chPtr = (Tcl_UniChar) UnicodeTbl[ku - 1][ten - 1];
  233. return 2;
  234. } else
  235. #endif /* JISX0208 */
  236. if (byte < 0xC0) {
  237. /* Handles properly formed UTF-8 characters between
  238. * 0x01 and 0x7F. Also treats \0 and naked trail
  239. * bytes 0x80 to 0xBF as valid characters representing
  240. * themselves.
  241. */
  242. *chPtr = (Tcl_UniChar) byte;
  243. return 1;
  244. } else if (byte < 0xE0) {
  245. if ((str[1] & 0xC0) == 0x80) {
  246. /* Two-byte-character lead-byte followed by a trail-byte. */
  247. *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (str[1] & 0x3F));
  248. return 2;
  249. }
  250. /*
  251. * A two-byte-character lead-byte not followed by trail-byte
  252. * represents itself.
  253. */
  254. *chPtr = (Tcl_UniChar) byte;
  255. return 1;
  256. } else if (byte < 0xF0) {
  257. if (((str[1] & 0xC0) == 0x80) && ((str[2] & 0xC0) == 0x80)) {
  258. /* Three-byte-character lead byte followed by two trail bytes. */
  259. *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12) | ((str[1] & 0x3F) << 6) | (str[2] & 0x3F));
  260. return 3;
  261. }
  262. /* A three-byte-character lead-byte not followed by two trail-bytes represents itself. */
  263. *chPtr = (Tcl_UniChar) byte;
  264. return 1;
  265. }
  266. #if TCL_UTF_MAX > 3
  267. else {
  268. int ch, total, trail;
  269. total = totalBytes[byte];
  270. trail = total - 1;
  271. if (trail > 0) {
  272. ch = byte & (0x3F >> trail);
  273. do {
  274. str++;
  275. if ((*str & 0xC0) != 0x80) {
  276. *chPtr = byte;
  277. return 1;
  278. }
  279. ch <<= 6;
  280. ch |= (*str & 0x3F);
  281. trail--;
  282. } while (trail > 0);
  283. *chPtr = ch;
  284. return total;
  285. }
  286. }
  287. #endif
  288. *chPtr = (Tcl_UniChar) byte;
  289. return 1;
  290. }
  291. /********************************************************************/
  292. /* font cache functions */
  293. static int fontTest (void *element, void *key)
  294. {
  295. font_t *a = (font_t *) element;
  296. fontkey_t *b = (fontkey_t *) key;
  297. if (strcmp (a->fontlist, b->fontlist) == 0) {
  298. switch (b->preferred_map) {
  299. case gdFTEX_Unicode:
  300. if (a->have_char_map_unicode) {
  301. return 1;
  302. }
  303. break;
  304. case gdFTEX_Shift_JIS:
  305. if (a->have_char_map_sjis) {
  306. return 1;
  307. }
  308. break;
  309. case gdFTEX_Big5:
  310. if (a->have_char_map_sjis) {
  311. return 1;
  312. }
  313. break;
  314. }
  315. }
  316. return 0;
  317. }
  318. static void *fontFetch (char **error, void *key)
  319. {
  320. font_t *a;
  321. fontkey_t *b = (fontkey_t *) key;
  322. int n;
  323. int font_found = 0;
  324. unsigned short platform, encoding;
  325. char *fontsearchpath, *fontlist;
  326. char fullname[MAXPATHLEN], cur_dir[MAXPATHLEN];
  327. char *name, *path=NULL, *dir;
  328. char *strtok_ptr;
  329. FT_Error err;
  330. FT_CharMap found = 0;
  331. FT_CharMap charmap;
  332. a = (font_t *) gdPMalloc(sizeof(font_t));
  333. a->fontlist = gdPEstrdup(b->fontlist);
  334. a->library = b->library;
  335. /*
  336. * Search the pathlist for any of a list of font names.
  337. */
  338. fontsearchpath = getenv ("GDFONTPATH");
  339. if (!fontsearchpath) {
  340. fontsearchpath = DEFAULT_FONTPATH;
  341. }
  342. fontlist = gdEstrdup(a->fontlist);
  343. /*
  344. * Must use gd_strtok_r becasuse strtok() isn't thread safe
  345. */
  346. for (name = gd_strtok_r (fontlist, LISTSEPARATOR, &strtok_ptr); name; name = gd_strtok_r (0, LISTSEPARATOR, &strtok_ptr)) {
  347. char *strtok_ptr_path;
  348. /* make a fresh copy each time - strtok corrupts it. */
  349. path = gdEstrdup (fontsearchpath);
  350. /* if name is an absolute filename then test directly */
  351. #ifdef NETWARE
  352. if (*name == '/' || (name[0] != 0 && strstr(name, ":/"))) {
  353. #else
  354. if (*name == '/' || (name[0] != 0 && name[1] == ':' && (name[2] == '/' || name[2] == '\\'))) {
  355. #endif
  356. snprintf(fullname, sizeof(fullname) - 1, "%s", name);
  357. if (access(fullname, R_OK) == 0) {
  358. font_found++;
  359. break;
  360. }
  361. }
  362. for (dir = gd_strtok_r (path, PATHSEPARATOR, &strtok_ptr_path); dir;
  363. dir = gd_strtok_r (0, PATHSEPARATOR, &strtok_ptr_path)) {
  364. if (!strcmp(dir, ".")) {
  365. #ifdef HAVE_GETCWD
  366. dir = VCWD_GETCWD(cur_dir, MAXPATHLEN);
  367. #elif defined(HAVE_GETWD)
  368. dir = VCWD_GETWD(cur_dir);
  369. #endif
  370. if (!dir) {
  371. continue;
  372. }
  373. }
  374. #define GD_CHECK_FONT_PATH(ext) \
  375. snprintf(fullname, sizeof(fullname) - 1, "%s/%s%s", dir, name, ext); \
  376. if (access(fullname, R_OK) == 0) { \
  377. font_found++; \
  378. break; \
  379. } \
  380. GD_CHECK_FONT_PATH("");
  381. GD_CHECK_FONT_PATH(".ttf");
  382. GD_CHECK_FONT_PATH(".pfa");
  383. GD_CHECK_FONT_PATH(".pfb");
  384. GD_CHECK_FONT_PATH(".dfont");
  385. }
  386. gdFree(path);
  387. path = NULL;
  388. if (font_found) {
  389. break;
  390. }
  391. }
  392. if (path) {
  393. gdFree(path);
  394. }
  395. gdFree(fontlist);
  396. if (!font_found) {
  397. gdPFree(a->fontlist);
  398. gdPFree(a);
  399. *error = "Could not find/open font";
  400. return NULL;
  401. }
  402. err = FT_New_Face (*b->library, fullname, 0, &a->face);
  403. if (err) {
  404. gdPFree(a->fontlist);
  405. gdPFree(a);
  406. *error = "Could not read font";
  407. return NULL;
  408. }
  409. /* FIXME - This mapping stuff is incomplete - where is the spec? */
  410. /* EAM - It's worse than that. It's pointless to match character encodings here.
  411. * As currently written, the stored a->face->charmap only matches one of
  412. * the actual charmaps and we cannot know at this stage if it is the right
  413. * one. We should just skip all this stuff, and check in gdImageStringFTEx
  414. * if some particular charmap is preferred and if so whether it is held in
  415. * one of the a->face->charmaps[0..num_charmaps].
  416. * And why is it so bad not to find any recognized charmap? The user may
  417. * still know what mapping to use, even if we do not. In that case we can
  418. * just use the map in a->face->charmaps[num_charmaps] and be done with it.
  419. */
  420. for (n = 0; n < a->face->num_charmaps; n++) {
  421. charmap = a->face->charmaps[n];
  422. platform = charmap->platform_id;
  423. encoding = charmap->encoding_id;
  424. /* Whatever is the last value is what should be set */
  425. a->have_char_map_unicode = 0;
  426. a->have_char_map_big5 = 0;
  427. a->have_char_map_sjis = 0;
  428. a->have_char_map_apple_roman = 0;
  429. /* EAM DEBUG - Newer versions of libfree2 make it easier by defining encodings */
  430. #if (defined(FREETYPE_MAJOR) && ((FREETYPE_MAJOR == 2 && ((FREETYPE_MINOR == 1 && FREETYPE_PATCH >= 3) || FREETYPE_MINOR > 1) || FREETYPE_MAJOR > 2)))
  431. if (charmap->encoding == FT_ENCODING_MS_SYMBOL
  432. || charmap->encoding == FT_ENCODING_ADOBE_CUSTOM
  433. || charmap->encoding == FT_ENCODING_ADOBE_STANDARD) {
  434. a->have_char_map_unicode = 1;
  435. found = charmap;
  436. a->face->charmap = charmap;
  437. return (void *)a;
  438. }
  439. #endif /* Freetype 2.1.3 or better */
  440. /* EAM DEBUG */
  441. if ((platform == 3 && encoding == 1) /* Windows Unicode */
  442. || (platform == 3 && encoding == 0) /* Windows Symbol */
  443. || (platform == 2 && encoding == 1) /* ISO Unicode */
  444. || (platform == 0))
  445. { /* Apple Unicode */
  446. a->have_char_map_unicode = 1;
  447. found = charmap;
  448. if (b->preferred_map == gdFTEX_Unicode) {
  449. break;
  450. }
  451. } else if (platform == 3 && encoding == 4) { /* Windows Big5 */
  452. a->have_char_map_big5 = 1;
  453. found = charmap;
  454. if (b->preferred_map == gdFTEX_Big5) {
  455. break;
  456. }
  457. } else if (platform == 3 && encoding == 2) { /* Windows Sjis */
  458. a->have_char_map_sjis = 1;
  459. found = charmap;
  460. if (b->preferred_map == gdFTEX_Shift_JIS) {
  461. break;
  462. }
  463. } else if ((platform == 1 && encoding == 0) /* Apple Roman */
  464. || (platform == 2 && encoding == 0))
  465. { /* ISO ASCII */
  466. a->have_char_map_apple_roman = 1;
  467. found = charmap;
  468. if (b->preferred_map == gdFTEX_MacRoman) {
  469. break;
  470. }
  471. }
  472. }
  473. if (!found) {
  474. gdPFree(a->fontlist);
  475. gdPFree(a);
  476. *error = "Unable to find a CharMap that I can handle";
  477. return NULL;
  478. }
  479. /* 2.0.5: we should actually return this */
  480. a->face->charmap = found;
  481. return (void *) a;
  482. }
  483. static void fontRelease (void *element)
  484. {
  485. font_t *a = (font_t *) element;
  486. FT_Done_Face (a->face);
  487. gdPFree(a->fontlist);
  488. gdPFree((char *) element);
  489. }
  490. /********************************************************************/
  491. /* tweencolor cache functions */
  492. static int tweenColorTest (void *element, void *key)
  493. {
  494. tweencolor_t *a = (tweencolor_t *) element;
  495. tweencolorkey_t *b = (tweencolorkey_t *) key;
  496. return (a->pixel == b->pixel && a->bgcolor == b->bgcolor && a->fgcolor == b->fgcolor && a->im == b->im);
  497. }
  498. /*
  499. * Computes a color in im's color table that is part way between
  500. * the background and foreground colors proportional to the gray
  501. * pixel value in the range 0-NUMCOLORS. The fg and bg colors must already
  502. * be in the color table for palette images. For truecolor images the
  503. * returned value simply has an alpha component and gdImageAlphaBlend
  504. * does the work so that text can be alpha blended across a complex
  505. * background (TBB; and for real in 2.0.2).
  506. */
  507. static void * tweenColorFetch (char **error, void *key)
  508. {
  509. tweencolor_t *a;
  510. tweencolorkey_t *b = (tweencolorkey_t *) key;
  511. int pixel, npixel, bg, fg;
  512. gdImagePtr im;
  513. a = (tweencolor_t *) gdMalloc (sizeof (tweencolor_t));
  514. pixel = a->pixel = b->pixel;
  515. bg = a->bgcolor = b->bgcolor;
  516. fg = a->fgcolor = b->fgcolor;
  517. im = a->im = b->im;
  518. /* if fg is specified by a negative color idx, then don't antialias */
  519. if (fg < 0) {
  520. if ((pixel + pixel) >= NUMCOLORS) {
  521. a->tweencolor = -fg;
  522. } else {
  523. a->tweencolor = bg;
  524. }
  525. } else {
  526. npixel = NUMCOLORS - pixel;
  527. if (im->trueColor) {
  528. /* 2.0.1: use gdImageSetPixel to do the alpha blending work,
  529. * or to just store the alpha level. All we have to do here
  530. * is incorporate our knowledge of the percentage of this
  531. * pixel that is really "lit" by pushing the alpha value
  532. * up toward transparency in edge regions.
  533. */
  534. a->tweencolor = gdTrueColorAlpha(
  535. gdTrueColorGetRed(fg),
  536. gdTrueColorGetGreen(fg),
  537. gdTrueColorGetBlue(fg),
  538. gdAlphaMax - (gdTrueColorGetAlpha (fg) * pixel / NUMCOLORS));
  539. } else {
  540. a->tweencolor = gdImageColorResolve(im,
  541. (pixel * im->red[fg] + npixel * im->red[bg]) / NUMCOLORS,
  542. (pixel * im->green[fg] + npixel * im->green[bg]) / NUMCOLORS,
  543. (pixel * im->blue[fg] + npixel * im->blue[bg]) / NUMCOLORS);
  544. }
  545. }
  546. return (void *) a;
  547. }
  548. static void tweenColorRelease (void *element)
  549. {
  550. gdFree((char *) element);
  551. }
  552. /* draw_bitmap - transfers glyph bitmap to GD image */
  553. static char * gdft_draw_bitmap (gdCache_head_t *tc_cache, gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
  554. {
  555. unsigned char *pixel = NULL;
  556. int *tpixel = NULL;
  557. int opixel;
  558. int x, y, row, col, pc, pcr;
  559. tweencolor_t *tc_elem;
  560. tweencolorkey_t tc_key;
  561. /* copy to image, mapping colors */
  562. tc_key.fgcolor = fg;
  563. tc_key.im = im;
  564. /* Truecolor version; does not require the cache */
  565. if (im->trueColor) {
  566. for (row = 0; row < bitmap.rows; row++) {
  567. pc = row * bitmap.pitch;
  568. pcr = pc;
  569. y = pen_y + row;
  570. /* clip if out of bounds */
  571. /* 2.0.16: clipping rectangle, not image bounds */
  572. if ((y > im->cy2) || (y < im->cy1)) {
  573. continue;
  574. }
  575. for (col = 0; col < bitmap.width; col++, pc++) {
  576. int level;
  577. if (bitmap.pixel_mode == ft_pixel_mode_grays) {
  578. /* Scale to 128 levels of alpha for gd use.
  579. * alpha 0 is opacity, so be sure to invert at the end
  580. */
  581. level = (bitmap.buffer[pc] * gdAlphaMax / (bitmap.num_grays - 1));
  582. } else if (bitmap.pixel_mode == ft_pixel_mode_mono) {
  583. /* 2.0.5: mode_mono fix from Giuliano Pochini */
  584. level = ((bitmap.buffer[(col>>3)+pcr]) & (1<<(~col&0x07))) ? gdAlphaTransparent : gdAlphaOpaque;
  585. } else {
  586. return "Unsupported ft_pixel_mode";
  587. }
  588. if (level == 0) /* if background */
  589. continue;
  590. if ((fg >= 0) && (im->trueColor)) {
  591. /* Consider alpha in the foreground color itself to be an
  592. * upper bound on how opaque things get, when truecolor is
  593. * available. Without truecolor this results in far too many
  594. * color indexes.
  595. */
  596. level = level * (gdAlphaMax - gdTrueColorGetAlpha(fg)) / gdAlphaMax;
  597. }
  598. level = gdAlphaMax - level;
  599. x = pen_x + col;
  600. /* clip if out of bounds */
  601. /* 2.0.16: clip to clipping rectangle, Matt McNabb */
  602. if ((x > im->cx2) || (x < im->cx1)) {
  603. continue;
  604. }
  605. /* get pixel location in gd buffer */
  606. tpixel = &im->tpixels[y][x];
  607. if (fg < 0) {
  608. if (level < (gdAlphaMax / 2)) {
  609. *tpixel = -fg;
  610. }
  611. } else {
  612. if (im->alphaBlendingFlag) {
  613. opixel = *tpixel;
  614. if (gdTrueColorGetAlpha(opixel) != gdAlphaTransparent) {
  615. *tpixel = gdAlphaBlend (opixel,
  616. (level << 24) + (fg & 0xFFFFFF));
  617. } else {
  618. *tpixel = (level << 24) + (fg & 0xFFFFFF);
  619. }
  620. } else {
  621. *tpixel = (level << 24) + (fg & 0xFFFFFF);
  622. }
  623. }
  624. }
  625. }
  626. return (char *) NULL;
  627. }
  628. /* Non-truecolor case, restored to its more or less original form */
  629. for (row = 0; row < bitmap.rows; row++) {
  630. int pcr;
  631. pc = row * bitmap.pitch;
  632. pcr = pc;
  633. if (bitmap.pixel_mode==ft_pixel_mode_mono) {
  634. pc *= 8; /* pc is measured in bits for monochrome images */
  635. }
  636. y = pen_y + row;
  637. /* clip if out of bounds */
  638. if (y >= im->sy || y < 0) {
  639. continue;
  640. }
  641. for (col = 0; col < bitmap.width; col++, pc++) {
  642. if (bitmap.pixel_mode == ft_pixel_mode_grays) {
  643. /*
  644. * Round to NUMCOLORS levels of antialiasing for
  645. * index color images since only 256 colors are
  646. * available.
  647. */
  648. tc_key.pixel = ((bitmap.buffer[pc] * NUMCOLORS) + bitmap.num_grays / 2) / (bitmap.num_grays - 1);
  649. } else if (bitmap.pixel_mode == ft_pixel_mode_mono) {
  650. tc_key.pixel = ((bitmap.buffer[pc / 8] << (pc % 8)) & 128) ? NUMCOLORS : 0;
  651. /* 2.0.5: mode_mono fix from Giuliano Pochini */
  652. tc_key.pixel = ((bitmap.buffer[(col>>3)+pcr]) & (1<<(~col&0x07))) ? NUMCOLORS : 0;
  653. } else {
  654. return "Unsupported ft_pixel_mode";
  655. }
  656. if (tc_key.pixel > 0) { /* if not background */
  657. x = pen_x + col;
  658. /* clip if out of bounds */
  659. if (x >= im->sx || x < 0) {
  660. continue;
  661. }
  662. /* get pixel location in gd buffer */
  663. pixel = &im->pixels[y][x];
  664. if (tc_key.pixel == NUMCOLORS) {
  665. /* use fg color directly. gd 2.0.2: watch out for
  666. * negative indexes (thanks to David Marwood).
  667. */
  668. *pixel = (fg < 0) ? -fg : fg;
  669. } else {
  670. /* find antialised color */
  671. tc_key.bgcolor = *pixel;
  672. tc_elem = (tweencolor_t *) gdCacheGet(tc_cache, &tc_key);
  673. *pixel = tc_elem->tweencolor;
  674. }
  675. }
  676. }
  677. }
  678. return (char *) NULL;
  679. }
  680. static int
  681. gdroundupdown (FT_F26Dot6 v1, int roundup)
  682. {
  683. return (!roundup) ? v1 >> 6 : (v1 + 63) >> 6;
  684. }
  685. void gdFontCacheShutdown()
  686. {
  687. gdMutexLock(gdFontCacheMutex);
  688. if (fontCache) {
  689. gdCacheDelete(fontCache);
  690. fontCache = NULL;
  691. FT_Done_FreeType(library);
  692. }
  693. gdMutexUnlock(gdFontCacheMutex);
  694. }
  695. void gdFreeFontCache(void)
  696. {
  697. gdFontCacheShutdown();
  698. }
  699. void gdFontCacheMutexSetup()
  700. {
  701. gdMutexSetup(gdFontCacheMutex);
  702. }
  703. void gdFontCacheMutexShutdown()
  704. {
  705. gdMutexShutdown(gdFontCacheMutex);
  706. }
  707. int gdFontCacheSetup(void)
  708. {
  709. if (fontCache) {
  710. /* Already set up */
  711. return 0;
  712. }
  713. if (FT_Init_FreeType(&library)) {
  714. return -1;
  715. }
  716. fontCache = gdCacheCreate (FONTCACHESIZE, fontTest, fontFetch, fontRelease);
  717. return 0;
  718. }
  719. /********************************************************************/
  720. /* gdImageStringFT - render a utf8 string onto a gd image */
  721. char *
  722. gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
  723. double ptsize, double angle, int x, int y, char *string)
  724. {
  725. return gdImageStringFTEx(im, brect, fg, fontlist, ptsize, angle, x, y, string, 0);
  726. }
  727. char *
  728. gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, double ptsize, double angle, int x, int y, char *string, gdFTStringExtraPtr strex)
  729. {
  730. FT_BBox bbox, glyph_bbox;
  731. FT_Matrix matrix;
  732. FT_Vector pen, delta, penf;
  733. FT_Face face;
  734. FT_Glyph image;
  735. FT_GlyphSlot slot;
  736. FT_Bool use_kerning;
  737. FT_UInt glyph_index, previous;
  738. double sin_a = sin (angle);
  739. double cos_a = cos (angle);
  740. int len, i = 0, ch;
  741. int x1 = 0, y1 = 0;
  742. font_t *font;
  743. fontkey_t fontkey;
  744. char *next;
  745. char *tmpstr = NULL;
  746. int render = (im && (im->trueColor || (fg <= 255 && fg >= -255)));
  747. FT_BitmapGlyph bm;
  748. /* 2.0.13: Bob Ostermann: don't force autohint, that's just for testing freetype and doesn't look as good */
  749. int render_mode = FT_LOAD_DEFAULT;
  750. int m, mfound;
  751. /* Now tuneable thanks to Wez Furlong */
  752. double linespace = LINESPACE;
  753. /* 2.0.6: put this declaration with the other declarations! */
  754. /*
  755. * make a new tweenColorCache on every call
  756. * because caching colormappings between calls
  757. * is not safe. If the im-pointer points to a
  758. * brand new image, the cache gives out bogus
  759. * colorindexes. -- 27.06.2001 <krisku@arrak.fi>
  760. */
  761. gdCache_head_t *tc_cache;
  762. /* Tuneable horizontal and vertical resolution in dots per inch */
  763. int hdpi, vdpi;
  764. if (strex && ((strex->flags & gdFTEX_LINESPACE) == gdFTEX_LINESPACE)) {
  765. linespace = strex->linespacing;
  766. }
  767. tc_cache = gdCacheCreate(TWEENCOLORCACHESIZE, tweenColorTest, tweenColorFetch, tweenColorRelease);
  768. /***** initialize font library and font cache on first call ******/
  769. gdMutexLock(gdFontCacheMutex);
  770. if (!fontCache) {
  771. if (gdFontCacheSetup() != 0) {
  772. gdCacheDelete(tc_cache);
  773. gdMutexUnlock(gdFontCacheMutex);
  774. return "Failure to initialize font library";
  775. }
  776. }
  777. /*****/
  778. /* 2.0.12: allow explicit specification of the preferred map;
  779. * but we still fall back if it is not available.
  780. */
  781. m = gdFTEX_Unicode;
  782. if (strex && (strex->flags & gdFTEX_CHARMAP)) {
  783. m = strex->charmap;
  784. }
  785. /* get the font (via font cache) */
  786. fontkey.fontlist = fontlist;
  787. fontkey.library = &library;
  788. fontkey.preferred_map = m;
  789. font = (font_t *) gdCacheGet (fontCache, &fontkey);
  790. if (!font) {
  791. gdCacheDelete(tc_cache);
  792. gdMutexUnlock(gdFontCacheMutex);
  793. return fontCache->error;
  794. }
  795. face = font->face; /* shortcut */
  796. slot = face->glyph; /* shortcut */
  797. /*
  798. * Added hdpi and vdpi to support images at non-screen resolutions, i.e. 300 dpi TIFF,
  799. * or 100h x 50v dpi FAX format. 2.0.23.
  800. * 2004/02/27 Mark Shackelford, mark.shackelford@acs-inc.com
  801. */
  802. hdpi = GD_RESOLUTION;
  803. vdpi = GD_RESOLUTION;
  804. if (strex && (strex->flags & gdFTEX_RESOLUTION)) {
  805. hdpi = strex->hdpi;
  806. vdpi = strex->vdpi;
  807. }
  808. if (FT_Set_Char_Size(face, 0, (FT_F26Dot6) (ptsize * 64), hdpi, vdpi)) {
  809. gdCacheDelete(tc_cache);
  810. gdMutexUnlock(gdFontCacheMutex);
  811. return "Could not set character size";
  812. }
  813. matrix.xx = (FT_Fixed) (cos_a * (1 << 16));
  814. matrix.yx = (FT_Fixed) (sin_a * (1 << 16));
  815. matrix.xy = -matrix.yx;
  816. matrix.yy = matrix.xx;
  817. penf.x = penf.y = 0; /* running position of non-rotated string */
  818. pen.x = pen.y = 0; /* running position of rotated string */
  819. bbox.xMin = bbox.xMax = bbox.yMin = bbox.yMax = 0;
  820. use_kerning = FT_HAS_KERNING (face);
  821. previous = 0;
  822. if (fg < 0) {
  823. render_mode |= FT_LOAD_MONOCHROME;
  824. }
  825. /* Try all three types of maps, but start with the specified one */
  826. mfound = 0;
  827. for (i = 0; i < 3; i++) {
  828. switch (m) {
  829. case gdFTEX_Unicode:
  830. if (font->have_char_map_unicode) {
  831. mfound = 1;
  832. }
  833. break;
  834. case gdFTEX_Shift_JIS:
  835. if (font->have_char_map_sjis) {
  836. mfound = 1;
  837. }
  838. break;
  839. case gdFTEX_Big5:
  840. /* This was the 'else' case, we can't really 'detect' it */
  841. mfound = 1;
  842. break;
  843. }
  844. if (mfound) {
  845. break;
  846. }
  847. m++;
  848. m %= 3;
  849. }
  850. if (!mfound) {
  851. /* No character set found! */
  852. gdMutexUnlock(gdFontCacheMutex);
  853. return "No character set found";
  854. }
  855. #ifndef JISX0208
  856. if (font->have_char_map_sjis) {
  857. #endif
  858. tmpstr = (char *) gdMalloc(BUFSIZ);
  859. any2eucjp(tmpstr, string, BUFSIZ);
  860. next = tmpstr;
  861. #ifndef JISX0208
  862. } else {
  863. next = string;
  864. }
  865. #endif
  866. i = 0;
  867. while (*next) {
  868. ch = *next;
  869. /* carriage returns */
  870. if (ch == '\r') {
  871. penf.x = 0;
  872. x1 = (int)(- penf.y * sin_a + 32) / 64;
  873. y1 = (int)(- penf.y * cos_a + 32) / 64;
  874. pen.x = pen.y = 0;
  875. previous = 0; /* clear kerning flag */
  876. next++;
  877. continue;
  878. }
  879. /* newlines */
  880. if (ch == '\n') {
  881. if (!*(++next)) break;
  882. /* 2.0.13: reset penf.x. Christopher J. Grayce */
  883. penf.x = 0;
  884. penf.y -= (long)(face->size->metrics.height * linespace);
  885. penf.y = (penf.y - 32) & -64; /* round to next pixel row */
  886. x1 = (int)(- penf.y * sin_a + 32) / 64;
  887. y1 = (int)(- penf.y * cos_a + 32) / 64;
  888. pen.x = pen.y = 0;
  889. previous = 0; /* clear kerning flag */
  890. continue;
  891. }
  892. /* EAM DEBUG */
  893. #if (defined(FREETYPE_MAJOR) && ((FREETYPE_MAJOR == 2 && ((FREETYPE_MINOR == 1 && FREETYPE_PATCH >= 3) || FREETYPE_MINOR > 1) || FREETYPE_MAJOR > 2)))
  894. if (font->face->family_name && font->face->charmap->encoding &&
  895. font->face->charmap->encoding == FT_ENCODING_MS_SYMBOL && strcmp(font->face->family_name, "Symbol") == 0) {
  896. /* I do not know the significance of the constant 0xf000.
  897. * It was determined by inspection of the character codes
  898. * stored in Microsoft font symbol.
  899. * Added by Pierre (pajoye@php.net):
  900. * Convert to the Symbol glyph range only for a Symbol family member
  901. */
  902. len = gdTcl_UtfToUniChar (next, &ch);
  903. ch |= 0xf000;
  904. next += len;
  905. } else
  906. #endif /* Freetype 2.1 or better */
  907. /* EAM DEBUG */
  908. switch (m) {
  909. case gdFTEX_Unicode:
  910. if (font->have_char_map_unicode) {
  911. /* use UTF-8 mapping from ASCII */
  912. len = gdTcl_UtfToUniChar(next, &ch);
  913. next += len;
  914. }
  915. break;
  916. case gdFTEX_Shift_JIS:
  917. if (font->have_char_map_sjis) {
  918. unsigned char c;
  919. int jiscode;
  920. c = *next;
  921. if (0xA1 <= c && c <= 0xFE) {
  922. next++;
  923. jiscode = 0x100 * (c & 0x7F) + ((*next) & 0x7F);
  924. ch = (jiscode >> 8) & 0xFF;
  925. jiscode &= 0xFF;
  926. if (ch & 1) {
  927. jiscode += 0x40 - 0x21;
  928. } else {
  929. jiscode += 0x9E - 0x21;
  930. }
  931. if (jiscode >= 0x7F) {
  932. jiscode++;
  933. }
  934. ch = (ch - 0x21) / 2 + 0x81;
  935. if (ch >= 0xA0) {
  936. ch += 0x40;
  937. }
  938. ch = (ch << 8) + jiscode;
  939. } else {
  940. ch = c & 0xFF; /* don't extend sign */
  941. }
  942. if (*next) next++;
  943. }
  944. break;
  945. case gdFTEX_Big5: {
  946. /*
  947. * Big 5 mapping:
  948. * use "JIS-8 half-width katakana" coding from 8-bit characters. Ref:
  949. * ftp://ftp.ora.com/pub/examples/nutshell/ujip/doc/japan.inf-032092.sjs
  950. */
  951. ch = (*next) & 0xFF; /* don't extend sign */
  952. next++;
  953. if (ch >= 161 /* first code of JIS-8 pair */
  954. && *next) { /* don't advance past '\0' */
  955. /* TBB: Fix from Kwok Wah On: & 255 needed */
  956. ch = (ch * 256) + ((*next) & 255);
  957. next++;
  958. }
  959. }
  960. break;
  961. }
  962. /* set rotation transform */
  963. FT_Set_Transform(face, &matrix, NULL);
  964. /* Convert character code to glyph index */
  965. glyph_index = FT_Get_Char_Index(face, ch);
  966. /* retrieve kerning distance and move pen position */
  967. if (use_kerning && previous && glyph_index) {
  968. FT_Get_Kerning(face, previous, glyph_index, ft_kerning_default, &delta);
  969. pen.x += (int)(delta.x * cos_a);
  970. pen.y -= (int)(delta.x * sin_a);
  971. penf.x += delta.x;
  972. }
  973. if (brect) { /* only if need brect */
  974. /* load glyph image into the slot (erase previous one) */
  975. if (FT_Load_Glyph(face, glyph_index, render_mode | FT_LOAD_IGNORE_TRANSFORM)) {
  976. if (tmpstr) {
  977. gdFree(tmpstr);
  978. }
  979. gdCacheDelete(tc_cache);
  980. gdMutexUnlock(gdFontCacheMutex);
  981. return "Problem loading glyph";
  982. }
  983. /* transform glyph image */
  984. if (FT_Get_Glyph(slot, &image)) {
  985. if (tmpstr) {
  986. gdFree(tmpstr);
  987. }
  988. gdCacheDelete(tc_cache);
  989. gdMutexUnlock(gdFontCacheMutex);
  990. return "Problem loading glyph";
  991. }
  992. FT_Glyph_Get_CBox(image, ft_glyph_bbox_gridfit, &glyph_bbox);
  993. glyph_bbox.xMin += penf.x;
  994. glyph_bbox.yMin += penf.y;
  995. glyph_bbox.xMax += penf.x;
  996. glyph_bbox.yMax += penf.y;
  997. if (ch == ' ') { /* special case for trailing space */
  998. glyph_bbox.xMax += slot->metrics.horiAdvance;
  999. }
  1000. if (!i) { /* if first character, init BB corner values */
  1001. bbox.xMin = glyph_bbox.xMin;
  1002. bbox.yMin = glyph_bbox.yMin;
  1003. bbox.xMax = glyph_bbox.xMax;
  1004. bbox.yMax = glyph_bbox.yMax;
  1005. } else {
  1006. if (bbox.xMin > glyph_bbox.xMin) {
  1007. bbox.xMin = glyph_bbox.xMin;
  1008. }
  1009. if (bbox.yMin > glyph_bbox.yMin) {
  1010. bbox.yMin = glyph_bbox.yMin;
  1011. }
  1012. if (bbox.xMax < glyph_bbox.xMax) {
  1013. bbox.xMax = glyph_bbox.xMax;
  1014. }
  1015. if (bbox.yMax < glyph_bbox.yMax) {
  1016. bbox.yMax = glyph_bbox.yMax;
  1017. }
  1018. }
  1019. i++;
  1020. }
  1021. /* increment (unrotated) pen position */
  1022. penf.x += slot->metrics.horiAdvance;
  1023. if (render) {
  1024. if (!brect || angle != 0) {
  1025. /* reload the rotated glyph (for bbox we needed FT_LOAD_IGNORE_TRANSFORM - bbox is rotated later) */
  1026. FT_Done_Glyph(image);
  1027. /* load glyph image into the slot (erase previous one) */
  1028. if (FT_Load_Glyph(face, glyph_index, render_mode)) {
  1029. if (tmpstr) {
  1030. gdFree(tmpstr);
  1031. }
  1032. gdCacheDelete(tc_cache);
  1033. gdMutexUnlock(gdFontCacheMutex);
  1034. return "Problem loading glyph";
  1035. }
  1036. /* transform glyph image */
  1037. if (FT_Get_Glyph(slot, &image)) {
  1038. if (tmpstr) {
  1039. gdFree(tmpstr);
  1040. }
  1041. gdCacheDelete(tc_cache);
  1042. gdMutexUnlock(gdFontCacheMutex);
  1043. return "Problem loading glyph";
  1044. }
  1045. }
  1046. if (image->format != ft_glyph_format_bitmap && FT_Glyph_To_Bitmap(&image, ft_render_mode_normal, 0, 1)) {
  1047. FT_Done_Glyph(image);
  1048. if (tmpstr) {
  1049. gdFree(tmpstr);
  1050. }
  1051. gdCacheDelete(tc_cache);
  1052. gdMutexUnlock(gdFontCacheMutex);
  1053. return "Problem rendering glyph";
  1054. }
  1055. /* now, draw to our target surface */
  1056. bm = (FT_BitmapGlyph) image;
  1057. gdft_draw_bitmap(tc_cache, im, fg, bm->bitmap, x + x1 + ((pen.x + 31) >> 6) + bm->left, y + y1 + ((pen.y + 31) >> 6) - bm->top);
  1058. }
  1059. /* record current glyph index for kerning */
  1060. previous = glyph_index;
  1061. /* increment pen position */
  1062. pen.x += image->advance.x >> 10;
  1063. pen.y -= image->advance.y >> 10;
  1064. FT_Done_Glyph(image);
  1065. }
  1066. if (brect) { /* only if need brect */
  1067. /* For perfect rounding, must get sin(a + pi/4) and sin(a - pi/4). */
  1068. double d1 = sin (angle + 0.78539816339744830962);
  1069. double d2 = sin (angle - 0.78539816339744830962);
  1070. /* rotate bounding rectangle (at 0, 0) */
  1071. brect[0] = (int) (bbox.xMin * cos_a - bbox.yMin * sin_a);
  1072. brect[1] = (int) (bbox.xMin * sin_a + bbox.yMin * cos_a);
  1073. brect[2] = (int) (bbox.xMax * cos_a - bbox.yMin * sin_a);
  1074. brect[3] = (int) (bbox.xMax * sin_a + bbox.yMin * cos_a);
  1075. brect[4] = (int) (bbox.xMax * cos_a - bbox.yMax * sin_a);
  1076. brect[5] = (int) (bbox.xMax * sin_a + bbox.yMax * cos_a);
  1077. brect[6] = (int) (bbox.xMin * cos_a - bbox.yMax * sin_a);
  1078. brect[7] = (int) (bbox.xMin * sin_a + bbox.yMax * cos_a);
  1079. /* scale, round and offset brect */
  1080. brect[0] = x + gdroundupdown(brect[0], d2 > 0);
  1081. brect[1] = y - gdroundupdown(brect[1], d1 < 0);
  1082. brect[2] = x + gdroundupdown(brect[2], d1 > 0);
  1083. brect[3] = y - gdroundupdown(brect[3], d2 > 0);
  1084. brect[4] = x + gdroundupdown(brect[4], d2 < 0);
  1085. brect[5] = y - gdroundupdown(brect[5], d1 > 0);
  1086. brect[6] = x + gdroundupdown(brect[6], d1 < 0);
  1087. brect[7] = y - gdroundupdown(brect[7], d2 < 0);
  1088. }
  1089. if (tmpstr) {
  1090. gdFree(tmpstr);
  1091. }
  1092. gdCacheDelete(tc_cache);
  1093. gdMutexUnlock(gdFontCacheMutex);
  1094. return (char *) NULL;
  1095. }
  1096. #endif /* HAVE_LIBFREETYPE */