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