Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / sfnt / ttcmap.c
blobb283f6d162100c1582575781782d1103f00a4096
1 /***************************************************************************/
2 /* */
3 /* ttcmap.c */
4 /* */
5 /* TrueType character mapping table (cmap) support (body). */
6 /* */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_INTERNAL_DEBUG_H
22 #include "sferrors.h" /* must come before FT_INTERNAL_VALIDATE_H */
24 #include FT_INTERNAL_VALIDATE_H
25 #include FT_INTERNAL_STREAM_H
26 #include "ttload.h"
27 #include "ttcmap.h"
28 #include "sfntpic.h"
31 /*************************************************************************/
32 /* */
33 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
34 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
35 /* messages during execution. */
36 /* */
37 #undef FT_COMPONENT
38 #define FT_COMPONENT trace_ttcmap
41 #define TT_PEEK_SHORT FT_PEEK_SHORT
42 #define TT_PEEK_USHORT FT_PEEK_USHORT
43 #define TT_PEEK_UINT24 FT_PEEK_UOFF3
44 #define TT_PEEK_LONG FT_PEEK_LONG
45 #define TT_PEEK_ULONG FT_PEEK_ULONG
47 #define TT_NEXT_SHORT FT_NEXT_SHORT
48 #define TT_NEXT_USHORT FT_NEXT_USHORT
49 #define TT_NEXT_UINT24 FT_NEXT_UOFF3
50 #define TT_NEXT_LONG FT_NEXT_LONG
51 #define TT_NEXT_ULONG FT_NEXT_ULONG
54 FT_CALLBACK_DEF( FT_Error )
55 tt_cmap_init( TT_CMap cmap,
56 FT_Byte* table )
58 cmap->data = table;
59 return SFNT_Err_Ok;
63 /*************************************************************************/
64 /*************************************************************************/
65 /***** *****/
66 /***** FORMAT 0 *****/
67 /***** *****/
68 /*************************************************************************/
69 /*************************************************************************/
71 /*************************************************************************/
72 /* */
73 /* TABLE OVERVIEW */
74 /* -------------- */
75 /* */
76 /* NAME OFFSET TYPE DESCRIPTION */
77 /* */
78 /* format 0 USHORT must be 0 */
79 /* length 2 USHORT table length in bytes */
80 /* language 4 USHORT Mac language code */
81 /* glyph_ids 6 BYTE[256] array of glyph indices */
82 /* 262 */
83 /* */
85 #ifdef TT_CONFIG_CMAP_FORMAT_0
87 FT_CALLBACK_DEF( FT_Error )
88 tt_cmap0_validate( FT_Byte* table,
89 FT_Validator valid )
91 FT_Byte* p = table + 2;
92 FT_UInt length = TT_NEXT_USHORT( p );
95 if ( table + length > valid->limit || length < 262 )
96 FT_INVALID_TOO_SHORT;
98 /* check glyph indices whenever necessary */
99 if ( valid->level >= FT_VALIDATE_TIGHT )
101 FT_UInt n, idx;
104 p = table + 6;
105 for ( n = 0; n < 256; n++ )
107 idx = *p++;
108 if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
109 FT_INVALID_GLYPH_ID;
113 return SFNT_Err_Ok;
117 FT_CALLBACK_DEF( FT_UInt )
118 tt_cmap0_char_index( TT_CMap cmap,
119 FT_UInt32 char_code )
121 FT_Byte* table = cmap->data;
124 return char_code < 256 ? table[6 + char_code] : 0;
128 FT_CALLBACK_DEF( FT_UInt32 )
129 tt_cmap0_char_next( TT_CMap cmap,
130 FT_UInt32 *pchar_code )
132 FT_Byte* table = cmap->data;
133 FT_UInt32 charcode = *pchar_code;
134 FT_UInt32 result = 0;
135 FT_UInt gindex = 0;
138 table += 6; /* go to glyph IDs */
139 while ( ++charcode < 256 )
141 gindex = table[charcode];
142 if ( gindex != 0 )
144 result = charcode;
145 break;
149 *pchar_code = result;
150 return gindex;
154 FT_CALLBACK_DEF( FT_Error )
155 tt_cmap0_get_info( TT_CMap cmap,
156 TT_CMapInfo *cmap_info )
158 FT_Byte* p = cmap->data + 4;
161 cmap_info->format = 0;
162 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
164 return SFNT_Err_Ok;
168 FT_DEFINE_TT_CMAP(tt_cmap0_class_rec,
169 sizeof ( TT_CMapRec ),
171 (FT_CMap_InitFunc) tt_cmap_init,
172 (FT_CMap_DoneFunc) NULL,
173 (FT_CMap_CharIndexFunc)tt_cmap0_char_index,
174 (FT_CMap_CharNextFunc) tt_cmap0_char_next,
176 NULL, NULL, NULL, NULL, NULL
179 (TT_CMap_ValidateFunc) tt_cmap0_validate,
180 (TT_CMap_Info_GetFunc) tt_cmap0_get_info
183 #endif /* TT_CONFIG_CMAP_FORMAT_0 */
186 /*************************************************************************/
187 /*************************************************************************/
188 /***** *****/
189 /***** FORMAT 2 *****/
190 /***** *****/
191 /***** This is used for certain CJK encodings that encode text in a *****/
192 /***** mixed 8/16 bits encoding along the following lines: *****/
193 /***** *****/
194 /***** * Certain byte values correspond to an 8-bit character code *****/
195 /***** (typically in the range 0..127 for ASCII compatibility). *****/
196 /***** *****/
197 /***** * Certain byte values signal the first byte of a 2-byte *****/
198 /***** character code (but these values are also valid as the *****/
199 /***** second byte of a 2-byte character). *****/
200 /***** *****/
201 /***** The following charmap lookup and iteration functions all *****/
202 /***** assume that the value "charcode" correspond to following: *****/
203 /***** *****/
204 /***** - For one byte characters, "charcode" is simply the *****/
205 /***** character code. *****/
206 /***** *****/
207 /***** - For two byte characters, "charcode" is the 2-byte *****/
208 /***** character code in big endian format. More exactly: *****/
209 /***** *****/
210 /***** (charcode >> 8) is the first byte value *****/
211 /***** (charcode & 0xFF) is the second byte value *****/
212 /***** *****/
213 /***** Note that not all values of "charcode" are valid according *****/
214 /***** to these rules, and the function moderately check the *****/
215 /***** arguments. *****/
216 /***** *****/
217 /*************************************************************************/
218 /*************************************************************************/
220 /*************************************************************************/
221 /* */
222 /* TABLE OVERVIEW */
223 /* -------------- */
224 /* */
225 /* NAME OFFSET TYPE DESCRIPTION */
226 /* */
227 /* format 0 USHORT must be 2 */
228 /* length 2 USHORT table length in bytes */
229 /* language 4 USHORT Mac language code */
230 /* keys 6 USHORT[256] sub-header keys */
231 /* subs 518 SUBHEAD[NSUBS] sub-headers array */
232 /* glyph_ids 518+NSUB*8 USHORT[] glyph ID array */
233 /* */
234 /* The `keys' table is used to map charcode high-bytes to sub-headers. */
235 /* The value of `NSUBS' is the number of sub-headers defined in the */
236 /* table and is computed by finding the maximum of the `keys' table. */
237 /* */
238 /* Note that for any n, `keys[n]' is a byte offset within the `subs' */
239 /* table, i.e., it is the corresponding sub-header index multiplied */
240 /* by 8. */
241 /* */
242 /* Each sub-header has the following format: */
243 /* */
244 /* NAME OFFSET TYPE DESCRIPTION */
245 /* */
246 /* first 0 USHORT first valid low-byte */
247 /* count 2 USHORT number of valid low-bytes */
248 /* delta 4 SHORT see below */
249 /* offset 6 USHORT see below */
250 /* */
251 /* A sub-header defines, for each high-byte, the range of valid */
252 /* low-bytes within the charmap. Note that the range defined by `first' */
253 /* and `count' must be completely included in the interval [0..255] */
254 /* according to the specification. */
255 /* */
256 /* If a character code is contained within a given sub-header, then */
257 /* mapping it to a glyph index is done as follows: */
258 /* */
259 /* * The value of `offset' is read. This is a _byte_ distance from the */
260 /* location of the `offset' field itself into a slice of the */
261 /* `glyph_ids' table. Let's call it `slice' (it is a USHORT[] too). */
262 /* */
263 /* * The value `slice[char.lo - first]' is read. If it is 0, there is */
264 /* no glyph for the charcode. Otherwise, the value of `delta' is */
265 /* added to it (modulo 65536) to form a new glyph index. */
266 /* */
267 /* It is up to the validation routine to check that all offsets fall */
268 /* within the glyph IDs table (and not within the `subs' table itself or */
269 /* outside of the CMap). */
270 /* */
272 #ifdef TT_CONFIG_CMAP_FORMAT_2
274 FT_CALLBACK_DEF( FT_Error )
275 tt_cmap2_validate( FT_Byte* table,
276 FT_Validator valid )
278 FT_Byte* p = table + 2; /* skip format */
279 FT_UInt length = TT_PEEK_USHORT( p );
280 FT_UInt n, max_subs;
281 FT_Byte* keys; /* keys table */
282 FT_Byte* subs; /* sub-headers */
283 FT_Byte* glyph_ids; /* glyph ID array */
286 if ( table + length > valid->limit || length < 6 + 512 )
287 FT_INVALID_TOO_SHORT;
289 keys = table + 6;
291 /* parse keys to compute sub-headers count */
292 p = keys;
293 max_subs = 0;
294 for ( n = 0; n < 256; n++ )
296 FT_UInt idx = TT_NEXT_USHORT( p );
299 /* value must be multiple of 8 */
300 if ( valid->level >= FT_VALIDATE_PARANOID && ( idx & 7 ) != 0 )
301 FT_INVALID_DATA;
303 idx >>= 3;
305 if ( idx > max_subs )
306 max_subs = idx;
309 FT_ASSERT( p == table + 518 );
311 subs = p;
312 glyph_ids = subs + (max_subs + 1) * 8;
313 if ( glyph_ids > valid->limit )
314 FT_INVALID_TOO_SHORT;
316 /* parse sub-headers */
317 for ( n = 0; n <= max_subs; n++ )
319 FT_UInt first_code, code_count, offset;
320 FT_Int delta;
321 FT_Byte* ids;
324 first_code = TT_NEXT_USHORT( p );
325 code_count = TT_NEXT_USHORT( p );
326 delta = TT_NEXT_SHORT( p );
327 offset = TT_NEXT_USHORT( p );
329 /* many Dynalab fonts have empty sub-headers */
330 if ( code_count == 0 )
331 continue;
333 /* check range within 0..255 */
334 if ( valid->level >= FT_VALIDATE_PARANOID )
336 if ( first_code >= 256 || first_code + code_count > 256 )
337 FT_INVALID_DATA;
340 /* check offset */
341 if ( offset != 0 )
343 ids = p - 2 + offset;
344 if ( ids < glyph_ids || ids + code_count*2 > table + length )
345 FT_INVALID_OFFSET;
347 /* check glyph IDs */
348 if ( valid->level >= FT_VALIDATE_TIGHT )
350 FT_Byte* limit = p + code_count * 2;
351 FT_UInt idx;
354 for ( ; p < limit; )
356 idx = TT_NEXT_USHORT( p );
357 if ( idx != 0 )
359 idx = ( idx + delta ) & 0xFFFFU;
360 if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
361 FT_INVALID_GLYPH_ID;
368 return SFNT_Err_Ok;
372 /* return sub header corresponding to a given character code */
373 /* NULL on invalid charcode */
374 static FT_Byte*
375 tt_cmap2_get_subheader( FT_Byte* table,
376 FT_UInt32 char_code )
378 FT_Byte* result = NULL;
381 if ( char_code < 0x10000UL )
383 FT_UInt char_lo = (FT_UInt)( char_code & 0xFF );
384 FT_UInt char_hi = (FT_UInt)( char_code >> 8 );
385 FT_Byte* p = table + 6; /* keys table */
386 FT_Byte* subs = table + 518; /* subheaders table */
387 FT_Byte* sub;
390 if ( char_hi == 0 )
392 /* an 8-bit character code -- we use subHeader 0 in this case */
393 /* to test whether the character code is in the charmap */
394 /* */
395 sub = subs; /* jump to first sub-header */
397 /* check that the sub-header for this byte is 0, which */
398 /* indicates that it is really a valid one-byte value */
399 /* Otherwise, return 0 */
400 /* */
401 p += char_lo * 2;
402 if ( TT_PEEK_USHORT( p ) != 0 )
403 goto Exit;
405 else
407 /* a 16-bit character code */
409 /* jump to key entry */
410 p += char_hi * 2;
411 /* jump to sub-header */
412 sub = subs + ( FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 8 ) );
414 /* check that the high byte isn't a valid one-byte value */
415 if ( sub == subs )
416 goto Exit;
418 result = sub;
420 Exit:
421 return result;
425 FT_CALLBACK_DEF( FT_UInt )
426 tt_cmap2_char_index( TT_CMap cmap,
427 FT_UInt32 char_code )
429 FT_Byte* table = cmap->data;
430 FT_UInt result = 0;
431 FT_Byte* subheader;
434 subheader = tt_cmap2_get_subheader( table, char_code );
435 if ( subheader )
437 FT_Byte* p = subheader;
438 FT_UInt idx = (FT_UInt)(char_code & 0xFF);
439 FT_UInt start, count;
440 FT_Int delta;
441 FT_UInt offset;
444 start = TT_NEXT_USHORT( p );
445 count = TT_NEXT_USHORT( p );
446 delta = TT_NEXT_SHORT ( p );
447 offset = TT_PEEK_USHORT( p );
449 idx -= start;
450 if ( idx < count && offset != 0 )
452 p += offset + 2 * idx;
453 idx = TT_PEEK_USHORT( p );
455 if ( idx != 0 )
456 result = (FT_UInt)( idx + delta ) & 0xFFFFU;
459 return result;
463 FT_CALLBACK_DEF( FT_UInt32 )
464 tt_cmap2_char_next( TT_CMap cmap,
465 FT_UInt32 *pcharcode )
467 FT_Byte* table = cmap->data;
468 FT_UInt gindex = 0;
469 FT_UInt32 result = 0;
470 FT_UInt32 charcode = *pcharcode + 1;
471 FT_Byte* subheader;
474 while ( charcode < 0x10000UL )
476 subheader = tt_cmap2_get_subheader( table, charcode );
477 if ( subheader )
479 FT_Byte* p = subheader;
480 FT_UInt start = TT_NEXT_USHORT( p );
481 FT_UInt count = TT_NEXT_USHORT( p );
482 FT_Int delta = TT_NEXT_SHORT ( p );
483 FT_UInt offset = TT_PEEK_USHORT( p );
484 FT_UInt char_lo = (FT_UInt)( charcode & 0xFF );
485 FT_UInt pos, idx;
488 if ( offset == 0 )
489 goto Next_SubHeader;
491 if ( char_lo < start )
493 char_lo = start;
494 pos = 0;
496 else
497 pos = (FT_UInt)( char_lo - start );
499 p += offset + pos * 2;
500 charcode = FT_PAD_FLOOR( charcode, 256 ) + char_lo;
502 for ( ; pos < count; pos++, charcode++ )
504 idx = TT_NEXT_USHORT( p );
506 if ( idx != 0 )
508 gindex = ( idx + delta ) & 0xFFFFU;
509 if ( gindex != 0 )
511 result = charcode;
512 goto Exit;
518 /* jump to next sub-header, i.e. higher byte value */
519 Next_SubHeader:
520 charcode = FT_PAD_FLOOR( charcode, 256 ) + 256;
523 Exit:
524 *pcharcode = result;
526 return gindex;
530 FT_CALLBACK_DEF( FT_Error )
531 tt_cmap2_get_info( TT_CMap cmap,
532 TT_CMapInfo *cmap_info )
534 FT_Byte* p = cmap->data + 4;
537 cmap_info->format = 2;
538 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
540 return SFNT_Err_Ok;
544 FT_DEFINE_TT_CMAP(tt_cmap2_class_rec,
545 sizeof ( TT_CMapRec ),
547 (FT_CMap_InitFunc) tt_cmap_init,
548 (FT_CMap_DoneFunc) NULL,
549 (FT_CMap_CharIndexFunc)tt_cmap2_char_index,
550 (FT_CMap_CharNextFunc) tt_cmap2_char_next,
552 NULL, NULL, NULL, NULL, NULL
555 (TT_CMap_ValidateFunc) tt_cmap2_validate,
556 (TT_CMap_Info_GetFunc) tt_cmap2_get_info
559 #endif /* TT_CONFIG_CMAP_FORMAT_2 */
562 /*************************************************************************/
563 /*************************************************************************/
564 /***** *****/
565 /***** FORMAT 4 *****/
566 /***** *****/
567 /*************************************************************************/
568 /*************************************************************************/
570 /*************************************************************************/
571 /* */
572 /* TABLE OVERVIEW */
573 /* -------------- */
574 /* */
575 /* NAME OFFSET TYPE DESCRIPTION */
576 /* */
577 /* format 0 USHORT must be 4 */
578 /* length 2 USHORT table length */
579 /* in bytes */
580 /* language 4 USHORT Mac language code */
581 /* */
582 /* segCountX2 6 USHORT 2*NUM_SEGS */
583 /* searchRange 8 USHORT 2*(1 << LOG_SEGS) */
584 /* entrySelector 10 USHORT LOG_SEGS */
585 /* rangeShift 12 USHORT segCountX2 - */
586 /* searchRange */
587 /* */
588 /* endCount 14 USHORT[NUM_SEGS] end charcode for */
589 /* each segment; last */
590 /* is 0xFFFF */
591 /* */
592 /* pad 14+NUM_SEGS*2 USHORT padding */
593 /* */
594 /* startCount 16+NUM_SEGS*2 USHORT[NUM_SEGS] first charcode for */
595 /* each segment */
596 /* */
597 /* idDelta 16+NUM_SEGS*4 SHORT[NUM_SEGS] delta for each */
598 /* segment */
599 /* idOffset 16+NUM_SEGS*6 SHORT[NUM_SEGS] range offset for */
600 /* each segment; can be */
601 /* zero */
602 /* */
603 /* glyphIds 16+NUM_SEGS*8 USHORT[] array of glyph ID */
604 /* ranges */
605 /* */
606 /* Character codes are modelled by a series of ordered (increasing) */
607 /* intervals called segments. Each segment has start and end codes, */
608 /* provided by the `startCount' and `endCount' arrays. Segments must */
609 /* not overlap, and the last segment should always contain the value */
610 /* 0xFFFF for `endCount'. */
611 /* */
612 /* The fields `searchRange', `entrySelector' and `rangeShift' are better */
613 /* ignored (they are traces of over-engineering in the TrueType */
614 /* specification). */
615 /* */
616 /* Each segment also has a signed `delta', as well as an optional offset */
617 /* within the `glyphIds' table. */
618 /* */
619 /* If a segment's idOffset is 0, the glyph index corresponding to any */
620 /* charcode within the segment is obtained by adding the value of */
621 /* `idDelta' directly to the charcode, modulo 65536. */
622 /* */
623 /* Otherwise, a glyph index is taken from the glyph IDs sub-array for */
624 /* the segment, and the value of `idDelta' is added to it. */
625 /* */
626 /* */
627 /* Finally, note that a lot of fonts contain an invalid last segment, */
628 /* where `start' and `end' are correctly set to 0xFFFF but both `delta' */
629 /* and `offset' are incorrect (e.g., `opens___.ttf' which comes with */
630 /* OpenOffice.org). We need special code to deal with them correctly. */
631 /* */
633 #ifdef TT_CONFIG_CMAP_FORMAT_4
635 typedef struct TT_CMap4Rec_
637 TT_CMapRec cmap;
638 FT_UInt32 cur_charcode; /* current charcode */
639 FT_UInt cur_gindex; /* current glyph index */
641 FT_UInt num_ranges;
642 FT_UInt cur_range;
643 FT_UInt cur_start;
644 FT_UInt cur_end;
645 FT_Int cur_delta;
646 FT_Byte* cur_values;
648 } TT_CMap4Rec, *TT_CMap4;
651 FT_CALLBACK_DEF( FT_Error )
652 tt_cmap4_init( TT_CMap4 cmap,
653 FT_Byte* table )
655 FT_Byte* p;
658 cmap->cmap.data = table;
660 p = table + 6;
661 cmap->num_ranges = FT_PEEK_USHORT( p ) >> 1;
662 cmap->cur_charcode = (FT_UInt32)0xFFFFFFFFUL;
663 cmap->cur_gindex = 0;
665 return SFNT_Err_Ok;
669 static FT_Int
670 tt_cmap4_set_range( TT_CMap4 cmap,
671 FT_UInt range_index )
673 FT_Byte* table = cmap->cmap.data;
674 FT_Byte* p;
675 FT_UInt num_ranges = cmap->num_ranges;
678 while ( range_index < num_ranges )
680 FT_UInt offset;
683 p = table + 14 + range_index * 2;
684 cmap->cur_end = FT_PEEK_USHORT( p );
686 p += 2 + num_ranges * 2;
687 cmap->cur_start = FT_PEEK_USHORT( p );
689 p += num_ranges * 2;
690 cmap->cur_delta = FT_PEEK_SHORT( p );
692 p += num_ranges * 2;
693 offset = FT_PEEK_USHORT( p );
695 /* some fonts have an incorrect last segment; */
696 /* we have to catch it */
697 if ( range_index >= num_ranges - 1 &&
698 cmap->cur_start == 0xFFFFU &&
699 cmap->cur_end == 0xFFFFU )
701 TT_Face face = (TT_Face)cmap->cmap.cmap.charmap.face;
702 FT_Byte* limit = face->cmap_table + face->cmap_size;
705 if ( offset && p + offset + 2 > limit )
707 cmap->cur_delta = 1;
708 offset = 0;
712 if ( offset != 0xFFFFU )
714 cmap->cur_values = offset ? p + offset : NULL;
715 cmap->cur_range = range_index;
716 return 0;
719 /* we skip empty segments */
720 range_index++;
723 return -1;
727 /* search the index of the charcode next to cmap->cur_charcode; */
728 /* caller should call tt_cmap4_set_range with proper range */
729 /* before calling this function */
730 /* */
731 static void
732 tt_cmap4_next( TT_CMap4 cmap )
734 FT_UInt charcode;
737 if ( cmap->cur_charcode >= 0xFFFFUL )
738 goto Fail;
740 charcode = (FT_UInt)cmap->cur_charcode + 1;
742 if ( charcode < cmap->cur_start )
743 charcode = cmap->cur_start;
745 for ( ;; )
747 FT_Byte* values = cmap->cur_values;
748 FT_UInt end = cmap->cur_end;
749 FT_Int delta = cmap->cur_delta;
752 if ( charcode <= end )
754 if ( values )
756 FT_Byte* p = values + 2 * ( charcode - cmap->cur_start );
761 FT_UInt gindex = FT_NEXT_USHORT( p );
764 if ( gindex != 0 )
766 gindex = (FT_UInt)( ( gindex + delta ) & 0xFFFFU );
767 if ( gindex != 0 )
769 cmap->cur_charcode = charcode;
770 cmap->cur_gindex = gindex;
771 return;
774 } while ( ++charcode <= end );
776 else
780 FT_UInt gindex = (FT_UInt)( ( charcode + delta ) & 0xFFFFU );
783 if ( gindex != 0 )
785 cmap->cur_charcode = charcode;
786 cmap->cur_gindex = gindex;
787 return;
789 } while ( ++charcode <= end );
793 /* we need to find another range */
794 if ( tt_cmap4_set_range( cmap, cmap->cur_range + 1 ) < 0 )
795 break;
797 if ( charcode < cmap->cur_start )
798 charcode = cmap->cur_start;
801 Fail:
802 cmap->cur_charcode = (FT_UInt32)0xFFFFFFFFUL;
803 cmap->cur_gindex = 0;
807 FT_CALLBACK_DEF( FT_Error )
808 tt_cmap4_validate( FT_Byte* table,
809 FT_Validator valid )
811 FT_Byte* p = table + 2; /* skip format */
812 FT_UInt length = TT_NEXT_USHORT( p );
813 FT_Byte *ends, *starts, *offsets, *deltas, *glyph_ids;
814 FT_UInt num_segs;
815 FT_Error error = SFNT_Err_Ok;
818 if ( length < 16 )
819 FT_INVALID_TOO_SHORT;
821 /* in certain fonts, the `length' field is invalid and goes */
822 /* out of bound. We try to correct this here... */
823 if ( table + length > valid->limit )
825 if ( valid->level >= FT_VALIDATE_TIGHT )
826 FT_INVALID_TOO_SHORT;
828 length = (FT_UInt)( valid->limit - table );
831 p = table + 6;
832 num_segs = TT_NEXT_USHORT( p ); /* read segCountX2 */
834 if ( valid->level >= FT_VALIDATE_PARANOID )
836 /* check that we have an even value here */
837 if ( num_segs & 1 )
838 FT_INVALID_DATA;
841 num_segs /= 2;
843 if ( length < 16 + num_segs * 2 * 4 )
844 FT_INVALID_TOO_SHORT;
846 /* check the search parameters - even though we never use them */
847 /* */
848 if ( valid->level >= FT_VALIDATE_PARANOID )
850 /* check the values of `searchRange', `entrySelector', `rangeShift' */
851 FT_UInt search_range = TT_NEXT_USHORT( p );
852 FT_UInt entry_selector = TT_NEXT_USHORT( p );
853 FT_UInt range_shift = TT_NEXT_USHORT( p );
856 if ( ( search_range | range_shift ) & 1 ) /* must be even values */
857 FT_INVALID_DATA;
859 search_range /= 2;
860 range_shift /= 2;
862 /* `search range' is the greatest power of 2 that is <= num_segs */
864 if ( search_range > num_segs ||
865 search_range * 2 < num_segs ||
866 search_range + range_shift != num_segs ||
867 search_range != ( 1U << entry_selector ) )
868 FT_INVALID_DATA;
871 ends = table + 14;
872 starts = table + 16 + num_segs * 2;
873 deltas = starts + num_segs * 2;
874 offsets = deltas + num_segs * 2;
875 glyph_ids = offsets + num_segs * 2;
877 /* check last segment; its end count value must be 0xFFFF */
878 if ( valid->level >= FT_VALIDATE_PARANOID )
880 p = ends + ( num_segs - 1 ) * 2;
881 if ( TT_PEEK_USHORT( p ) != 0xFFFFU )
882 FT_INVALID_DATA;
886 FT_UInt start, end, offset, n;
887 FT_UInt last_start = 0, last_end = 0;
888 FT_Int delta;
889 FT_Byte* p_start = starts;
890 FT_Byte* p_end = ends;
891 FT_Byte* p_delta = deltas;
892 FT_Byte* p_offset = offsets;
895 for ( n = 0; n < num_segs; n++ )
897 p = p_offset;
898 start = TT_NEXT_USHORT( p_start );
899 end = TT_NEXT_USHORT( p_end );
900 delta = TT_NEXT_SHORT( p_delta );
901 offset = TT_NEXT_USHORT( p_offset );
903 if ( start > end )
904 FT_INVALID_DATA;
906 /* this test should be performed at default validation level; */
907 /* unfortunately, some popular Asian fonts have overlapping */
908 /* ranges in their charmaps */
909 /* */
910 if ( start <= last_end && n > 0 )
912 if ( valid->level >= FT_VALIDATE_TIGHT )
913 FT_INVALID_DATA;
914 else
916 /* allow overlapping segments, provided their start points */
917 /* and end points, respectively, are in ascending order */
918 /* */
919 if ( last_start > start || last_end > end )
920 error |= TT_CMAP_FLAG_UNSORTED;
921 else
922 error |= TT_CMAP_FLAG_OVERLAPPING;
926 if ( offset && offset != 0xFFFFU )
928 p += offset; /* start of glyph ID array */
930 /* check that we point within the glyph IDs table only */
931 if ( valid->level >= FT_VALIDATE_TIGHT )
933 if ( p < glyph_ids ||
934 p + ( end - start + 1 ) * 2 > table + length )
935 FT_INVALID_DATA;
937 /* Some fonts handle the last segment incorrectly. In */
938 /* theory, 0xFFFF might point to an ordinary glyph -- */
939 /* a cmap 4 is versatile and could be used for any */
940 /* encoding, not only Unicode. However, reality shows */
941 /* that far too many fonts are sloppy and incorrectly */
942 /* set all fields but `start' and `end' for the last */
943 /* segment if it contains only a single character. */
944 /* */
945 /* We thus omit the test here, delaying it to the */
946 /* routines which actually access the cmap. */
947 else if ( n != num_segs - 1 ||
948 !( start == 0xFFFFU && end == 0xFFFFU ) )
950 if ( p < glyph_ids ||
951 p + ( end - start + 1 ) * 2 > valid->limit )
952 FT_INVALID_DATA;
955 /* check glyph indices within the segment range */
956 if ( valid->level >= FT_VALIDATE_TIGHT )
958 FT_UInt i, idx;
961 for ( i = start; i < end; i++ )
963 idx = FT_NEXT_USHORT( p );
964 if ( idx != 0 )
966 idx = (FT_UInt)( idx + delta ) & 0xFFFFU;
968 if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
969 FT_INVALID_GLYPH_ID;
974 else if ( offset == 0xFFFFU )
976 /* some fonts (erroneously?) use a range offset of 0xFFFF */
977 /* to mean missing glyph in cmap table */
978 /* */
979 if ( valid->level >= FT_VALIDATE_PARANOID ||
980 n != num_segs - 1 ||
981 !( start == 0xFFFFU && end == 0xFFFFU ) )
982 FT_INVALID_DATA;
985 last_start = start;
986 last_end = end;
990 return error;
994 static FT_UInt
995 tt_cmap4_char_map_linear( TT_CMap cmap,
996 FT_UInt32* pcharcode,
997 FT_Bool next )
999 FT_UInt num_segs2, start, end, offset;
1000 FT_Int delta;
1001 FT_UInt i, num_segs;
1002 FT_UInt32 charcode = *pcharcode;
1003 FT_UInt gindex = 0;
1004 FT_Byte* p;
1007 p = cmap->data + 6;
1008 num_segs2 = FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 2 );
1010 num_segs = num_segs2 >> 1;
1012 if ( !num_segs )
1013 return 0;
1015 if ( next )
1016 charcode++;
1018 /* linear search */
1019 for ( ; charcode <= 0xFFFFU; charcode++ )
1021 FT_Byte* q;
1024 p = cmap->data + 14; /* ends table */
1025 q = cmap->data + 16 + num_segs2; /* starts table */
1027 for ( i = 0; i < num_segs; i++ )
1029 end = TT_NEXT_USHORT( p );
1030 start = TT_NEXT_USHORT( q );
1032 if ( charcode >= start && charcode <= end )
1034 p = q - 2 + num_segs2;
1035 delta = TT_PEEK_SHORT( p );
1036 p += num_segs2;
1037 offset = TT_PEEK_USHORT( p );
1039 /* some fonts have an incorrect last segment; */
1040 /* we have to catch it */
1041 if ( i >= num_segs - 1 &&
1042 start == 0xFFFFU && end == 0xFFFFU )
1044 TT_Face face = (TT_Face)cmap->cmap.charmap.face;
1045 FT_Byte* limit = face->cmap_table + face->cmap_size;
1048 if ( offset && p + offset + 2 > limit )
1050 delta = 1;
1051 offset = 0;
1055 if ( offset == 0xFFFFU )
1056 continue;
1058 if ( offset )
1060 p += offset + ( charcode - start ) * 2;
1061 gindex = TT_PEEK_USHORT( p );
1062 if ( gindex != 0 )
1063 gindex = (FT_UInt)( gindex + delta ) & 0xFFFFU;
1065 else
1066 gindex = (FT_UInt)( charcode + delta ) & 0xFFFFU;
1068 break;
1072 if ( !next || gindex )
1073 break;
1076 if ( next && gindex )
1077 *pcharcode = charcode;
1079 return gindex;
1083 static FT_UInt
1084 tt_cmap4_char_map_binary( TT_CMap cmap,
1085 FT_UInt32* pcharcode,
1086 FT_Bool next )
1088 FT_UInt num_segs2, start, end, offset;
1089 FT_Int delta;
1090 FT_UInt max, min, mid, num_segs;
1091 FT_UInt charcode = (FT_UInt)*pcharcode;
1092 FT_UInt gindex = 0;
1093 FT_Byte* p;
1096 p = cmap->data + 6;
1097 num_segs2 = FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 2 );
1099 if ( !num_segs2 )
1100 return 0;
1102 num_segs = num_segs2 >> 1;
1104 /* make compiler happy */
1105 mid = num_segs;
1106 end = 0xFFFFU;
1108 if ( next )
1109 charcode++;
1111 min = 0;
1112 max = num_segs;
1114 /* binary search */
1115 while ( min < max )
1117 mid = ( min + max ) >> 1;
1118 p = cmap->data + 14 + mid * 2;
1119 end = TT_PEEK_USHORT( p );
1120 p += 2 + num_segs2;
1121 start = TT_PEEK_USHORT( p );
1123 if ( charcode < start )
1124 max = mid;
1125 else if ( charcode > end )
1126 min = mid + 1;
1127 else
1129 p += num_segs2;
1130 delta = TT_PEEK_SHORT( p );
1131 p += num_segs2;
1132 offset = TT_PEEK_USHORT( p );
1134 /* some fonts have an incorrect last segment; */
1135 /* we have to catch it */
1136 if ( mid >= num_segs - 1 &&
1137 start == 0xFFFFU && end == 0xFFFFU )
1139 TT_Face face = (TT_Face)cmap->cmap.charmap.face;
1140 FT_Byte* limit = face->cmap_table + face->cmap_size;
1143 if ( offset && p + offset + 2 > limit )
1145 delta = 1;
1146 offset = 0;
1150 /* search the first segment containing `charcode' */
1151 if ( cmap->flags & TT_CMAP_FLAG_OVERLAPPING )
1153 FT_UInt i;
1156 /* call the current segment `max' */
1157 max = mid;
1159 if ( offset == 0xFFFFU )
1160 mid = max + 1;
1162 /* search in segments before the current segment */
1163 for ( i = max ; i > 0; i-- )
1165 FT_UInt prev_end;
1166 FT_Byte* old_p;
1169 old_p = p;
1170 p = cmap->data + 14 + ( i - 1 ) * 2;
1171 prev_end = TT_PEEK_USHORT( p );
1173 if ( charcode > prev_end )
1175 p = old_p;
1176 break;
1179 end = prev_end;
1180 p += 2 + num_segs2;
1181 start = TT_PEEK_USHORT( p );
1182 p += num_segs2;
1183 delta = TT_PEEK_SHORT( p );
1184 p += num_segs2;
1185 offset = TT_PEEK_USHORT( p );
1187 if ( offset != 0xFFFFU )
1188 mid = i - 1;
1191 /* no luck */
1192 if ( mid == max + 1 )
1194 if ( i != max )
1196 p = cmap->data + 14 + max * 2;
1197 end = TT_PEEK_USHORT( p );
1198 p += 2 + num_segs2;
1199 start = TT_PEEK_USHORT( p );
1200 p += num_segs2;
1201 delta = TT_PEEK_SHORT( p );
1202 p += num_segs2;
1203 offset = TT_PEEK_USHORT( p );
1206 mid = max;
1208 /* search in segments after the current segment */
1209 for ( i = max + 1; i < num_segs; i++ )
1211 FT_UInt next_end, next_start;
1214 p = cmap->data + 14 + i * 2;
1215 next_end = TT_PEEK_USHORT( p );
1216 p += 2 + num_segs2;
1217 next_start = TT_PEEK_USHORT( p );
1219 if ( charcode < next_start )
1220 break;
1222 end = next_end;
1223 start = next_start;
1224 p += num_segs2;
1225 delta = TT_PEEK_SHORT( p );
1226 p += num_segs2;
1227 offset = TT_PEEK_USHORT( p );
1229 if ( offset != 0xFFFFU )
1230 mid = i;
1232 i--;
1234 /* still no luck */
1235 if ( mid == max )
1237 mid = i;
1239 break;
1243 /* end, start, delta, and offset are for the i'th segment */
1244 if ( mid != i )
1246 p = cmap->data + 14 + mid * 2;
1247 end = TT_PEEK_USHORT( p );
1248 p += 2 + num_segs2;
1249 start = TT_PEEK_USHORT( p );
1250 p += num_segs2;
1251 delta = TT_PEEK_SHORT( p );
1252 p += num_segs2;
1253 offset = TT_PEEK_USHORT( p );
1256 else
1258 if ( offset == 0xFFFFU )
1259 break;
1262 if ( offset )
1264 p += offset + ( charcode - start ) * 2;
1265 gindex = TT_PEEK_USHORT( p );
1266 if ( gindex != 0 )
1267 gindex = (FT_UInt)( gindex + delta ) & 0xFFFFU;
1269 else
1270 gindex = (FT_UInt)( charcode + delta ) & 0xFFFFU;
1272 break;
1276 if ( next )
1278 TT_CMap4 cmap4 = (TT_CMap4)cmap;
1281 /* if `charcode' is not in any segment, then `mid' is */
1282 /* the segment nearest to `charcode' */
1283 /* */
1285 if ( charcode > end )
1287 mid++;
1288 if ( mid == num_segs )
1289 return 0;
1292 if ( tt_cmap4_set_range( cmap4, mid ) )
1294 if ( gindex )
1295 *pcharcode = charcode;
1297 else
1299 cmap4->cur_charcode = charcode;
1301 if ( gindex )
1302 cmap4->cur_gindex = gindex;
1303 else
1305 cmap4->cur_charcode = charcode;
1306 tt_cmap4_next( cmap4 );
1307 gindex = cmap4->cur_gindex;
1310 if ( gindex )
1311 *pcharcode = cmap4->cur_charcode;
1315 return gindex;
1319 FT_CALLBACK_DEF( FT_UInt )
1320 tt_cmap4_char_index( TT_CMap cmap,
1321 FT_UInt32 char_code )
1323 if ( char_code >= 0x10000UL )
1324 return 0;
1326 if ( cmap->flags & TT_CMAP_FLAG_UNSORTED )
1327 return tt_cmap4_char_map_linear( cmap, &char_code, 0 );
1328 else
1329 return tt_cmap4_char_map_binary( cmap, &char_code, 0 );
1333 FT_CALLBACK_DEF( FT_UInt32 )
1334 tt_cmap4_char_next( TT_CMap cmap,
1335 FT_UInt32 *pchar_code )
1337 FT_UInt gindex;
1340 if ( *pchar_code >= 0xFFFFU )
1341 return 0;
1343 if ( cmap->flags & TT_CMAP_FLAG_UNSORTED )
1344 gindex = tt_cmap4_char_map_linear( cmap, pchar_code, 1 );
1345 else
1347 TT_CMap4 cmap4 = (TT_CMap4)cmap;
1350 /* no need to search */
1351 if ( *pchar_code == cmap4->cur_charcode )
1353 tt_cmap4_next( cmap4 );
1354 gindex = cmap4->cur_gindex;
1355 if ( gindex )
1356 *pchar_code = cmap4->cur_charcode;
1358 else
1359 gindex = tt_cmap4_char_map_binary( cmap, pchar_code, 1 );
1362 return gindex;
1366 FT_CALLBACK_DEF( FT_Error )
1367 tt_cmap4_get_info( TT_CMap cmap,
1368 TT_CMapInfo *cmap_info )
1370 FT_Byte* p = cmap->data + 4;
1373 cmap_info->format = 4;
1374 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
1376 return SFNT_Err_Ok;
1380 FT_DEFINE_TT_CMAP(tt_cmap4_class_rec,
1381 sizeof ( TT_CMap4Rec ),
1382 (FT_CMap_InitFunc) tt_cmap4_init,
1383 (FT_CMap_DoneFunc) NULL,
1384 (FT_CMap_CharIndexFunc)tt_cmap4_char_index,
1385 (FT_CMap_CharNextFunc) tt_cmap4_char_next,
1387 NULL, NULL, NULL, NULL, NULL
1390 (TT_CMap_ValidateFunc) tt_cmap4_validate,
1391 (TT_CMap_Info_GetFunc) tt_cmap4_get_info
1394 #endif /* TT_CONFIG_CMAP_FORMAT_4 */
1397 /*************************************************************************/
1398 /*************************************************************************/
1399 /***** *****/
1400 /***** FORMAT 6 *****/
1401 /***** *****/
1402 /*************************************************************************/
1403 /*************************************************************************/
1405 /*************************************************************************/
1406 /* */
1407 /* TABLE OVERVIEW */
1408 /* -------------- */
1409 /* */
1410 /* NAME OFFSET TYPE DESCRIPTION */
1411 /* */
1412 /* format 0 USHORT must be 4 */
1413 /* length 2 USHORT table length in bytes */
1414 /* language 4 USHORT Mac language code */
1415 /* */
1416 /* first 6 USHORT first segment code */
1417 /* count 8 USHORT segment size in chars */
1418 /* glyphIds 10 USHORT[count] glyph IDs */
1419 /* */
1420 /* A very simplified segment mapping. */
1421 /* */
1423 #ifdef TT_CONFIG_CMAP_FORMAT_6
1425 FT_CALLBACK_DEF( FT_Error )
1426 tt_cmap6_validate( FT_Byte* table,
1427 FT_Validator valid )
1429 FT_Byte* p;
1430 FT_UInt length, count;
1433 if ( table + 10 > valid->limit )
1434 FT_INVALID_TOO_SHORT;
1436 p = table + 2;
1437 length = TT_NEXT_USHORT( p );
1439 p = table + 8; /* skip language and start index */
1440 count = TT_NEXT_USHORT( p );
1442 if ( table + length > valid->limit || length < 10 + count * 2 )
1443 FT_INVALID_TOO_SHORT;
1445 /* check glyph indices */
1446 if ( valid->level >= FT_VALIDATE_TIGHT )
1448 FT_UInt gindex;
1451 for ( ; count > 0; count-- )
1453 gindex = TT_NEXT_USHORT( p );
1454 if ( gindex >= TT_VALID_GLYPH_COUNT( valid ) )
1455 FT_INVALID_GLYPH_ID;
1459 return SFNT_Err_Ok;
1463 FT_CALLBACK_DEF( FT_UInt )
1464 tt_cmap6_char_index( TT_CMap cmap,
1465 FT_UInt32 char_code )
1467 FT_Byte* table = cmap->data;
1468 FT_UInt result = 0;
1469 FT_Byte* p = table + 6;
1470 FT_UInt start = TT_NEXT_USHORT( p );
1471 FT_UInt count = TT_NEXT_USHORT( p );
1472 FT_UInt idx = (FT_UInt)( char_code - start );
1475 if ( idx < count )
1477 p += 2 * idx;
1478 result = TT_PEEK_USHORT( p );
1480 return result;
1484 FT_CALLBACK_DEF( FT_UInt32 )
1485 tt_cmap6_char_next( TT_CMap cmap,
1486 FT_UInt32 *pchar_code )
1488 FT_Byte* table = cmap->data;
1489 FT_UInt32 result = 0;
1490 FT_UInt32 char_code = *pchar_code + 1;
1491 FT_UInt gindex = 0;
1493 FT_Byte* p = table + 6;
1494 FT_UInt start = TT_NEXT_USHORT( p );
1495 FT_UInt count = TT_NEXT_USHORT( p );
1496 FT_UInt idx;
1499 if ( char_code >= 0x10000UL )
1500 goto Exit;
1502 if ( char_code < start )
1503 char_code = start;
1505 idx = (FT_UInt)( char_code - start );
1506 p += 2 * idx;
1508 for ( ; idx < count; idx++ )
1510 gindex = TT_NEXT_USHORT( p );
1511 if ( gindex != 0 )
1513 result = char_code;
1514 break;
1516 char_code++;
1519 Exit:
1520 *pchar_code = result;
1521 return gindex;
1525 FT_CALLBACK_DEF( FT_Error )
1526 tt_cmap6_get_info( TT_CMap cmap,
1527 TT_CMapInfo *cmap_info )
1529 FT_Byte* p = cmap->data + 4;
1532 cmap_info->format = 6;
1533 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
1535 return SFNT_Err_Ok;
1539 FT_DEFINE_TT_CMAP(tt_cmap6_class_rec,
1540 sizeof ( TT_CMapRec ),
1542 (FT_CMap_InitFunc) tt_cmap_init,
1543 (FT_CMap_DoneFunc) NULL,
1544 (FT_CMap_CharIndexFunc)tt_cmap6_char_index,
1545 (FT_CMap_CharNextFunc) tt_cmap6_char_next,
1547 NULL, NULL, NULL, NULL, NULL
1550 (TT_CMap_ValidateFunc) tt_cmap6_validate,
1551 (TT_CMap_Info_GetFunc) tt_cmap6_get_info
1554 #endif /* TT_CONFIG_CMAP_FORMAT_6 */
1557 /*************************************************************************/
1558 /*************************************************************************/
1559 /***** *****/
1560 /***** FORMAT 8 *****/
1561 /***** *****/
1562 /***** It is hard to completely understand what the OpenType spec *****/
1563 /***** says about this format, but here is my conclusion. *****/
1564 /***** *****/
1565 /***** The purpose of this format is to easily map UTF-16 text to *****/
1566 /***** glyph indices. Basically, the `char_code' must be in one of *****/
1567 /***** the following formats: *****/
1568 /***** *****/
1569 /***** - A 16-bit value that isn't part of the Unicode Surrogates *****/
1570 /***** Area (i.e. U+D800-U+DFFF). *****/
1571 /***** *****/
1572 /***** - A 32-bit value, made of two surrogate values, i.e.. if *****/
1573 /***** `char_code = (char_hi << 16) | char_lo', then both *****/
1574 /***** `char_hi' and `char_lo' must be in the Surrogates Area. *****/
1575 /***** Area. *****/
1576 /***** *****/
1577 /***** The `is32' table embedded in the charmap indicates whether a *****/
1578 /***** given 16-bit value is in the surrogates area or not. *****/
1579 /***** *****/
1580 /***** So, for any given `char_code', we can assert the following: *****/
1581 /***** *****/
1582 /***** If `char_hi == 0' then we must have `is32[char_lo] == 0'. *****/
1583 /***** *****/
1584 /***** If `char_hi != 0' then we must have both *****/
1585 /***** `is32[char_hi] != 0' and `is32[char_lo] != 0'. *****/
1586 /***** *****/
1587 /*************************************************************************/
1588 /*************************************************************************/
1590 /*************************************************************************/
1591 /* */
1592 /* TABLE OVERVIEW */
1593 /* -------------- */
1594 /* */
1595 /* NAME OFFSET TYPE DESCRIPTION */
1596 /* */
1597 /* format 0 USHORT must be 8 */
1598 /* reserved 2 USHORT reserved */
1599 /* length 4 ULONG length in bytes */
1600 /* language 8 ULONG Mac language code */
1601 /* is32 12 BYTE[8192] 32-bitness bitmap */
1602 /* count 8204 ULONG number of groups */
1603 /* */
1604 /* This header is followed by `count' groups of the following format: */
1605 /* */
1606 /* start 0 ULONG first charcode */
1607 /* end 4 ULONG last charcode */
1608 /* startId 8 ULONG start glyph ID for the group */
1609 /* */
1611 #ifdef TT_CONFIG_CMAP_FORMAT_8
1613 FT_CALLBACK_DEF( FT_Error )
1614 tt_cmap8_validate( FT_Byte* table,
1615 FT_Validator valid )
1617 FT_Byte* p = table + 4;
1618 FT_Byte* is32;
1619 FT_UInt32 length;
1620 FT_UInt32 num_groups;
1623 if ( table + 16 + 8192 > valid->limit )
1624 FT_INVALID_TOO_SHORT;
1626 length = TT_NEXT_ULONG( p );
1627 if ( length > (FT_UInt32)( valid->limit - table ) || length < 8192 + 16 )
1628 FT_INVALID_TOO_SHORT;
1630 is32 = table + 12;
1631 p = is32 + 8192; /* skip `is32' array */
1632 num_groups = TT_NEXT_ULONG( p );
1634 if ( p + num_groups * 12 > valid->limit )
1635 FT_INVALID_TOO_SHORT;
1637 /* check groups, they must be in increasing order */
1639 FT_UInt32 n, start, end, start_id, count, last = 0;
1642 for ( n = 0; n < num_groups; n++ )
1644 FT_UInt hi, lo;
1647 start = TT_NEXT_ULONG( p );
1648 end = TT_NEXT_ULONG( p );
1649 start_id = TT_NEXT_ULONG( p );
1651 if ( start > end )
1652 FT_INVALID_DATA;
1654 if ( n > 0 && start <= last )
1655 FT_INVALID_DATA;
1657 if ( valid->level >= FT_VALIDATE_TIGHT )
1659 if ( start_id + end - start >= TT_VALID_GLYPH_COUNT( valid ) )
1660 FT_INVALID_GLYPH_ID;
1662 count = (FT_UInt32)( end - start + 1 );
1664 if ( start & ~0xFFFFU )
1666 /* start_hi != 0; check that is32[i] is 1 for each i in */
1667 /* the `hi' and `lo' of the range [start..end] */
1668 for ( ; count > 0; count--, start++ )
1670 hi = (FT_UInt)( start >> 16 );
1671 lo = (FT_UInt)( start & 0xFFFFU );
1673 if ( (is32[hi >> 3] & ( 0x80 >> ( hi & 7 ) ) ) == 0 )
1674 FT_INVALID_DATA;
1676 if ( (is32[lo >> 3] & ( 0x80 >> ( lo & 7 ) ) ) == 0 )
1677 FT_INVALID_DATA;
1680 else
1682 /* start_hi == 0; check that is32[i] is 0 for each i in */
1683 /* the range [start..end] */
1685 /* end_hi cannot be != 0! */
1686 if ( end & ~0xFFFFU )
1687 FT_INVALID_DATA;
1689 for ( ; count > 0; count--, start++ )
1691 lo = (FT_UInt)( start & 0xFFFFU );
1693 if ( (is32[lo >> 3] & ( 0x80 >> ( lo & 7 ) ) ) != 0 )
1694 FT_INVALID_DATA;
1699 last = end;
1703 return SFNT_Err_Ok;
1707 FT_CALLBACK_DEF( FT_UInt )
1708 tt_cmap8_char_index( TT_CMap cmap,
1709 FT_UInt32 char_code )
1711 FT_Byte* table = cmap->data;
1712 FT_UInt result = 0;
1713 FT_Byte* p = table + 8204;
1714 FT_UInt32 num_groups = TT_NEXT_ULONG( p );
1715 FT_UInt32 start, end, start_id;
1718 for ( ; num_groups > 0; num_groups-- )
1720 start = TT_NEXT_ULONG( p );
1721 end = TT_NEXT_ULONG( p );
1722 start_id = TT_NEXT_ULONG( p );
1724 if ( char_code < start )
1725 break;
1727 if ( char_code <= end )
1729 result = (FT_UInt)( start_id + char_code - start );
1730 break;
1733 return result;
1737 FT_CALLBACK_DEF( FT_UInt32 )
1738 tt_cmap8_char_next( TT_CMap cmap,
1739 FT_UInt32 *pchar_code )
1741 FT_UInt32 result = 0;
1742 FT_UInt32 char_code = *pchar_code + 1;
1743 FT_UInt gindex = 0;
1744 FT_Byte* table = cmap->data;
1745 FT_Byte* p = table + 8204;
1746 FT_UInt32 num_groups = TT_NEXT_ULONG( p );
1747 FT_UInt32 start, end, start_id;
1750 p = table + 8208;
1752 for ( ; num_groups > 0; num_groups-- )
1754 start = TT_NEXT_ULONG( p );
1755 end = TT_NEXT_ULONG( p );
1756 start_id = TT_NEXT_ULONG( p );
1758 if ( char_code < start )
1759 char_code = start;
1761 if ( char_code <= end )
1763 gindex = (FT_UInt)( char_code - start + start_id );
1764 if ( gindex != 0 )
1766 result = char_code;
1767 goto Exit;
1772 Exit:
1773 *pchar_code = result;
1774 return gindex;
1778 FT_CALLBACK_DEF( FT_Error )
1779 tt_cmap8_get_info( TT_CMap cmap,
1780 TT_CMapInfo *cmap_info )
1782 FT_Byte* p = cmap->data + 8;
1785 cmap_info->format = 8;
1786 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
1788 return SFNT_Err_Ok;
1792 FT_DEFINE_TT_CMAP(tt_cmap8_class_rec,
1793 sizeof ( TT_CMapRec ),
1795 (FT_CMap_InitFunc) tt_cmap_init,
1796 (FT_CMap_DoneFunc) NULL,
1797 (FT_CMap_CharIndexFunc)tt_cmap8_char_index,
1798 (FT_CMap_CharNextFunc) tt_cmap8_char_next,
1800 NULL, NULL, NULL, NULL, NULL
1803 (TT_CMap_ValidateFunc) tt_cmap8_validate,
1804 (TT_CMap_Info_GetFunc) tt_cmap8_get_info
1807 #endif /* TT_CONFIG_CMAP_FORMAT_8 */
1810 /*************************************************************************/
1811 /*************************************************************************/
1812 /***** *****/
1813 /***** FORMAT 10 *****/
1814 /***** *****/
1815 /*************************************************************************/
1816 /*************************************************************************/
1818 /*************************************************************************/
1819 /* */
1820 /* TABLE OVERVIEW */
1821 /* -------------- */
1822 /* */
1823 /* NAME OFFSET TYPE DESCRIPTION */
1824 /* */
1825 /* format 0 USHORT must be 10 */
1826 /* reserved 2 USHORT reserved */
1827 /* length 4 ULONG length in bytes */
1828 /* language 8 ULONG Mac language code */
1829 /* */
1830 /* start 12 ULONG first char in range */
1831 /* count 16 ULONG number of chars in range */
1832 /* glyphIds 20 USHORT[count] glyph indices covered */
1833 /* */
1835 #ifdef TT_CONFIG_CMAP_FORMAT_10
1837 FT_CALLBACK_DEF( FT_Error )
1838 tt_cmap10_validate( FT_Byte* table,
1839 FT_Validator valid )
1841 FT_Byte* p = table + 4;
1842 FT_ULong length, count;
1845 if ( table + 20 > valid->limit )
1846 FT_INVALID_TOO_SHORT;
1848 length = TT_NEXT_ULONG( p );
1849 p = table + 16;
1850 count = TT_NEXT_ULONG( p );
1852 if ( length > (FT_ULong)( valid->limit - table ) ||
1853 length < 20 + count * 2 )
1854 FT_INVALID_TOO_SHORT;
1856 /* check glyph indices */
1857 if ( valid->level >= FT_VALIDATE_TIGHT )
1859 FT_UInt gindex;
1862 for ( ; count > 0; count-- )
1864 gindex = TT_NEXT_USHORT( p );
1865 if ( gindex >= TT_VALID_GLYPH_COUNT( valid ) )
1866 FT_INVALID_GLYPH_ID;
1870 return SFNT_Err_Ok;
1874 FT_CALLBACK_DEF( FT_UInt )
1875 tt_cmap10_char_index( TT_CMap cmap,
1876 FT_UInt32 char_code )
1878 FT_Byte* table = cmap->data;
1879 FT_UInt result = 0;
1880 FT_Byte* p = table + 12;
1881 FT_UInt32 start = TT_NEXT_ULONG( p );
1882 FT_UInt32 count = TT_NEXT_ULONG( p );
1883 FT_UInt32 idx = (FT_ULong)( char_code - start );
1886 if ( idx < count )
1888 p += 2 * idx;
1889 result = TT_PEEK_USHORT( p );
1891 return result;
1895 FT_CALLBACK_DEF( FT_UInt32 )
1896 tt_cmap10_char_next( TT_CMap cmap,
1897 FT_UInt32 *pchar_code )
1899 FT_Byte* table = cmap->data;
1900 FT_UInt32 char_code = *pchar_code + 1;
1901 FT_UInt gindex = 0;
1902 FT_Byte* p = table + 12;
1903 FT_UInt32 start = TT_NEXT_ULONG( p );
1904 FT_UInt32 count = TT_NEXT_ULONG( p );
1905 FT_UInt32 idx;
1908 if ( char_code < start )
1909 char_code = start;
1911 idx = (FT_UInt32)( char_code - start );
1912 p += 2 * idx;
1914 for ( ; idx < count; idx++ )
1916 gindex = TT_NEXT_USHORT( p );
1917 if ( gindex != 0 )
1918 break;
1919 char_code++;
1922 *pchar_code = char_code;
1923 return gindex;
1927 FT_CALLBACK_DEF( FT_Error )
1928 tt_cmap10_get_info( TT_CMap cmap,
1929 TT_CMapInfo *cmap_info )
1931 FT_Byte* p = cmap->data + 8;
1934 cmap_info->format = 10;
1935 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
1937 return SFNT_Err_Ok;
1941 FT_DEFINE_TT_CMAP(tt_cmap10_class_rec,
1942 sizeof ( TT_CMapRec ),
1944 (FT_CMap_InitFunc) tt_cmap_init,
1945 (FT_CMap_DoneFunc) NULL,
1946 (FT_CMap_CharIndexFunc)tt_cmap10_char_index,
1947 (FT_CMap_CharNextFunc) tt_cmap10_char_next,
1949 NULL, NULL, NULL, NULL, NULL
1952 (TT_CMap_ValidateFunc) tt_cmap10_validate,
1953 (TT_CMap_Info_GetFunc) tt_cmap10_get_info
1956 #endif /* TT_CONFIG_CMAP_FORMAT_10 */
1959 /*************************************************************************/
1960 /*************************************************************************/
1961 /***** *****/
1962 /***** FORMAT 12 *****/
1963 /***** *****/
1964 /*************************************************************************/
1965 /*************************************************************************/
1967 /*************************************************************************/
1968 /* */
1969 /* TABLE OVERVIEW */
1970 /* -------------- */
1971 /* */
1972 /* NAME OFFSET TYPE DESCRIPTION */
1973 /* */
1974 /* format 0 USHORT must be 12 */
1975 /* reserved 2 USHORT reserved */
1976 /* length 4 ULONG length in bytes */
1977 /* language 8 ULONG Mac language code */
1978 /* count 12 ULONG number of groups */
1979 /* 16 */
1980 /* */
1981 /* This header is followed by `count' groups of the following format: */
1982 /* */
1983 /* start 0 ULONG first charcode */
1984 /* end 4 ULONG last charcode */
1985 /* startId 8 ULONG start glyph ID for the group */
1986 /* */
1988 #ifdef TT_CONFIG_CMAP_FORMAT_12
1990 typedef struct TT_CMap12Rec_
1992 TT_CMapRec cmap;
1993 FT_Bool valid;
1994 FT_ULong cur_charcode;
1995 FT_UInt cur_gindex;
1996 FT_ULong cur_group;
1997 FT_ULong num_groups;
1999 } TT_CMap12Rec, *TT_CMap12;
2002 FT_CALLBACK_DEF( FT_Error )
2003 tt_cmap12_init( TT_CMap12 cmap,
2004 FT_Byte* table )
2006 cmap->cmap.data = table;
2008 table += 12;
2009 cmap->num_groups = FT_PEEK_ULONG( table );
2011 cmap->valid = 0;
2013 return SFNT_Err_Ok;
2017 FT_CALLBACK_DEF( FT_Error )
2018 tt_cmap12_validate( FT_Byte* table,
2019 FT_Validator valid )
2021 FT_Byte* p;
2022 FT_ULong length;
2023 FT_ULong num_groups;
2026 if ( table + 16 > valid->limit )
2027 FT_INVALID_TOO_SHORT;
2029 p = table + 4;
2030 length = TT_NEXT_ULONG( p );
2032 p = table + 12;
2033 num_groups = TT_NEXT_ULONG( p );
2035 if ( length > (FT_ULong)( valid->limit - table ) ||
2036 length < 16 + 12 * num_groups )
2037 FT_INVALID_TOO_SHORT;
2039 /* check groups, they must be in increasing order */
2041 FT_ULong n, start, end, start_id, last = 0;
2044 for ( n = 0; n < num_groups; n++ )
2046 start = TT_NEXT_ULONG( p );
2047 end = TT_NEXT_ULONG( p );
2048 start_id = TT_NEXT_ULONG( p );
2050 if ( start > end )
2051 FT_INVALID_DATA;
2053 if ( n > 0 && start <= last )
2054 FT_INVALID_DATA;
2056 if ( valid->level >= FT_VALIDATE_TIGHT )
2058 if ( start_id + end - start >= TT_VALID_GLYPH_COUNT( valid ) )
2059 FT_INVALID_GLYPH_ID;
2062 last = end;
2066 return SFNT_Err_Ok;
2070 /* search the index of the charcode next to cmap->cur_charcode */
2071 /* cmap->cur_group should be set up properly by caller */
2072 /* */
2073 static void
2074 tt_cmap12_next( TT_CMap12 cmap )
2076 FT_Byte* p;
2077 FT_ULong start, end, start_id, char_code;
2078 FT_ULong n;
2079 FT_UInt gindex;
2082 if ( cmap->cur_charcode >= 0xFFFFFFFFUL )
2083 goto Fail;
2085 char_code = cmap->cur_charcode + 1;
2087 n = cmap->cur_group;
2089 for ( n = cmap->cur_group; n < cmap->num_groups; n++ )
2091 p = cmap->cmap.data + 16 + 12 * n;
2092 start = TT_NEXT_ULONG( p );
2093 end = TT_NEXT_ULONG( p );
2094 start_id = TT_PEEK_ULONG( p );
2096 if ( char_code < start )
2097 char_code = start;
2099 for ( ; char_code <= end; char_code++ )
2101 gindex = (FT_UInt)( start_id + char_code - start );
2103 if ( gindex )
2105 cmap->cur_charcode = char_code;;
2106 cmap->cur_gindex = gindex;
2107 cmap->cur_group = n;
2109 return;
2114 Fail:
2115 cmap->valid = 0;
2119 static FT_UInt
2120 tt_cmap12_char_map_binary( TT_CMap cmap,
2121 FT_UInt32* pchar_code,
2122 FT_Bool next )
2124 FT_UInt gindex = 0;
2125 FT_Byte* p = cmap->data + 12;
2126 FT_UInt32 num_groups = TT_PEEK_ULONG( p );
2127 FT_UInt32 char_code = *pchar_code;
2128 FT_UInt32 start, end, start_id;
2129 FT_UInt32 max, min, mid;
2132 if ( !num_groups )
2133 return 0;
2135 /* make compiler happy */
2136 mid = num_groups;
2137 end = 0xFFFFFFFFUL;
2139 if ( next )
2140 char_code++;
2142 min = 0;
2143 max = num_groups;
2145 /* binary search */
2146 while ( min < max )
2148 mid = ( min + max ) >> 1;
2149 p = cmap->data + 16 + 12 * mid;
2151 start = TT_NEXT_ULONG( p );
2152 end = TT_NEXT_ULONG( p );
2154 if ( char_code < start )
2155 max = mid;
2156 else if ( char_code > end )
2157 min = mid + 1;
2158 else
2160 start_id = TT_PEEK_ULONG( p );
2161 gindex = (FT_UInt)( start_id + char_code - start );
2163 break;
2167 if ( next )
2169 TT_CMap12 cmap12 = (TT_CMap12)cmap;
2172 /* if `char_code' is not in any group, then `mid' is */
2173 /* the group nearest to `char_code' */
2174 /* */
2176 if ( char_code > end )
2178 mid++;
2179 if ( mid == num_groups )
2180 return 0;
2183 cmap12->valid = 1;
2184 cmap12->cur_charcode = char_code;
2185 cmap12->cur_group = mid;
2187 if ( !gindex )
2189 tt_cmap12_next( cmap12 );
2191 if ( cmap12->valid )
2192 gindex = cmap12->cur_gindex;
2194 else
2195 cmap12->cur_gindex = gindex;
2197 if ( gindex )
2198 *pchar_code = cmap12->cur_charcode;
2201 return gindex;
2205 FT_CALLBACK_DEF( FT_UInt )
2206 tt_cmap12_char_index( TT_CMap cmap,
2207 FT_UInt32 char_code )
2209 return tt_cmap12_char_map_binary( cmap, &char_code, 0 );
2213 FT_CALLBACK_DEF( FT_UInt32 )
2214 tt_cmap12_char_next( TT_CMap cmap,
2215 FT_UInt32 *pchar_code )
2217 TT_CMap12 cmap12 = (TT_CMap12)cmap;
2218 FT_ULong gindex;
2221 if ( cmap12->cur_charcode >= 0xFFFFFFFFUL )
2222 return 0;
2224 /* no need to search */
2225 if ( cmap12->valid && cmap12->cur_charcode == *pchar_code )
2227 tt_cmap12_next( cmap12 );
2228 if ( cmap12->valid )
2230 gindex = cmap12->cur_gindex;
2232 /* XXX: check cur_charcode overflow is expected */
2233 if ( gindex )
2234 *pchar_code = (FT_UInt32)cmap12->cur_charcode;
2236 else
2237 gindex = 0;
2239 else
2240 gindex = tt_cmap12_char_map_binary( cmap, pchar_code, 1 );
2242 /* XXX: check gindex overflow is expected */
2243 return (FT_UInt32)gindex;
2247 FT_CALLBACK_DEF( FT_Error )
2248 tt_cmap12_get_info( TT_CMap cmap,
2249 TT_CMapInfo *cmap_info )
2251 FT_Byte* p = cmap->data + 8;
2254 cmap_info->format = 12;
2255 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
2257 return SFNT_Err_Ok;
2261 FT_DEFINE_TT_CMAP(tt_cmap12_class_rec,
2262 sizeof ( TT_CMap12Rec ),
2264 (FT_CMap_InitFunc) tt_cmap12_init,
2265 (FT_CMap_DoneFunc) NULL,
2266 (FT_CMap_CharIndexFunc)tt_cmap12_char_index,
2267 (FT_CMap_CharNextFunc) tt_cmap12_char_next,
2269 NULL, NULL, NULL, NULL, NULL
2272 (TT_CMap_ValidateFunc) tt_cmap12_validate,
2273 (TT_CMap_Info_GetFunc) tt_cmap12_get_info
2276 #endif /* TT_CONFIG_CMAP_FORMAT_12 */
2279 /*************************************************************************/
2280 /*************************************************************************/
2281 /***** *****/
2282 /***** FORMAT 13 *****/
2283 /***** *****/
2284 /*************************************************************************/
2285 /*************************************************************************/
2287 /*************************************************************************/
2288 /* */
2289 /* TABLE OVERVIEW */
2290 /* -------------- */
2291 /* */
2292 /* NAME OFFSET TYPE DESCRIPTION */
2293 /* */
2294 /* format 0 USHORT must be 13 */
2295 /* reserved 2 USHORT reserved */
2296 /* length 4 ULONG length in bytes */
2297 /* language 8 ULONG Mac language code */
2298 /* count 12 ULONG number of groups */
2299 /* 16 */
2300 /* */
2301 /* This header is followed by `count' groups of the following format: */
2302 /* */
2303 /* start 0 ULONG first charcode */
2304 /* end 4 ULONG last charcode */
2305 /* glyphId 8 ULONG glyph ID for the whole group */
2306 /* */
2308 #ifdef TT_CONFIG_CMAP_FORMAT_13
2310 typedef struct TT_CMap13Rec_
2312 TT_CMapRec cmap;
2313 FT_Bool valid;
2314 FT_ULong cur_charcode;
2315 FT_UInt cur_gindex;
2316 FT_ULong cur_group;
2317 FT_ULong num_groups;
2319 } TT_CMap13Rec, *TT_CMap13;
2322 FT_CALLBACK_DEF( FT_Error )
2323 tt_cmap13_init( TT_CMap13 cmap,
2324 FT_Byte* table )
2326 cmap->cmap.data = table;
2328 table += 12;
2329 cmap->num_groups = FT_PEEK_ULONG( table );
2331 cmap->valid = 0;
2333 return SFNT_Err_Ok;
2337 FT_CALLBACK_DEF( FT_Error )
2338 tt_cmap13_validate( FT_Byte* table,
2339 FT_Validator valid )
2341 FT_Byte* p;
2342 FT_ULong length;
2343 FT_ULong num_groups;
2346 if ( table + 16 > valid->limit )
2347 FT_INVALID_TOO_SHORT;
2349 p = table + 4;
2350 length = TT_NEXT_ULONG( p );
2352 p = table + 12;
2353 num_groups = TT_NEXT_ULONG( p );
2355 if ( length > (FT_ULong)( valid->limit - table ) ||
2356 length < 16 + 12 * num_groups )
2357 FT_INVALID_TOO_SHORT;
2359 /* check groups, they must be in increasing order */
2361 FT_ULong n, start, end, glyph_id, last = 0;
2364 for ( n = 0; n < num_groups; n++ )
2366 start = TT_NEXT_ULONG( p );
2367 end = TT_NEXT_ULONG( p );
2368 glyph_id = TT_NEXT_ULONG( p );
2370 if ( start > end )
2371 FT_INVALID_DATA;
2373 if ( n > 0 && start <= last )
2374 FT_INVALID_DATA;
2376 if ( valid->level >= FT_VALIDATE_TIGHT )
2378 if ( glyph_id >= TT_VALID_GLYPH_COUNT( valid ) )
2379 FT_INVALID_GLYPH_ID;
2382 last = end;
2386 return SFNT_Err_Ok;
2390 /* search the index of the charcode next to cmap->cur_charcode */
2391 /* cmap->cur_group should be set up properly by caller */
2392 /* */
2393 static void
2394 tt_cmap13_next( TT_CMap13 cmap )
2396 FT_Byte* p;
2397 FT_ULong start, end, glyph_id, char_code;
2398 FT_ULong n;
2399 FT_UInt gindex;
2402 if ( cmap->cur_charcode >= 0xFFFFFFFFUL )
2403 goto Fail;
2405 char_code = cmap->cur_charcode + 1;
2407 n = cmap->cur_group;
2409 for ( n = cmap->cur_group; n < cmap->num_groups; n++ )
2411 p = cmap->cmap.data + 16 + 12 * n;
2412 start = TT_NEXT_ULONG( p );
2413 end = TT_NEXT_ULONG( p );
2414 glyph_id = TT_PEEK_ULONG( p );
2416 if ( char_code < start )
2417 char_code = start;
2419 if ( char_code <= end )
2421 gindex = (FT_UInt)glyph_id;
2423 if ( gindex )
2425 cmap->cur_charcode = char_code;;
2426 cmap->cur_gindex = gindex;
2427 cmap->cur_group = n;
2429 return;
2434 Fail:
2435 cmap->valid = 0;
2439 static FT_UInt
2440 tt_cmap13_char_map_binary( TT_CMap cmap,
2441 FT_UInt32* pchar_code,
2442 FT_Bool next )
2444 FT_UInt gindex = 0;
2445 FT_Byte* p = cmap->data + 12;
2446 FT_UInt32 num_groups = TT_PEEK_ULONG( p );
2447 FT_UInt32 char_code = *pchar_code;
2448 FT_UInt32 start, end;
2449 FT_UInt32 max, min, mid;
2452 if ( !num_groups )
2453 return 0;
2455 /* make compiler happy */
2456 mid = num_groups;
2457 end = 0xFFFFFFFFUL;
2459 if ( next )
2460 char_code++;
2462 min = 0;
2463 max = num_groups;
2465 /* binary search */
2466 while ( min < max )
2468 mid = ( min + max ) >> 1;
2469 p = cmap->data + 16 + 12 * mid;
2471 start = TT_NEXT_ULONG( p );
2472 end = TT_NEXT_ULONG( p );
2474 if ( char_code < start )
2475 max = mid;
2476 else if ( char_code > end )
2477 min = mid + 1;
2478 else
2480 gindex = (FT_UInt)TT_PEEK_ULONG( p );
2482 break;
2486 if ( next )
2488 TT_CMap13 cmap13 = (TT_CMap13)cmap;
2491 /* if `char_code' is not in any group, then `mid' is */
2492 /* the group nearest to `char_code' */
2493 /* */
2495 if ( char_code > end )
2497 mid++;
2498 if ( mid == num_groups )
2499 return 0;
2502 cmap13->valid = 1;
2503 cmap13->cur_charcode = char_code;
2504 cmap13->cur_group = mid;
2506 if ( !gindex )
2508 tt_cmap13_next( cmap13 );
2510 if ( cmap13->valid )
2511 gindex = cmap13->cur_gindex;
2513 else
2514 cmap13->cur_gindex = gindex;
2516 if ( gindex )
2517 *pchar_code = cmap13->cur_charcode;
2520 return gindex;
2524 FT_CALLBACK_DEF( FT_UInt )
2525 tt_cmap13_char_index( TT_CMap cmap,
2526 FT_UInt32 char_code )
2528 return tt_cmap13_char_map_binary( cmap, &char_code, 0 );
2532 FT_CALLBACK_DEF( FT_UInt32 )
2533 tt_cmap13_char_next( TT_CMap cmap,
2534 FT_UInt32 *pchar_code )
2536 TT_CMap13 cmap13 = (TT_CMap13)cmap;
2537 FT_UInt gindex;
2540 if ( cmap13->cur_charcode >= 0xFFFFFFFFUL )
2541 return 0;
2543 /* no need to search */
2544 if ( cmap13->valid && cmap13->cur_charcode == *pchar_code )
2546 tt_cmap13_next( cmap13 );
2547 if ( cmap13->valid )
2549 gindex = cmap13->cur_gindex;
2550 if ( gindex )
2551 *pchar_code = cmap13->cur_charcode;
2553 else
2554 gindex = 0;
2556 else
2557 gindex = tt_cmap13_char_map_binary( cmap, pchar_code, 1 );
2559 return gindex;
2563 FT_CALLBACK_DEF( FT_Error )
2564 tt_cmap13_get_info( TT_CMap cmap,
2565 TT_CMapInfo *cmap_info )
2567 FT_Byte* p = cmap->data + 8;
2570 cmap_info->format = 13;
2571 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
2573 return SFNT_Err_Ok;
2577 FT_DEFINE_TT_CMAP(tt_cmap13_class_rec,
2578 sizeof ( TT_CMap13Rec ),
2580 (FT_CMap_InitFunc) tt_cmap13_init,
2581 (FT_CMap_DoneFunc) NULL,
2582 (FT_CMap_CharIndexFunc)tt_cmap13_char_index,
2583 (FT_CMap_CharNextFunc) tt_cmap13_char_next,
2585 NULL, NULL, NULL, NULL, NULL
2588 (TT_CMap_ValidateFunc) tt_cmap13_validate,
2589 (TT_CMap_Info_GetFunc) tt_cmap13_get_info
2592 #endif /* TT_CONFIG_CMAP_FORMAT_13 */
2595 /*************************************************************************/
2596 /*************************************************************************/
2597 /***** *****/
2598 /***** FORMAT 14 *****/
2599 /***** *****/
2600 /*************************************************************************/
2601 /*************************************************************************/
2603 /*************************************************************************/
2604 /* */
2605 /* TABLE OVERVIEW */
2606 /* -------------- */
2607 /* */
2608 /* NAME OFFSET TYPE DESCRIPTION */
2609 /* */
2610 /* format 0 USHORT must be 14 */
2611 /* length 2 ULONG table length in bytes */
2612 /* numSelector 6 ULONG number of variation sel. records */
2613 /* */
2614 /* Followed by numSelector records, each of which looks like */
2615 /* */
2616 /* varSelector 0 UINT24 Unicode codepoint of sel. */
2617 /* defaultOff 3 ULONG offset to a default UVS table */
2618 /* describing any variants to be found in */
2619 /* the normal Unicode subtable. */
2620 /* nonDefOff 7 ULONG offset to a non-default UVS table */
2621 /* describing any variants not in the */
2622 /* standard cmap, with GIDs here */
2623 /* (either offset may be 0 NULL) */
2624 /* */
2625 /* Selectors are sorted by code point. */
2626 /* */
2627 /* A default Unicode Variation Selector (UVS) subtable is just a list of */
2628 /* ranges of code points which are to be found in the standard cmap. No */
2629 /* glyph IDs (GIDs) here. */
2630 /* */
2631 /* numRanges 0 ULONG number of ranges following */
2632 /* */
2633 /* A range looks like */
2634 /* */
2635 /* uniStart 0 UINT24 code point of the first character in */
2636 /* this range */
2637 /* additionalCnt 3 UBYTE count of additional characters in this */
2638 /* range (zero means a range of a single */
2639 /* character) */
2640 /* */
2641 /* Ranges are sorted by `uniStart'. */
2642 /* */
2643 /* A non-default Unicode Variation Selector (UVS) subtable is a list of */
2644 /* mappings from codepoint to GID. */
2645 /* */
2646 /* numMappings 0 ULONG number of mappings */
2647 /* */
2648 /* A range looks like */
2649 /* */
2650 /* uniStart 0 UINT24 code point of the first character in */
2651 /* this range */
2652 /* GID 3 USHORT and its GID */
2653 /* */
2654 /* Ranges are sorted by `uniStart'. */
2656 #ifdef TT_CONFIG_CMAP_FORMAT_14
2658 typedef struct TT_CMap14Rec_
2660 TT_CMapRec cmap;
2661 FT_ULong num_selectors;
2663 /* This array is used to store the results of various
2664 * cmap 14 query functions. The data is overwritten
2665 * on each call to these functions.
2667 FT_UInt32 max_results;
2668 FT_UInt32* results;
2669 FT_Memory memory;
2671 } TT_CMap14Rec, *TT_CMap14;
2674 FT_CALLBACK_DEF( void )
2675 tt_cmap14_done( TT_CMap14 cmap )
2677 FT_Memory memory = cmap->memory;
2680 cmap->max_results = 0;
2681 if ( memory != NULL && cmap->results != NULL )
2682 FT_FREE( cmap->results );
2686 static FT_Error
2687 tt_cmap14_ensure( TT_CMap14 cmap,
2688 FT_UInt32 num_results,
2689 FT_Memory memory )
2691 FT_UInt32 old_max = cmap->max_results;
2692 FT_Error error = 0;
2695 if ( num_results > cmap->max_results )
2697 cmap->memory = memory;
2699 if ( FT_QRENEW_ARRAY( cmap->results, old_max, num_results ) )
2700 return error;
2702 cmap->max_results = num_results;
2705 return error;
2709 FT_CALLBACK_DEF( FT_Error )
2710 tt_cmap14_init( TT_CMap14 cmap,
2711 FT_Byte* table )
2713 cmap->cmap.data = table;
2715 table += 6;
2716 cmap->num_selectors = FT_PEEK_ULONG( table );
2717 cmap->max_results = 0;
2718 cmap->results = NULL;
2720 return SFNT_Err_Ok;
2724 FT_CALLBACK_DEF( FT_Error )
2725 tt_cmap14_validate( FT_Byte* table,
2726 FT_Validator valid )
2728 FT_Byte* p = table + 2;
2729 FT_ULong length = TT_NEXT_ULONG( p );
2730 FT_ULong num_selectors = TT_NEXT_ULONG( p );
2733 if ( length > (FT_ULong)( valid->limit - table ) ||
2734 length < 10 + 11 * num_selectors )
2735 FT_INVALID_TOO_SHORT;
2737 /* check selectors, they must be in increasing order */
2739 /* we start lastVarSel at 1 because a variant selector value of 0
2740 * isn't valid.
2742 FT_ULong n, lastVarSel = 1;
2745 for ( n = 0; n < num_selectors; n++ )
2747 FT_ULong varSel = TT_NEXT_UINT24( p );
2748 FT_ULong defOff = TT_NEXT_ULONG( p );
2749 FT_ULong nondefOff = TT_NEXT_ULONG( p );
2752 if ( defOff >= length || nondefOff >= length )
2753 FT_INVALID_TOO_SHORT;
2755 if ( varSel < lastVarSel )
2756 FT_INVALID_DATA;
2758 lastVarSel = varSel + 1;
2760 /* check the default table (these glyphs should be reached */
2761 /* through the normal Unicode cmap, no GIDs, just check order) */
2762 if ( defOff != 0 )
2764 FT_Byte* defp = table + defOff;
2765 FT_ULong numRanges = TT_NEXT_ULONG( defp );
2766 FT_ULong i;
2767 FT_ULong lastBase = 0;
2770 if ( defp + numRanges * 4 > valid->limit )
2771 FT_INVALID_TOO_SHORT;
2773 for ( i = 0; i < numRanges; ++i )
2775 FT_ULong base = TT_NEXT_UINT24( defp );
2776 FT_ULong cnt = FT_NEXT_BYTE( defp );
2779 if ( base + cnt >= 0x110000UL ) /* end of Unicode */
2780 FT_INVALID_DATA;
2782 if ( base < lastBase )
2783 FT_INVALID_DATA;
2785 lastBase = base + cnt + 1U;
2789 /* and the non-default table (these glyphs are specified here) */
2790 if ( nondefOff != 0 ) {
2791 FT_Byte* ndp = table + nondefOff;
2792 FT_ULong numMappings = TT_NEXT_ULONG( ndp );
2793 FT_ULong i, lastUni = 0;
2796 if ( numMappings * 4 > (FT_ULong)( valid->limit - ndp ) )
2797 FT_INVALID_TOO_SHORT;
2799 for ( i = 0; i < numMappings; ++i )
2801 FT_ULong uni = TT_NEXT_UINT24( ndp );
2802 FT_ULong gid = TT_NEXT_USHORT( ndp );
2805 if ( uni >= 0x110000UL ) /* end of Unicode */
2806 FT_INVALID_DATA;
2808 if ( uni < lastUni )
2809 FT_INVALID_DATA;
2811 lastUni = uni + 1U;
2813 if ( valid->level >= FT_VALIDATE_TIGHT &&
2814 gid >= TT_VALID_GLYPH_COUNT( valid ) )
2815 FT_INVALID_GLYPH_ID;
2821 return SFNT_Err_Ok;
2825 FT_CALLBACK_DEF( FT_UInt )
2826 tt_cmap14_char_index( TT_CMap cmap,
2827 FT_UInt32 char_code )
2829 FT_UNUSED( cmap );
2830 FT_UNUSED( char_code );
2832 /* This can't happen */
2833 return 0;
2837 FT_CALLBACK_DEF( FT_UInt32 )
2838 tt_cmap14_char_next( TT_CMap cmap,
2839 FT_UInt32 *pchar_code )
2841 FT_UNUSED( cmap );
2843 /* This can't happen */
2844 *pchar_code = 0;
2845 return 0;
2849 FT_CALLBACK_DEF( FT_Error )
2850 tt_cmap14_get_info( TT_CMap cmap,
2851 TT_CMapInfo *cmap_info )
2853 FT_UNUSED( cmap );
2855 cmap_info->format = 14;
2856 /* subtable 14 does not define a language field */
2857 cmap_info->language = 0xFFFFFFFFUL;
2859 return SFNT_Err_Ok;
2863 static FT_UInt
2864 tt_cmap14_char_map_def_binary( FT_Byte *base,
2865 FT_UInt32 char_code )
2867 FT_UInt32 numRanges = TT_PEEK_ULONG( base );
2868 FT_UInt32 max, min;
2871 min = 0;
2872 max = numRanges;
2874 base += 4;
2876 /* binary search */
2877 while ( min < max )
2879 FT_UInt32 mid = ( min + max ) >> 1;
2880 FT_Byte* p = base + 4 * mid;
2881 FT_ULong start = TT_NEXT_UINT24( p );
2882 FT_UInt cnt = FT_NEXT_BYTE( p );
2885 if ( char_code < start )
2886 max = mid;
2887 else if ( char_code > start+cnt )
2888 min = mid + 1;
2889 else
2890 return TRUE;
2893 return FALSE;
2897 static FT_UInt
2898 tt_cmap14_char_map_nondef_binary( FT_Byte *base,
2899 FT_UInt32 char_code )
2901 FT_UInt32 numMappings = TT_PEEK_ULONG( base );
2902 FT_UInt32 max, min;
2905 min = 0;
2906 max = numMappings;
2908 base += 4;
2910 /* binary search */
2911 while ( min < max )
2913 FT_UInt32 mid = ( min + max ) >> 1;
2914 FT_Byte* p = base + 5 * mid;
2915 FT_UInt32 uni = (FT_UInt32)TT_NEXT_UINT24( p );
2918 if ( char_code < uni )
2919 max = mid;
2920 else if ( char_code > uni )
2921 min = mid + 1;
2922 else
2923 return TT_PEEK_USHORT( p );
2926 return 0;
2930 static FT_Byte*
2931 tt_cmap14_find_variant( FT_Byte *base,
2932 FT_UInt32 variantCode )
2934 FT_UInt32 numVar = TT_PEEK_ULONG( base );
2935 FT_UInt32 max, min;
2938 min = 0;
2939 max = numVar;
2941 base += 4;
2943 /* binary search */
2944 while ( min < max )
2946 FT_UInt32 mid = ( min + max ) >> 1;
2947 FT_Byte* p = base + 11 * mid;
2948 FT_ULong varSel = TT_NEXT_UINT24( p );
2951 if ( variantCode < varSel )
2952 max = mid;
2953 else if ( variantCode > varSel )
2954 min = mid + 1;
2955 else
2956 return p;
2959 return NULL;
2963 FT_CALLBACK_DEF( FT_UInt )
2964 tt_cmap14_char_var_index( TT_CMap cmap,
2965 TT_CMap ucmap,
2966 FT_UInt32 charcode,
2967 FT_UInt32 variantSelector)
2969 FT_Byte* p = tt_cmap14_find_variant( cmap->data + 6, variantSelector );
2970 FT_ULong defOff;
2971 FT_ULong nondefOff;
2974 if ( !p )
2975 return 0;
2977 defOff = TT_NEXT_ULONG( p );
2978 nondefOff = TT_PEEK_ULONG( p );
2980 if ( defOff != 0 &&
2981 tt_cmap14_char_map_def_binary( cmap->data + defOff, charcode ) )
2983 /* This is the default variant of this charcode. GID not stored */
2984 /* here; stored in the normal Unicode charmap instead. */
2985 return ucmap->cmap.clazz->char_index( &ucmap->cmap, charcode );
2988 if ( nondefOff != 0 )
2989 return tt_cmap14_char_map_nondef_binary( cmap->data + nondefOff,
2990 charcode );
2992 return 0;
2996 FT_CALLBACK_DEF( FT_Int )
2997 tt_cmap14_char_var_isdefault( TT_CMap cmap,
2998 FT_UInt32 charcode,
2999 FT_UInt32 variantSelector )
3001 FT_Byte* p = tt_cmap14_find_variant( cmap->data + 6, variantSelector );
3002 FT_ULong defOff;
3003 FT_ULong nondefOff;
3006 if ( !p )
3007 return -1;
3009 defOff = TT_NEXT_ULONG( p );
3010 nondefOff = TT_NEXT_ULONG( p );
3012 if ( defOff != 0 &&
3013 tt_cmap14_char_map_def_binary( cmap->data + defOff, charcode ) )
3014 return 1;
3016 if ( nondefOff != 0 &&
3017 tt_cmap14_char_map_nondef_binary( cmap->data + nondefOff,
3018 charcode ) != 0 )
3019 return 0;
3021 return -1;
3025 FT_CALLBACK_DEF( FT_UInt32* )
3026 tt_cmap14_variants( TT_CMap cmap,
3027 FT_Memory memory )
3029 TT_CMap14 cmap14 = (TT_CMap14)cmap;
3030 FT_UInt32 count = cmap14->num_selectors;
3031 FT_Byte* p = cmap->data + 10;
3032 FT_UInt32* result;
3033 FT_UInt32 i;
3036 if ( tt_cmap14_ensure( cmap14, ( count + 1 ), memory ) )
3037 return NULL;
3039 result = cmap14->results;
3040 for ( i = 0; i < count; ++i )
3042 result[i] = (FT_UInt32)TT_NEXT_UINT24( p );
3043 p += 8;
3045 result[i] = 0;
3047 return result;
3051 FT_CALLBACK_DEF( FT_UInt32 * )
3052 tt_cmap14_char_variants( TT_CMap cmap,
3053 FT_Memory memory,
3054 FT_UInt32 charCode )
3056 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3057 FT_UInt32 count = cmap14->num_selectors;
3058 FT_Byte* p = cmap->data + 10;
3059 FT_UInt32* q;
3062 if ( tt_cmap14_ensure( cmap14, ( count + 1 ), memory ) )
3063 return NULL;
3065 for ( q = cmap14->results; count > 0; --count )
3067 FT_UInt32 varSel = TT_NEXT_UINT24( p );
3068 FT_ULong defOff = TT_NEXT_ULONG( p );
3069 FT_ULong nondefOff = TT_NEXT_ULONG( p );
3072 if ( ( defOff != 0 &&
3073 tt_cmap14_char_map_def_binary( cmap->data + defOff,
3074 charCode ) ) ||
3075 ( nondefOff != 0 &&
3076 tt_cmap14_char_map_nondef_binary( cmap->data + nondefOff,
3077 charCode ) != 0 ) )
3079 q[0] = varSel;
3080 q++;
3083 q[0] = 0;
3085 return cmap14->results;
3089 static FT_UInt
3090 tt_cmap14_def_char_count( FT_Byte *p )
3092 FT_UInt32 numRanges = (FT_UInt32)TT_NEXT_ULONG( p );
3093 FT_UInt tot = 0;
3096 p += 3; /* point to the first `cnt' field */
3097 for ( ; numRanges > 0; numRanges-- )
3099 tot += 1 + p[0];
3100 p += 4;
3103 return tot;
3107 static FT_UInt32*
3108 tt_cmap14_get_def_chars( TT_CMap cmap,
3109 FT_Byte* p,
3110 FT_Memory memory )
3112 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3113 FT_UInt32 numRanges;
3114 FT_UInt cnt;
3115 FT_UInt32* q;
3118 cnt = tt_cmap14_def_char_count( p );
3119 numRanges = (FT_UInt32)TT_NEXT_ULONG( p );
3121 if ( tt_cmap14_ensure( cmap14, ( cnt + 1 ), memory ) )
3122 return NULL;
3124 for ( q = cmap14->results; numRanges > 0; --numRanges )
3126 FT_UInt32 uni = (FT_UInt32)TT_NEXT_UINT24( p );
3129 cnt = FT_NEXT_BYTE( p ) + 1;
3132 q[0] = uni;
3133 uni += 1;
3134 q += 1;
3135 } while ( --cnt != 0 );
3137 q[0] = 0;
3139 return cmap14->results;
3143 static FT_UInt32*
3144 tt_cmap14_get_nondef_chars( TT_CMap cmap,
3145 FT_Byte *p,
3146 FT_Memory memory )
3148 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3149 FT_UInt32 numMappings;
3150 FT_UInt i;
3151 FT_UInt32 *ret;
3154 numMappings = (FT_UInt32)TT_NEXT_ULONG( p );
3156 if ( tt_cmap14_ensure( cmap14, ( numMappings + 1 ), memory ) )
3157 return NULL;
3159 ret = cmap14->results;
3160 for ( i = 0; i < numMappings; ++i )
3162 ret[i] = (FT_UInt32)TT_NEXT_UINT24( p );
3163 p += 2;
3165 ret[i] = 0;
3167 return ret;
3171 FT_CALLBACK_DEF( FT_UInt32 * )
3172 tt_cmap14_variant_chars( TT_CMap cmap,
3173 FT_Memory memory,
3174 FT_UInt32 variantSelector )
3176 FT_Byte *p = tt_cmap14_find_variant( cmap->data + 6,
3177 variantSelector );
3178 FT_UInt32 *ret;
3179 FT_Int i;
3180 FT_ULong defOff;
3181 FT_ULong nondefOff;
3184 if ( !p )
3185 return NULL;
3187 defOff = TT_NEXT_ULONG( p );
3188 nondefOff = TT_NEXT_ULONG( p );
3190 if ( defOff == 0 && nondefOff == 0 )
3191 return NULL;
3193 if ( defOff == 0 )
3194 return tt_cmap14_get_nondef_chars( cmap, cmap->data + nondefOff,
3195 memory );
3196 else if ( nondefOff == 0 )
3197 return tt_cmap14_get_def_chars( cmap, cmap->data + defOff,
3198 memory );
3199 else
3201 /* Both a default and a non-default glyph set? That's probably not */
3202 /* good font design, but the spec allows for it... */
3203 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3204 FT_UInt32 numRanges;
3205 FT_UInt32 numMappings;
3206 FT_UInt32 duni;
3207 FT_UInt32 dcnt;
3208 FT_UInt32 nuni;
3209 FT_Byte* dp;
3210 FT_UInt di, ni, k;
3213 p = cmap->data + nondefOff;
3214 dp = cmap->data + defOff;
3216 numMappings = (FT_UInt32)TT_NEXT_ULONG( p );
3217 dcnt = tt_cmap14_def_char_count( dp );
3218 numRanges = (FT_UInt32)TT_NEXT_ULONG( dp );
3220 if ( numMappings == 0 )
3221 return tt_cmap14_get_def_chars( cmap, cmap->data + defOff,
3222 memory );
3223 if ( dcnt == 0 )
3224 return tt_cmap14_get_nondef_chars( cmap, cmap->data + nondefOff,
3225 memory );
3227 if ( tt_cmap14_ensure( cmap14, ( dcnt + numMappings + 1 ), memory ) )
3228 return NULL;
3230 ret = cmap14->results;
3231 duni = (FT_UInt32)TT_NEXT_UINT24( dp );
3232 dcnt = FT_NEXT_BYTE( dp );
3233 di = 1;
3234 nuni = (FT_UInt32)TT_NEXT_UINT24( p );
3235 p += 2;
3236 ni = 1;
3237 i = 0;
3239 for ( ;; )
3241 if ( nuni > duni + dcnt )
3243 for ( k = 0; k <= dcnt; ++k )
3244 ret[i++] = duni + k;
3246 ++di;
3248 if ( di > numRanges )
3249 break;
3251 duni = (FT_UInt32)TT_NEXT_UINT24( dp );
3252 dcnt = FT_NEXT_BYTE( dp );
3254 else
3256 if ( nuni < duni )
3257 ret[i++] = nuni;
3258 /* If it is within the default range then ignore it -- */
3259 /* that should not have happened */
3260 ++ni;
3261 if ( ni > numMappings )
3262 break;
3264 nuni = (FT_UInt32)TT_NEXT_UINT24( p );
3265 p += 2;
3269 if ( ni <= numMappings )
3271 /* If we get here then we have run out of all default ranges. */
3272 /* We have read one non-default mapping which we haven't stored */
3273 /* and there may be others that need to be read. */
3274 ret[i++] = nuni;
3275 while ( ni < numMappings )
3277 ret[i++] = (FT_UInt32)TT_NEXT_UINT24( p );
3278 p += 2;
3279 ++ni;
3282 else if ( di <= numRanges )
3284 /* If we get here then we have run out of all non-default */
3285 /* mappings. We have read one default range which we haven't */
3286 /* stored and there may be others that need to be read. */
3287 for ( k = 0; k <= dcnt; ++k )
3288 ret[i++] = duni + k;
3290 while ( di < numRanges )
3292 duni = (FT_UInt32)TT_NEXT_UINT24( dp );
3293 dcnt = FT_NEXT_BYTE( dp );
3295 for ( k = 0; k <= dcnt; ++k )
3296 ret[i++] = duni + k;
3297 ++di;
3301 ret[i] = 0;
3303 return ret;
3308 FT_DEFINE_TT_CMAP(tt_cmap14_class_rec,
3309 sizeof ( TT_CMap14Rec ),
3311 (FT_CMap_InitFunc) tt_cmap14_init,
3312 (FT_CMap_DoneFunc) tt_cmap14_done,
3313 (FT_CMap_CharIndexFunc)tt_cmap14_char_index,
3314 (FT_CMap_CharNextFunc) tt_cmap14_char_next,
3316 /* Format 14 extension functions */
3317 (FT_CMap_CharVarIndexFunc) tt_cmap14_char_var_index,
3318 (FT_CMap_CharVarIsDefaultFunc)tt_cmap14_char_var_isdefault,
3319 (FT_CMap_VariantListFunc) tt_cmap14_variants,
3320 (FT_CMap_CharVariantListFunc) tt_cmap14_char_variants,
3321 (FT_CMap_VariantCharListFunc) tt_cmap14_variant_chars
3324 (TT_CMap_ValidateFunc)tt_cmap14_validate,
3325 (TT_CMap_Info_GetFunc)tt_cmap14_get_info
3328 #endif /* TT_CONFIG_CMAP_FORMAT_14 */
3331 #ifndef FT_CONFIG_OPTION_PIC
3333 static const TT_CMap_Class tt_cmap_classes[] =
3335 #define TTCMAPCITEM(a) &a,
3336 #include "ttcmapc.h"
3337 NULL,
3340 #else /*FT_CONFIG_OPTION_PIC*/
3342 void FT_Destroy_Class_tt_cmap_classes(FT_Library library, TT_CMap_Class* clazz)
3344 FT_Memory memory = library->memory;
3345 if ( clazz )
3346 FT_FREE( clazz );
3349 FT_Error FT_Create_Class_tt_cmap_classes(FT_Library library, TT_CMap_Class** output_class)
3351 TT_CMap_Class* clazz;
3352 TT_CMap_ClassRec* recs;
3353 FT_Error error;
3354 FT_Memory memory = library->memory;
3355 int i = 0;
3357 #define TTCMAPCITEM(a) i++;
3358 #include "ttcmapc.h"
3360 /* allocate enough space for both the pointers +terminator and the class instances */
3361 if ( FT_ALLOC( clazz, sizeof(*clazz)*(i+1)+sizeof(TT_CMap_ClassRec)*i ) )
3362 return error;
3364 /* the location of the class instances follows the array of pointers */
3365 recs = (TT_CMap_ClassRec*) (((char*)clazz)+(sizeof(*clazz)*(i+1)));
3366 i=0;
3368 #undef TTCMAPCITEM
3369 #define TTCMAPCITEM(a) \
3370 FT_Init_Class_##a(&recs[i]); \
3371 clazz[i] = &recs[i]; \
3372 i++;
3373 #include "ttcmapc.h"
3375 clazz[i] = NULL;
3377 *output_class = clazz;
3378 return FT_Err_Ok;
3381 #endif /*FT_CONFIG_OPTION_PIC*/
3384 /* parse the `cmap' table and build the corresponding TT_CMap objects */
3385 /* in the current face */
3386 /* */
3387 FT_LOCAL_DEF( FT_Error )
3388 tt_face_build_cmaps( TT_Face face )
3390 FT_Byte* table = face->cmap_table;
3391 FT_Byte* limit = table + face->cmap_size;
3392 FT_UInt volatile num_cmaps;
3393 FT_Byte* volatile p = table;
3394 FT_Library library = FT_FACE_LIBRARY(face);
3395 FT_UNUSED(library);
3398 if ( p + 4 > limit )
3399 return SFNT_Err_Invalid_Table;
3401 /* only recognize format 0 */
3402 if ( TT_NEXT_USHORT( p ) != 0 )
3404 p -= 2;
3405 FT_ERROR(( "tt_face_build_cmaps:"
3406 " unsupported `cmap' table format = %d\n",
3407 TT_PEEK_USHORT( p ) ));
3408 return SFNT_Err_Invalid_Table;
3411 num_cmaps = TT_NEXT_USHORT( p );
3413 for ( ; num_cmaps > 0 && p + 8 <= limit; num_cmaps-- )
3415 FT_CharMapRec charmap;
3416 FT_UInt32 offset;
3419 charmap.platform_id = TT_NEXT_USHORT( p );
3420 charmap.encoding_id = TT_NEXT_USHORT( p );
3421 charmap.face = FT_FACE( face );
3422 charmap.encoding = FT_ENCODING_NONE; /* will be filled later */
3423 offset = TT_NEXT_ULONG( p );
3425 if ( offset && offset <= face->cmap_size - 2 )
3427 FT_Byte* volatile cmap = table + offset;
3428 volatile FT_UInt format = TT_PEEK_USHORT( cmap );
3429 const TT_CMap_Class* volatile pclazz = FT_TT_CMAP_CLASSES_GET;
3430 TT_CMap_Class volatile clazz;
3433 for ( ; *pclazz; pclazz++ )
3435 clazz = *pclazz;
3436 if ( clazz->format == format )
3438 volatile TT_ValidatorRec valid;
3439 volatile FT_Error error = SFNT_Err_Ok;
3442 ft_validator_init( FT_VALIDATOR( &valid ), cmap, limit,
3443 FT_VALIDATE_DEFAULT );
3445 valid.num_glyphs = (FT_UInt)face->max_profile.numGlyphs;
3447 if ( ft_setjmp(
3448 *((ft_jmp_buf*)&FT_VALIDATOR( &valid )->jump_buffer) ) == 0 )
3450 /* validate this cmap sub-table */
3451 error = clazz->validate( cmap, FT_VALIDATOR( &valid ) );
3454 if ( valid.validator.error == 0 )
3456 FT_CMap ttcmap;
3459 /* It might make sense to store the single variation selector */
3460 /* cmap somewhere special. But it would have to be in the */
3461 /* public FT_FaceRec, and we can't change that. */
3463 if ( !FT_CMap_New( (FT_CMap_Class)clazz,
3464 cmap, &charmap, &ttcmap ) )
3466 /* it is simpler to directly set `flags' than adding */
3467 /* a parameter to FT_CMap_New */
3468 ((TT_CMap)ttcmap)->flags = (FT_Int)error;
3471 else
3473 FT_TRACE0(( "tt_face_build_cmaps:"
3474 " broken cmap sub-table ignored\n" ));
3476 break;
3480 if ( *pclazz == NULL )
3482 FT_TRACE0(( "tt_face_build_cmaps:"
3483 " unsupported cmap sub-table ignored\n" ));
3488 return SFNT_Err_Ok;
3492 FT_LOCAL( FT_Error )
3493 tt_get_cmap_info( FT_CharMap charmap,
3494 TT_CMapInfo *cmap_info )
3496 FT_CMap cmap = (FT_CMap)charmap;
3497 TT_CMap_Class clazz = (TT_CMap_Class)cmap->clazz;
3500 return clazz->get_cmap_info( charmap, cmap_info );
3504 /* END */