< prev index next >

src/java.desktop/share/native/libfreetype/src/sfnt/ttcmap.c

Print this page


   1 /***************************************************************************/
   2 /*                                                                         */
   3 /*  ttcmap.c                                                               */
   4 /*                                                                         */
   5 /*    TrueType character mapping table (cmap) support (body).              */
   6 /*                                                                         */
   7 /*  Copyright 2002-2018 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 /***************************************************************************/
  17 
  18 
  19 #include <ft2build.h>
  20 #include FT_INTERNAL_DEBUG_H
  21 
  22 #include "sferrors.h"           /* must come before FT_INTERNAL_VALIDATE_H */
  23 
  24 #include FT_INTERNAL_VALIDATE_H
  25 #include FT_INTERNAL_STREAM_H
  26 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
  27 #include "ttload.h"
  28 #include "ttcmap.h"
  29 #include "ttpost.h"
  30 #include "sfntpic.h"
  31 
  32 
  33   /*************************************************************************/
  34   /*                                                                       */
  35   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  36   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  37   /* messages during execution.                                            */
  38   /*                                                                       */
  39 #undef  FT_COMPONENT
  40 #define FT_COMPONENT  trace_ttcmap
  41 
  42 
  43 #define TT_PEEK_SHORT   FT_PEEK_SHORT
  44 #define TT_PEEK_USHORT  FT_PEEK_USHORT
  45 #define TT_PEEK_UINT24  FT_PEEK_UOFF3
  46 #define TT_PEEK_LONG    FT_PEEK_LONG
  47 #define TT_PEEK_ULONG   FT_PEEK_ULONG
  48 
  49 #define TT_NEXT_SHORT   FT_NEXT_SHORT
  50 #define TT_NEXT_USHORT  FT_NEXT_USHORT
  51 #define TT_NEXT_UINT24  FT_NEXT_UOFF3
  52 #define TT_NEXT_LONG    FT_NEXT_LONG
  53 #define TT_NEXT_ULONG   FT_NEXT_ULONG
  54 
  55 
  56   /* Too large glyph index return values are caught in `FT_Get_Char_Index' */
  57   /* and `FT_Get_Next_Char' (the latter calls the internal `next' function */
  58   /* again in this case).  To mark character code return values as invalid */
  59   /* it is sufficient to set the corresponding glyph index return value to */
  60   /* zero.                                                                 */
  61 
  62 
  63   FT_CALLBACK_DEF( FT_Error )
  64   tt_cmap_init( TT_CMap   cmap,
  65                 FT_Byte*  table )
  66   {
  67     cmap->data = table;
  68     return FT_Err_Ok;
  69   }
  70 
  71 
  72   /*************************************************************************/
  73   /*************************************************************************/
  74   /*****                                                               *****/
  75   /*****                           FORMAT 0                            *****/
  76   /*****                                                               *****/
  77   /*************************************************************************/
  78   /*************************************************************************/
  79 
  80   /*************************************************************************/
  81   /*                                                                       */
  82   /* TABLE OVERVIEW                                                        */
  83   /* --------------                                                        */
  84   /*                                                                       */
  85   /*   NAME        OFFSET         TYPE          DESCRIPTION                */
  86   /*                                                                       */
  87   /*   format      0              USHORT        must be 0                  */
  88   /*   length      2              USHORT        table length in bytes      */
  89   /*   language    4              USHORT        Mac language code          */
  90   /*   glyph_ids   6              BYTE[256]     array of glyph indices     */
  91   /*               262                                                     */
  92   /*                                                                       */
  93 
  94 #ifdef TT_CONFIG_CMAP_FORMAT_0
  95 
  96   FT_CALLBACK_DEF( FT_Error )
  97   tt_cmap0_validate( FT_Byte*      table,
  98                      FT_Validator  valid )
  99   {
 100     FT_Byte*  p;
 101     FT_UInt   length;
 102 
 103 
 104     if ( table + 2 + 2 > valid->limit )
 105       FT_INVALID_TOO_SHORT;
 106 
 107     p      = table + 2;           /* skip format */
 108     length = TT_NEXT_USHORT( p );
 109 
 110     if ( table + length > valid->limit || length < 262 )
 111       FT_INVALID_TOO_SHORT;
 112 


 221   /*****                                                               *****/
 222   /***** The following charmap lookup and iteration functions all      *****/
 223   /***** assume that the value `charcode' fulfills the following.      *****/
 224   /*****                                                               *****/
 225   /*****   - For one-byte characters, `charcode' is simply the         *****/
 226   /*****     character code.                                           *****/
 227   /*****                                                               *****/
 228   /*****   - For two-byte characters, `charcode' is the 2-byte         *****/
 229   /*****     character code in big endian format.  More precisely:     *****/
 230   /*****                                                               *****/
 231   /*****       (charcode >> 8)    is the first byte value              *****/
 232   /*****       (charcode & 0xFF)  is the second byte value             *****/
 233   /*****                                                               *****/
 234   /***** Note that not all values of `charcode' are valid according    *****/
 235   /***** to these rules, and the function moderately checks the        *****/
 236   /***** arguments.                                                    *****/
 237   /*****                                                               *****/
 238   /*************************************************************************/
 239   /*************************************************************************/
 240 
 241   /*************************************************************************/
 242   /*                                                                       */
 243   /* TABLE OVERVIEW                                                        */
 244   /* --------------                                                        */
 245   /*                                                                       */
 246   /*   NAME        OFFSET         TYPE            DESCRIPTION              */
 247   /*                                                                       */
 248   /*   format      0              USHORT          must be 2                */
 249   /*   length      2              USHORT          table length in bytes    */
 250   /*   language    4              USHORT          Mac language code        */
 251   /*   keys        6              USHORT[256]     sub-header keys          */
 252   /*   subs        518            SUBHEAD[NSUBS]  sub-headers array        */
 253   /*   glyph_ids   518+NSUB*8     USHORT[]        glyph ID array           */
 254   /*                                                                       */
 255   /* The `keys' table is used to map charcode high bytes to sub-headers.   */
 256   /* The value of `NSUBS' is the number of sub-headers defined in the      */
 257   /* table and is computed by finding the maximum of the `keys' table.     */
 258   /*                                                                       */
 259   /* Note that for any `n', `keys[n]' is a byte offset within the `subs'   */
 260   /* table, i.e., it is the corresponding sub-header index multiplied      */
 261   /* by 8.                                                                 */
 262   /*                                                                       */
 263   /* Each sub-header has the following format.                             */
 264   /*                                                                       */
 265   /*   NAME        OFFSET      TYPE            DESCRIPTION                 */
 266   /*                                                                       */
 267   /*   first       0           USHORT          first valid low-byte        */
 268   /*   count       2           USHORT          number of valid low-bytes   */
 269   /*   delta       4           SHORT           see below                   */
 270   /*   offset      6           USHORT          see below                   */
 271   /*                                                                       */
 272   /* A sub-header defines, for each high byte, the range of valid          */
 273   /* low bytes within the charmap.  Note that the range defined by `first' */
 274   /* and `count' must be completely included in the interval [0..255]      */
 275   /* according to the specification.                                       */
 276   /*                                                                       */
 277   /* If a character code is contained within a given sub-header, then      */
 278   /* mapping it to a glyph index is done as follows.                       */
 279   /*                                                                       */
 280   /* * The value of `offset' is read.  This is a _byte_ distance from the  */
 281   /*   location of the `offset' field itself into a slice of the           */
 282   /*   `glyph_ids' table.  Let's call it `slice' (it is a USHORT[], too).  */
 283   /*                                                                       */
 284   /* * The value `slice[char.lo - first]' is read.  If it is 0, there is   */
 285   /*   no glyph for the charcode.  Otherwise, the value of `delta' is      */
 286   /*   added to it (modulo 65536) to form a new glyph index.               */
 287   /*                                                                       */
 288   /* It is up to the validation routine to check that all offsets fall     */
 289   /* within the glyph IDs table (and not within the `subs' table itself or */
 290   /* outside of the CMap).                                                 */
 291   /*                                                                       */
 292 
 293 #ifdef TT_CONFIG_CMAP_FORMAT_2
 294 
 295   FT_CALLBACK_DEF( FT_Error )
 296   tt_cmap2_validate( FT_Byte*      table,
 297                      FT_Validator  valid )
 298   {
 299     FT_Byte*  p;
 300     FT_UInt   length;
 301 
 302     FT_UInt   n, max_subs;
 303     FT_Byte*  keys;        /* keys table     */
 304     FT_Byte*  subs;        /* sub-headers    */
 305     FT_Byte*  glyph_ids;   /* glyph ID array */
 306 
 307 
 308     if ( table + 2 + 2 > valid->limit )
 309       FT_INVALID_TOO_SHORT;
 310 
 311     p      = table + 2;           /* skip format */


 609       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
 610       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
 611       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
 612 
 613     2,
 614     (TT_CMap_ValidateFunc)tt_cmap2_validate,  /* validate      */
 615     (TT_CMap_Info_GetFunc)tt_cmap2_get_info   /* get_cmap_info */
 616   )
 617 
 618 #endif /* TT_CONFIG_CMAP_FORMAT_2 */
 619 
 620 
 621   /*************************************************************************/
 622   /*************************************************************************/
 623   /*****                                                               *****/
 624   /*****                           FORMAT 4                            *****/
 625   /*****                                                               *****/
 626   /*************************************************************************/
 627   /*************************************************************************/
 628 
 629   /*************************************************************************/
 630   /*                                                                       */
 631   /* TABLE OVERVIEW                                                        */
 632   /* --------------                                                        */
 633   /*                                                                       */
 634   /*   NAME          OFFSET         TYPE              DESCRIPTION          */
 635   /*                                                                       */
 636   /*   format        0              USHORT            must be 4            */
 637   /*   length        2              USHORT            table length         */
 638   /*                                                  in bytes             */
 639   /*   language      4              USHORT            Mac language code    */
 640   /*                                                                       */
 641   /*   segCountX2    6              USHORT            2*NUM_SEGS           */
 642   /*   searchRange   8              USHORT            2*(1 << LOG_SEGS)    */
 643   /*   entrySelector 10             USHORT            LOG_SEGS             */
 644   /*   rangeShift    12             USHORT            segCountX2 -         */
 645   /*                                                    searchRange        */
 646   /*                                                                       */
 647   /*   endCount      14             USHORT[NUM_SEGS]  end charcode for     */
 648   /*                                                  each segment; last   */
 649   /*                                                  is 0xFFFF            */
 650   /*                                                                       */
 651   /*   pad           14+NUM_SEGS*2  USHORT            padding              */
 652   /*                                                                       */
 653   /*   startCount    16+NUM_SEGS*2  USHORT[NUM_SEGS]  first charcode for   */
 654   /*                                                  each segment         */
 655   /*                                                                       */
 656   /*   idDelta       16+NUM_SEGS*4  SHORT[NUM_SEGS]   delta for each       */
 657   /*                                                  segment              */
 658   /*   idOffset      16+NUM_SEGS*6  SHORT[NUM_SEGS]   range offset for     */
 659   /*                                                  each segment; can be */
 660   /*                                                  zero                 */
 661   /*                                                                       */
 662   /*   glyphIds      16+NUM_SEGS*8  USHORT[]          array of glyph ID    */
 663   /*                                                  ranges               */
 664   /*                                                                       */
 665   /* Character codes are modelled by a series of ordered (increasing)      */
 666   /* intervals called segments.  Each segment has start and end codes,     */
 667   /* provided by the `startCount' and `endCount' arrays.  Segments must    */
 668   /* not overlap, and the last segment should always contain the value     */
 669   /* 0xFFFF for `endCount'.                                                */
 670   /*                                                                       */
 671   /* The fields `searchRange', `entrySelector' and `rangeShift' are better */
 672   /* ignored (they are traces of over-engineering in the TrueType          */
 673   /* specification).                                                       */
 674   /*                                                                       */
 675   /* Each segment also has a signed `delta', as well as an optional offset */
 676   /* within the `glyphIds' table.                                          */
 677   /*                                                                       */
 678   /* If a segment's idOffset is 0, the glyph index corresponding to any    */
 679   /* charcode within the segment is obtained by adding the value of        */
 680   /* `idDelta' directly to the charcode, modulo 65536.                     */
 681   /*                                                                       */
 682   /* Otherwise, a glyph index is taken from the glyph IDs sub-array for    */
 683   /* the segment, and the value of `idDelta' is added to it.               */
 684   /*                                                                       */
 685   /*                                                                       */
 686   /* Finally, note that a lot of fonts contain an invalid last segment,    */
 687   /* where `start' and `end' are correctly set to 0xFFFF but both `delta'  */
 688   /* and `offset' are incorrect (e.g., `opens___.ttf' which comes with     */
 689   /* OpenOffice.org).  We need special code to deal with them correctly.   */
 690   /*                                                                       */
 691 
 692 #ifdef TT_CONFIG_CMAP_FORMAT_4
 693 
 694   typedef struct  TT_CMap4Rec_
 695   {
 696     TT_CMapRec  cmap;
 697     FT_UInt32   cur_charcode;   /* current charcode */
 698     FT_UInt     cur_gindex;     /* current glyph index */
 699 
 700     FT_UInt     num_ranges;
 701     FT_UInt     cur_range;
 702     FT_UInt     cur_start;
 703     FT_UInt     cur_end;
 704     FT_Int      cur_delta;
 705     FT_Byte*    cur_values;
 706 
 707   } TT_CMap4Rec, *TT_CMap4;
 708 
 709 
 710   FT_CALLBACK_DEF( FT_Error )


1556       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
1557       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
1558       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
1559 
1560     4,
1561     (TT_CMap_ValidateFunc)tt_cmap4_validate,  /* validate      */
1562     (TT_CMap_Info_GetFunc)tt_cmap4_get_info   /* get_cmap_info */
1563   )
1564 
1565 #endif /* TT_CONFIG_CMAP_FORMAT_4 */
1566 
1567 
1568   /*************************************************************************/
1569   /*************************************************************************/
1570   /*****                                                               *****/
1571   /*****                          FORMAT 6                             *****/
1572   /*****                                                               *****/
1573   /*************************************************************************/
1574   /*************************************************************************/
1575 
1576   /*************************************************************************/
1577   /*                                                                       */
1578   /* TABLE OVERVIEW                                                        */
1579   /* --------------                                                        */
1580   /*                                                                       */
1581   /*   NAME        OFFSET          TYPE             DESCRIPTION            */
1582   /*                                                                       */
1583   /*   format       0              USHORT           must be 6              */
1584   /*   length       2              USHORT           table length in bytes  */
1585   /*   language     4              USHORT           Mac language code      */
1586   /*                                                                       */
1587   /*   first        6              USHORT           first segment code     */
1588   /*   count        8              USHORT           segment size in chars  */
1589   /*   glyphIds     10             USHORT[count]    glyph IDs              */
1590   /*                                                                       */
1591   /* A very simplified segment mapping.                                    */
1592   /*                                                                       */
1593 
1594 #ifdef TT_CONFIG_CMAP_FORMAT_6
1595 
1596   FT_CALLBACK_DEF( FT_Error )
1597   tt_cmap6_validate( FT_Byte*      table,
1598                      FT_Validator  valid )
1599   {
1600     FT_Byte*  p;
1601     FT_UInt   length, count;
1602 
1603 
1604     if ( table + 10 > valid->limit )
1605       FT_INVALID_TOO_SHORT;
1606 
1607     p      = table + 2;
1608     length = TT_NEXT_USHORT( p );
1609 
1610     p      = table + 8;             /* skip language and start index */
1611     count  = TT_NEXT_USHORT( p );
1612 


1751   /*****     Area (i.e. U+D800-U+DFFF).                                *****/
1752   /*****                                                               *****/
1753   /*****   - A 32-bit value, made of two surrogate values, i.e.. if    *****/
1754   /*****     `char_code = (char_hi << 16) | char_lo', then both        *****/
1755   /*****     `char_hi' and `char_lo' must be in the Surrogates Area.   *****/
1756   /*****      Area.                                                    *****/
1757   /*****                                                               *****/
1758   /***** The `is32' table embedded in the charmap indicates whether a  *****/
1759   /***** given 16-bit value is in the surrogates area or not.          *****/
1760   /*****                                                               *****/
1761   /***** So, for any given `char_code', we can assert the following.   *****/
1762   /*****                                                               *****/
1763   /*****   If `char_hi == 0' then we must have `is32[char_lo] == 0'.   *****/
1764   /*****                                                               *****/
1765   /*****   If `char_hi != 0' then we must have both                    *****/
1766   /*****   `is32[char_hi] != 0' and `is32[char_lo] != 0'.              *****/
1767   /*****                                                               *****/
1768   /*************************************************************************/
1769   /*************************************************************************/
1770 
1771   /*************************************************************************/
1772   /*                                                                       */
1773   /* TABLE OVERVIEW                                                        */
1774   /* --------------                                                        */
1775   /*                                                                       */
1776   /*   NAME        OFFSET         TYPE        DESCRIPTION                  */
1777   /*                                                                       */
1778   /*   format      0              USHORT      must be 8                    */
1779   /*   reserved    2              USHORT      reserved                     */
1780   /*   length      4              ULONG       length in bytes              */
1781   /*   language    8              ULONG       Mac language code            */
1782   /*   is32        12             BYTE[8192]  32-bitness bitmap            */
1783   /*   count       8204           ULONG       number of groups             */
1784   /*                                                                       */
1785   /* This header is followed by `count' groups of the following format:    */
1786   /*                                                                       */
1787   /*   start       0              ULONG       first charcode               */
1788   /*   end         4              ULONG       last charcode                */
1789   /*   startId     8              ULONG       start glyph ID for the group */
1790   /*                                                                       */
1791 
1792 #ifdef TT_CONFIG_CMAP_FORMAT_8
1793 
1794   FT_CALLBACK_DEF( FT_Error )
1795   tt_cmap8_validate( FT_Byte*      table,
1796                      FT_Validator  valid )
1797   {
1798     FT_Byte*   p = table + 4;
1799     FT_Byte*   is32;
1800     FT_UInt32  length;
1801     FT_UInt32  num_groups;
1802 
1803 
1804     if ( table + 16 + 8192 > valid->limit )
1805       FT_INVALID_TOO_SHORT;
1806 
1807     length = TT_NEXT_ULONG( p );
1808     if ( length > (FT_UInt32)( valid->limit - table ) || length < 8192 + 16 )
1809       FT_INVALID_TOO_SHORT;
1810 


2020       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2021       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2022       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2023 
2024     8,
2025     (TT_CMap_ValidateFunc)tt_cmap8_validate,  /* validate      */
2026     (TT_CMap_Info_GetFunc)tt_cmap8_get_info   /* get_cmap_info */
2027   )
2028 
2029 #endif /* TT_CONFIG_CMAP_FORMAT_8 */
2030 
2031 
2032   /*************************************************************************/
2033   /*************************************************************************/
2034   /*****                                                               *****/
2035   /*****                          FORMAT 10                            *****/
2036   /*****                                                               *****/
2037   /*************************************************************************/
2038   /*************************************************************************/
2039 
2040   /*************************************************************************/
2041   /*                                                                       */
2042   /* TABLE OVERVIEW                                                        */
2043   /* --------------                                                        */
2044   /*                                                                       */
2045   /*   NAME      OFFSET  TYPE               DESCRIPTION                    */
2046   /*                                                                       */
2047   /*   format     0      USHORT             must be 10                     */
2048   /*   reserved   2      USHORT             reserved                       */
2049   /*   length     4      ULONG              length in bytes                */
2050   /*   language   8      ULONG              Mac language code              */
2051   /*                                                                       */
2052   /*   start     12      ULONG              first char in range            */
2053   /*   count     16      ULONG              number of chars in range       */
2054   /*   glyphIds  20      USHORT[count]      glyph indices covered          */
2055   /*                                                                       */
2056 
2057 #ifdef TT_CONFIG_CMAP_FORMAT_10
2058 
2059   FT_CALLBACK_DEF( FT_Error )
2060   tt_cmap10_validate( FT_Byte*      table,
2061                       FT_Validator  valid )
2062   {
2063     FT_Byte*  p = table + 4;
2064     FT_ULong  length, count;
2065 
2066 
2067     if ( table + 20 > valid->limit )
2068       FT_INVALID_TOO_SHORT;
2069 
2070     length = TT_NEXT_ULONG( p );
2071     p      = table + 16;
2072     count  = TT_NEXT_ULONG( p );
2073 
2074     if ( length > (FT_ULong)( valid->limit - table ) ||
2075          /* length < 20 + count * 2 ? */


2192       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2193       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2194       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2195 
2196     10,
2197     (TT_CMap_ValidateFunc)tt_cmap10_validate,  /* validate      */
2198     (TT_CMap_Info_GetFunc)tt_cmap10_get_info   /* get_cmap_info */
2199   )
2200 
2201 #endif /* TT_CONFIG_CMAP_FORMAT_10 */
2202 
2203 
2204   /*************************************************************************/
2205   /*************************************************************************/
2206   /*****                                                               *****/
2207   /*****                          FORMAT 12                            *****/
2208   /*****                                                               *****/
2209   /*************************************************************************/
2210   /*************************************************************************/
2211 
2212   /*************************************************************************/
2213   /*                                                                       */
2214   /* TABLE OVERVIEW                                                        */
2215   /* --------------                                                        */
2216   /*                                                                       */
2217   /*   NAME        OFFSET     TYPE       DESCRIPTION                       */
2218   /*                                                                       */
2219   /*   format      0          USHORT     must be 12                        */
2220   /*   reserved    2          USHORT     reserved                          */
2221   /*   length      4          ULONG      length in bytes                   */
2222   /*   language    8          ULONG      Mac language code                 */
2223   /*   count       12         ULONG      number of groups                  */
2224   /*               16                                                      */
2225   /*                                                                       */
2226   /* This header is followed by `count' groups of the following format:    */
2227   /*                                                                       */
2228   /*   start       0          ULONG      first charcode                    */
2229   /*   end         4          ULONG      last charcode                     */
2230   /*   startId     8          ULONG      start glyph ID for the group      */
2231   /*                                                                       */
2232 
2233 #ifdef TT_CONFIG_CMAP_FORMAT_12
2234 
2235   typedef struct  TT_CMap12Rec_
2236   {
2237     TT_CMapRec  cmap;
2238     FT_Bool     valid;
2239     FT_ULong    cur_charcode;
2240     FT_UInt     cur_gindex;
2241     FT_ULong    cur_group;
2242     FT_ULong    num_groups;
2243 
2244   } TT_CMap12Rec, *TT_CMap12;
2245 
2246 
2247   FT_CALLBACK_DEF( FT_Error )
2248   tt_cmap12_init( TT_CMap12  cmap,
2249                   FT_Byte*   table )
2250   {
2251     cmap->cmap.data  = table;


2548       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2549       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2550       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2551 
2552     12,
2553     (TT_CMap_ValidateFunc)tt_cmap12_validate,  /* validate      */
2554     (TT_CMap_Info_GetFunc)tt_cmap12_get_info   /* get_cmap_info */
2555   )
2556 
2557 #endif /* TT_CONFIG_CMAP_FORMAT_12 */
2558 
2559 
2560   /*************************************************************************/
2561   /*************************************************************************/
2562   /*****                                                               *****/
2563   /*****                          FORMAT 13                            *****/
2564   /*****                                                               *****/
2565   /*************************************************************************/
2566   /*************************************************************************/
2567 
2568   /*************************************************************************/
2569   /*                                                                       */
2570   /* TABLE OVERVIEW                                                        */
2571   /* --------------                                                        */
2572   /*                                                                       */
2573   /*   NAME        OFFSET     TYPE       DESCRIPTION                       */
2574   /*                                                                       */
2575   /*   format      0          USHORT     must be 13                        */
2576   /*   reserved    2          USHORT     reserved                          */
2577   /*   length      4          ULONG      length in bytes                   */
2578   /*   language    8          ULONG      Mac language code                 */
2579   /*   count       12         ULONG      number of groups                  */
2580   /*               16                                                      */
2581   /*                                                                       */
2582   /* This header is followed by `count' groups of the following format:    */
2583   /*                                                                       */
2584   /*   start       0          ULONG      first charcode                    */
2585   /*   end         4          ULONG      last charcode                     */
2586   /*   glyphId     8          ULONG      glyph ID for the whole group      */
2587   /*                                                                       */
2588 
2589 #ifdef TT_CONFIG_CMAP_FORMAT_13
2590 
2591   typedef struct  TT_CMap13Rec_
2592   {
2593     TT_CMapRec  cmap;
2594     FT_Bool     valid;
2595     FT_ULong    cur_charcode;
2596     FT_UInt     cur_gindex;
2597     FT_ULong    cur_group;
2598     FT_ULong    num_groups;
2599 
2600   } TT_CMap13Rec, *TT_CMap13;
2601 
2602 
2603   FT_CALLBACK_DEF( FT_Error )
2604   tt_cmap13_init( TT_CMap13  cmap,
2605                   FT_Byte*   table )
2606   {
2607     cmap->cmap.data  = table;


2874       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2875       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2876       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2877 
2878     13,
2879     (TT_CMap_ValidateFunc)tt_cmap13_validate,  /* validate      */
2880     (TT_CMap_Info_GetFunc)tt_cmap13_get_info   /* get_cmap_info */
2881   )
2882 
2883 #endif /* TT_CONFIG_CMAP_FORMAT_13 */
2884 
2885 
2886   /*************************************************************************/
2887   /*************************************************************************/
2888   /*****                                                               *****/
2889   /*****                           FORMAT 14                           *****/
2890   /*****                                                               *****/
2891   /*************************************************************************/
2892   /*************************************************************************/
2893 
2894   /*************************************************************************/
2895   /*                                                                       */
2896   /* TABLE OVERVIEW                                                        */
2897   /* --------------                                                        */
2898   /*                                                                       */
2899   /*   NAME         OFFSET  TYPE    DESCRIPTION                            */
2900   /*                                                                       */
2901   /*   format         0     USHORT  must be 14                             */
2902   /*   length         2     ULONG   table length in bytes                  */
2903   /*   numSelector    6     ULONG   number of variation sel. records       */
2904   /*                                                                       */
2905   /* Followed by numSelector records, each of which looks like             */
2906   /*                                                                       */
2907   /*   varSelector    0     UINT24  Unicode codepoint of sel.              */
2908   /*   defaultOff     3     ULONG   offset to a default UVS table          */
2909   /*                                describing any variants to be found in */
2910   /*                                the normal Unicode subtable.           */
2911   /*   nonDefOff      7     ULONG   offset to a non-default UVS table      */
2912   /*                                describing any variants not in the     */
2913   /*                                standard cmap, with GIDs here          */
2914   /* (either offset may be 0 NULL)                                         */
2915   /*                                                                       */
2916   /* Selectors are sorted by code point.                                   */
2917   /*                                                                       */
2918   /* A default Unicode Variation Selector (UVS) subtable is just a list of */
2919   /* ranges of code points which are to be found in the standard cmap.  No */
2920   /* glyph IDs (GIDs) here.                                                */
2921   /*                                                                       */
2922   /*   numRanges      0     ULONG   number of ranges following             */
2923   /*                                                                       */
2924   /* A range looks like                                                    */
2925   /*                                                                       */
2926   /*   uniStart       0     UINT24  code point of the first character in   */
2927   /*                                this range                             */
2928   /*   additionalCnt  3     UBYTE   count of additional characters in this */
2929   /*                                range (zero means a range of a single  */
2930   /*                                character)                             */
2931   /*                                                                       */
2932   /* Ranges are sorted by `uniStart'.                                      */
2933   /*                                                                       */
2934   /* A non-default Unicode Variation Selector (UVS) subtable is a list of  */
2935   /* mappings from codepoint to GID.                                       */
2936   /*                                                                       */
2937   /*   numMappings    0     ULONG   number of mappings                     */
2938   /*                                                                       */
2939   /* A range looks like                                                    */
2940   /*                                                                       */
2941   /*   uniStart       0     UINT24  code point of the first character in   */
2942   /*                                this range                             */
2943   /*   GID            3     USHORT  and its GID                            */
2944   /*                                                                       */
2945   /* Ranges are sorted by `uniStart'.                                      */

2946 
2947 #ifdef TT_CONFIG_CMAP_FORMAT_14
2948 
2949   typedef struct  TT_CMap14Rec_
2950   {
2951     TT_CMapRec  cmap;
2952     FT_ULong    num_selectors;
2953 
2954     /* This array is used to store the results of various
2955      * cmap 14 query functions.  The data is overwritten
2956      * on each call to these functions.
2957      */
2958     FT_UInt32   max_results;
2959     FT_UInt32*  results;
2960     FT_Memory   memory;
2961 
2962   } TT_CMap14Rec, *TT_CMap14;
2963 
2964 
2965   FT_CALLBACK_DEF( void )


3664     FT_String*  PSname;
3665 
3666 
3667     tt_face_get_ps_name( face, idx, &PSname );
3668 
3669     return PSname;
3670   }
3671 
3672 
3673   FT_CALLBACK_DEF( FT_Error )
3674   tt_cmap_unicode_init( PS_Unicodes  unicodes,
3675                         FT_Pointer   pointer )
3676   {
3677     TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
3678     FT_Memory           memory  = FT_FACE_MEMORY( face );
3679     FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)face->psnames;
3680 
3681     FT_UNUSED( pointer );
3682 
3683 



3684     return psnames->unicodes_init( memory,
3685                                    unicodes,
3686                                    face->root.num_glyphs,
3687                                    (PS_GetGlyphNameFunc)&tt_get_glyph_name,
3688                                    (PS_FreeGlyphNameFunc)NULL,
3689                                    (FT_Pointer)face );
3690   }
3691 
3692 
3693   FT_CALLBACK_DEF( void )
3694   tt_cmap_unicode_done( PS_Unicodes  unicodes )
3695   {
3696     FT_Face    face   = FT_CMAP_FACE( unicodes );
3697     FT_Memory  memory = FT_FACE_MEMORY( face );
3698 
3699 
3700     FT_FREE( unicodes->maps );
3701     unicodes->num_maps = 0;
3702   }
3703 


