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