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