Synchronize with FreeType.
[ttfautohint.git] / lib / tacvt.c
blobbb02604d75b73ebe1ae5aab01db3f408299717dd
1 /* tacvt.c */
3 /*
4 * Copyright (C) 2011-2014 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #include "ta.h"
19 static FT_Error
20 TA_sfnt_compute_global_hints(SFNT* sfnt,
21 FONT* font,
22 TA_Style style_idx)
24 FT_Error error;
25 FT_Face face = sfnt->face;
26 FT_ULong glyph_index;
27 FT_Long y_offset;
28 FT_Int32 load_flags;
31 error = FT_Select_Charmap(face, FT_ENCODING_UNICODE);
32 if (error)
34 if (font->symbol)
36 error = FT_Select_Charmap(face, FT_ENCODING_MS_SYMBOL);
37 if (error)
38 return TA_Err_Missing_Symbol_CMap;
40 else
41 return TA_Err_Missing_Unicode_CMap;
45 TA_FaceGlobals globals = (TA_FaceGlobals)sfnt->face->autohint.data;
46 FT_Byte* gstyles = globals->glyph_styles;
48 TA_StyleClass style_class = ta_style_classes[style_idx];
49 TA_ScriptClass script_class = ta_script_classes[style_class->script];
51 TA_StyleMetricsRec dummy;
54 /* we don't have a `TA_Loader' object yet */
55 dummy.globals = globals;
56 dummy.style_class = style_class;
59 * We check more than a single standard character to catch features
60 * like `c2sc' (small caps from caps) that don't contain lowercase
61 * letters by definition, or other features that mainly operate on
62 * numerals.
64 ta_get_char_index(&dummy,
65 script_class->standard_char1,
66 &glyph_index,
67 &y_offset);
68 if (!glyph_index)
70 if (script_class->standard_char2)
72 ta_get_char_index(&dummy,
73 script_class->standard_char2,
74 &glyph_index,
75 &y_offset);
76 if (!glyph_index)
78 if (script_class->standard_char3)
79 ta_get_char_index(&dummy,
80 script_class->standard_char3,
81 &glyph_index,
82 &y_offset);
87 if (!glyph_index)
88 return TA_Err_Missing_Glyph;
91 * We now know that HarfBuzz can access the standard character in the
92 * current OpenType feature. However, this doesn't guarantee that there
93 * actually *is* a standard character in the corresponding coverage,
94 * since glyphs shifted with data from the GPOS table are ignored in the
95 * coverage (but neverless used to derive stem widths). For this
96 * reason, search an arbitrary character from the current coverage to
97 * trigger the coverage's metrics computation.
99 if (gstyles[glyph_index] != style_idx)
101 FT_Int i;
104 for (i = 0; i < globals->glyph_count; i++)
106 if (gstyles[i] == style_idx)
107 break;
110 if (i == globals->glyph_count)
111 return TA_Err_Missing_Glyph;
113 glyph_index = i;
117 load_flags = 1 << 29; /* vertical hinting only */
118 error = ta_loader_load_glyph(font, face, glyph_index, load_flags);
120 return error;
124 static FT_Error
125 TA_table_build_cvt(FT_Byte** cvt,
126 FT_ULong* cvt_len,
127 SFNT* sfnt,
128 FONT* font)
130 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
131 glyf_Data* data = (glyf_Data*)glyf_table->data;
133 TA_LatinAxis haxis;
134 TA_LatinAxis vaxis;
136 FT_UInt hwidth_count;
137 FT_UInt vwidth_count;
138 FT_UInt blue_count;
140 FT_UInt i, j;
141 FT_UInt buf_len;
142 FT_UInt len;
143 FT_Byte* buf;
144 FT_Byte* bufp;
145 FT_UInt cvt_offset;
147 FT_Error error;
150 /* loop over all styles and collect the relevant CVT data */
151 /* to compute the necessary array sizes and meta-information */
152 hwidth_count = 0;
153 vwidth_count = 0;
154 blue_count = 0;
156 data->num_used_styles = 0;
158 for (i = 0; i < TA_STYLE_MAX; i++)
160 error = TA_sfnt_compute_global_hints(sfnt, font, (TA_Style)i);
161 if (error == TA_Err_Missing_Glyph)
163 TA_FaceGlobals globals = (TA_FaceGlobals)sfnt->face->autohint.data;
164 FT_Byte* gstyles = globals->glyph_styles;
165 FT_Int nn;
168 data->style_ids[i] = 0xFFFFU;
170 /* remove all references to this style; */
171 /* otherwise blue zones are computed later on, which we don't want */
172 for (nn = 0; nn < globals->glyph_count; nn++)
174 if ((gstyles[nn] & ~TA_DIGIT) == i)
176 gstyles[nn] &= ~TA_STYLE_UNASSIGNED;
177 gstyles[nn] |= globals->font->fallback_style;
181 continue;
183 if (error)
184 return error;
186 data->style_ids[i] = data->num_used_styles++;
188 if (i == TA_STYLE_NONE_DFLT)
189 continue;
190 else
192 /* XXX: generalize this to handle other metrics also */
193 haxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[0];
194 vaxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[1];
196 hwidth_count += haxis->width_count;
197 vwidth_count += vaxis->width_count;
198 /* there are two artificial blue zones at the end of the array */
199 /* that are not part of `vaxis->blue_count' */
200 blue_count += vaxis->blue_count + 2;
204 /* exit if the font doesn't contain a single supported style, */
205 /* and we don't have a symbol font */
206 if (!data->num_used_styles && !font->symbol)
207 return TA_Err_Missing_Glyph;
209 buf_len = cvtl_max_runtime /* runtime values 1 */
210 + data->num_used_styles /* runtime values 2 (for scaling) */
211 + 2 * data->num_used_styles /* runtime values 3 (blue data) */
212 + 2 * data->num_used_styles /* vert. and horiz. std. widths */
213 + hwidth_count
214 + vwidth_count
215 + 2 * blue_count; /* round and flat blue zones */
216 buf_len <<= 1; /* we have 16bit values */
218 /* buffer length must be a multiple of four */
219 len = (buf_len + 3) & ~3;
220 buf = (FT_Byte*)malloc(len);
221 if (!buf)
222 return FT_Err_Out_Of_Memory;
224 /* pad end of buffer with zeros */
225 buf[len - 1] = 0x00;
226 buf[len - 2] = 0x00;
227 buf[len - 3] = 0x00;
229 bufp = buf;
232 * some CVT values are initialized (and modified) at runtime:
234 * (1) the `cvtl_xxx' values (see `tabytecode.h')
235 * (2) a scaling value for each style
236 * (3) offset and size of the vertical widths array
237 * (needed by `bci_{smooth,strong}_stem_width') for each style
239 for (i = 0; i < (cvtl_max_runtime
240 + data->num_used_styles
241 + 2 * data->num_used_styles) * 2; i++)
242 *(bufp++) = 0;
244 cvt_offset = bufp - buf;
246 /* loop again over all styles and copy CVT data */
247 for (i = 0; i < TA_STYLE_MAX; i++)
249 /* collect offsets */
250 data->cvt_offsets[i] = ((FT_UInt)(bufp - buf) - cvt_offset) >> 1;
252 error = TA_sfnt_compute_global_hints(sfnt, font, (TA_Style)i);
253 if (error == TA_Err_Missing_Glyph)
254 continue;
255 if (error)
256 return error;
258 if (font->loader->hints.metrics->style_class == &ta_none_dflt_style_class)
260 haxis = NULL;
261 vaxis = NULL;
263 hwidth_count = 0;
264 vwidth_count = 0;
265 blue_count = 0;
267 else
269 haxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[0];
270 vaxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[1];
272 hwidth_count = haxis->width_count;
273 vwidth_count = vaxis->width_count;
274 blue_count = vaxis->blue_count + 2; /* with artificial blue zones */
277 /* horizontal standard width */
278 if (hwidth_count > 0)
280 *(bufp++) = HIGH(haxis->widths[0].org);
281 *(bufp++) = LOW(haxis->widths[0].org);
283 else
285 *(bufp++) = 0;
286 *(bufp++) = 50;
289 for (j = 0; j < hwidth_count; j++)
291 if (haxis->widths[j].org > 0xFFFF)
292 goto Err;
293 *(bufp++) = HIGH(haxis->widths[j].org);
294 *(bufp++) = LOW(haxis->widths[j].org);
297 /* vertical standard width */
298 if (vwidth_count > 0)
300 *(bufp++) = HIGH(vaxis->widths[0].org);
301 *(bufp++) = LOW(vaxis->widths[0].org);
303 else
305 *(bufp++) = 0;
306 *(bufp++) = 50;
309 for (j = 0; j < vwidth_count; j++)
311 if (vaxis->widths[j].org > 0xFFFF)
312 goto Err;
313 *(bufp++) = HIGH(vaxis->widths[j].org);
314 *(bufp++) = LOW(vaxis->widths[j].org);
317 data->cvt_blue_adjustment_offsets[i] = 0xFFFFU;
319 for (j = 0; j < blue_count; j++)
321 if (vaxis->blues[j].ref.org > 0xFFFF)
322 goto Err;
323 *(bufp++) = HIGH(vaxis->blues[j].ref.org);
324 *(bufp++) = LOW(vaxis->blues[j].ref.org);
327 for (j = 0; j < blue_count; j++)
329 if (vaxis->blues[j].shoot.org > 0xFFFF)
330 goto Err;
331 *(bufp++) = HIGH(vaxis->blues[j].shoot.org);
332 *(bufp++) = LOW(vaxis->blues[j].shoot.org);
334 if (vaxis->blues[j].flags & TA_LATIN_BLUE_ADJUSTMENT)
335 data->cvt_blue_adjustment_offsets[i] = j;
338 data->cvt_horz_width_sizes[i] = hwidth_count;
339 data->cvt_vert_width_sizes[i] = vwidth_count;
340 data->cvt_blue_zone_sizes[i] = blue_count;
343 *cvt = buf;
344 *cvt_len = buf_len;
346 return FT_Err_Ok;
348 Err:
349 free(buf);
350 return TA_Err_Hinter_Overflow;
354 FT_Error
355 TA_sfnt_build_cvt_table(SFNT* sfnt,
356 FONT* font)
358 FT_Error error;
360 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
361 glyf_Data* data = (glyf_Data*)glyf_table->data;
363 FT_Byte* cvt_buf;
364 FT_ULong cvt_len;
367 error = TA_sfnt_add_table_info(sfnt);
368 if (error)
369 goto Exit;
371 /* `glyf', `cvt', `fpgm', and `prep' are always used in parallel */
372 if (glyf_table->processed)
374 sfnt->table_infos[sfnt->num_table_infos - 1] = data->cvt_idx;
375 goto Exit;
378 error = TA_table_build_cvt(&cvt_buf, &cvt_len, sfnt, font);
379 if (error)
380 goto Exit;
382 /* in case of success, `cvt_buf' gets linked */
383 /* and is eventually freed in `TA_font_unload' */
384 error = TA_font_add_table(font,
385 &sfnt->table_infos[sfnt->num_table_infos - 1],
386 TTAG_cvt, cvt_len, cvt_buf);
387 if (error)
388 free(cvt_buf);
389 else
390 data->cvt_idx = sfnt->table_infos[sfnt->num_table_infos - 1];
392 Exit:
393 return error;
396 /* end of tacvt.c */