2
/********************************************/
3
/* gd interface to freetype library */
5
/* John Ellson ellson@graphviz.org */
6
/********************************************/
13
#include "gdhelpers.h"
20
# define R_OK 04 /* Needed in Windows */
25
#define access _access
31
/* number of antialised colors for indexed bitmaps */
32
/* overwrite Windows GDI define in case of windows build */
39
gdImageStringTTF (gdImage * im, int *brect, int fg, char *fontlist,
40
double ptsize, double angle, int x, int y, char *string)
42
/* 2.0.6: valid return */
43
return gdImageStringFT (im, brect, fg, fontlist, ptsize, angle, x, y, string);
46
#ifndef HAVE_LIBFREETYPE
48
gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist,
49
double ptsize, double angle, int x, int y, char *string,
50
gdFTStringExtraPtr strex)
52
return "libgd was not built with FreeType font support\n";
56
gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
57
double ptsize, double angle, int x, int y, char *string)
59
return "libgd was not built with FreeType font support\n";
65
#include FT_FREETYPE_H
68
/* number of fonts cached before least recently used is replaced */
69
#define FONTCACHESIZE 6
71
/* number of antialias color lookups cached */
72
#define TWEENCOLORCACHESIZE 32
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.
79
#define LINESPACE 1.05
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.
85
#define LISTSEPARATOR ";"
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.
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"
97
#define DEFAULT_FONTPATH "/usr/share/fonts/truetype"
100
#ifndef PATHSEPARATOR
101
#define PATHSEPARATOR ":"
110
#define MAX(a,b) ((a)>(b)?(a):(b))
114
#define MIN(a,b) ((a)<(b)?(a):(b))
119
char *fontlist; /* key */
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;
128
char *fontlist; /* key */
135
int bgcolor; /* key */
136
int fgcolor; /* key *//* -ve means no antialias */
137
gdImagePtr im; /* key */
144
int bgcolor; /* key */
145
int fgcolor; /* key *//* -ve means no antialias */
146
gdImagePtr im; /* key */
149
/********************************************************************
150
* gdTcl_UtfToUniChar is borrowed from Tcl ...
155
* Routines for manipulating UTF-8 strings.
157
* Copyright (c) 1997-1998 Sun Microsystems, Inc.
159
* See the file "license.terms" for information on usage and redistribution
160
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
162
* SCCS: @(#) tclUtf.c 1.25 98/01/28 18:02:43
166
*---------------------------------------------------------------------------
168
* gdTcl_UtfToUniChar --
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().
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.
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.
188
*---------------------------------------------------------------------------
192
#include "jisx0208.h"
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 */
203
/* HTML4.0 entities in decimal form, e.g. Å */
204
byte = *((unsigned char *) str);
208
byte = *((unsigned char *) (str + 1));
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');
219
*chPtr = (Tcl_UniChar) n;
225
/* Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones. */
227
byte = *((unsigned char *) str);
229
if (0xA1 <= byte && byte <= 0xFE) {
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;
239
*chPtr = (Tcl_UniChar) UnicodeTbl[ku - 1][ten - 1];
242
#endif /* JISX0208 */
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
250
*chPtr = (Tcl_UniChar) byte;
252
} else if (byte < 0xE0) {
253
if ((str[1] & 0xC0) == 0x80) {
254
/* Two-byte-character lead-byte followed by a trail-byte. */
256
*chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (str[1] & 0x3F));
260
* A two-byte-character lead-byte not followed by trail-byte
264
*chPtr = (Tcl_UniChar) byte;
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. */
270
*chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12) | ((str[1] & 0x3F) << 6) | (str[2] & 0x3F));
273
/* A three-byte-character lead-byte not followed by two trail-bytes represents itself. */
275
*chPtr = (Tcl_UniChar) byte;
280
int ch, total, trail;
282
total = totalBytes[byte];
286
ch = byte & (0x3F >> trail);
289
if ((*str & 0xC0) != 0x80) {
303
*chPtr = (Tcl_UniChar) byte;
307
/********************************************************************/
308
/* font cache functions */
310
static int fontTest (void *element, void *key)
312
font_t *a = (font_t *) element;
313
fontkey_t *b = (fontkey_t *) key;
315
return (strcmp (a->fontlist, b->fontlist) == 0);
318
static void *fontFetch (char **error, void *key)
321
fontkey_t *b = (fontkey_t *) key;
324
unsigned short platform, encoding;
325
char *fontsearchpath, *fontlist;
326
char fullname[MAXPATHLEN], cur_dir[MAXPATHLEN];
327
char *name, *path=NULL, *dir;
330
FT_CharMap found = 0;
333
a = (font_t *) gdPMalloc(sizeof(font_t));
334
a->fontlist = gdPEstrdup(b->fontlist);
335
a->library = b->library;
338
* Search the pathlist for any of a list of font names.
340
fontsearchpath = getenv ("GDFONTPATH");
341
if (!fontsearchpath) {
342
fontsearchpath = DEFAULT_FONTPATH;
344
fontlist = gdEstrdup(a->fontlist);
347
* Must use gd_strtok_r else pointer corrupted by strtok in nested loop.
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);
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) {
361
for (dir = strtok (path, PATHSEPARATOR); dir; dir = strtok (0, PATHSEPARATOR)) {
362
if (!strcmp(dir, ".")) {
365
dir = VCWD_GETCWD(cur_dir, MAXPATHLEN);
367
dir = VCWD_GETWD(cur_dir);
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) { \
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");
401
gdPFree(a->fontlist);
403
*error = "Could not find/open font";
407
err = FT_New_Face (*b->library, fullname, 0, &a->face);
409
gdPFree(a->fontlist);
411
*error = "Could not read font";
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.
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;
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;
443
a->face->charmap = charmap;
446
#endif /* Freetype 2.1.3 or better */
449
if ((platform == 3 && encoding == 1) /* Windows Unicode */
450
|| (platform == 3 && encoding == 0) /* Windows Symbol */
451
|| (platform == 2 && encoding == 1) /* ISO Unicode */
453
{ /* Apple Unicode */
454
a->have_char_map_unicode = 1;
456
} else if (platform == 3 && encoding == 4) { /* Windows Big5 */
457
a->have_char_map_big5 = 1;
459
} else if (platform == 3 && encoding == 2) { /* Windows Sjis */
460
a->have_char_map_sjis = 1;
462
} else if ((platform == 1 && encoding == 0) /* Apple Roman */
463
|| (platform == 2 && encoding == 0))
465
a->have_char_map_apple_roman = 1;
470
gdPFree(a->fontlist);
472
*error = "Unable to find a CharMap that I can handle";
476
/* 2.0.5: we should actually return this */
477
a->face->charmap = found;
481
static void fontRelease (void *element)
483
font_t *a = (font_t *) element;
485
FT_Done_Face (a->face);
486
gdPFree(a->fontlist);
487
gdPFree((char *) element);
490
/********************************************************************/
491
/* tweencolor cache functions */
493
static int tweenColorTest (void *element, void *key)
495
tweencolor_t *a = (tweencolor_t *) element;
496
tweencolorkey_t *b = (tweencolorkey_t *) key;
498
return (a->pixel == b->pixel && a->bgcolor == b->bgcolor && a->fgcolor == b->fgcolor && a->im == b->im);
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).
510
static void * tweenColorFetch (char **error, void *key)
513
tweencolorkey_t *b = (tweencolorkey_t *) key;
514
int pixel, npixel, bg, fg;
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;
523
/* if fg is specified by a negative color idx, then don't antialias */
525
if ((pixel + pixel) >= NUMCOLORS) {
531
npixel = NUMCOLORS - pixel;
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.
539
a->tweencolor = gdTrueColorAlpha(
540
gdTrueColorGetRed(fg),
541
gdTrueColorGetGreen(fg),
542
gdTrueColorGetBlue(fg),
543
gdAlphaMax - (gdTrueColorGetAlpha (fg) * pixel / NUMCOLORS));
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);
554
static void tweenColorRelease (void *element)
556
gdFree((char *) element);
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)
562
unsigned char *pixel = NULL;
564
int x, y, row, col, pc, pcr;
566
tweencolor_t *tc_elem;
567
tweencolorkey_t tc_key;
569
/* copy to image, mapping colors */
572
/* Truecolor version; does not require the cache */
574
for (row = 0; row < bitmap.rows; row++) {
575
pc = row * bitmap.pitch;
578
/* clip if out of bounds */
579
/* 2.0.16: clipping rectangle, not image bounds */
580
if ((y > im->cy2) || (y < im->cy1)) {
583
for (col = 0; col < bitmap.width; col++, pc++) {
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
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;
594
return "Unsupported ft_pixel_mode";
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
602
level = level * (gdAlphaMax - gdTrueColorGetAlpha(fg)) / gdAlphaMax;
604
level = gdAlphaMax - level;
606
/* clip if out of bounds */
607
/* 2.0.16: clip to clipping rectangle, Matt McNabb */
608
if ((x > im->cx2) || (x < im->cx1)) {
611
/* get pixel location in gd buffer */
612
tpixel = &im->tpixels[y][x];
614
if (level < (gdAlphaMax / 2)) {
618
if (im->alphaBlendingFlag) {
619
*tpixel = gdAlphaBlend(*tpixel, (level << 24) + (fg & 0xFFFFFF));
621
*tpixel = (level << 24) + (fg & 0xFFFFFF);
626
return (char *) NULL;
628
/* Non-truecolor case, restored to its more or less original form */
629
for (row = 0; row < bitmap.rows; row++) {
631
pc = row * bitmap.pitch;
633
if (bitmap.pixel_mode==ft_pixel_mode_mono) {
634
pc *= 8; /* pc is measured in bits for monochrome images */
638
/* clip if out of bounds */
639
if (y >= im->sy || y < 0) {
643
for (col = 0; col < bitmap.width; col++, pc++) {
644
if (bitmap.pixel_mode == ft_pixel_mode_grays) {
646
* Round to NUMCOLORS levels of antialiasing for
647
* index color images since only 256 colors are
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;
656
return "Unsupported ft_pixel_mode";
658
if (tc_key.pixel > 0) { /* if not background */
661
/* clip if out of bounds */
662
if (x >= im->sx || x < 0) {
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).
671
*pixel = (fg < 0) ? -fg : fg;
673
/* find antialised color */
674
tc_key.bgcolor = *pixel;
675
tc_elem = (tweencolor_t *) gdCacheGet(tc_cache, &tc_key);
676
*pixel = tc_elem->tweencolor;
681
return (char *) NULL;
685
gdroundupdown (FT_F26Dot6 v1, int updown)
687
return (!updown) ? (v1 < 0 ? ((v1 - 63) >> 6) : v1 >> 6) : (v1 > 0 ? ((v1 + 63) >> 6) : v1 >> 6);
690
extern int any2eucjp (char *, char *, unsigned int);
692
/* Persistent font cache until explicitly cleared */
693
/* Fonts can be used across multiple images */
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;
700
void gdFontCacheShutdown()
703
gdMutexShutdown(gdFontCacheMutex);
704
gdCacheDelete(fontCache);
706
FT_Done_FreeType(library);
710
void gdFreeFontCache()
712
gdFontCacheShutdown();
715
int gdFontCacheSetup(void)
721
gdMutexSetup(gdFontCacheMutex);
722
if (FT_Init_FreeType(&library)) {
723
gdMutexShutdown(gdFontCacheMutex);
726
fontCache = gdCacheCreate (FONTCACHESIZE, fontTest, fontFetch, fontRelease);
731
/********************************************************************/
732
/* gdImageStringFT - render a utf8 string onto a gd image */
735
gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
736
double ptsize, double angle, int x, int y, char *string)
738
return gdImageStringFTEx(im, brect, fg, fontlist, ptsize, angle, x, y, string, 0);
742
gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, double ptsize, double angle, int x, int y, char *string, gdFTStringExtraPtr strex)
744
FT_BBox bbox, glyph_bbox;
746
FT_Vector pen, delta, penf;
751
FT_UInt glyph_index, previous;
752
double sin_a = sin (angle);
753
double cos_a = cos (angle);
760
int render = (im && (im->trueColor || (fg <= 255 && fg >= -255)));
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;
765
/* Now tuneable thanks to Wez Furlong */
766
double linespace = LINESPACE;
767
/* 2.0.6: put this declaration with the other declarations! */
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>
775
gdCache_head_t *tc_cache;
776
/* Tuneable horizontal and vertical resolution in dots per inch */
779
if (strex && ((strex->flags & gdFTEX_LINESPACE) == gdFTEX_LINESPACE)) {
780
linespace = strex->linespacing;
782
tc_cache = gdCacheCreate(TWEENCOLORCACHESIZE, tweenColorTest, tweenColorFetch, tweenColorRelease);
784
/***** initialize font library and font cache on first call ******/
787
if (gdFontCacheSetup() != 0) {
788
gdCacheDelete(tc_cache);
789
return "Failure to initialize font library";
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);
800
gdCacheDelete(tc_cache);
801
gdMutexUnlock(gdFontCacheMutex);
802
return fontCache->error;
804
face = font->face; /* shortcut */
805
slot = face->glyph; /* shortcut */
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
812
hdpi = GD_RESOLUTION;
813
vdpi = GD_RESOLUTION;
814
if (strex && (strex->flags & gdFTEX_RESOLUTION)) {
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";
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;
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;
834
use_kerning = FT_HAS_KERNING (face);
837
render_mode |= FT_LOAD_MONOCHROME;
839
/* 2.0.12: allow explicit specification of the preferred map;
840
* but we still fall back if it is not available.
843
if (strex && (strex->flags & gdFTEX_CHARMAP)) {
846
/* Try all three types of maps, but start with the specified one */
848
for (i = 0; i < 3; i++) {
851
if (font->have_char_map_unicode) {
855
case gdFTEX_Shift_JIS:
856
if (font->have_char_map_sjis) {
861
/* This was the 'else' case, we can't really 'detect' it */
872
/* No character set found! */
873
gdMutexUnlock(gdFontCacheMutex);
874
return "No character set found";
878
if (font->have_char_map_sjis) {
880
tmpstr = (char *) gdMalloc(BUFSIZ);
881
any2eucjp(tmpstr, string, BUFSIZ);
892
/* carriage returns */
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;
898
previous = 0; /* clear kerning flag */
904
/* 2.0.13: reset penf.x. Christopher J. Grayce */
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;
911
previous = 0; /* clear kerning flag */
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.
923
len = gdTcl_UtfToUniChar (next, &ch);
927
#endif /* Freetype 2.1 or better */
932
if (font->have_char_map_unicode) {
933
/* use UTF-8 mapping from ASCII */
934
len = gdTcl_UtfToUniChar(next, &ch);
938
case gdFTEX_Shift_JIS:
939
if (font->have_char_map_sjis) {
943
if (0xA1 <= c && c <= 0xFE) {
945
jiscode = 0x100 * (c & 0x7F) + ((*next) & 0x7F);
947
ch = (jiscode >> 8) & 0xFF;
951
jiscode += 0x40 - 0x21;
953
jiscode += 0x9E - 0x21;
956
if (jiscode >= 0x7F) {
959
ch = (ch - 0x21) / 2 + 0x81;
964
ch = (ch << 8) + jiscode;
966
ch = c & 0xFF; /* don't extend sign */
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
977
ch = (*next) & 0xFF; /* don't extend sign */
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);
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);
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);
1001
/* load glyph image into the slot (erase previous one) */
1002
if (FT_Load_Glyph(face, glyph_index, render_mode)) {
1006
gdCacheDelete(tc_cache);
1007
gdMutexUnlock(gdFontCacheMutex);
1008
return "Problem loading glyph";
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;
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;
1028
if (bbox.xMin > glyph_bbox.xMin) {
1029
bbox.xMin = glyph_bbox.xMin;
1031
if (bbox.yMin > glyph_bbox.yMin) {
1032
bbox.yMin = glyph_bbox.yMin;
1034
if (bbox.xMax < glyph_bbox.xMax) {
1035
bbox.xMax = glyph_bbox.xMax;
1037
if (bbox.yMax < glyph_bbox.yMax) {
1038
bbox.yMax = glyph_bbox.yMax;
1045
if (image->format != ft_glyph_format_bitmap && FT_Glyph_To_Bitmap(&image, ft_render_mode_normal, 0, 1)) {
1049
gdCacheDelete(tc_cache);
1050
gdMutexUnlock(gdFontCacheMutex);
1051
return "Problem rendering glyph";
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);
1059
/* record current glyph index for kerning */
1060
previous = glyph_index;
1062
/* increment pen position */
1063
pen.x += image->advance.x >> 10;
1064
pen.y -= image->advance.y >> 10;
1066
penf.x += slot->metrics.horiAdvance;
1068
FT_Done_Glyph(image);
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);
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);
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);
1100
gdCacheDelete(tc_cache);
1101
gdMutexUnlock(gdFontCacheMutex);
1102
return (char *) NULL;
1105
#endif /* HAVE_LIBFREETYPE */