1 /****************************************************************************
2  *
3  * cfftypes.h
4  *
5  *   Basic OpenType/CFF type definitions and interface (specification
6  *   only).
7  *
8  * Copyright (C) 1996-2019 by
9  * David Turner, Robert Wilhelm, and Werner Lemberg.
10  *
11  * This file is part of the FreeType project, and may only be used,
12  * modified, and distributed under the terms of the FreeType project
13  * license, LICENSE.TXT.  By continuing to use, modify, or distribute
14  * this file you indicate that you have read the license and
15  * understand and accept it fully.
16  *
17  */
18 
19 
20 #ifndef CFFTYPES_H_
21 #define CFFTYPES_H_
22 
23 
24 #include <ft2build.h>
25 #include FT_FREETYPE_H
26 #include FT_TYPE1_TABLES_H
27 #include FT_INTERNAL_SERVICE_H
28 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
29 #include FT_INTERNAL_POSTSCRIPT_HINTS_H
30 #include FT_INTERNAL_TYPE1_TYPES_H
31 
32 
33 FT_BEGIN_HEADER
34 
35 
36   /**************************************************************************
37    *
38    * @struct:
39    *   CFF_IndexRec
40    *
41    * @description:
42    *   A structure used to model a CFF Index table.
43    *
44    * @fields:
45    *   stream ::
46    *     The source input stream.
47    *
48    *   start ::
49    *     The position of the first index byte in the input stream.
50    *
51    *   count ::
52    *     The number of elements in the index.
53    *
54    *   off_size ::
55    *     The size in bytes of object offsets in index.
56    *
57    *   data_offset ::
58    *     The position of first data byte in the index's bytes.
59    *
60    *   data_size ::
61    *     The size of the data table in this index.
62    *
63    *   offsets ::
64    *     A table of element offsets in the index.  Must be loaded explicitly.
65    *
66    *   bytes ::
67    *     If the index is loaded in memory, its bytes.
68    */
69   typedef struct  CFF_IndexRec_
70   {
71     FT_Stream  stream;
72     FT_ULong   start;
73     FT_UInt    hdr_size;
74     FT_UInt    count;
75     FT_Byte    off_size;
76     FT_ULong   data_offset;
77     FT_ULong   data_size;
78 
79     FT_ULong*  offsets;
80     FT_Byte*   bytes;
81 
82   } CFF_IndexRec, *CFF_Index;
83 
84 
85   typedef struct  CFF_EncodingRec_
86   {
87     FT_UInt     format;
88     FT_ULong    offset;
89 
90     FT_UInt     count;
91     FT_UShort   sids [256];  /* avoid dynamic allocations */
92     FT_UShort   codes[256];
93 
94   } CFF_EncodingRec, *CFF_Encoding;
95 
96 
97   typedef struct  CFF_CharsetRec_
98   {
99 
100     FT_UInt     format;
101     FT_ULong    offset;
102 
103     FT_UShort*  sids;
104     FT_UShort*  cids;       /* the inverse mapping of `sids'; only needed */
105                             /* for CID-keyed fonts                        */
106     FT_UInt     max_cid;
107     FT_UInt     num_glyphs;
108 
109   } CFF_CharsetRec, *CFF_Charset;
110 
111 
112   /* cf. similar fields in file `ttgxvar.h' from the `truetype' module */
113 
114   typedef struct  CFF_VarData_
115   {
116 #if 0
117     FT_UInt  itemCount;       /* not used; always zero */
118     FT_UInt  shortDeltaCount; /* not used; always zero */
119 #endif
120 
121     FT_UInt   regionIdxCount; /* number of region indexes           */
122     FT_UInt*  regionIndices;  /* array of `regionIdxCount' indices; */
123                               /* these index `varRegionList'        */
124   } CFF_VarData;
125 
126 
127   /* contribution of one axis to a region */
128   typedef struct  CFF_AxisCoords_
129   {
130     FT_Fixed  startCoord;
131     FT_Fixed  peakCoord;      /* zero peak means no effect (factor = 1) */
132     FT_Fixed  endCoord;
133 
134   } CFF_AxisCoords;
135 
136 
137   typedef struct  CFF_VarRegion_
138   {
139     CFF_AxisCoords*  axisList;      /* array of axisCount records */
140 
141   } CFF_VarRegion;
142 
143 
144   typedef struct  CFF_VStoreRec_
145   {
146     FT_UInt         dataCount;
147     CFF_VarData*    varData;        /* array of dataCount records      */
148                                     /* vsindex indexes this array      */
149     FT_UShort       axisCount;
150     FT_UInt         regionCount;    /* total number of regions defined */
151     CFF_VarRegion*  varRegionList;
152 
153   } CFF_VStoreRec, *CFF_VStore;
154 
155 
156   /* forward reference */
157   typedef struct CFF_FontRec_*  CFF_Font;
158 
159 
160   /* This object manages one cached blend vector.                  */
161   /*                                                               */
162   /* There is a BlendRec for Private DICT parsing in each subfont  */
163   /* and a BlendRec for charstrings in CF2_Font instance data.     */
164   /* A cached BV may be used across DICTs or Charstrings if inputs */
165   /* have not changed.                                             */
166   /*                                                               */
167   /* `usedBV' is reset at the start of each parse or charstring.   */
168   /* vsindex cannot be changed after a BV is used.                 */
169   /*                                                               */
170   /* Note: NDV is long (32/64 bit), while BV is 16.16 (FT_Int32).  */
171   typedef struct  CFF_BlendRec_
172   {
173     FT_Bool    builtBV;        /* blendV has been built           */
174     FT_Bool    usedBV;         /* blendV has been used            */
175     CFF_Font   font;           /* top level font struct           */
176     FT_UInt    lastVsindex;    /* last vsindex used               */
177     FT_UInt    lenNDV;         /* normDV length (aka numAxes)     */
178     FT_Fixed*  lastNDV;        /* last NDV used                   */
179     FT_UInt    lenBV;          /* BlendV length (aka numMasters)  */
180     FT_Int32*  BV;             /* current blendV (per DICT/glyph) */
181 
182   } CFF_BlendRec, *CFF_Blend;
183 
184 
185   typedef struct  CFF_FontRecDictRec_
186   {
187     FT_UInt    version;
188     FT_UInt    notice;
189     FT_UInt    copyright;
190     FT_UInt    full_name;
191     FT_UInt    family_name;
192     FT_UInt    weight;
193     FT_Bool    is_fixed_pitch;
194     FT_Fixed   italic_angle;
195     FT_Fixed   underline_position;
196     FT_Fixed   underline_thickness;
197     FT_Int     paint_type;
198     FT_Int     charstring_type;
199     FT_Matrix  font_matrix;
200     FT_Bool    has_font_matrix;
201     FT_ULong   units_per_em;  /* temporarily used as scaling value also */
202     FT_Vector  font_offset;
203     FT_ULong   unique_id;
204     FT_BBox    font_bbox;
205     FT_Pos     stroke_width;
206     FT_ULong   charset_offset;
207     FT_ULong   encoding_offset;
208     FT_ULong   charstrings_offset;
209     FT_ULong   private_offset;
210     FT_ULong   private_size;
211     FT_Long    synthetic_base;
212     FT_UInt    embedded_postscript;
213 
214     /* these should only be used for the top-level font dictionary */
215     FT_UInt    cid_registry;
216     FT_UInt    cid_ordering;
217     FT_Long    cid_supplement;
218 
219     FT_Long    cid_font_version;
220     FT_Long    cid_font_revision;
221     FT_Long    cid_font_type;
222     FT_ULong   cid_count;
223     FT_ULong   cid_uid_base;
224     FT_ULong   cid_fd_array_offset;
225     FT_ULong   cid_fd_select_offset;
226     FT_UInt    cid_font_name;
227 
228     /* the next fields come from the data of the deprecated          */
229     /* `MultipleMaster' operator; they are needed to parse the (also */
230     /* deprecated) `blend' operator in Type 2 charstrings            */
231     FT_UShort  num_designs;
232     FT_UShort  num_axes;
233 
234     /* fields for CFF2 */
235     FT_ULong   vstore_offset;
236     FT_UInt    maxstack;
237 
238   } CFF_FontRecDictRec, *CFF_FontRecDict;
239 
240 
241   /* forward reference */
242   typedef struct CFF_SubFontRec_*  CFF_SubFont;
243 
244 
245   typedef struct  CFF_PrivateRec_
246   {
247     FT_Byte   num_blue_values;
248     FT_Byte   num_other_blues;
249     FT_Byte   num_family_blues;
250     FT_Byte   num_family_other_blues;
251 
252     FT_Pos    blue_values[14];
253     FT_Pos    other_blues[10];
254     FT_Pos    family_blues[14];
255     FT_Pos    family_other_blues[10];
256 
257     FT_Fixed  blue_scale;
258     FT_Pos    blue_shift;
259     FT_Pos    blue_fuzz;
260     FT_Pos    standard_width;
261     FT_Pos    standard_height;
262 
263     FT_Byte   num_snap_widths;
264     FT_Byte   num_snap_heights;
265     FT_Pos    snap_widths[13];
266     FT_Pos    snap_heights[13];
267     FT_Bool   force_bold;
268     FT_Fixed  force_bold_threshold;
269     FT_Int    lenIV;
270     FT_Int    language_group;
271     FT_Fixed  expansion_factor;
272     FT_Long   initial_random_seed;
273     FT_ULong  local_subrs_offset;
274     FT_Pos    default_width;
275     FT_Pos    nominal_width;
276 
277     /* fields for CFF2 */
278     FT_UInt      vsindex;
279     CFF_SubFont  subfont;
280 
281   } CFF_PrivateRec, *CFF_Private;
282 
283 
284   typedef struct  CFF_FDSelectRec_
285   {
286     FT_Byte   format;
287     FT_UInt   range_count;
288 
289     /* that's the table, taken from the file `as is' */
290     FT_Byte*  data;
291     FT_UInt   data_size;
292 
293     /* small cache for format 3 only */
294     FT_UInt   cache_first;
295     FT_UInt   cache_count;
296     FT_Byte   cache_fd;
297 
298   } CFF_FDSelectRec, *CFF_FDSelect;
299 
300 
301   /* A SubFont packs a font dict and a private dict together.  They are */
302   /* needed to support CID-keyed CFF fonts.                             */
303   typedef struct  CFF_SubFontRec_
304   {
305     CFF_FontRecDictRec  font_dict;
306     CFF_PrivateRec      private_dict;
307 
308     /* fields for CFF2 */
309     CFF_BlendRec  blend;      /* current blend vector       */
310     FT_UInt       lenNDV;     /* current length NDV or zero */
311     FT_Fixed*     NDV;        /* ptr to current NDV or NULL */
312 
313     /* `blend_stack' is a writable buffer to hold blend results.          */
314     /* This buffer is to the side of the normal cff parser stack;         */
315     /* `cff_parse_blend' and `cff_blend_doBlend' push blend results here. */
316     /* The normal stack then points to these values instead of the DICT   */
317     /* because all other operators in Private DICT clear the stack.       */
318     /* `blend_stack' could be cleared at each operator other than blend.  */
319     /* Blended values are stored as 5-byte fixed point values.            */
320 
321     FT_Byte*  blend_stack;    /* base of stack allocation     */
322     FT_Byte*  blend_top;      /* first empty slot             */
323     FT_UInt   blend_used;     /* number of bytes in use       */
324     FT_UInt   blend_alloc;    /* number of bytes allocated    */
325 
326     CFF_IndexRec  local_subrs_index;
327     FT_Byte**     local_subrs; /* array of pointers           */
328                                /* into Local Subrs INDEX data */
329 
330     FT_UInt32  random;
331 
332   } CFF_SubFontRec;
333 
334 
335 #define CFF_MAX_CID_FONTS  256
336 
337 
338   typedef struct  CFF_FontRec_
339   {
340     FT_Library       library;
341     FT_Stream        stream;
342     FT_Memory        memory;        /* TODO: take this from stream->memory? */
343     FT_ULong         base_offset;   /* offset to start of CFF */
344     FT_UInt          num_faces;
345     FT_UInt          num_glyphs;
346 
347     FT_Byte          version_major;
348     FT_Byte          version_minor;
349     FT_Byte          header_size;
350 
351     FT_UInt          top_dict_length;   /* cff2 only */
352 
353     FT_Bool          cff2;
354 
355     CFF_IndexRec     name_index;
356     CFF_IndexRec     top_dict_index;
357     CFF_IndexRec     global_subrs_index;
358 
359     CFF_EncodingRec  encoding;
360     CFF_CharsetRec   charset;
361 
362     CFF_IndexRec     charstrings_index;
363     CFF_IndexRec     font_dict_index;
364     CFF_IndexRec     private_index;
365     CFF_IndexRec     local_subrs_index;
366 
367     FT_String*       font_name;
368 
369     /* array of pointers into Global Subrs INDEX data */
370     FT_Byte**        global_subrs;
371 
372     /* array of pointers into String INDEX data stored at string_pool */
373     FT_UInt          num_strings;
374     FT_Byte**        strings;
375     FT_Byte*         string_pool;
376     FT_ULong         string_pool_size;
377 
378     CFF_SubFontRec   top_font;
379     FT_UInt          num_subfonts;
380     CFF_SubFont      subfonts[CFF_MAX_CID_FONTS];
381 
382     CFF_FDSelectRec  fd_select;
383 
384     /* interface to PostScript hinter */
385     PSHinter_Service  pshinter;
386 
387     /* interface to Postscript Names service */
388     FT_Service_PsCMaps  psnames;
389 
390     /* interface to CFFLoad service */
391     const void*  cffload;
392 
393     /* since version 2.3.0 */
394     PS_FontInfoRec*  font_info;   /* font info dictionary */
395 
396     /* since version 2.3.6 */
397     FT_String*       registry;
398     FT_String*       ordering;
399 
400     /* since version 2.4.12 */
401     FT_Generic       cf2_instance;
402 
403     /* since version 2.7.1 */
404     CFF_VStoreRec    vstore;        /* parsed vstore structure */
405 
406     /* since version 2.9 */
407     PS_FontExtraRec*  font_extra;
408 
409   } CFF_FontRec;
410 
411 
412 FT_END_HEADER
413 
414 #endif /* CFFTYPES_H_ */
415 
416 
417 /* END */
418