1 /*  GRAPHITE2 LICENSING
2 
3     Copyright 2010, SIL International
4     All rights reserved.
5 
6     This library is free software; you can redistribute it and/or modify
7     it under the terms of the GNU Lesser General Public License as published
8     by the Free Software Foundation; either version 2.1 of License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15 
16     You should also have received a copy of the GNU Lesser General Public
17     License along with this library in the file named "LICENSE".
18     If not, write to the Free Software Foundation, 51 Franklin Street,
19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20     internet at http://www.fsf.org/licenses/lgpl.html.
21 
22 Alternatively, the contents of this file may be used under the terms of the
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24 License, as published by the Free Software Foundation, either version 2
25 of the License or (at your option) any later version.
26 */
27 #pragma once
28 /*--------------------------------------------------------------------*//*:Ignore this sentence.
29 
30 File: TtfTypes.h
31 Responsibility: Tim Eves
32 Last reviewed: Not yet.
33 
34 Description:
35 Provides types required to represent the TTF basic types.
36 -------------------------------------------------------------------------------*//*:End Ignore*/
37 
38 
39 //**********************************************************************************************
40 //  Include files
41 //**********************************************************************************************
42 namespace graphite2
43 {
44 namespace TtfUtil
45 {
46 //**********************************************************************************************
47 //  Forward declarations
48 //**********************************************************************************************
49 
50 
51 //**********************************************************************************************
52 //  Type declarations
53 //**********************************************************************************************
54 typedef unsigned char   uint8;
55 typedef uint8           byte;
56 typedef signed char     int8;
57 typedef unsigned short  uint16;
58 typedef short           int16;
59 typedef unsigned int    uint32;
60 typedef int             int32;
61 
62 typedef int16   short_frac;
63 typedef int32   fixed;
64 typedef int16   fword;
65 typedef uint16  ufword;
66 typedef int16   f2dot14;
67 typedef uint32  long_date_time[2];
68 
69 //**********************************************************************************************
70 //  Constants and enum types
71 //**********************************************************************************************/
72 enum
73 {
74     OneFix = 1<<16
75 };
76 
77 //**********************************************************************************************
78 //  Table declarations
79 //**********************************************************************************************
80 namespace Sfnt
81 {
82 #pragma pack(push,1) // We need this or the structure members aren't aligned
83                         // correctly.  Fortunately this form of pragma is supposed
84                         // to be recognised by VS C++ too (at least according to
85                         // MSDN).
86 
87     struct OffsetSubTable
88     {
89         uint32  scaler_type;
90         uint16  num_tables,
91             search_range,
92             entry_selector,
93             range_shift;
94         struct Entry
95         {
96             uint32  tag,
97                 checksum,
98                 offset,
99                 length;
100         } table_directory[1];
101 
102         enum ScalerType
103         {
104             TrueTypeMac = 0x74727565U,
105             TrueTypeWin = 0x00010000U,
106             Type1   = 0x74797031U
107         };
108     };
109 
110 
111 
112 
113     struct CharacterCodeMap
114     {
115         uint16  version,
116             num_subtables;
117         struct
118         {
119             uint16  platform_id,
120                 platform_specific_id;
121             uint32  offset;
122         } encoding[1];
123     };
124 
125     struct CmapSubTable
126     {
127         uint16  format,
128             length,
129             language;
130     };
131 
132     struct CmapSubTableFormat4 : CmapSubTable
133     {
134         uint16  seg_count_x2,
135             search_range,
136             entry_selector,
137             range_shift,
138             end_code[1];
139         // There are arrarys after this which need their
140         // start positions calculated since end_code is
141         // seg_count uint16s long.
142     };
143 
144     struct CmapSubTableFormat12
145     {
146         fixed   format;
147         uint32  length,
148             language,
149             num_groups;
150         struct
151         {
152             uint32  start_char_code,
153                 end_char_code,
154                 start_glyph_id;
155         } group[1];
156     };
157 
158 
159 
160     struct FontHeader
161     {
162         fixed   version,
163             font_revision;
164         uint32  check_sum_adjustment,
165             magic_number;
166         uint16  flags,
167             units_per_em;
168         long_date_time  created,
169                 modified;
170         fword   x_min,
171             y_min,
172             x_max,
173             y_max;
174         uint16  mac_style,
175             lowest_rec_ppem;
176         int16   font_direction_hint,
177             index_to_loc_format,
178             glyph_data_format;
179         enum
180         {
181             MagicNumber = 0x5F0F3CF5,
182             GlypDataFormat = 0
183         };
184         enum {ShortIndexLocFormat, LongIndexLocFormat};
185     };
186 
187 
188 
189 
190     struct PostScriptGlyphName
191     {
192         fixed   format,
193             italic_angle;
194         fword   underline_position,
195             underline_thickness;
196         uint32  is_fixed_pitch,
197             min_mem_type42,
198             max_mem_type42,
199             min_mem_type1,
200             max_mem_type1;
201         enum
202         {
203             Format1  = 0x10000,
204             Format2  = 0x20000,
205             Format25 = 0x28000,
206             Format3  = 0x30000,
207             Format4  = 0x40000
208         };
209     };
210 
211     struct PostScriptGlyphName2 : PostScriptGlyphName
212     {
213         uint16  number_of_glyphs,
214             glyph_name_index[1];
215     };
216 
217     struct PostScriptGlyphName25 : PostScriptGlyphName
218     {
219         uint16  number_of_glyphs;
220         int8    offset[1];
221     };
222 
223     struct PostScriptGlyphName3 : PostScriptGlyphName {};
224 
225     struct PostScriptGlyphName4 : PostScriptGlyphName
226     {
227         uint16 glyph_to_char_map[1];
228     };
229 
230 
231     struct HorizontalHeader
232     {
233         fixed   version;
234         fword   ascent,
235             descent,
236             line_gap;
237         ufword  advance_width_max;
238         fword   min_left_side_bearing,
239             max_left_side_bearing,
240             x_max_element;
241         int16   caret_slope_rise,
242             caret_slope_run;
243         fword   caret_offset;
244         int16   reserved[4],
245             metric_data_format;
246         uint16  num_long_hor_metrics;
247     };
248 
249     struct MaximumProfile
250     {
251         fixed   version;
252         uint16  num_glyphs,
253             max_points,
254             max_contours,
255             max_component_points,
256             max_component_contours,
257             max_zones,
258             max_twilight_points,
259             max_storage,
260             max_function_defs,
261             max_instruction_defs,
262             max_stack_elements,
263             max_size_of_instructions,
264             max_component_elements,
265             max_component_depth;
266     };
267 
268 
269     typedef byte    Panose[10];
270 
271     struct Compatibility0
272     {
273         uint16  version;
274         int16   x_avg_char_width;
275         uint16  weight_class,
276             width_class;
277         int16   fs_type,
278             y_subscript_x_size,
279             y_subscript_y_size,
280             y_subscript_x_offset,
281             y_subscript_y_offset,
282             y_superscript_x_size,
283             y_superscript_y_size,
284             y_superscript_x_offset,
285             y_superscript_y_offset,
286             y_strikeout_size,
287             y_strikeout_position,
288             family_class;
289         Panose  panose;
290         uint32  unicode_range[4];
291         int8    ach_vend_id[4];
292         uint16  fs_selection,
293             fs_first_char_index,
294             fs_last_char_index, // Acording to Apple's spec this is where v0 should end
295             typo_ascender,
296             typo_descender,
297             type_linegap,
298             win_ascent,
299             win_descent;
300 
301         enum
302         {
303             Italic    =0x01,
304             Underscore=0x02,
305             Negative  =0x04,
306             Outlined  =0x08,
307             StrikeOut =0x10,
308             Bold      =0x20
309         };
310     };
311 
312     struct Compatibility1 : Compatibility0
313     {
314         uint32  codepage_range[2];
315     };
316 
317     struct Compatibility2 : Compatibility1
318     {
319             int16   x_height,
320                 cap_height;
321             uint16  default_char,
322                 break_char,
323                 max_context;
324     };
325 
326     struct Compatibility3 : Compatibility2 {};
327 
328     typedef Compatibility3  Compatibility;
329 
330 
331     struct NameRecord
332     {
333         uint16  platform_id,
334             platform_specific_id,
335             language_id,
336             name_id,
337             length,
338             offset;
339         enum    {Unicode, Mactintosh, Reserved, Microsoft};
340         enum
341         {
342             Copyright, Family, Subfamily, UniqueSubfamily,
343             Fullname, Version, PostScript
344         };
345     };
346 
347     struct LangTagRecord
348     {
349         uint16 length,
350             offset;
351     };
352 
353     struct FontNames
354     {
355         uint16  format,
356             count,
357             string_offset;
358         NameRecord name_record[1];
359     };
360 
361 
362     struct HorizontalMetric
363     {
364         uint16  advance_width;
365         int16   left_side_bearing;
366     };
367 
368 
369     struct Glyph
370     {
371         int16   number_of_contours;
372         fword   x_min,
373             y_min,
374             x_max,
375             y_max;
376     };
377 
378     struct SimpleGlyph : Glyph
379     {
380         uint16  end_pts_of_contours[1];
381         enum
382         {
383             OnCurve = 0x01,
384             XShort  = 0x02,
385             YShort  = 0x04,
386             Repeat  = 0x08,
387             XIsSame = 0x10,
388             XIsPos  = 0x10,
389             YIsSame = 0x20,
390             YIsPos  = 0x20
391         };
392     };
393 
394     struct CompoundGlyph : Glyph
395     {
396         uint16  flags,
397             glyph_index;
398         enum
399         {
400             Arg1Arg2Words   = 0x01,
401             ArgsAreXYValues = 0x02,
402             RoundXYToGrid   = 0x04,
403             HaveScale       = 0x08,
404             MoreComponents  = 0x20,
405             HaveXAndYScale  = 0x40,
406             HaveTwoByTwo    = 0x80,
407             HaveInstructions = 0x100,
408             UseMyMetrics    = 0x200,
409             OverlapCompund  = 0x400,
410             ScaledOffset    = 0x800,
411             UnscaledOffset  = 0x1000
412         };
413     };
414 
415 #pragma pack(pop)
416 } // end of namespace Sfnt
417 
418 } // end of namespace TtfUtil
419 } // end of namespace graphite2
420