~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/gd/libgd/gdft.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

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