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