Skip to content

Instantly share code, notes, and snippets.

@scottmac
Created February 15, 2014 02:44
Show Gist options
  • Save scottmac/9013834 to your computer and use it in GitHub Desktop.
Save scottmac/9013834 to your computer and use it in GitHub Desktop.
Fix gdft to load the preferred map rather than the last one in the font
diff --git a/ext/gd/libgd/gdft.c b/ext/gd/libgd/gdft.c
index aa8e8d3..d09ede5 100644
--- a/ext/gd/libgd/gdft.c
+++ b/ext/gd/libgd/gdft.c
@@ -126,6 +126,7 @@ typedef struct
typedef struct
{
char *fontlist; /* key */
+ int preferred_map;
FT_Library *library;
} fontkey_t;
@@ -338,7 +339,26 @@ static int fontTest (void *element, void *key)
font_t *a = (font_t *) element;
fontkey_t *b = (fontkey_t *) key;
- return (strcmp (a->fontlist, b->fontlist) == 0);
+ 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)
@@ -454,15 +474,17 @@ static void *fontFetch (char **error, void *key)
* just use the map in a->face->charmaps[num_charmaps] and be done with it.
*/
- 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;
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
@@ -483,12 +505,21 @@ static void *fontFetch (char **error, void *key)
{ /* 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 */
@@ -826,9 +857,18 @@ gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, double ptsi
}
/*****/
+ /* 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);
@@ -870,13 +910,7 @@ gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, double ptsi
if (fg < 0) {
render_mode |= FT_LOAD_MONOCHROME;
}
- /* 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;
- }
+
/* Try all three types of maps, but start with the specified one */
mfound = 0;
for (i = 0; i < 3; i++) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment