4 * Copyright (C) 2012-2021 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
20 typedef struct Lang_Tag_Record_
26 typedef struct Name_Record_
28 FT_UShort platform_id
;
29 FT_UShort encoding_id
;
30 FT_UShort language_id
;
37 typedef struct Naming_Table_
40 FT_UShort string_offset
;
43 Name_Record
* name_records
;
45 FT_UShort lang_tag_count
;
46 Lang_Tag_Record
* lang_tag_records
;
51 parse_name_header(FT_Byte
** curp
,
65 n
->format
= NEXT_USHORT(p
);
66 n
->name_count
= NEXT_USHORT(p
);
67 n
->string_offset
= NEXT_USHORT(p
);
69 n
->name_records
= NULL
;
70 n
->lang_tag_records
= NULL
;
72 /* all name strings must be between `startp' and `endp' */
73 startp
= buf
+ 6 + 12 * n
->name_count
;
76 /* format 1 also has language tag records */
84 return FT_Err_Invalid_Table
;
86 n
->lang_tag_count
= NEXT_USHORT(q
);
88 startp
+= 2 + 4 * n
->lang_tag_count
;
91 n
->lang_tag_count
= 0;
94 return FT_Err_Invalid_Table
;
105 parse_name_records(FT_Byte
** curp
,
121 /* allocate name records array */
122 n
->name_records
= (Name_Record
*)calloc(1, n
->name_count
123 * sizeof (Name_Record
));
124 if (!n
->name_records
)
125 return FT_Err_Out_Of_Memory
;
127 /* walk over all name records */
129 for (i
= 0; i
< n
->name_count
; i
++)
131 Name_Record
* r
= &n
->name_records
[count
];
138 r
->platform_id
= NEXT_USHORT(p
);
139 r
->encoding_id
= NEXT_USHORT(p
);
140 r
->language_id
= NEXT_USHORT(p
);
141 r
->name_id
= NEXT_USHORT(p
);
143 r
->len
= NEXT_USHORT(p
);
145 offset
= NEXT_USHORT(p
);
147 s
= buf
+ n
->string_offset
+ offset
;
149 /* ignore invalid entries */
150 if (s
< startp
|| s
+ r
->len
> endp
)
153 /* mac encoding or non-Unicode Windows encoding? */
154 if (r
->platform_id
== 1
155 || (r
->platform_id
== 3 && !(r
->encoding_id
== 1
156 || r
->encoding_id
== 10)))
158 /* one-byte or multi-byte encodings */
160 /* skip everything after a NULL byte (if any) */
161 for (l
= 0; l
< r
->len
; l
++)
165 /* ignore zero-length entries */
173 /* (two-byte) UTF-16BE for everything else */
175 /* we need an even number of bytes */
178 /* ignore entries which contain only NULL bytes */
179 for (l
= 0; l
< r
->len
; l
++)
186 /* this string gets probably modified */
187 /* by the `info' or `info_post' callbacks, */
188 /* so we have to call the allocation function provided by the user */
189 r
->str
= (FT_Byte
*)font
->allocate(r
->len
);
191 return FT_Err_Out_Of_Memory
;
192 memcpy(r
->str
, s
, r
->len
);
196 /* we ignore the return value of `font->info' */
197 font
->info(r
->platform_id
,
209 /* let the user modify `name' table entries */
210 if (font
->info
&& font
->info_post
)
211 /* we ignore the return value of `font->info_post' */
212 font
->info_post(font
->info_data
);
214 /* shrink name record array if necessary */
215 n
->name_records
= (Name_Record
*)realloc(n
->name_records
,
216 count
* sizeof (Name_Record
));
217 n
->name_count
= count
;
224 parse_lang_tag_records(FT_Byte
** curp
,
236 if (!n
->lang_tag_count
)
239 /* allocate language tags array */
240 n
->lang_tag_records
= (Lang_Tag_Record
*)calloc(
241 1, n
->lang_tag_count
* sizeof (Lang_Tag_Record
));
242 if (!n
->lang_tag_records
)
243 return FT_Err_Out_Of_Memory
;
245 /* walk over all language tag records (if any) */
246 for (i
= 0; i
< n
->lang_tag_count
; i
++)
248 Lang_Tag_Record
* r
= &n
->lang_tag_records
[i
];
254 r
->len
= NEXT_USHORT(p
);
256 offset
= NEXT_USHORT(p
);
258 s
= buf
+ n
->string_offset
+ offset
;
260 /* ignore an invalid entry -- */
261 /* contrary to name records, we can't simply remove it */
262 /* because references to it should still work */
263 /* (we don't apply more fixes */
264 /* since ttfautohint is not a TrueType sanitizing tool) */
265 if (s
< startp
|| s
+ r
->len
> endp
)
268 /* we don't massage the data since we only make a copy */
269 r
->str
= (FT_Byte
*)malloc(r
->len
);
271 return FT_Err_Out_Of_Memory
;
273 memcpy(r
->str
, s
, r
->len
);
280 /* we build a non-optimized `name' table, this is, */
281 /* we don't fold strings `foo' and `foobar' into one string */
284 build_name_table(Naming_Table
* n
,
285 SFNT_Table
* name_table
)
288 FT_Byte
* buf_new_resized
;
289 FT_ULong buf_new_len
;
297 FT_UShort string_offset
;
298 FT_ULong data_offset
;
302 /* we reallocate the array to its real size later on */
303 buf_new_len
= 6 + 12 * n
->name_count
;
305 buf_new_len
+= 2 + 4 * n
->lang_tag_count
;
307 buf_new
= (FT_Byte
*)malloc(buf_new_len
);
309 return FT_Err_Out_Of_Memory
;
311 /* note that the OpenType specification says that `string_offset' is the */
312 /* `offset to the start of string storage (from start of table)', */
313 /* but this isn't enforced by the major rendering engines */
314 /* as long as the final string offsets are valid */
315 string_offset
= (buf_new_len
<= 0xFFFF) ? (FT_UShort
)buf_new_len
: 0xFFFF;
319 *(p
++) = HIGH(n
->format
);
320 *(p
++) = LOW(n
->format
);
321 *(p
++) = HIGH(n
->name_count
);
322 *(p
++) = LOW(n
->name_count
);
323 *(p
++) = HIGH(string_offset
);
324 *(p
++) = LOW(string_offset
);
329 for (i
= 0; i
< n
->name_count
; i
++)
331 Name_Record
* r
= &n
->name_records
[i
];
334 *(p
++) = HIGH(r
->platform_id
);
335 *(p
++) = LOW(r
->platform_id
);
336 *(p
++) = HIGH(r
->encoding_id
);
337 *(p
++) = LOW(r
->encoding_id
);
338 *(p
++) = HIGH(r
->language_id
);
339 *(p
++) = LOW(r
->language_id
);
340 *(p
++) = HIGH(r
->name_id
);
341 *(p
++) = LOW(r
->name_id
);
343 *(p
++) = HIGH(r
->len
);
344 *(p
++) = LOW(r
->len
);
346 /* the offset field gets filled in later */
354 *(p
++) = HIGH(n
->lang_tag_count
);
355 *(p
++) = LOW(n
->lang_tag_count
);
357 for (i
= 0; i
< n
->lang_tag_count
; i
++)
359 Lang_Tag_Record
* r
= &n
->lang_tag_records
[i
];
362 *(p
++) = HIGH(r
->len
);
363 *(p
++) = LOW(r
->len
);
365 /* the offset field gets filled in later */
372 if (buf_new_len
+ data_len
> 2 * 0xFFFF)
374 /* the table would become too large, so we do nothing */
379 data_offset
= buf_new_len
;
381 /* reallocate the buffer to fit its real size */
382 buf_new_len
+= data_len
;
383 /* make the allocated buffer length a multiple of 4 */
384 len
= (buf_new_len
+ 3) & ~3U;
386 buf_new_resized
= (FT_Byte
*)realloc(buf_new
, len
);
387 if (!buf_new_resized
)
390 return FT_Err_Out_Of_Memory
;
392 buf_new
= buf_new_resized
;
394 base
= buf_new
+ string_offset
;
395 p
= buf_new
+ data_offset
;
399 /* the first name record offset */
400 q
= &buf_new
[6 + 10];
401 for (i
= 0; i
< n
->name_count
; i
++)
403 Name_Record
* r
= &n
->name_records
[i
];
408 *(q
+ 1) = LOW(p
- base
);
411 memcpy(p
, r
->str
, r
->len
);
419 /* the first language tag record offset */
420 q
= &buf_new
[6 + 12 * n
->name_count
+ 2 + 2];
421 for (i
= 0; i
< n
->lang_tag_count
; i
++)
423 Lang_Tag_Record
* r
= &n
->lang_tag_records
[i
];
428 *(q
+ 1) = LOW(p
- base
);
431 memcpy(p
, r
->str
, r
->len
);
438 /* pad end of buffer with zeros */
439 switch (buf_new_len
% 4)
454 /* we are done; replace the old buffer with the new one */
455 free(name_table
->buf
);
457 name_table
->buf
= buf_new
;
458 name_table
->len
= buf_new_len
;
464 /* we handle the `name' table as optional; */
465 /* if there are problems not related to allocation, */
466 /* simply return (or continue, if possible) without signaling an error, */
467 /* and the original `name' table is not modified */
470 TA_sfnt_update_name_table(SFNT
* sfnt
,
475 SFNT_Table
* name_table
;
488 if (sfnt
->name_idx
== MISSING
)
491 name_table
= &font
->tables
[sfnt
->name_idx
];
492 buf
= name_table
->buf
;
493 buf_len
= name_table
->len
;
495 if (name_table
->processed
)
500 error
= parse_name_header(&p
, &n
, buf_len
, &startp
, &endp
);
504 /* due to the structure of the `name' table, */
505 /* we must parse it completely, apply our changes, */
506 /* and rebuild it from scratch */
507 error
= parse_name_records(&p
, &n
, buf
, startp
, endp
, font
);
511 error
= parse_lang_tag_records(&p
, &n
, buf
, startp
, endp
);
515 error
= build_name_table(&n
, name_table
);
519 name_table
->checksum
= TA_table_compute_checksum(name_table
->buf
,
523 for (i
= 0; i
< n
.name_count
; i
++)
524 font
->deallocate(n
.name_records
[i
].str
);
525 for (i
= 0; i
< n
.lang_tag_count
; i
++)
526 free(n
.lang_tag_records
[i
].str
);
528 free(n
.name_records
);
529 free(n
.lang_tag_records
);
531 name_table
->processed
= 1;
536 /* end of taname.c */