root/src/sfnt.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


     1 /* sfnt format font support for GNU Emacs.
     2 
     3 Copyright (C) 2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 #ifndef _SFNT_H_
    21 #define _SFNT_H_
    22 
    23 #include <stdint.h>
    24 #include <stddef.h>
    25 #include <setjmp.h>
    26 
    27 #include <sys/types.h>
    28 
    29 
    30 
    31 /* Container structure and enumerator definitions.  */
    32 
    33 /* The sfnt container format is organized into different tables, such
    34    as ``cmap'' or ``glyf''.  Each of these tables has a specific
    35    format and use.  These are all the tables known to Emacs.  */
    36 
    37 enum sfnt_table
    38   {
    39     SFNT_TABLE_CMAP,
    40     SFNT_TABLE_GLYF,
    41     SFNT_TABLE_HEAD,
    42     SFNT_TABLE_HHEA,
    43     SFNT_TABLE_HMTX,
    44     SFNT_TABLE_LOCA,
    45     SFNT_TABLE_MAXP,
    46     SFNT_TABLE_NAME,
    47     SFNT_TABLE_META,
    48     SFNT_TABLE_CVT ,
    49     SFNT_TABLE_FPGM,
    50     SFNT_TABLE_PREP,
    51     SFNT_TABLE_FVAR,
    52     SFNT_TABLE_GVAR,
    53     SFNT_TABLE_CVAR,
    54     SFNT_TABLE_AVAR,
    55   };
    56 
    57 #define SFNT_ENDOF(type, field, type1)                  \
    58   ((size_t) offsetof (type, field) + sizeof (type1))
    59 
    60 /* Each of these structures must be aligned so that no compiler will
    61    ever generate padding bytes on platforms where the alignment
    62    requirements for uint32_t and uint16_t are no larger than 4 and 2
    63    bytes respectively.
    64 
    65    Pointer types are assumed to impose an alignmnent requirement no
    66    less than that of uint32_t.
    67 
    68    If a table has more than one kind of variable-length subtable array
    69    at the end, make sure to pad subsequent subtables
    70    appropriately.  */
    71 
    72 struct sfnt_offset_subtable
    73 {
    74   /* The scaler type.  */
    75   uint32_t scaler_type;
    76 
    77   /* The number of tables.  */
    78   uint16_t num_tables;
    79 
    80   /* (Maximum power of 2 <= numTables) * 16.  */
    81   uint16_t search_range;
    82 
    83   /* log2 (maximum power of 2 <= numTables) */
    84   uint16_t entry_selector;
    85 
    86   /* numTables * 16 - searchRange.  */
    87   uint16_t range_shift;
    88 
    89   /* Variable length data.  */
    90   struct sfnt_table_directory *subtables;
    91 };
    92 
    93 /* The table directory.  Follows the offset subtable, with one for
    94    each table.  */
    95 
    96 struct sfnt_table_directory
    97 {
    98   /* 4-byte identifier for each table.  See sfnt_table_names.  */
    99   uint32_t tag;
   100 
   101   /* Table checksum.  */
   102   uint32_t checksum;
   103 
   104   /* Offset from the start of the file.  */
   105   uint32_t offset;
   106 
   107   /* Length of the table in bytes, not subject to padding.  */
   108   uint32_t length;
   109 };
   110 
   111 enum sfnt_scaler_type
   112   {
   113     SFNT_SCALER_TRUE = 0x74727565,
   114     SFNT_SCALER_VER1 = 0x00010000,
   115     SFNT_SCALER_TYP1 = 0x74797031,
   116     SFNT_SCALER_OTTO = 0x4F54544F,
   117   };
   118 
   119 typedef int32_t sfnt_fixed;
   120 typedef int16_t sfnt_fword;
   121 typedef uint16_t sfnt_ufword;
   122 
   123 #define sfnt_coerce_fixed(fixed) ((sfnt_fixed) (fixed) / 65535.0)
   124 
   125 typedef unsigned int sfnt_glyph;
   126 typedef unsigned int sfnt_char;
   127 
   128 struct sfnt_head_table
   129 {
   130   /* The version.  This is a 16.16 fixed point number.  */
   131   sfnt_fixed version;
   132 
   133   /* The revision.  */
   134   sfnt_fixed revision;
   135 
   136   /* Checksum adjustment.  */
   137   uint32_t checksum_adjustment;
   138 
   139   /* Magic number, should be 0x5F0F3CF5.  */
   140   uint32_t magic;
   141 
   142   /* Flags for the font.  */
   143   uint16_t flags;
   144 
   145   /* Units per em.  */
   146   uint16_t units_per_em;
   147 
   148   /* Time of creation.  */
   149   uint32_t created_high, created_low;
   150 
   151   /* Time of modification.  */
   152   uint32_t modified_high, modified_low;
   153 
   154   /* Minimum bounds.  */
   155   sfnt_fword xmin, ymin, xmax, ymax;
   156 
   157   /* Mac specific stuff.  */
   158   uint16_t mac_style;
   159 
   160   /* Smallest readable size in pixels.  */
   161   uint16_t lowest_rec_ppem;
   162 
   163   /* Font direction hint.  */
   164   int16_t font_direction_hint;
   165 
   166   /* Index to loc format.  0 for short offsets, 1 for long.  */
   167   int16_t index_to_loc_format;
   168 
   169   /* Unused.  */
   170   int16_t glyph_data_format;
   171 };
   172 
   173 struct sfnt_hhea_table
   174 {
   175   /* The version.  This is a 16.16 fixed point number.  */
   176   sfnt_fixed version;
   177 
   178   /* The maximum ascent and descent values for this font.  */
   179   sfnt_fword ascent, descent;
   180 
   181   /* The typographic line gap.  */
   182   sfnt_fword line_gap;
   183 
   184   /* The maximum advance width.  */
   185   sfnt_ufword advance_width_max;
   186 
   187   /* The minimum bearings on either side.  */
   188   sfnt_fword min_left_side_bearing, min_right_side_bearing;
   189 
   190   /* The maximum extent.  */
   191   sfnt_fword x_max_extent;
   192 
   193   /* Caret slope.  */
   194   int16_t caret_slope_rise, caret_slope_run;
   195 
   196   /* Caret offset for non slanted fonts.  */
   197   sfnt_fword caret_offset;
   198 
   199   /* Reserved values.  */
   200   int16_t reserved1, reserved2, reserved3, reserved4;
   201 
   202   /* Should always be zero.  */
   203   int16_t metric_data_format;
   204 
   205   /* Number of advanced widths in metrics table.  */
   206   uint16_t num_of_long_hor_metrics;
   207 };
   208 
   209 struct sfnt_cmap_table
   210 {
   211   /* Should be zero.  */
   212   uint16_t version;
   213 
   214   /* Number of subtables.  */
   215   uint16_t num_subtables;
   216 };
   217 
   218 enum sfnt_platform_id
   219   {
   220     SFNT_PLATFORM_UNICODE   = 0,
   221     SFNT_PLATFORM_MACINTOSH = 1,
   222     SFNT_PLATFORM_RESERVED  = 2,
   223     SFNT_PLATFORM_MICROSOFT = 3,
   224   };
   225 
   226 enum sfnt_unicode_platform_specific_id
   227   {
   228     SFNT_UNICODE_1_0                 = 0,
   229     SFNT_UNICODE_1_1                 = 1,
   230     SFNT_UNICODE_ISO_10646_1993      = 2,
   231     SFNT_UNICODE_2_0_BMP             = 3,
   232     SFNT_UNICODE_2_0                 = 4,
   233     SFNT_UNICODE_VARIATION_SEQUENCES = 5,
   234     SFNT_UNICODE_LAST_RESORT         = 6,
   235   };
   236 
   237 enum sfnt_macintosh_platform_specific_id
   238   {
   239     SFNT_MACINTOSH_ROMAN               = 0,
   240     SFNT_MACINTOSH_JAPANESE            = 1,
   241     SFNT_MACINTOSH_TRADITIONAL_CHINESE = 2,
   242     SFNT_MACINTOSH_KOREAN              = 3,
   243     SFNT_MACINTOSH_ARABIC              = 4,
   244     SFNT_MACINTOSH_HEBREW              = 5,
   245     SFNT_MACINTOSH_GREEK               = 6,
   246     SFNT_MACINTOSH_RUSSIAN             = 7,
   247     SFNT_MACINTOSH_RSYMBOL             = 8,
   248     SFNT_MACINTOSH_DEVANGARI           = 9,
   249     SFNT_MACINTOSH_GURMUKHI            = 10,
   250     SFNT_MACINTOSH_GUJARATI            = 11,
   251     SFNT_MACINTOSH_ORIYA               = 12,
   252     SFNT_MACINTOSH_BENGALI             = 13,
   253     SFNT_MACINTOSH_TAMIL               = 14,
   254     SFNT_MACINTOSH_TELUGU              = 15,
   255     SFNT_MACINTOSH_KANNADA             = 16,
   256     SFNT_MACINTOSH_MALAYALAM           = 17,
   257     SFNT_MACINTOSH_SINHALESE           = 18,
   258     SFNT_MACINTOSH_BURMESE             = 19,
   259     SFNT_MACINTOSH_KHMER               = 20,
   260     SFNT_MACINTOSH_THAI                = 21,
   261     SFNT_MACINTOSH_LAOTIAN             = 22,
   262     SFNT_MACINTOSH_GEORGIAN            = 23,
   263     SFNT_MACINTOSH_ARMENIAN            = 24,
   264     SFNT_MACINTOSH_SIMPLIFIED_CHINESE  = 25,
   265     SFNT_MACINTOSH_TIBETIAN            = 26,
   266     SFNT_MACINTOSH_MONGOLIAN           = 27,
   267     SFNT_MACINTOSH_GEEZ                = 28,
   268     SFNT_MACINTOSH_SLAVIC              = 29,
   269     SFNT_MACINTOSH_VIETNAMESE          = 30,
   270     SFNT_MACINTOSH_SINDHI              = 31,
   271     SFNT_MACINTOSH_UNINTERPRETED       = 32,
   272   };
   273 
   274 enum sfnt_microsoft_platform_specific_id
   275   {
   276     SFNT_MICROSOFT_SYMBOL        = 0,
   277     SFNT_MICROSOFT_UNICODE_BMP   = 1,
   278     SFNT_MICROSOFT_SHIFT_JIS     = 2,
   279     SFNT_MICROSOFT_PRC           = 3,
   280     SFNT_MICROSOFT_BIG_FIVE      = 4,
   281     SFNT_MICROSOFT_WANSUNG       = 5,
   282     SFNT_MICROSOFT_JOHAB         = 6,
   283     SFNT_MICROSOFT_UNICODE_UCS_4 = 10,
   284   };
   285 
   286 struct sfnt_cmap_encoding_subtable
   287 {
   288   /* The platform ID.  */
   289   uint16_t platform_id;
   290 
   291   /* Platform specific ID.  */
   292   uint16_t platform_specific_id;
   293 
   294   /* Mapping table offset.  */
   295   uint32_t offset;
   296 };
   297 
   298 struct sfnt_cmap_encoding_subtable_data
   299 {
   300   /* Format and possibly the length in bytes.  */
   301   uint16_t format, length;
   302 };
   303 
   304 struct sfnt_cmap_format_0
   305 {
   306   /* Format, set to 0.  */
   307   uint16_t format;
   308 
   309   /* Length in bytes.  Should be 262.  */
   310   uint16_t length;
   311 
   312   /* Language code.  */
   313   uint16_t language;
   314 
   315   /* Character code to glyph index map.  */
   316   uint8_t glyph_index_array[256];
   317 };
   318 
   319 struct sfnt_cmap_format_2_subheader
   320 {
   321   uint16_t first_code;
   322   uint16_t entry_count;
   323   int16_t id_delta;
   324   uint16_t id_range_offset;
   325 };
   326 
   327 struct sfnt_cmap_format_2
   328 {
   329   /* Format, set to 2.  */
   330   uint16_t format;
   331 
   332   /* Length in bytes.  */
   333   uint16_t length;
   334 
   335   /* Language code.  */
   336   uint16_t language;
   337 
   338   /* Array mapping high bytes to subheaders.  */
   339   uint16_t sub_header_keys[256];
   340 
   341   /* Variable length data.  */
   342   struct sfnt_cmap_format_2_subheader *subheaders;
   343   uint16_t *glyph_index_array;
   344   uint16_t num_glyphs;
   345 };
   346 
   347 struct sfnt_cmap_format_4
   348 {
   349   /* Format, set to 4.  */
   350   uint16_t format;
   351 
   352   /* Length in bytes.  */
   353   uint16_t length;
   354 
   355   /* Language code.  */
   356   uint16_t language;
   357 
   358   /* 2 * seg_count.  */
   359   uint16_t seg_count_x2;
   360 
   361   /* 2 * (2**FLOOR(log2(segCount))) */
   362   uint16_t search_range;
   363 
   364   /* log2(searchRange/2) */
   365   uint16_t entry_selector;
   366 
   367   /* (2 * segCount) - searchRange */
   368   uint16_t range_shift;
   369 
   370   /* Variable-length data.  */
   371   uint16_t *end_code;
   372   uint16_t *reserved_pad;
   373   uint16_t *start_code;
   374   int16_t *id_delta;
   375   int16_t *id_range_offset;
   376   uint16_t *glyph_index_array;
   377 
   378   /* The number of elements in glyph_index_array.  */
   379   size_t glyph_index_size;
   380 };
   381 
   382 struct sfnt_cmap_format_6
   383 {
   384   /* Format, set to 6.  */
   385   uint16_t format;
   386 
   387   /* Length in bytes.  */
   388   uint16_t length;
   389 
   390   /* Language code.  */
   391   uint16_t language;
   392 
   393   /* First character code in subrange.  */
   394   uint16_t first_code;
   395 
   396   /* Number of character codes.  */
   397   uint16_t entry_count;
   398 
   399   /* Variable-length data.  */
   400   uint16_t *glyph_index_array;
   401 };
   402 
   403 struct sfnt_cmap_format_8_or_12_group
   404 {
   405   uint32_t start_char_code;
   406   uint32_t end_char_code;
   407   uint32_t start_glyph_code;
   408 };
   409 
   410 struct sfnt_cmap_format_8
   411 {
   412   /* Format, set to 8.  */
   413   uint16_t format;
   414 
   415   /* Reserved.  */
   416   uint16_t reserved;
   417 
   418   /* Length in bytes.  */
   419   uint32_t length;
   420 
   421   /* Language code.  */
   422   uint32_t language;
   423 
   424   /* Tightly packed array of bits (8K bytes total) indicating whether
   425      the particular 16-bit (index) value is the start of a 32-bit
   426      character code.  */
   427   uint8_t is32[65536];
   428 
   429   /* Number of groups.  */
   430   uint32_t num_groups;
   431 
   432   /* Variable length data.  */
   433   struct sfnt_cmap_format_8_or_12_group *groups;
   434 };
   435 
   436 /* cmap formats 10, 13 unsupported.  */
   437 
   438 struct sfnt_cmap_format_12
   439 {
   440   /* Format, set to 12.  */
   441   uint16_t format;
   442 
   443   /* Reserved.  */
   444   uint16_t reserved;
   445 
   446   /* Length in bytes.  */
   447   uint32_t length;
   448 
   449   /* Language code.  */
   450   uint32_t language;
   451 
   452   /* Number of groups.  */
   453   uint32_t num_groups;
   454 
   455   /* Variable length data.  */
   456   struct sfnt_cmap_format_8_or_12_group *groups;
   457 };
   458 
   459 struct sfnt_cmap_format_14
   460 {
   461   /* Format, set to 14.  */
   462   uint16_t format;
   463 
   464   /* The length of the table in bytes.  */
   465   uint32_t length;
   466 
   467   /* Number of variation selector records.  */
   468   uint16_t num_var_selector_records;
   469 
   470   /* The offset of this table in the font file.  */
   471   off_t offset;
   472 
   473   /* Variable length data.  */
   474   struct sfnt_variation_selector_record *records;
   475 };
   476 
   477 struct sfnt_variation_selector_record
   478 {
   479   /* 24-bit unsigned variation selector.  */
   480   unsigned int var_selector;
   481 
   482   /* Offset to default UVS table.  */
   483   uint32_t default_uvs_offset;
   484 
   485   /* Offset to non-default UVS table.  */
   486   uint32_t nondefault_uvs_offset;
   487 };
   488 
   489 struct sfnt_maxp_table
   490 {
   491   /* Table version.  */
   492   sfnt_fixed version;
   493 
   494   /* The number of glyphs in this font - 1.  Set at version 0.5 or
   495      later.  */
   496   uint16_t num_glyphs;
   497 
   498   /* These fields are only set in version 1.0 or later.  Maximum
   499      points in a non-composite glyph.  */
   500   uint16_t max_points;
   501 
   502   /* Maximum contours in a non-composite glyph.  */
   503   uint16_t max_contours;
   504 
   505   /* Maximum points in a composite glyph.  */
   506   uint16_t max_composite_points;
   507 
   508   /* Maximum contours in a composite glyph.  */
   509   uint16_t max_composite_contours;
   510 
   511   /* 1 if instructions do not use the twilight zone (Z0), or 2 if
   512      instructions do use Z0; should be set to 2 in most cases.  */
   513   uint16_t max_zones;
   514 
   515   /* Maximum points used in Z0.  */
   516   uint16_t max_twilight_points;
   517 
   518   /* Number of Storage Area locations.  */
   519   uint16_t max_storage;
   520 
   521   /* Number of FDEFs, equal to the highest function number + 1.  */
   522   uint16_t max_function_defs;
   523 
   524   /* Number of IDEFs.  */
   525   uint16_t max_instruction_defs;
   526 
   527   /* Maximum stack depth across Font Program ('fpgm' table), CVT
   528      Program ('prep' table) and all glyph instructions (in the 'glyf'
   529      table).  */
   530   uint16_t max_stack_elements;
   531 
   532   /* Maximum byte count for glyph instructions.  */
   533   uint16_t max_size_of_instructions;
   534 
   535   /* Maximum number of components referenced at ``top level'' for any
   536      composite glyph.  */
   537   uint16_t max_component_elements;
   538 
   539   /* Maximum levels of recursion; 1 for simple components.  */
   540   uint16_t max_component_depth;
   541 };
   542 
   543 struct sfnt_loca_table_short
   544 {
   545   /* Offsets to glyph data divided by two.  */
   546   uint16_t *offsets;
   547 
   548   /* Size of the offsets list.  */
   549   size_t num_offsets;
   550 };
   551 
   552 struct sfnt_loca_table_long
   553 {
   554   /* Offsets to glyph data.  */
   555   uint32_t *offsets;
   556 
   557   /* Size of the offsets list.  */
   558   size_t num_offsets;
   559 };
   560 
   561 struct sfnt_glyf_table
   562 {
   563   /* Size of the glyph data.  */
   564   size_t size;
   565 
   566   /* Pointer to possibly unaligned glyph data.  */
   567   unsigned char *glyphs;
   568 
   569   /* Pointer to the start of the mapping.
   570      Only initialized if this table was mmapped.  */
   571   unsigned char *start;
   572 };
   573 
   574 struct sfnt_simple_glyph
   575 {
   576   /* The total number of points in this glyph.  */
   577   size_t number_of_points;
   578 
   579   /* Array containing the last points of each contour.  */
   580   uint16_t *restrict end_pts_of_contours;
   581 
   582   /* Total number of bytes needed for instructions.  */
   583   uint16_t instruction_length;
   584 
   585   /* Instruction data.  */
   586   uint8_t *restrict instructions;
   587 
   588   /* Array of flags.  */
   589   uint8_t *restrict flags;
   590 
   591   /* Array of X coordinates.  */
   592   int16_t *restrict x_coordinates;
   593 
   594   /* Array of Y coordinates.  */
   595   int16_t *restrict y_coordinates;
   596 
   597   /* Pointer to the end of that array.  */
   598   int16_t *restrict y_coordinates_end;
   599 };
   600 
   601 struct sfnt_compound_glyph_component
   602 {
   603   /* Compound glyph flags.  */
   604   uint16_t flags;
   605 
   606   /* Component glyph index.  */
   607   uint16_t glyph_index;
   608 
   609   /* X-offset for component or point number; type depends on bits 0
   610      and 1 in component flags.  */
   611   union {
   612     uint8_t a;
   613     int8_t b;
   614     uint16_t c;
   615     int16_t d;
   616   } argument1;
   617 
   618   /* Y-offset for component or point number; type depends on bits 0
   619      and 1 in component flags.  */
   620   union {
   621     uint8_t a;
   622     int8_t b;
   623     uint16_t c;
   624     int16_t d;
   625   } argument2;
   626 
   627   /* Various scale formats.  */
   628   union {
   629     int16_t scale;
   630     struct {
   631       int16_t xscale;
   632       int16_t yscale;
   633     } a;
   634     struct {
   635       int16_t xscale;
   636       int16_t scale01;
   637       int16_t scale10;
   638       int16_t yscale;
   639     } b;
   640   } u;
   641 };
   642 
   643 struct sfnt_compound_glyph
   644 {
   645   /* Pointer to array of components.  */
   646   struct sfnt_compound_glyph_component *components;
   647 
   648   /* Number of elements in that array.  */
   649   size_t num_components;
   650 
   651   /* Instruction data.  */
   652   uint8_t *instructions;
   653 
   654   /* Length of instructions.  */
   655   uint16_t instruction_length;
   656 };
   657 
   658 struct sfnt_glyph
   659 {
   660   /* Number of contours in this glyph.  */
   661   int16_t number_of_contours;
   662 
   663   /* Coordinate bounds.  */
   664   sfnt_fword xmin, ymin, xmax, ymax;
   665 
   666   /* Distortion applied to the right side phantom point.  */
   667   sfnt_fword advance_distortion;
   668 
   669   /* Distortion applied to the origin point.  */
   670   sfnt_fword origin_distortion;
   671 
   672   /* Either a simple glyph or a compound glyph, depending on which is
   673      set.  */
   674   struct sfnt_simple_glyph *simple;
   675   struct sfnt_compound_glyph *compound;
   676 };
   677 
   678 
   679 
   680 /* Glyph outline decomposition.  */
   681 
   682 struct sfnt_point
   683 {
   684   /* X and Y in em space.  */
   685   sfnt_fixed x, y;
   686 };
   687 
   688 typedef void (*sfnt_move_to_proc) (struct sfnt_point, void *);
   689 typedef void (*sfnt_line_to_proc) (struct sfnt_point, void *);
   690 typedef void (*sfnt_curve_to_proc) (struct sfnt_point,
   691                                     struct sfnt_point,
   692                                     void *);
   693 
   694 /* Forward declaration for use in sfnt_get_metrics_proc.  */
   695 struct sfnt_glyph_metrics;
   696 
   697 typedef struct sfnt_glyph *(*sfnt_get_glyph_proc) (sfnt_glyph, void *,
   698                                                    bool *);
   699 typedef void (*sfnt_free_glyph_proc) (struct sfnt_glyph *, void *);
   700 typedef int (*sfnt_get_metrics_proc) (sfnt_glyph,
   701                                       struct sfnt_glyph_metrics *,
   702                                       void *);
   703 
   704 
   705 
   706 /* Decomposed glyph outline.  */
   707 
   708 struct sfnt_glyph_outline_command
   709 {
   710   /* Flags for this outline command.  */
   711   int flags;
   712 
   713   /* X and Y position of this command.  */
   714   sfnt_fixed x, y;
   715 };
   716 
   717 /* Structure describing a single recorded outline in fixed pixel
   718    space.  */
   719 
   720 struct sfnt_glyph_outline
   721 {
   722   /* Array of outlines elements.  */
   723   struct sfnt_glyph_outline_command *outline;
   724 
   725   /* Size of the outline data, and how much is full.  */
   726   size_t outline_size, outline_used;
   727 
   728   /* Rectangle defining bounds of the outline.  Namely, the minimum
   729      and maximum X and Y positions.  */
   730   sfnt_fixed xmin, ymin, xmax, ymax;
   731 
   732   /* The origin point of the outline on the X axis.  Value defaults to
   733      0.  */
   734   sfnt_fixed origin;
   735 
   736   /* Reference count.  Initially zero.  */
   737   short refcount;
   738 };
   739 
   740 enum sfnt_glyph_outline_flags
   741   {
   742     SFNT_GLYPH_OUTLINE_LINETO     = (1 << 1),
   743   };
   744 
   745 
   746 
   747 /* Glyph rasterization.  */
   748 
   749 struct sfnt_raster
   750 {
   751   /* Pointer to coverage data.  */
   752   unsigned char *cells;
   753 
   754   /* Basic dimensions of the raster.  */
   755   unsigned short width, height;
   756 
   757   /* Integer offset to apply to positions in the raster so that they
   758      start from the origin point of the glyph.  */
   759   short offx, offy;
   760 
   761   /* The raster stride.  */
   762   unsigned short stride;
   763 
   764   /* Reference count.  Initially zero.  */
   765   unsigned short refcount;
   766 };
   767 
   768 struct sfnt_edge
   769 {
   770   /* Next edge in this chain.  */
   771   struct sfnt_edge *next;
   772 
   773   /* Winding direction.  1 if clockwise, -1 if counterclockwise.  */
   774   int winding;
   775 
   776   /* X position, top and bottom of edges.  */
   777   sfnt_fixed x, top, bottom;
   778 
   779   /* Amount to move X by upon each change of Y.  */
   780   sfnt_fixed step_x;
   781 };
   782 
   783 
   784 
   785 /* Polygon rasterization constants.  */
   786 
   787 enum
   788   {
   789     SFNT_POLY_SHIFT  = 3,
   790     SFNT_POLY_SAMPLE = (1 << SFNT_POLY_SHIFT),
   791     SFNT_POLY_MASK   = (SFNT_POLY_SAMPLE - 1),
   792     SFNT_POLY_STEP   = (0x10000 >> SFNT_POLY_SHIFT),
   793     SFNT_POLY_START  = (SFNT_POLY_STEP >> 1),
   794   };
   795 
   796 
   797 
   798 /* Glyph metrics computation.  */
   799 
   800 struct sfnt_long_hor_metric
   801 {
   802   uint16_t advance_width;
   803   int16_t left_side_bearing;
   804 };
   805 
   806 struct sfnt_hmtx_table
   807 {
   808   /* Array of horizontal metrics for each glyph.  */
   809   struct sfnt_long_hor_metric *h_metrics;
   810 
   811   /* Lbearing for remaining glyphs.  */
   812   int16_t *left_side_bearing;
   813 };
   814 
   815 /* Structure describing the metrics of a single glyph.  The fields
   816    mean the same as in XCharStruct, except they are 16.16 fixed point
   817    values, and are missing significant information.  */
   818 
   819 struct sfnt_glyph_metrics
   820 {
   821   /* Distance between origin and left edge of raster.  Positive
   822      changes move rightwards.
   823 
   824      If sfnt_lookup_glyph_metrics is given a pixel size of -1,
   825      this is actually a sign extended fword.  */
   826   sfnt_fixed lbearing;
   827 
   828   /* Advance to next glyph's origin.
   829 
   830      If sfnt_lookup_glyph_metrics is given a pixel size of -1, this is
   831      actually a sign extended fword.  */
   832   sfnt_fixed advance;
   833 };
   834 
   835 
   836 
   837 /* Font style parsing.  */
   838 
   839 struct sfnt_name_record
   840 {
   841   /* Platform identifier code.  */
   842   uint16_t platform_id;
   843 
   844   /* Platform specific ID.  */
   845   uint16_t platform_specific_id;
   846 
   847   /* Language identifier.  */
   848   uint16_t language_id;
   849 
   850   /* Name identifier.  */
   851   uint16_t name_id;
   852 
   853   /* String length in bytes.  */
   854   uint16_t length;
   855 
   856   /* Offset from start of storage area.  */
   857   uint16_t offset;
   858 };
   859 
   860 struct sfnt_name_table
   861 {
   862   /* Format selector of name table.  */
   863   uint16_t format;
   864 
   865   /* Number of name records.  */
   866   uint16_t count;
   867 
   868   /* Offset to start of string data.  */
   869   uint16_t string_offset;
   870 
   871   /* Variable length data.  */
   872   struct sfnt_name_record *name_records;
   873 
   874   /* Start of string data.  */
   875   unsigned char *data;
   876 };
   877 
   878 /* Name identifier codes.  These are Apple's codes, not
   879    Microsoft's.  */
   880 
   881 enum sfnt_name_identifier_code
   882   {
   883     SFNT_NAME_COPYRIGHT_NOTICE                  = 0,
   884     SFNT_NAME_FONT_FAMILY                       = 1,
   885     SFNT_NAME_FONT_SUBFAMILY                    = 2,
   886     SFNT_NAME_UNIQUE_SUBFAMILY_IDENTIFICATION   = 3,
   887     SFNT_NAME_FULL_NAME                         = 4,
   888     SFNT_NAME_NAME_TABLE_VERSION                = 5,
   889     SFNT_NAME_POSTSCRIPT_NAME                   = 6,
   890     SFNT_NAME_TRADEMARK_NOTICE                  = 7,
   891     SFNT_NAME_MANUFACTURER_NAME                 = 8,
   892     SFNT_NAME_DESIGNER                          = 9,
   893     SFNT_NAME_DESCRIPTION                       = 10,
   894     SFNT_NAME_FONT_VENDOR_URL                   = 11,
   895     SFNT_NAME_FONT_DESIGNER_URL                 = 12,
   896     SFNT_NAME_LICENSE_DESCRIPTION               = 13,
   897     SFNT_NAME_LICENSE_INFORMATION_URL           = 14,
   898     SFNT_NAME_PREFERRED_FAMILY                  = 16,
   899     SFNT_NAME_PREFERRED_SUBFAMILY               = 17,
   900     SFNT_NAME_COMPATIBLE_FULL                   = 18,
   901     SFNT_NAME_SAMPLE_TEXT                       = 19,
   902     SFNT_NAME_VARIATIONS_POSTSCRIPT_NAME_PREFIX = 25,
   903   };
   904 
   905 struct sfnt_meta_data_map
   906 {
   907   /* Identifier for the tag.  */
   908   uint32_t tag;
   909 
   910   /* Offset from start of table to data.  */
   911   uint32_t data_offset;
   912 
   913   /* Length of the data.  */
   914   uint32_t data_length;
   915 };
   916 
   917 struct sfnt_meta_table
   918 {
   919   /* Version of the table.  Currently set to 1.  */
   920   uint32_t version;
   921 
   922   /* Flags.  Currently 0.  */
   923   uint32_t flags;
   924 
   925   /* Offset from start of table to beginning of variable length
   926      data.  */
   927   uint32_t data_offset;
   928 
   929   /* Number of data maps in the table.  */
   930   uint32_t num_data_maps;
   931 
   932   /* Beginning of variable length data.  */
   933   struct sfnt_meta_data_map *data_maps;
   934 
   935   /* The whole table contents.  */
   936   unsigned char *data;
   937 };
   938 
   939 enum sfnt_meta_data_tag
   940   {
   941     SFNT_META_DATA_TAG_DLNG = 0x646c6e67,
   942     SFNT_META_DATA_TAG_SLNG = 0x736c6e67,
   943   };
   944 
   945 
   946 
   947 /* TrueType collection format support.  */
   948 
   949 struct sfnt_ttc_header
   950 {
   951   /* TrueType collection ID tag.  */
   952   uint32_t ttctag;
   953 
   954   /* Version of the TTC header.  */
   955   uint32_t version;
   956 
   957   /* Number of fonts in the TTC header.  */
   958   uint32_t num_fonts;
   959 
   960   /* Array of offsets to the offset table for each font in the
   961      file.  */
   962   uint32_t *offset_table;
   963 
   964   /* Tag indicating that a DSIG table exists, or 0.  Fields from here
   965      on are only set on version 2.0 headers or later.  */
   966   uint32_t ul_dsig_tag;
   967 
   968   /* Length in bytes of the signature table, or 0 if there is no
   969      signature.  */
   970   uint32_t ul_dsig_length;
   971 
   972   /* Offset in bytes of the dsig table from the beginning of the TTC
   973      file.  */
   974   uint32_t ul_dsig_offset;
   975 };
   976 
   977 enum sfnt_ttc_tag
   978   {
   979     SFNT_TTC_TTCF = 0x74746366,
   980     SFNT_TTC_DSIG = 0x44534947,
   981   };
   982 
   983 
   984 
   985 /* Unicode Variation Sequence (UVS) support.  */
   986 
   987 struct sfnt_default_uvs_table
   988 {
   989   /* Number of ranges that follow.  */
   990   uint32_t num_unicode_value_ranges;
   991 
   992   /* Variable length data.  */
   993   struct sfnt_unicode_value_range *ranges;
   994 };
   995 
   996 struct sfnt_unicode_value_range
   997 {
   998   /* First value in this range.  */
   999   unsigned int start_unicode_value;
  1000 
  1001   /* Number of additional values in this range.  */
  1002   unsigned char additional_count;
  1003 };
  1004 
  1005 struct sfnt_nondefault_uvs_table
  1006 {
  1007   /* Number of UVS mappings which follow.  */
  1008   uint32_t num_uvs_mappings;
  1009 
  1010   /* Variable length data.  */
  1011   struct sfnt_uvs_mapping *mappings;
  1012 };
  1013 
  1014 struct sfnt_uvs_mapping
  1015 {
  1016   /* Base character value.  */
  1017   unsigned int unicode_value;
  1018 
  1019   /* Glyph ID of the base character value.  */
  1020   uint16_t base_character_value;
  1021 };
  1022 
  1023 struct sfnt_mapped_variation_selector_record
  1024 {
  1025   /* The variation selector.  */
  1026   unsigned int selector;
  1027 
  1028   /* Its default UVS table.  */
  1029   struct sfnt_default_uvs_table *default_uvs;
  1030 
  1031   /* Its nondefault UVS table.  */
  1032   struct sfnt_nondefault_uvs_table *nondefault_uvs;
  1033 };
  1034 
  1035 /* Structure describing a single offset to load into a variation
  1036    selection context.  */
  1037 
  1038 struct sfnt_table_offset_rec
  1039 {
  1040   /* The offset from the start of the font file.  */
  1041   off_t offset;
  1042 
  1043   /* Whether or not the offset points to a non-default UVS table.  */
  1044   bool is_nondefault_table;
  1045 
  1046   /* Pointer to the UVS table.  */
  1047   void *table;
  1048 };
  1049 
  1050 struct sfnt_uvs_context
  1051 {
  1052   /* Number of records and tables.  */
  1053   size_t num_records, nmemb;
  1054 
  1055   /* Array of UVS tables.  */
  1056   struct sfnt_table_offset_rec *tables;
  1057 
  1058   /* Array of variation selector records mapped to
  1059      their corresponding tables.  */
  1060   struct sfnt_mapped_variation_selector_record *records;
  1061 };
  1062 
  1063 
  1064 
  1065 #if defined HAVE_MMAP && !defined TEST
  1066 
  1067 /* Memory mapping support.  */
  1068 
  1069 struct sfnt_mapped_table
  1070 {
  1071   /* Pointer to table data.  */
  1072   void *data;
  1073 
  1074   /* Pointer to table mapping.  */
  1075   void *mapping;
  1076 
  1077   /* Size of mapped data and size of mapping.  */
  1078   size_t length, size;
  1079 };
  1080 
  1081 #endif /* HAVE_MMAP && !TEST */
  1082 
  1083 
  1084 
  1085 /* Glyph variation support.  */
  1086 
  1087 /* 2.14 fixed point type used to represent versors of unit
  1088    vectors.  */
  1089 typedef int16_t sfnt_f2dot14;
  1090 
  1091 /* Forward declaration used only for the distortable font stuff.  */
  1092 struct sfnt_cvt_table;
  1093 
  1094 struct sfnt_variation_axis
  1095 {
  1096   /* The axis tag.  */
  1097   uint32_t axis_tag;
  1098 
  1099   /* The minimum style coordinate for the axis.  */
  1100   sfnt_fixed min_value;
  1101 
  1102   /* The default style coordinate for the axis.  */
  1103   sfnt_fixed default_value;
  1104 
  1105   /* The maximum style coordinate for the axis.  */
  1106   sfnt_fixed max_value;
  1107 
  1108   /* Set to zero.  */
  1109   uint16_t flags;
  1110 
  1111   /* Identifier under which this axis's name will be found in the
  1112      `name' table.  */
  1113   uint16_t name_id;
  1114 };
  1115 
  1116 struct sfnt_instance
  1117 {
  1118   /* The instance name ID.  */
  1119   uint16_t name_id;
  1120 
  1121   /* Flags.  */
  1122   uint16_t flags;
  1123 
  1124   /* Optional PostScript name.  */
  1125   uint16_t ps_name_id;
  1126 
  1127   /* Coordinates of each defined instance.  */
  1128   sfnt_fixed *coords;
  1129 };
  1130 
  1131 struct sfnt_fvar_table
  1132 {
  1133   /* Major version; should be 1.  */
  1134   uint16_t major_version;
  1135 
  1136   /* Minor version; should be 0.  */
  1137   uint16_t minor_version;
  1138 
  1139   /* Offset in bytes from the beginning of the table to the beginning
  1140      of the first axis data.  */
  1141   uint16_t offset_to_data;
  1142 
  1143   /* Reserved field; always 2.  */
  1144   uint16_t count_size_pairs;
  1145 
  1146   /* Number of style axes in this font.  */
  1147   uint16_t axis_count;
  1148 
  1149   /* The number of bytes in each variation axis record.  Currently 20
  1150      bytes.  */
  1151   uint16_t axis_size;
  1152 
  1153   /* The number of named instances for the font found in the
  1154      instance array.  */
  1155   uint16_t instance_count;
  1156 
  1157   /* The size of each instance record.  */
  1158   uint16_t instance_size;
  1159 
  1160   /* Variable length data.  */
  1161   struct sfnt_variation_axis *axis;
  1162   struct sfnt_instance *instance;
  1163 };
  1164 
  1165 struct sfnt_short_frac_correspondence
  1166 {
  1167   /* Value in normalized user space.  */
  1168   sfnt_f2dot14 from_coord;
  1169 
  1170   /* Value in normalized axis space.  */
  1171   sfnt_f2dot14 to_coord;
  1172 };
  1173 
  1174 struct sfnt_short_frac_segment
  1175 {
  1176   /* The number of pairs for this axis.  */
  1177   uint16_t pair_count;
  1178 
  1179   /* Variable length data.  */
  1180   struct sfnt_short_frac_correspondence *correspondence;
  1181 };
  1182 
  1183 struct sfnt_avar_table
  1184 {
  1185   /* The version of the table.  Should be 1.0.  */
  1186   sfnt_fixed version;
  1187 
  1188   /* Number of variation axes defined in this table.
  1189      XXX: why is this signed? */
  1190   int32_t axis_count;
  1191 
  1192   /* Variable length data.  */
  1193   struct sfnt_short_frac_segment *segments;
  1194 };
  1195 
  1196 struct sfnt_tuple_variation
  1197 {
  1198   /* Tuple point numbers.  */
  1199   uint16_t *points;
  1200 
  1201   /* Deltas.  */
  1202   sfnt_fword *deltas;
  1203 
  1204   /* Tuple coordinates.  One for each axis specified in the [gaf]var
  1205      tables.  */
  1206   sfnt_f2dot14 *coordinates;
  1207 
  1208   /* Intermediate start and end coordinates.  */
  1209   sfnt_f2dot14 *restrict intermediate_start;
  1210 
  1211   /* Intermediate start and end coordinates.  */
  1212   sfnt_f2dot14 *restrict intermediate_end;
  1213 
  1214   /* The number of points and deltas present.
  1215 
  1216      UINT16_MAX and POINTS set to NULL means there are deltas for each
  1217      CVT entry.  */
  1218   uint16_t num_points;
  1219 };
  1220 
  1221 struct sfnt_cvar_table
  1222 {
  1223   /* The version of this CVT variations table.  */
  1224   sfnt_fixed version;
  1225 
  1226   /* Flags.  */
  1227   uint16_t tuple_count;
  1228 
  1229   /* Offset from the beginning of the table to the tuple data.  */
  1230   uint16_t data_offset;
  1231 
  1232   /* Variable length data.  */
  1233   struct sfnt_tuple_variation *variation;
  1234 };
  1235 
  1236 struct sfnt_gvar_table
  1237 {
  1238   /* Version of the glyph variations table.  */
  1239   uint16_t version;
  1240 
  1241   /* Reserved, currently 0.  */
  1242   uint16_t reserved;
  1243 
  1244   /* The number of style axes for this font.  This must be the same
  1245      number as axisCount in the 'fvar' table.  */
  1246   uint16_t axis_count;
  1247 
  1248   /* The number of shared coordinates.  */
  1249   uint16_t shared_coord_count;
  1250 
  1251   /* Byte offset from the beginning of this table to the list of
  1252      shared style coordinates.  */
  1253   uint32_t offset_to_coord;
  1254 
  1255   /* The number of glyphs in this font; this should match the number
  1256      of the glyphs store elsewhere in the font.  */
  1257   uint16_t glyph_count;
  1258 
  1259   /* Bit-field that gives the format of the offset array that
  1260      follows. If the flag is 0, the type is uint16. If the flag is 1,
  1261      the type is unit 32.  */
  1262   uint16_t flags;
  1263 
  1264   /* Byte offset from the beginning of this table to the first glyph
  1265      glyphVariationData.  */
  1266   uint32_t offset_to_data;
  1267 
  1268   /* Number of bytes in the glyph variation data.  */
  1269   size_t data_size;
  1270 
  1271   /* Byte offsets from the beginning of the glyphVariationData array
  1272      to the glyphVariationData for each glyph in the font.  The format
  1273      of this field is set by the flags field.  */
  1274   union {
  1275     uint16_t *offset_word;
  1276     uint32_t *offset_long;
  1277   } u;
  1278 
  1279   /* Other variable length data.  */
  1280   sfnt_f2dot14 *global_coords;
  1281   unsigned char *glyph_variation_data;
  1282 };
  1283 
  1284 /* Structure repesenting a set of axis coordinates and their
  1285    normalized equivalents.
  1286 
  1287    To use this structure, call
  1288 
  1289      sfnt_init_blend (&blend, fvar, gvar)
  1290 
  1291    on a `struct sfnt_blend *', with an appropriate fvar and gvar
  1292    table.
  1293 
  1294    Then, fill in blend.coords with the un-normalized coordinates,
  1295    and call
  1296 
  1297      sfnt_normalize_blend (&blend)
  1298 
  1299    finally, call sfnt_vary_simple_glyph and related functions.  */
  1300 
  1301 struct sfnt_blend
  1302 {
  1303   /* The fvar table.  This determines the number of elements in each
  1304      of the arrays below.  */
  1305   struct sfnt_fvar_table *fvar;
  1306 
  1307   /* The gvar table.  This provides the glyph variation data.  */
  1308   struct sfnt_gvar_table *gvar;
  1309 
  1310   /* The avar table.  This provides adjustments to normalized axis
  1311      values, and may be NULL.  */
  1312   struct sfnt_avar_table *avar;
  1313 
  1314   /* The cvar table.  This provides adjustments to CVT values, and may
  1315      be NULL.  */
  1316   struct sfnt_cvar_table *cvar;
  1317 
  1318   /* Un-normalized coordinates.  */
  1319   sfnt_fixed *coords;
  1320 
  1321   /* Normalized coordinates.  */
  1322   sfnt_fixed *norm_coords;
  1323 };
  1324 
  1325 struct sfnt_metrics_distortion
  1326 {
  1327   /* Distortion applied to the origin point.  */
  1328   sfnt_fword origin;
  1329 
  1330   /* Distortion applied to the advance point.  */
  1331   sfnt_fword advance;
  1332 };
  1333 
  1334 
  1335 
  1336 #define SFNT_CEIL_FIXED(fixed)  (((fixed) + 0177777) & 037777600000)
  1337 #define SFNT_FLOOR_FIXED(fixed) ((fixed) & 037777600000)
  1338 
  1339 
  1340 
  1341 /* Function declarations.  Keep these sorted by the order in which
  1342    they appear in sfnt.c.  Keep each line no longer than 80
  1343    columns.  */
  1344 
  1345 #ifndef TEST
  1346 
  1347 extern struct sfnt_offset_subtable *sfnt_read_table_directory (int);
  1348 
  1349 #define PROTOTYPE                               \
  1350   int, struct sfnt_offset_subtable *,           \
  1351   struct sfnt_cmap_encoding_subtable **,        \
  1352   struct sfnt_cmap_encoding_subtable_data ***
  1353 extern struct sfnt_cmap_table *sfnt_read_cmap_table (PROTOTYPE);
  1354 #undef PROTOTYPE
  1355 
  1356 extern sfnt_glyph sfnt_lookup_glyph (sfnt_char,
  1357                                      struct sfnt_cmap_encoding_subtable_data *);
  1358 
  1359 #define PROTOTYPE int, struct sfnt_offset_subtable *
  1360 extern struct sfnt_head_table *sfnt_read_head_table (PROTOTYPE);
  1361 extern struct sfnt_hhea_table *sfnt_read_hhea_table (PROTOTYPE);
  1362 extern struct sfnt_loca_table_short *sfnt_read_loca_table_short (PROTOTYPE);
  1363 extern struct sfnt_loca_table_long *sfnt_read_loca_table_long (PROTOTYPE);
  1364 extern struct sfnt_maxp_table *sfnt_read_maxp_table (PROTOTYPE);
  1365 extern struct sfnt_glyf_table *sfnt_read_glyf_table (PROTOTYPE);
  1366 
  1367 #ifdef HAVE_MMAP
  1368 extern struct sfnt_glyf_table *sfnt_map_glyf_table (PROTOTYPE);
  1369 extern int sfnt_unmap_glyf_table (struct sfnt_glyf_table *);
  1370 #endif /* HAVE_MMAP */
  1371 #undef PROTOTYPE
  1372 
  1373 extern struct sfnt_glyph *sfnt_read_glyph (sfnt_glyph, struct sfnt_glyf_table *,
  1374                                            struct sfnt_loca_table_short *,
  1375                                            struct sfnt_loca_table_long *);
  1376 extern void sfnt_free_glyph (struct sfnt_glyph *);
  1377 
  1378 #define PROTOTYPE               \
  1379   struct sfnt_glyph *,          \
  1380   sfnt_fixed,                   \
  1381   struct sfnt_glyph_metrics *,  \
  1382   sfnt_get_glyph_proc,          \
  1383   sfnt_free_glyph_proc,         \
  1384   sfnt_get_metrics_proc,        \
  1385   void *
  1386 extern struct sfnt_glyph_outline *sfnt_build_glyph_outline (PROTOTYPE);
  1387 #undef PROTOTYPE
  1388 
  1389 extern void sfnt_prepare_raster (struct sfnt_raster *,
  1390                                  struct sfnt_glyph_outline *);
  1391 
  1392 #define PROTOTYPE struct sfnt_glyph_outline *
  1393 extern struct sfnt_raster *sfnt_raster_glyph_outline (PROTOTYPE);
  1394 #undef PROTOTYPE
  1395 
  1396 #define PROTOTYPE                       \
  1397   int,                                  \
  1398   struct sfnt_offset_subtable *,        \
  1399   struct sfnt_hhea_table *,             \
  1400   struct sfnt_maxp_table *
  1401 extern struct sfnt_hmtx_table *sfnt_read_hmtx_table (PROTOTYPE);
  1402 #undef PROTOTYPE
  1403 
  1404 extern int sfnt_lookup_glyph_metrics (sfnt_glyph, int,
  1405                                       struct sfnt_glyph_metrics *,
  1406                                       struct sfnt_hmtx_table *,
  1407                                       struct sfnt_hhea_table *,
  1408                                       struct sfnt_head_table *,
  1409                                       struct sfnt_maxp_table *);
  1410 
  1411 extern void sfnt_scale_metrics (struct sfnt_glyph_metrics *,
  1412                                 sfnt_fixed);
  1413 extern sfnt_fixed sfnt_get_scale (struct sfnt_head_table *, int);
  1414 
  1415 #define PROTOTYPE int, struct sfnt_offset_subtable *
  1416 extern struct sfnt_name_table *sfnt_read_name_table (PROTOTYPE);
  1417 #undef PROTOTYPE
  1418 
  1419 extern unsigned char *sfnt_find_name (struct sfnt_name_table *,
  1420                                       enum sfnt_name_identifier_code,
  1421                                       struct sfnt_name_record *);
  1422 
  1423 #define PROTOTYPE int, struct sfnt_offset_subtable *
  1424 extern struct sfnt_meta_table *sfnt_read_meta_table (PROTOTYPE);
  1425 #undef PROTOTYPE
  1426 
  1427 extern char *sfnt_find_metadata (struct sfnt_meta_table *,
  1428                                  enum sfnt_meta_data_tag,
  1429                                  struct sfnt_meta_data_map *);
  1430 
  1431 extern struct sfnt_ttc_header *sfnt_read_ttc_header (int);
  1432 
  1433 
  1434 
  1435 #define PROTOTYPE struct sfnt_cmap_format_14 *, int
  1436 
  1437 extern struct sfnt_uvs_context *sfnt_create_uvs_context (PROTOTYPE);
  1438 
  1439 #undef PROTOTYPE
  1440 
  1441 extern void sfnt_free_uvs_context (struct sfnt_uvs_context *);
  1442 
  1443 #define PROTOTYPE struct sfnt_nondefault_uvs_table *, sfnt_char
  1444 
  1445 extern sfnt_glyph sfnt_variation_glyph_for_char (PROTOTYPE);
  1446 
  1447 #undef PROTOTYPE
  1448 
  1449 
  1450 
  1451 #ifdef HAVE_MMAP
  1452 
  1453 extern int sfnt_map_table (int, struct sfnt_offset_subtable *,
  1454                            uint32_t, struct sfnt_mapped_table *);
  1455 extern int sfnt_unmap_table (struct sfnt_mapped_table *);
  1456 
  1457 #endif /* HAVE_MMAP */
  1458 
  1459 
  1460 
  1461 extern void *sfnt_read_table (int, struct sfnt_offset_subtable *,
  1462                               uint32_t, size_t *);
  1463 
  1464 
  1465 
  1466 #define PROTOTYPE int, struct sfnt_offset_subtable *
  1467 
  1468 extern struct sfnt_fvar_table *sfnt_read_fvar_table (PROTOTYPE);
  1469 extern struct sfnt_gvar_table *sfnt_read_gvar_table (PROTOTYPE);
  1470 extern struct sfnt_avar_table *sfnt_read_avar_table (PROTOTYPE);
  1471 
  1472 #undef PROTOTYPE
  1473 
  1474 #define PROTOTYPE                               \
  1475   int,                                          \
  1476   struct sfnt_offset_subtable *,                \
  1477   struct sfnt_fvar_table *,                     \
  1478   struct sfnt_cvt_table *
  1479 
  1480 extern struct sfnt_cvar_table *sfnt_read_cvar_table (PROTOTYPE);
  1481 
  1482 #undef PROTOTYPE
  1483 
  1484 
  1485 
  1486 extern void sfnt_init_blend (struct sfnt_blend *,
  1487                              struct sfnt_fvar_table *,
  1488                              struct sfnt_gvar_table *,
  1489                              struct sfnt_avar_table *,
  1490                              struct sfnt_cvar_table *);
  1491 extern void sfnt_free_blend (struct sfnt_blend *);
  1492 extern void sfnt_normalize_blend (struct sfnt_blend *);
  1493 
  1494 
  1495 
  1496 extern int sfnt_vary_simple_glyph (struct sfnt_blend *, sfnt_glyph,
  1497                                    struct sfnt_glyph *,
  1498                                    struct sfnt_metrics_distortion *);
  1499 extern int sfnt_vary_compound_glyph (struct sfnt_blend *, sfnt_glyph,
  1500                                      struct sfnt_glyph *,
  1501                                      struct sfnt_metrics_distortion *);
  1502 
  1503 #endif /* TEST */
  1504 
  1505 
  1506 
  1507 /* TrueType hinting support.  */
  1508 
  1509 /* Structure definitions for tables used by the TrueType
  1510    interpreter.  */
  1511 
  1512 struct sfnt_cvt_table
  1513 {
  1514   /* Number of elements in the control value table.  */
  1515   size_t num_elements;
  1516 
  1517   /* Pointer to elements in the control value table.  */
  1518   sfnt_fword *values;
  1519 };
  1520 
  1521 struct sfnt_fpgm_table
  1522 {
  1523   /* Number of instructions in the font program table.  */
  1524   size_t num_instructions;
  1525 
  1526   /* Pointer to elements in the font program table.  */
  1527   unsigned char *instructions;
  1528 };
  1529 
  1530 struct sfnt_prep_table
  1531 {
  1532   /* Number of instructions in the control value program (pre-program)
  1533      table.  */
  1534   size_t num_instructions;
  1535 
  1536   /* Pointer to elements in the preprogram table.  */
  1537   unsigned char *instructions;
  1538 };
  1539 
  1540 
  1541 
  1542 /* Fixed point types used by the TrueType interpreter.  */
  1543 
  1544 /* 26.6 fixed point type used within the interpreter.  */
  1545 typedef int32_t sfnt_f26dot6;
  1546 
  1547 /* 18.14 fixed point type used to calculate rounding details.  */
  1548 typedef int32_t sfnt_f18dot14;
  1549 
  1550 
  1551 
  1552 /* Interpreter execution environment.  */
  1553 
  1554 struct sfnt_unit_vector
  1555 {
  1556   /* X and Y versors of the 2d unit vector.  */
  1557   sfnt_f2dot14 x, y;
  1558 };
  1559 
  1560 struct sfnt_interpreter_definition
  1561 {
  1562   /* The opcode of this instruction or function.  */
  1563   uint16_t opcode;
  1564 
  1565   /* The number of instructions.  */
  1566   uint16_t instruction_count;
  1567 
  1568   /* Pointer to instructions belonging to the definition.  This
  1569      pointer points directly into the control value or font program.
  1570      Make sure both programs are kept around as long as the
  1571      interpreter continues to exist.  */
  1572   unsigned char *instructions;
  1573 };
  1574 
  1575 /* This structure represents a ``struct sfnt_glyph'' that has been
  1576    scaled to a given pixel size.
  1577 
  1578    It can either contain a simple glyph, or a decomposed compound
  1579    glyph; instructions are interpreted for both simple glyphs, simple
  1580    glyph components inside a compound glyph, and compound glyphs as a
  1581    whole.
  1582 
  1583    In addition to the glyph data itself, it also records various
  1584    information for the instruction interpretation process:
  1585 
  1586      - ``current'' point coordinates, which have been modified
  1587        by the instructing process.
  1588 
  1589      - two phantom points at the origin and the advance of the
  1590        glyph.  */
  1591 
  1592 struct sfnt_interpreter_zone
  1593 {
  1594   /* The number of points in this zone, including the two phantom
  1595      points at the end.  */
  1596   size_t num_points;
  1597 
  1598   /* The number of contours in this zone.  */
  1599   size_t num_contours;
  1600 
  1601   /* The end points of each contour.  */
  1602   size_t *contour_end_points;
  1603 
  1604   /* Pointer to the X axis point data.  */
  1605   sfnt_f26dot6 *restrict x_points;
  1606 
  1607   /* Pointer to the X axis current point data.  */
  1608   sfnt_f26dot6 *restrict x_current;
  1609 
  1610   /* Pointer to the Y axis point data.  */
  1611   sfnt_f26dot6 *restrict y_points;
  1612 
  1613   /* Pointer to the Y axis current point data.  */
  1614   sfnt_f26dot6 *restrict y_current;
  1615 
  1616   /* Pointer to the flags associated with this data.  */
  1617   unsigned char *flags;
  1618 };
  1619 
  1620 enum
  1621   {
  1622     /* Bits 1 stands for X_SHORT_VECTOR on disk and in the tables, but
  1623        this representation is not useful in memory.  Inside an
  1624        instructed glyph, this bit is repurposed to mean that the
  1625        corresponding point is a phantom point.  */
  1626     SFNT_POINT_PHANTOM  = (1 << 1),
  1627     /* Bits 7 and 6 of a glyph point's flags is reserved.  This scaler
  1628        uses it to mean that the point has been touched in one axis or
  1629        another.  */
  1630     SFNT_POINT_TOUCHED_X = (1 << 7),
  1631     SFNT_POINT_TOUCHED_Y = (1 << 6),
  1632     SFNT_POINT_TOUCHED_BOTH = (SFNT_POINT_TOUCHED_X
  1633                                | SFNT_POINT_TOUCHED_Y),
  1634   };
  1635 
  1636 /* This is needed because `round' below needs an interpreter
  1637    argument.  */
  1638 struct sfnt_interpreter;
  1639 
  1640 struct sfnt_graphics_state
  1641 {
  1642   /* Pointer to the function used for rounding.  This function is
  1643      asymmetric, so -0.5 rounds up to 0, not -1.  It is up to the
  1644      caller to handle negative values.
  1645 
  1646      Value is undefined unless sfnt_validate_gs has been called, and
  1647      the second argument may be used to provide detailed rounding
  1648      information (``super rounding state''.)  */
  1649   sfnt_f26dot6 (*round) (sfnt_f26dot6, struct sfnt_interpreter *);
  1650 
  1651   /* Pointer to the function used to project euclidean vectors onto
  1652      the projection vector.  Value is the magnitude of the projected
  1653      vector.  */
  1654   sfnt_f26dot6 (*project) (sfnt_f26dot6, sfnt_f26dot6,
  1655                            struct sfnt_interpreter *);
  1656 
  1657   /* Pointer to the function used to project euclidean vectors onto
  1658      the dual projection vector.  Value is the magnitude of the
  1659      projected vector.  */
  1660   sfnt_f26dot6 (*dual_project) (sfnt_f26dot6, sfnt_f26dot6,
  1661                                 struct sfnt_interpreter *);
  1662 
  1663   /* Pointer to the function used to move specified points
  1664      along the freedom vector by a distance specified in terms
  1665      of the projection vector.  */
  1666   void (*move) (sfnt_f26dot6 *restrict,
  1667                 sfnt_f26dot6 *restrict, size_t,
  1668                 struct sfnt_interpreter *,
  1669                 sfnt_f26dot6, unsigned char *);
  1670 
  1671   /* Dot product between the freedom and the projection vectors.  */
  1672   sfnt_f2dot14 vector_dot_product;
  1673 
  1674   /* Controls whether the sign of control value table entries will be
  1675      changed to match the sign of the actual distance measurement with
  1676      which it is compared.  Setting auto flip to TRUE makes it
  1677      possible to control distances measured with or against the
  1678      projection vector with a single control value table entry. When
  1679      auto flip is set to FALSE, distances must be measured with the
  1680      projection vector.  */
  1681   bool auto_flip;
  1682 
  1683   /* Limits the regularizing effects of control value table entries to
  1684      cases where the difference between the table value and the
  1685      measurement taken from the original outline is sufficiently
  1686      small.  */
  1687   sfnt_f26dot6 cvt_cut_in;
  1688 
  1689   /* Establishes the base value used to calculate the range of point
  1690      sizes to which a given DELTAC[] or DELTAP[] instruction will
  1691      apply.  The formulas given below are used to calculate the range
  1692      of the various DELTA instructions.
  1693 
  1694      DELTAC1 DELTAP1 (delta_base) through (delta_base + 15)
  1695      DELTAC2 DELTAP2 (delta_base + 16) through (delta_base + 31)
  1696      DELTAC3 DELTAP3 (delta_base + 32) through (delta_base + 47)
  1697 
  1698      Please keep this documentation in sync with the TrueType
  1699      reference manual.  */
  1700   unsigned short delta_base;
  1701 
  1702   /* Determines the range of movement and smallest magnitude of
  1703      movement (the step) in a DELTAC[] or DELTAP[] instruction.
  1704      Changing the value of the delta shift makes it possible to trade
  1705      off fine control of point movement for range of movement.  A low
  1706      delta shift favors range of movement over fine control.  A high
  1707      delta shift favors fine control over range of movement.  The step
  1708      has the value 1/2 to the power delta shift.  The range of
  1709      movement is calculated by taking the number of steps allowed (16)
  1710      and multiplying it by the step.
  1711 
  1712      The legal range for delta shift is zero through six.  Negative
  1713      values are illegal.  */
  1714   unsigned short delta_shift;
  1715 
  1716   /* A second projection vector set to a line defined by the original
  1717      outline location of two points.  The dual projection vector is
  1718      used when it is necessary to measure distances from the scaled
  1719      outline before any instructions were executed.  */
  1720   struct sfnt_unit_vector dual_projection_vector;
  1721 
  1722   /* A unit vector that establishes an axis along which points can
  1723      move.  */
  1724   struct sfnt_unit_vector freedom_vector;
  1725 
  1726   /* Makes it possible to turn off instructions under some
  1727      circumstances.  When flag 1 is set, changes to the graphics state
  1728      made in the control value program will be ignored.  When flag is
  1729      1, grid fitting instructions will be ignored.  */
  1730   unsigned char instruct_control;
  1731 
  1732   /* Makes it possible to repeat certain instructions a designated
  1733      number of times.  The default value of one assures that unless
  1734      the value of loop is altered, these instructions will execute one
  1735      time.  */
  1736   unsigned short loop;
  1737 
  1738   /* Establishes the smallest possible value to which a distance will
  1739      be rounded.  */
  1740   sfnt_f26dot6 minimum_distance;
  1741 
  1742   /* A unit vector whose direction establishes an axis along which
  1743      distances are measured.  */
  1744   struct sfnt_unit_vector projection_vector;
  1745 
  1746   /* Determines the manner in which values are rounded. Can be set to
  1747      a number of predefined states or to a customized state with the
  1748      SROUND or S45ROUND instructions.  */
  1749   int round_state;
  1750 
  1751   /* Reference points.  These reference point numbers, which together
  1752      with a zone designation, specify a point in either the glyph zone
  1753      or the twilight zone.  */
  1754   uint16_t rp0, rp1, rp2;
  1755 
  1756   /* Flags which determine whether the interpreter will activate
  1757      dropout control for the current glyph.  */
  1758   int scan_control;
  1759 
  1760   /* The distance difference below which the interpreter will replace
  1761      a CVT distance or an actual distance in favor of the single width
  1762      value.  */
  1763   sfnt_f26dot6 sw_cut_in;
  1764 
  1765   /* The value used in place of the control value table distance or
  1766      the actual distance value when the difference between that
  1767      distance and the single width value is less than the single width
  1768      cut-in.  */
  1769   sfnt_f26dot6 single_width_value;
  1770 
  1771   /* Zone pointers, which reference a zone.  */
  1772   int zp0, zp1, zp2;
  1773 };
  1774 
  1775 struct sfnt_interpreter
  1776 {
  1777   /* The number of elements in the stack.  */
  1778   uint16_t max_stack_elements;
  1779 
  1780   /* The number of instructions in INSTRUCTIONS.  */
  1781   uint16_t num_instructions;
  1782 
  1783   /* Size of the storage area.  */
  1784   uint16_t storage_size;
  1785 
  1786   /* Size of the function definition area.  */
  1787   uint16_t function_defs_size;
  1788 
  1789   /* Size of the instruction definition area.  */
  1790   uint16_t instruction_defs_size;
  1791 
  1792   /* Size of the twilight zone.  */
  1793   uint16_t twilight_zone_size;
  1794 
  1795   /* The instruction pointer.  This points to the instruction
  1796      currently being executed.  */
  1797   int IP;
  1798 
  1799   /* The current scale.  */
  1800   sfnt_fixed scale;
  1801 
  1802   /* The current ppem and point size.  */
  1803   int ppem, point_size;
  1804 
  1805   /* The execution stack.  This has at most max_stack_elements
  1806      elements.  */
  1807   uint32_t *stack;
  1808 
  1809   /* Pointer past the top of the stack.  */
  1810   uint32_t *SP;
  1811 
  1812   /* The size of the control value table.  */
  1813   size_t cvt_size;
  1814 
  1815   /* Pointer to instructions currently being executed.  */
  1816   unsigned char *restrict instructions;
  1817 
  1818   /* The twilight zone.  May not be NULL.  */
  1819   sfnt_f26dot6 *restrict twilight_x, *restrict twilight_y;
  1820 
  1821   /* The original X positions of points in the twilight zone.  */
  1822   sfnt_f26dot6 *restrict twilight_original_x;
  1823 
  1824   /* The original Y positions of points in the twilight zone.
  1825 
  1826      Apple does not directly say whether or not points in the twilight
  1827      zone can have their original positions changed.  But this is
  1828      implied by ``create points in the twilight zone''.  */
  1829   sfnt_f26dot6 *restrict twilight_original_y;
  1830 
  1831   /* The scaled outlines being manipulated.  May be NULL.  */
  1832   struct sfnt_interpreter_zone *glyph_zone;
  1833 
  1834   /* The glyph advance width.  Value is undefined unless GLYPH_ZONE is
  1835      set.  */
  1836   sfnt_f26dot6 advance_width;
  1837 
  1838   /* The storage area.  */
  1839   uint32_t *storage;
  1840 
  1841   /* Control value table values.  */
  1842   sfnt_f26dot6 *cvt;
  1843 
  1844   /* Function definitions.  */
  1845   struct sfnt_interpreter_definition *function_defs;
  1846 
  1847   /* Instruction definitions.  */
  1848   struct sfnt_interpreter_definition *instruction_defs;
  1849 
  1850   /* Interpreter registers.  */
  1851   struct sfnt_graphics_state state;
  1852 
  1853   /* Detailed rounding state used when state.round_state indicates
  1854      that fine grained rounding should be used.
  1855 
  1856      PERIOD says how often a round value occurs, for numbers
  1857      increasing from PHASE to infinity.
  1858 
  1859      THRESHOLD says when to round a value between two increasing
  1860      periods towards the larger period.  */
  1861   sfnt_f26dot6 period, phase, threshold;
  1862 
  1863   /* The depth of any ongoing calls.  */
  1864   int call_depth;
  1865 
  1866   /* Jump buffer for traps.  */
  1867   jmp_buf trap;
  1868 
  1869   /* What was the trap.  */
  1870   const char *trap_reason;
  1871 
  1872   /* Number of variation axes provided by this distortable font.  */
  1873   int n_axis;
  1874 
  1875   /* Normalized axis coordinates set for this distortable font.  */
  1876   sfnt_fixed *norm_coords;
  1877 
  1878 #ifdef TEST
  1879   /* If non-NULL, function called before each instruction is
  1880      executed.  */
  1881   void (*run_hook) (struct sfnt_interpreter *);
  1882 
  1883   /* If non-NULL, function called before each stack element is
  1884      pushed.  */
  1885   void (*push_hook) (struct sfnt_interpreter *, uint32_t);
  1886 
  1887   /* If non-NULL, function called before each stack element is
  1888      popped.  */
  1889   void (*pop_hook) (struct sfnt_interpreter *, uint32_t);
  1890 #endif
  1891 };
  1892 
  1893 
  1894 
  1895 /* Glyph hinting.  */
  1896 
  1897 /* Structure describing a single scaled and fitted outline.  */
  1898 
  1899 struct sfnt_instructed_outline
  1900 {
  1901   /* The number of points in this contour, including the two phantom
  1902      points at the end.  */
  1903   size_t num_points;
  1904 
  1905   /* The number of contours in this outline.  */
  1906   size_t num_contours;
  1907 
  1908   /* The end points of each contour.  */
  1909   size_t *contour_end_points;
  1910 
  1911   /* The points of each contour, with two additional phantom points at
  1912      the end.  */
  1913   sfnt_f26dot6 *restrict x_points, *restrict y_points;
  1914 
  1915   /* The flags of each point.  */
  1916   unsigned char *flags;
  1917 };
  1918 
  1919 
  1920 
  1921 /* Functions used to read tables used by the TrueType interpreter.  */
  1922 
  1923 #ifndef TEST
  1924 
  1925 #define PROTOTYPE int, struct sfnt_offset_subtable *
  1926 
  1927 extern struct sfnt_cvt_table *sfnt_read_cvt_table (PROTOTYPE);
  1928 extern struct sfnt_fpgm_table *sfnt_read_fpgm_table (PROTOTYPE);
  1929 extern struct sfnt_prep_table *sfnt_read_prep_table (PROTOTYPE);
  1930 
  1931 #undef PROTOTYPE
  1932 
  1933 #define PROTOTYPE                               \
  1934   struct sfnt_maxp_table *,                     \
  1935   struct sfnt_cvt_table *,                      \
  1936   struct sfnt_head_table *,                     \
  1937   struct sfnt_fvar_table *,                     \
  1938   int, int
  1939 
  1940 extern struct sfnt_interpreter *sfnt_make_interpreter (PROTOTYPE);
  1941 
  1942 #undef PROTOTYPE
  1943 
  1944 #define PROTOTYPE                               \
  1945   struct sfnt_interpreter *,                    \
  1946   struct sfnt_fpgm_table *
  1947 
  1948 extern const char *sfnt_interpret_font_program (PROTOTYPE);
  1949 
  1950 #undef PROTOTYPE
  1951 
  1952 #define PROTOTYPE                               \
  1953   struct sfnt_interpreter *,                    \
  1954   struct sfnt_prep_table *,                     \
  1955   struct sfnt_graphics_state *
  1956 
  1957 extern const char *sfnt_interpret_control_value_program (PROTOTYPE);
  1958 
  1959 #undef PROTOTYPE
  1960 
  1961 #define PROTOTYPE struct sfnt_instructed_outline *
  1962 
  1963 extern struct sfnt_glyph_outline *sfnt_build_instructed_outline (PROTOTYPE);
  1964 
  1965 #undef PROTOTYPE
  1966 
  1967 #define PROTOTYPE                               \
  1968   struct sfnt_glyph *,                          \
  1969   struct sfnt_interpreter *,                    \
  1970   struct sfnt_glyph_metrics *,                  \
  1971   struct sfnt_instructed_outline **
  1972 
  1973 extern const char *sfnt_interpret_simple_glyph (PROTOTYPE);
  1974 
  1975 #undef PROTOTYPE
  1976 
  1977 #define PROTOTYPE                               \
  1978   struct sfnt_glyph *,                          \
  1979   struct sfnt_interpreter *,                    \
  1980   struct sfnt_graphics_state *,                 \
  1981   sfnt_get_glyph_proc,                          \
  1982   sfnt_free_glyph_proc,                         \
  1983   struct sfnt_hmtx_table *,                     \
  1984   struct sfnt_hhea_table *,                     \
  1985   struct sfnt_maxp_table *,                     \
  1986   struct sfnt_glyph_metrics *,                  \
  1987   void *,                                       \
  1988   struct sfnt_instructed_outline **
  1989 
  1990 extern const char *sfnt_interpret_compound_glyph (PROTOTYPE);
  1991 
  1992 #undef PROTOTYPE
  1993 
  1994 
  1995 
  1996 extern void sfnt_vary_interpreter (struct sfnt_interpreter *,
  1997                                    struct sfnt_blend *);
  1998 
  1999 #endif /* TEST */
  2000 
  2001 
  2002 
  2003 #endif /* _SFNT_H_ */

/* [<][>][^][v][top][bottom][index][help] */