fixing the function that computes an ASCII face name
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
diff --git a/ChangeLog b/ChangeLog
index 19c96b2..9b4146a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
2002-03-30 David Turner <david@freetype.org>
+ * src/sfnt/sfobjs.c (tt_face_get_name): bug-fix
+
* include/freetype/internal/tttypes.h: adding comments to some of
the TT_FaceRec fields.
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index 5d866ee..21d4d71 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -38,6 +38,94 @@
#define FT_COMPONENT trace_sfobjs
+
+ /* convert a UTF-16 name entry to ASCII */
+ static FT_String*
+ tt_name_entry_ascii_from_utf16( TT_NameEntry entry,
+ FT_Memory memory )
+ {
+ FT_String* string;
+ FT_UInt len, code, n;
+ FT_Byte* read = (FT_Byte*) entry->string;
+
+ len = (FT_UInt) entry->stringLength/2;
+
+ if ( FT_MEM_NEW_ARRAY( string, len+1 ) )
+ return NULL;
+
+ for ( n = 0; n < len; n++ )
+ {
+ code = FT_NEXT_USHORT(read);
+ if ( code < 32 || code > 127 )
+ code = '?';
+
+ string[n] = (char)code;
+ }
+
+ string[len] = 0;
+
+ return string;
+ }
+
+
+ /* convert a UCS-4 name entry to ASCII */
+ static FT_String*
+ tt_name_entry_ascii_from_ucs4( TT_NameEntry entry,
+ FT_Memory memory )
+ {
+ FT_String* string;
+ FT_UInt len, code, n;
+ FT_Byte* read = (FT_Byte*) entry->string;
+
+ len = (FT_UInt) entry->stringLength/4;
+
+ if ( FT_MEM_NEW_ARRAY( string, len+1 ) )
+ return NULL;
+
+ for ( n = 0; n < len; n++ )
+ {
+ code = FT_NEXT_ULONG(read);
+ if ( code < 32 || code > 127 )
+ code = '?';
+
+ string[n] = (char)code;
+ }
+
+ string[len] = 0;
+
+ return string;
+ }
+
+
+ /* convert an Apple Roman or symbol name entry to ASCII */
+ static FT_String*
+ tt_name_entry_ascii_from_other( TT_NameEntry entry,
+ FT_Memory memory )
+ {
+ FT_String* string;
+ FT_UInt len, code, n;
+ FT_Byte* read = (FT_Byte*) entry->string;
+
+ len = (FT_UInt) entry->stringLength;
+
+ if ( FT_MEM_NEW_ARRAY( string, len+1 ) )
+ return NULL;
+
+ for ( n = 0; n < len; n++ )
+ {
+ code = *read++;
+ if ( code < 32 || code > 127 )
+ code = '?';
+
+ string[n] = (char)code;
+ }
+
+ string[len] = 0;
+
+ return string;
+ }
+
+
/*************************************************************************/
/* */
/* <Function> */
@@ -56,34 +144,48 @@
/* */
static FT_String*
tt_face_get_name( TT_Face face,
- FT_UShort nameid )
+ FT_UShort nameid )
{
- FT_Memory memory = face->root.memory;
- FT_UShort n;
+ FT_Memory memory = face->root.memory;
+ FT_String* result = NULL;
+ FT_UShort n;
TT_NameEntryRec* rec;
- FT_Bool wide_chars = 1;
- FT_Int found_apple = -1;
- FT_Int found_win = -1;
- FT_Int found_unicode = -1;
- FT_Int found;
+ FT_Int found_apple = -1;
+ FT_Int found_win = -1;
+ FT_Int found_unicode = -1;
rec = face->name_table.names;
for ( n = 0; n < face->name_table.numNameRecords; n++, rec++ )
{
+ /* according to the OpenType 1.3 specification, only Microsoft of */
+ /* Apple platform ids might be used in the 'name' table. The */
+ /* 'Unicode' platform is reserved for the 'cmap' table, and */
+ /* the 'Iso' one is deprecated */
+
+ /* however the Apple TrueType specification doesn't says the same */
+ /* thing and goes to suggest that all Unicode 'name' table entries */
+ /* should be coded in UTF-16 (in big-endian format I suppose) */
+ /* */
if ( rec->nameID == nameid && rec->string )
{
switch ( rec->platformID )
{
+
case TT_PLATFORM_APPLE_UNICODE:
+ case TT_PLATFORM_ISO:
{
+ /* there is 'languageID' to check there. We should use this */
+ /* field only as a last solution when nothing else is */
+ /* available.. */
+ /* */
found_unicode = n;
break;
}
-
+
case TT_PLATFORM_MACINTOSH:
{
- if ( rec->languageID == TT_MAC_ID_ROMAN )
+ if ( rec->languageID == TT_MAC_LANGID_ENGLISH )
found_apple = n;
break;
@@ -91,10 +193,22 @@
case TT_PLATFORM_MICROSOFT:
{
- if ( rec->encodingID <= TT_MS_ID_UNICODE_CS &&
- (rec->languageID & 0x3FF) == 0x009 )
+ /* we only take a non-English name when there is nothing */
+ /* else available in the font.. */
+ /* */
+ if ( found_win == -1 || (rec->languageID & 0x3FF) == 0x009 )
{
- found_win = n;
+ switch ( rec->encodingID )
+ {
+ case TT_MS_ID_SYMBOL_CS:
+ case TT_MS_ID_UNICODE_CS:
+ case TT_MS_ID_UCS_4:
+ found_win = n;
+ break;
+
+ default:
+ ;
+ }
}
break;
}
@@ -109,49 +223,40 @@
/* we will thus favor name encoded in Windows formats when they're */
/* available.. */
/* */
- found = found_win;
- if ( found < 0 )
+ if ( found_win >= 0 )
{
- found = found_apple;
- if ( found_apple < 0 )
- found = found_unicode;
- else
- wide_chars = 0;
- }
-
- /* found a Unicode name */
- if ( found >= 0 )
- {
- FT_String* string;
- FT_UInt len;
-
- rec = face->name_table.names + found;
- if ( wide_chars )
- {
- FT_UInt m;
-
-
- len = (FT_UInt)rec->stringLength / 2;
- if ( FT_MEM_ALLOC( string, len + 1 ) )
- return NULL;
-
- for ( m = 0; m < len; m ++ )
- string[m] = rec->string[2 * m + 1];
- }
- else
+ rec = face->name_table.names + found_win;
+ switch ( rec->encodingID )
{
- len = rec->stringLength;
- if ( FT_MEM_ALLOC( string, len + 1 ) )
- return NULL;
-
- FT_MEM_COPY( string, rec->string, len );
+ case TT_MS_ID_UNICODE_CS:
+ case TT_MS_ID_SYMBOL_CS:
+ {
+ result = tt_name_entry_ascii_from_utf16( rec, memory );
+ break;
+ }
+
+ case TT_MS_ID_UCS_4:
+ {
+ result = tt_name_entry_ascii_from_ucs4( rec, memory );
+ break;
+ }
+
+ default:
+ ;
}
-
- string[len] = '\0';
- return string;
}
-
- return NULL;
+ else if ( found_apple >= 0 )
+ {
+ rec = face->name_table.names + found_apple;
+ result = tt_name_entry_ascii_from_other( rec, memory );
+ }
+ else if ( found_unicode >= 0 )
+ {
+ rec = face->name_table.names + found_unicode;
+ result = tt_name_entry_ascii_from_utf16( rec, memory );
+ }
+
+ return result;
}