3732       sizeof ( PS_UnicodesRec ),
3733 
3734       (FT_CMap_InitFunc)     tt_cmap_unicode_init,        /* init       */
3735       (FT_CMap_DoneFunc)     tt_cmap_unicode_done,        /* done       */
3736       (FT_CMap_CharIndexFunc)tt_cmap_unicode_char_index,  /* char_index */
3737       (FT_CMap_CharNextFunc) tt_cmap_unicode_char_next,   /* char_next  */
3738 
3739       (FT_CMap_CharVarIndexFunc)    NULL,  /* char_var_index   */
3740       (FT_CMap_CharVarIsDefaultFunc)NULL,  /* char_var_default */
3741       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
3742       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
3743       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
3744 
3745     ~0U,
3746     (TT_CMap_ValidateFunc)NULL,  /* validate      */
3747     (TT_CMap_Info_GetFunc)NULL   /* get_cmap_info */
3748   )
3749 
3750 #endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
3751 
3752 #ifndef FT_CONFIG_OPTION_PIC
3753 
3754   static const TT_CMap_Class  tt_cmap_classes[] =
3755   {
3756 #define TTCMAPCITEM( a )  &a,
3757 #include "ttcmapc.h"
3758     NULL,
3759   };
3760 
3761 #else /*FT_CONFIG_OPTION_PIC*/
3762 
3763   void
3764   FT_Destroy_Class_tt_cmap_classes( FT_Library      library,
3765                                     TT_CMap_Class*  clazz )
3766   {
3767     FT_Memory  memory = library->memory;
3768 
3769 
3770     if ( clazz )
3771       FT_FREE( clazz );
3772   }
3773 
3774 
3775   FT_Error
3776   FT_Create_Class_tt_cmap_classes( FT_Library       library,
3777                                    TT_CMap_Class**  output_class )
3778   {
3779     TT_CMap_Class*     clazz  = NULL;
3780     TT_CMap_ClassRec*  recs;
3781     FT_Error           error;
3782     FT_Memory          memory = library->memory;
3783 
3784     int  i = 0;
3785 
3786 
3787 #define TTCMAPCITEM( a ) i++;
3788 #include "ttcmapc.h"
3789 
3790     /* allocate enough space for both the pointers */
3791     /* plus terminator and the class instances     */
3792     if ( FT_ALLOC( clazz, sizeof ( *clazz ) * ( i + 1 ) +
3793                           sizeof ( TT_CMap_ClassRec ) * i ) )
3794       return error;
3795 
3796     /* the location of the class instances follows the array of pointers */
3797     recs = (TT_CMap_ClassRec*)( (char*)clazz +
3798                                 sizeof ( *clazz ) * ( i + 1 ) );
3799     i    = 0;
3800 
3801 #undef TTCMAPCITEM
3802 #define  TTCMAPCITEM( a )             \
3803     FT_Init_Class_ ## a( &recs[i] );  \
3804     clazz[i] = &recs[i];              \
3805     i++;
3806 #include "ttcmapc.h"
3807 
3808     clazz[i] = NULL;
3809 
3810     *output_class = clazz;
3811     return FT_Err_Ok;
3812   }
3813 
3814 #endif /*FT_CONFIG_OPTION_PIC*/
3815 
3816 
3817   /* parse the `cmap' table and build the corresponding TT_CMap objects */
3818   /* in the current face                                                */
3819   /*                                                                    */
3820   FT_LOCAL_DEF( FT_Error )
3821   tt_face_build_cmaps( TT_Face  face )
3822   {
3823     FT_Byte*           table = face->cmap_table;
3824     FT_Byte*           limit = table + face->cmap_size;
3825     FT_UInt volatile   num_cmaps;
3826     FT_Byte* volatile  p     = table;
3827     FT_Library         library = FT_FACE_LIBRARY( face );
3828 
3829     FT_UNUSED( library );
3830 
3831 
3832     if ( !p || p + 4 > limit )
3833       return FT_THROW( Invalid_Table );
3834 
3835     /* only recognize format 0 */


3842     }
3843 
3844     num_cmaps = TT_NEXT_USHORT( p );
3845 
3846     for ( ; num_cmaps > 0 && p + 8 <= limit; num_cmaps-- )
3847     {
3848       FT_CharMapRec  charmap;
3849       FT_UInt32      offset;
3850 
3851 
3852       charmap.platform_id = TT_NEXT_USHORT( p );
3853       charmap.encoding_id = TT_NEXT_USHORT( p );
3854       charmap.face        = FT_FACE( face );
3855       charmap.encoding    = FT_ENCODING_NONE;  /* will be filled later */
3856       offset              = TT_NEXT_ULONG( p );
3857 
3858       if ( offset && offset <= face->cmap_size - 2 )
3859       {
3860         FT_Byte* volatile              cmap   = table + offset;
3861         volatile FT_UInt               format = TT_PEEK_USHORT( cmap );
3862         const TT_CMap_Class* volatile  pclazz = TT_CMAP_CLASSES_GET;
3863         TT_CMap_Class volatile         clazz;
3864 
3865 
3866         for ( ; *pclazz; pclazz++ )
3867         {
3868           clazz = *pclazz;
3869           if ( clazz->format == format )
3870           {
3871             volatile TT_ValidatorRec  valid;
3872             volatile FT_Error         error = FT_Err_Ok;
3873 
3874 
3875             ft_validator_init( FT_VALIDATOR( &valid ), cmap, limit,
3876                                FT_VALIDATE_DEFAULT );
3877 
3878             valid.num_glyphs = (FT_UInt)face->max_profile.numGlyphs;
3879 
3880             if ( ft_setjmp( FT_VALIDATOR( &valid )->jump_buffer) == 0 )
3881             {
3882               /* validate this cmap sub-table */


   1 /****************************************************************************
   2  *
   3  * ttcmap.c
   4  *
   5  *   TrueType character mapping table (cmap) support (body).
   6  *
   7  * Copyright (C) 2002-2019 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  */
  17 
  18 
  19 #include <ft2build.h>
  20 #include FT_INTERNAL_DEBUG_H
  21 
  22 #include "sferrors.h"           /* must come before FT_INTERNAL_VALIDATE_H */
  23 
  24 #include FT_INTERNAL_VALIDATE_H
  25 #include FT_INTERNAL_STREAM_H
  26 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
  27 #include "ttload.h"
  28 #include "ttcmap.h"
  29 #include "ttpost.h"

  30 
  31 
  32   /**************************************************************************
  33    *
  34    * The macro FT_COMPONENT is used in trace mode.  It is an implicit
  35    * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
  36    * messages during execution.
  37    */
  38 #undef  FT_COMPONENT
  39 #define FT_COMPONENT  ttcmap
  40 
  41 
  42 #define TT_PEEK_SHORT   FT_PEEK_SHORT
  43 #define TT_PEEK_USHORT  FT_PEEK_USHORT
  44 #define TT_PEEK_UINT24  FT_PEEK_UOFF3
  45 #define TT_PEEK_LONG    FT_PEEK_LONG
  46 #define TT_PEEK_ULONG   FT_PEEK_ULONG
  47 
  48 #define TT_NEXT_SHORT   FT_NEXT_SHORT
  49 #define TT_NEXT_USHORT  FT_NEXT_USHORT
  50 #define TT_NEXT_UINT24  FT_NEXT_UOFF3
  51 #define TT_NEXT_LONG    FT_NEXT_LONG
  52 #define TT_NEXT_ULONG   FT_NEXT_ULONG
  53 
  54 
  55   /* Too large glyph index return values are caught in `FT_Get_Char_Index' */
  56   /* and `FT_Get_Next_Char' (the latter calls the internal `next' function */
  57   /* again in this case).  To mark character code return values as invalid */
  58   /* it is sufficient to set the corresponding glyph index return value to */
  59   /* zero.                                                                 */
  60 
  61 
  62   FT_CALLBACK_DEF( FT_Error )
  63   tt_cmap_init( TT_CMap   cmap,
  64                 FT_Byte*  table )
  65   {
  66     cmap->data = table;
  67     return FT_Err_Ok;
  68   }
  69 
  70 
  71   /*************************************************************************/
  72   /*************************************************************************/
  73   /*****                                                               *****/
  74   /*****                           FORMAT 0                            *****/
  75   /*****                                                               *****/
  76   /*************************************************************************/
  77   /*************************************************************************/
  78 
  79   /**************************************************************************
  80    *
  81    * TABLE OVERVIEW
  82    * --------------
  83    *
  84    *   NAME        OFFSET         TYPE          DESCRIPTION
  85    *
  86    *   format      0              USHORT        must be 0
  87    *   length      2              USHORT        table length in bytes
  88    *   language    4              USHORT        Mac language code
  89    *   glyph_ids   6              BYTE[256]     array of glyph indices
  90    *               262
  91    */
  92 
  93 #ifdef TT_CONFIG_CMAP_FORMAT_0
  94 
  95   FT_CALLBACK_DEF( FT_Error )
  96   tt_cmap0_validate( FT_Byte*      table,
  97                      FT_Validator  valid )
  98   {
  99     FT_Byte*  p;
 100     FT_UInt   length;
 101 
 102 
 103     if ( table + 2 + 2 > valid->limit )
 104       FT_INVALID_TOO_SHORT;
 105 
 106     p      = table + 2;           /* skip format */
 107     length = TT_NEXT_USHORT( p );
 108 
 109     if ( table + length > valid->limit || length < 262 )
 110       FT_INVALID_TOO_SHORT;
 111 


 220   /*****                                                               *****/
 221   /***** The following charmap lookup and iteration functions all      *****/
 222   /***** assume that the value `charcode' fulfills the following.      *****/
 223   /*****                                                               *****/
 224   /*****   - For one-byte characters, `charcode' is simply the         *****/
 225   /*****     character code.                                           *****/
 226   /*****                                                               *****/
 227   /*****   - For two-byte characters, `charcode' is the 2-byte         *****/
 228   /*****     character code in big endian format.  More precisely:     *****/
 229   /*****                                                               *****/
 230   /*****       (charcode >> 8)    is the first byte value              *****/
 231   /*****       (charcode & 0xFF)  is the second byte value             *****/
 232   /*****                                                               *****/
 233   /***** Note that not all values of `charcode' are valid according    *****/
 234   /***** to these rules, and the function moderately checks the        *****/
 235   /***** arguments.                                                    *****/
 236   /*****                                                               *****/
 237   /*************************************************************************/
 238   /*************************************************************************/
 239 
 240   /**************************************************************************
 241    *
 242    * TABLE OVERVIEW
 243    * --------------
 244    *
 245    *   NAME        OFFSET         TYPE            DESCRIPTION
 246    *
 247    *   format      0              USHORT          must be 2
 248    *   length      2              USHORT          table length in bytes
 249    *   language    4              USHORT          Mac language code
 250    *   keys        6              USHORT[256]     sub-header keys
 251    *   subs        518            SUBHEAD[NSUBS]  sub-headers array
 252    *   glyph_ids   518+NSUB*8     USHORT[]        glyph ID array
 253    *
 254    * The `keys' table is used to map charcode high bytes to sub-headers.
 255    * The value of `NSUBS' is the number of sub-headers defined in the
 256    * table and is computed by finding the maximum of the `keys' table.
 257    *
 258    * Note that for any `n', `keys[n]' is a byte offset within the `subs'
 259    * table, i.e., it is the corresponding sub-header index multiplied
 260    * by 8.
 261    *
 262    * Each sub-header has the following format.
 263    *
 264    *   NAME        OFFSET      TYPE            DESCRIPTION
 265    *
 266    *   first       0           USHORT          first valid low-byte
 267    *   count       2           USHORT          number of valid low-bytes
 268    *   delta       4           SHORT           see below
 269    *   offset      6           USHORT          see below
 270    *
 271    * A sub-header defines, for each high byte, the range of valid
 272    * low bytes within the charmap.  Note that the range defined by `first'
 273    * and `count' must be completely included in the interval [0..255]
 274    * according to the specification.
 275    *
 276    * If a character code is contained within a given sub-header, then
 277    * mapping it to a glyph index is done as follows.
 278    *
 279    * - The value of `offset' is read.  This is a _byte_ distance from the
 280    *   location of the `offset' field itself into a slice of the
 281    *   `glyph_ids' table.  Let's call it `slice' (it is a USHORT[], too).
 282    *
 283    * - The value `slice[char.lo - first]' is read.  If it is 0, there is
 284    *   no glyph for the charcode.  Otherwise, the value of `delta' is
 285    *   added to it (modulo 65536) to form a new glyph index.
 286    *
 287    * It is up to the validation routine to check that all offsets fall
 288    * within the glyph IDs table (and not within the `subs' table itself or
 289    * outside of the CMap).
 290    */
 291 
 292 #ifdef TT_CONFIG_CMAP_FORMAT_2
 293 
 294   FT_CALLBACK_DEF( FT_Error )
 295   tt_cmap2_validate( FT_Byte*      table,
 296                      FT_Validator  valid )
 297   {
 298     FT_Byte*  p;
 299     FT_UInt   length;
 300 
 301     FT_UInt   n, max_subs;
 302     FT_Byte*  keys;        /* keys table     */
 303     FT_Byte*  subs;        /* sub-headers    */
 304     FT_Byte*  glyph_ids;   /* glyph ID array */
 305 
 306 
 307     if ( table + 2 + 2 > valid->limit )
 308       FT_INVALID_TOO_SHORT;
 309 
 310     p      = table + 2;           /* skip format */


 608       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
 609       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
 610       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
 611 
 612     2,
 613     (TT_CMap_ValidateFunc)tt_cmap2_validate,  /* validate      */
 614     (TT_CMap_Info_GetFunc)tt_cmap2_get_info   /* get_cmap_info */
 615   )
 616 
 617 #endif /* TT_CONFIG_CMAP_FORMAT_2 */
 618 
 619 
 620   /*************************************************************************/
 621   /*************************************************************************/
 622   /*****                                                               *****/
 623   /*****                           FORMAT 4                            *****/
 624   /*****                                                               *****/
 625   /*************************************************************************/
 626   /*************************************************************************/
 627 
 628   /**************************************************************************
 629    *
 630    * TABLE OVERVIEW
 631    * --------------
 632    *
 633    *   NAME          OFFSET         TYPE              DESCRIPTION
 634    *
 635    *   format        0              USHORT            must be 4
 636    *   length        2              USHORT            table length
 637    *                                                  in bytes
 638    *   language      4              USHORT            Mac language code
 639    *
 640    *   segCountX2    6              USHORT            2*NUM_SEGS
 641    *   searchRange   8              USHORT            2*(1 << LOG_SEGS)
 642    *   entrySelector 10             USHORT            LOG_SEGS
 643    *   rangeShift    12             USHORT            segCountX2 -
 644    *                                                    searchRange
 645    *
 646    *   endCount      14             USHORT[NUM_SEGS]  end charcode for
 647    *                                                  each segment; last
 648    *                                                  is 0xFFFF
 649    *
 650    *   pad           14+NUM_SEGS*2  USHORT            padding
 651    *
 652    *   startCount    16+NUM_SEGS*2  USHORT[NUM_SEGS]  first charcode for
 653    *                                                  each segment
 654    *
 655    *   idDelta       16+NUM_SEGS*4  SHORT[NUM_SEGS]   delta for each
 656    *                                                  segment
 657    *   idOffset      16+NUM_SEGS*6  SHORT[NUM_SEGS]   range offset for
 658    *                                                  each segment; can be
 659    *                                                  zero
 660    *
 661    *   glyphIds      16+NUM_SEGS*8  USHORT[]          array of glyph ID
 662    *                                                  ranges
 663    *
 664    * Character codes are modelled by a series of ordered (increasing)
 665    * intervals called segments.  Each segment has start and end codes,
 666    * provided by the `startCount' and `endCount' arrays.  Segments must
 667    * not overlap, and the last segment should always contain the value
 668    * 0xFFFF for `endCount'.
 669    *
 670    * The fields `searchRange', `entrySelector' and `rangeShift' are better
 671    * ignored (they are traces of over-engineering in the TrueType
 672    * specification).
 673    *
 674    * Each segment also has a signed `delta', as well as an optional offset
 675    * within the `glyphIds' table.
 676    *
 677    * If a segment's idOffset is 0, the glyph index corresponding to any
 678    * charcode within the segment is obtained by adding the value of
 679    * `idDelta' directly to the charcode, modulo 65536.
 680    *
 681    * Otherwise, a glyph index is taken from the glyph IDs sub-array for
 682    * the segment, and the value of `idDelta' is added to it.
 683    *
 684    *
 685    * Finally, note that a lot of fonts contain an invalid last segment,
 686    * where `start' and `end' are correctly set to 0xFFFF but both `delta'
 687    * and `offset' are incorrect (e.g., `opens___.ttf' which comes with
 688    * OpenOffice.org).  We need special code to deal with them correctly.
 689    */
 690 
 691 #ifdef TT_CONFIG_CMAP_FORMAT_4
 692 
 693   typedef struct  TT_CMap4Rec_
 694   {
 695     TT_CMapRec  cmap;
 696     FT_UInt32   cur_charcode;   /* current charcode */
 697     FT_UInt     cur_gindex;     /* current glyph index */
 698 
 699     FT_UInt     num_ranges;
 700     FT_UInt     cur_range;
 701     FT_UInt     cur_start;
 702     FT_UInt     cur_end;
 703     FT_Int      cur_delta;
 704     FT_Byte*    cur_values;
 705 
 706   } TT_CMap4Rec, *TT_CMap4;
 707 
 708 
 709   FT_CALLBACK_DEF( FT_Error )


1555       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
1556       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
1557       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
1558 
1559     4,
1560     (TT_CMap_ValidateFunc)tt_cmap4_validate,  /* validate      */
1561     (TT_CMap_Info_GetFunc)tt_cmap4_get_info   /* get_cmap_info */
1562   )
1563 
1564 #endif /* TT_CONFIG_CMAP_FORMAT_4 */
1565 
1566 
1567   /*************************************************************************/
1568   /*************************************************************************/
1569   /*****                                                               *****/
1570   /*****                          FORMAT 6                             *****/
1571   /*****                                                               *****/
1572   /*************************************************************************/
1573   /*************************************************************************/
1574 
1575   /**************************************************************************
1576    *
1577    * TABLE OVERVIEW
1578    * --------------
1579    *
1580    *   NAME        OFFSET          TYPE             DESCRIPTION
1581    *
1582    *   format       0              USHORT           must be 6
1583    *   length       2              USHORT           table length in bytes
1584    *   language     4              USHORT           Mac language code
1585    *
1586    *   first        6              USHORT           first segment code
1587    *   count        8              USHORT           segment size in chars
1588    *   glyphIds     10             USHORT[count]    glyph IDs
1589    *
1590    * A very simplified segment mapping.
1591    */
1592 
1593 #ifdef TT_CONFIG_CMAP_FORMAT_6
1594 
1595   FT_CALLBACK_DEF( FT_Error )
1596   tt_cmap6_validate( FT_Byte*      table,
1597                      FT_Validator  valid )
1598   {
1599     FT_Byte*  p;
1600     FT_UInt   length, count;
1601 
1602 
1603     if ( table + 10 > valid->limit )
1604       FT_INVALID_TOO_SHORT;
1605 
1606     p      = table + 2;
1607     length = TT_NEXT_USHORT( p );
1608 
1609     p      = table + 8;             /* skip language and start index */
1610     count  = TT_NEXT_USHORT( p );
1611 


1750   /*****     Area (i.e. U+D800-U+DFFF).                                *****/
1751   /*****                                                               *****/
1752   /*****   - A 32-bit value, made of two surrogate values, i.e.. if    *****/
1753   /*****     `char_code = (char_hi << 16) | char_lo', then both        *****/
1754   /*****     `char_hi' and `char_lo' must be in the Surrogates Area.   *****/
1755   /*****      Area.                                                    *****/
1756   /*****                                                               *****/
1757   /***** The `is32' table embedded in the charmap indicates whether a  *****/
1758   /***** given 16-bit value is in the surrogates area or not.          *****/
1759   /*****                                                               *****/
1760   /***** So, for any given `char_code', we can assert the following.   *****/
1761   /*****                                                               *****/
1762   /*****   If `char_hi == 0' then we must have `is32[char_lo] == 0'.   *****/
1763   /*****                                                               *****/
1764   /*****   If `char_hi != 0' then we must have both                    *****/
1765   /*****   `is32[char_hi] != 0' and `is32[char_lo] != 0'.              *****/
1766   /*****                                                               *****/
1767   /*************************************************************************/
1768   /*************************************************************************/
1769 
1770   /**************************************************************************
1771    *
1772    * TABLE OVERVIEW
1773    * --------------
1774    *
1775    *   NAME        OFFSET         TYPE        DESCRIPTION
1776    *
1777    *   format      0              USHORT      must be 8
1778    *   reserved    2              USHORT      reserved
1779    *   length      4              ULONG       length in bytes
1780    *   language    8              ULONG       Mac language code
1781    *   is32        12             BYTE[8192]  32-bitness bitmap
1782    *   count       8204           ULONG       number of groups
1783    *
1784    * This header is followed by `count' groups of the following format:
1785    *
1786    *   start       0              ULONG       first charcode
1787    *   end         4              ULONG       last charcode
1788    *   startId     8              ULONG       start glyph ID for the group
1789    */
1790 
1791 #ifdef TT_CONFIG_CMAP_FORMAT_8
1792 
1793   FT_CALLBACK_DEF( FT_Error )
1794   tt_cmap8_validate( FT_Byte*      table,
1795                      FT_Validator  valid )
1796   {
1797     FT_Byte*   p = table + 4;
1798     FT_Byte*   is32;
1799     FT_UInt32  length;
1800     FT_UInt32  num_groups;
1801 
1802 
1803     if ( table + 16 + 8192 > valid->limit )
1804       FT_INVALID_TOO_SHORT;
1805 
1806     length = TT_NEXT_ULONG( p );
1807     if ( length > (FT_UInt32)( valid->limit - table ) || length < 8192 + 16 )
1808       FT_INVALID_TOO_SHORT;
1809 


2019       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2020       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2021       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2022 
2023     8,
2024     (TT_CMap_ValidateFunc)tt_cmap8_validate,  /* validate      */
2025     (TT_CMap_Info_GetFunc)tt_cmap8_get_info   /* get_cmap_info */
2026   )
2027 
2028 #endif /* TT_CONFIG_CMAP_FORMAT_8 */
2029 
2030 
2031   /*************************************************************************/
2032   /*************************************************************************/
2033   /*****                                                               *****/
2034   /*****                          FORMAT 10                            *****/
2035   /*****                                                               *****/
2036   /*************************************************************************/
2037   /*************************************************************************/
2038 
2039   /**************************************************************************
2040    *
2041    * TABLE OVERVIEW
2042    * --------------
2043    *
2044    *   NAME      OFFSET  TYPE               DESCRIPTION
2045    *
2046    *   format     0      USHORT             must be 10
2047    *   reserved   2      USHORT             reserved
2048    *   length     4      ULONG              length in bytes
2049    *   language   8      ULONG              Mac language code
2050    *
2051    *   start     12      ULONG              first char in range
2052    *   count     16      ULONG              number of chars in range
2053    *   glyphIds  20      USHORT[count]      glyph indices covered
2054    */
2055 
2056 #ifdef TT_CONFIG_CMAP_FORMAT_10
2057 
2058   FT_CALLBACK_DEF( FT_Error )
2059   tt_cmap10_validate( FT_Byte*      table,
2060                       FT_Validator  valid )
2061   {
2062     FT_Byte*  p = table + 4;
2063     FT_ULong  length, count;
2064 
2065 
2066     if ( table + 20 > valid->limit )
2067       FT_INVALID_TOO_SHORT;
2068 
2069     length = TT_NEXT_ULONG( p );
2070     p      = table + 16;
2071     count  = TT_NEXT_ULONG( p );
2072 
2073     if ( length > (FT_ULong)( valid->limit - table ) ||
2074          /* length < 20 + count * 2 ? */


2191       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2192       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2193       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2194 
2195     10,
2196     (TT_CMap_ValidateFunc)tt_cmap10_validate,  /* validate      */
2197     (TT_CMap_Info_GetFunc)tt_cmap10_get_info   /* get_cmap_info */
2198   )
2199 
2200 #endif /* TT_CONFIG_CMAP_FORMAT_10 */
2201 
2202 
2203   /*************************************************************************/
2204   /*************************************************************************/
2205   /*****                                                               *****/
2206   /*****                          FORMAT 12                            *****/
2207   /*****                                                               *****/
2208   /*************************************************************************/
2209   /*************************************************************************/
2210 
2211   /**************************************************************************
2212    *
2213    * TABLE OVERVIEW
2214    * --------------
2215    *
2216    *   NAME        OFFSET     TYPE       DESCRIPTION
2217    *
2218    *   format      0          USHORT     must be 12
2219    *   reserved    2          USHORT     reserved
2220    *   length      4          ULONG      length in bytes
2221    *   language    8          ULONG      Mac language code
2222    *   count       12         ULONG      number of groups
2223    *               16
2224    *
2225    * This header is followed by `count' groups of the following format:
2226    *
2227    *   start       0          ULONG      first charcode
2228    *   end         4          ULONG      last charcode
2229    *   startId     8          ULONG      start glyph ID for the group
2230    */
2231 
2232 #ifdef TT_CONFIG_CMAP_FORMAT_12
2233 
2234   typedef struct  TT_CMap12Rec_
2235   {
2236     TT_CMapRec  cmap;
2237     FT_Bool     valid;
2238     FT_ULong    cur_charcode;
2239     FT_UInt     cur_gindex;
2240     FT_ULong    cur_group;
2241     FT_ULong    num_groups;
2242 
2243   } TT_CMap12Rec, *TT_CMap12;
2244 
2245 
2246   FT_CALLBACK_DEF( FT_Error )
2247   tt_cmap12_init( TT_CMap12  cmap,
2248                   FT_Byte*   table )
2249   {
2250     cmap->cmap.data  = table;


2547       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2548       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2549       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2550 
2551     12,
2552     (TT_CMap_ValidateFunc)tt_cmap12_validate,  /* validate      */
2553     (TT_CMap_Info_GetFunc)tt_cmap12_get_info   /* get_cmap_info */
2554   )
2555 
2556 #endif /* TT_CONFIG_CMAP_FORMAT_12 */
2557 
2558 
2559   /*************************************************************************/
2560   /*************************************************************************/
2561   /*****                                                               *****/
2562   /*****                          FORMAT 13                            *****/
2563   /*****                                                               *****/
2564   /*************************************************************************/
2565   /*************************************************************************/
2566 
2567   /**************************************************************************
2568    *
2569    * TABLE OVERVIEW
2570    * --------------
2571    *
2572    *   NAME        OFFSET     TYPE       DESCRIPTION
2573    *
2574    *   format      0          USHORT     must be 13
2575    *   reserved    2          USHORT     reserved
2576    *   length      4          ULONG      length in bytes
2577    *   language    8          ULONG      Mac language code
2578    *   count       12         ULONG      number of groups
2579    *               16
2580    *
2581    * This header is followed by `count' groups of the following format:
2582    *
2583    *   start       0          ULONG      first charcode
2584    *   end         4          ULONG      last charcode
2585    *   glyphId     8          ULONG      glyph ID for the whole group
2586    */
2587 
2588 #ifdef TT_CONFIG_CMAP_FORMAT_13
2589 
2590   typedef struct  TT_CMap13Rec_
2591   {
2592     TT_CMapRec  cmap;
2593     FT_Bool     valid;
2594     FT_ULong    cur_charcode;
2595     FT_UInt     cur_gindex;
2596     FT_ULong    cur_group;
2597     FT_ULong    num_groups;
2598 
2599   } TT_CMap13Rec, *TT_CMap13;
2600 
2601 
2602   FT_CALLBACK_DEF( FT_Error )
2603   tt_cmap13_init( TT_CMap13  cmap,
2604                   FT_Byte*   table )
2605   {
2606     cmap->cmap.data  = table;


2873       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
2874       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
2875       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
2876 
2877     13,
2878     (TT_CMap_ValidateFunc)tt_cmap13_validate,  /* validate      */
2879     (TT_CMap_Info_GetFunc)tt_cmap13_get_info   /* get_cmap_info */
2880   )
2881 
2882 #endif /* TT_CONFIG_CMAP_FORMAT_13 */
2883 
2884 
2885   /*************************************************************************/
2886   /*************************************************************************/
2887   /*****                                                               *****/
2888   /*****                           FORMAT 14                           *****/
2889   /*****                                                               *****/
2890   /*************************************************************************/
2891   /*************************************************************************/
2892 
2893   /**************************************************************************
2894    *
2895    * TABLE OVERVIEW
2896    * --------------
2897    *
2898    *   NAME         OFFSET  TYPE    DESCRIPTION
2899    *
2900    *   format         0     USHORT  must be 14
2901    *   length         2     ULONG   table length in bytes
2902    *   numSelector    6     ULONG   number of variation sel. records
2903    *
2904    * Followed by numSelector records, each of which looks like
2905    *
2906    *   varSelector    0     UINT24  Unicode codepoint of sel.
2907    *   defaultOff     3     ULONG   offset to a default UVS table
2908    *                                describing any variants to be found in
2909    *                                the normal Unicode subtable.
2910    *   nonDefOff      7     ULONG   offset to a non-default UVS table
2911    *                                describing any variants not in the
2912    *                                standard cmap, with GIDs here
2913    * (either offset may be 0 NULL)
2914    *
2915    * Selectors are sorted by code point.
2916    *
2917    * A default Unicode Variation Selector (UVS) subtable is just a list of
2918    * ranges of code points which are to be found in the standard cmap.  No
2919    * glyph IDs (GIDs) here.
2920    *
2921    *   numRanges      0     ULONG   number of ranges following
2922    *
2923    * A range looks like
2924    *
2925    *   uniStart       0     UINT24  code point of the first character in
2926    *                                this range
2927    *   additionalCnt  3     UBYTE   count of additional characters in this
2928    *                                range (zero means a range of a single
2929    *                                character)
2930    *
2931    * Ranges are sorted by `uniStart'.
2932    *
2933    * A non-default Unicode Variation Selector (UVS) subtable is a list of
2934    * mappings from codepoint to GID.
2935    *
2936    *   numMappings    0     ULONG   number of mappings
2937    *
2938    * A range looks like
2939    *
2940    *   uniStart       0     UINT24  code point of the first character in
2941    *                                this range
2942    *   GID            3     USHORT  and its GID
2943    *
2944    * Ranges are sorted by `uniStart'.
2945    */
2946 
2947 #ifdef TT_CONFIG_CMAP_FORMAT_14
2948 
2949   typedef struct  TT_CMap14Rec_
2950   {
2951     TT_CMapRec  cmap;
2952     FT_ULong    num_selectors;
2953 
2954     /* This array is used to store the results of various
2955      * cmap 14 query functions.  The data is overwritten
2956      * on each call to these functions.
2957      */
2958     FT_UInt32   max_results;
2959     FT_UInt32*  results;
2960     FT_Memory   memory;
2961 
2962   } TT_CMap14Rec, *TT_CMap14;
2963 
2964 
2965   FT_CALLBACK_DEF( void )


3664     FT_String*  PSname;
3665 
3666 
3667     tt_face_get_ps_name( face, idx, &PSname );
3668 
3669     return PSname;
3670   }
3671 
3672 
3673   FT_CALLBACK_DEF( FT_Error )
3674   tt_cmap_unicode_init( PS_Unicodes  unicodes,
3675                         FT_Pointer   pointer )
3676   {
3677     TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
3678     FT_Memory           memory  = FT_FACE_MEMORY( face );
3679     FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)face->psnames;
3680 
3681     FT_UNUSED( pointer );
3682 
3683 
3684     if ( !psnames->unicodes_init )
3685       return FT_THROW( Unimplemented_Feature );
3686 
3687     return psnames->unicodes_init( memory,
3688                                    unicodes,
3689                                    face->root.num_glyphs,
3690                                    (PS_GetGlyphNameFunc)&tt_get_glyph_name,
3691                                    (PS_FreeGlyphNameFunc)NULL,
3692                                    (FT_Pointer)face );
3693   }
3694 
3695 
3696   FT_CALLBACK_DEF( void )
3697   tt_cmap_unicode_done( PS_Unicodes  unicodes )
3698   {
3699     FT_Face    face   = FT_CMAP_FACE( unicodes );
3700     FT_Memory  memory = FT_FACE_MEMORY( face );
3701 
3702 
3703     FT_FREE( unicodes->maps );
3704     unicodes->num_maps = 0;
3705   }
3706 


3735       sizeof ( PS_UnicodesRec ),
3736 
3737       (FT_CMap_InitFunc)     tt_cmap_unicode_init,        /* init       */
3738       (FT_CMap_DoneFunc)     tt_cmap_unicode_done,        /* done       */
3739       (FT_CMap_CharIndexFunc)tt_cmap_unicode_char_index,  /* char_index */
3740       (FT_CMap_CharNextFunc) tt_cmap_unicode_char_next,   /* char_next  */
3741 
3742       (FT_CMap_CharVarIndexFunc)    NULL,  /* char_var_index   */
3743       (FT_CMap_CharVarIsDefaultFunc)NULL,  /* char_var_default */
3744       (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
3745       (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
3746       (FT_CMap_VariantCharListFunc) NULL,  /* variantchar_list */
3747 
3748     ~0U,
3749     (TT_CMap_ValidateFunc)NULL,  /* validate      */
3750     (TT_CMap_Info_GetFunc)NULL   /* get_cmap_info */
3751   )
3752 
3753 #endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
3754 

3755 
3756   static const TT_CMap_Class  tt_cmap_classes[] =
3757   {
3758 #define TTCMAPCITEM( a )  &a,
3759 #include "ttcmapc.h"
3760     NULL,
3761   };
3762 























































3763 
3764   /* parse the `cmap' table and build the corresponding TT_CMap objects */
3765   /* in the current face                                                */
3766   /*                                                                    */
3767   FT_LOCAL_DEF( FT_Error )
3768   tt_face_build_cmaps( TT_Face  face )
3769   {
3770     FT_Byte*           table = face->cmap_table;
3771     FT_Byte*           limit = table + face->cmap_size;
3772     FT_UInt volatile   num_cmaps;
3773     FT_Byte* volatile  p     = table;
3774     FT_Library         library = FT_FACE_LIBRARY( face );
3775 
3776     FT_UNUSED( library );
3777 
3778 
3779     if ( !p || p + 4 > limit )
3780       return FT_THROW( Invalid_Table );
3781 
3782     /* only recognize format 0 */


3789     }
3790 
3791     num_cmaps = TT_NEXT_USHORT( p );
3792 
3793     for ( ; num_cmaps > 0 && p + 8 <= limit; num_cmaps-- )
3794     {
3795       FT_CharMapRec  charmap;
3796       FT_UInt32      offset;
3797 
3798 
3799       charmap.platform_id = TT_NEXT_USHORT( p );
3800       charmap.encoding_id = TT_NEXT_USHORT( p );
3801       charmap.face        = FT_FACE( face );
3802       charmap.encoding    = FT_ENCODING_NONE;  /* will be filled later */
3803       offset              = TT_NEXT_ULONG( p );
3804 
3805       if ( offset && offset <= face->cmap_size - 2 )
3806       {
3807         FT_Byte* volatile              cmap   = table + offset;
3808         volatile FT_UInt               format = TT_PEEK_USHORT( cmap );
3809         const TT_CMap_Class* volatile  pclazz = tt_cmap_classes;
3810         TT_CMap_Class volatile         clazz;
3811 
3812 
3813         for ( ; *pclazz; pclazz++ )
3814         {
3815           clazz = *pclazz;
3816           if ( clazz->format == format )
3817           {
3818             volatile TT_ValidatorRec  valid;
3819             volatile FT_Error         error = FT_Err_Ok;
3820 
3821 
3822             ft_validator_init( FT_VALIDATOR( &valid ), cmap, limit,
3823                                FT_VALIDATE_DEFAULT );
3824 
3825             valid.num_glyphs = (FT_UInt)face->max_profile.numGlyphs;
3826 
3827             if ( ft_setjmp( FT_VALIDATOR( &valid )->jump_buffer) == 0 )
3828             {
3829               /* validate this cmap sub-table */


< prev index next >