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