Synchronize with FreeType.
[ttfautohint.git] / lib / tacvt.c
blob5f1343bb107362337a5631158a872ffcaa0ce052
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;
44 if (font->symbol)
45 glyph_index = 0;
46 else
48 TA_FaceGlobals globals = (TA_FaceGlobals)sfnt->face->autohint.data;
49 FT_Byte* gstyles = globals->glyph_styles;
51 TA_StyleClass style_class = ta_style_classes[style_idx];
52 TA_ScriptClass script_class = ta_script_classes[style_class->script];
54 TA_StyleMetricsRec dummy;
57 /* we don't have a `TA_Loader' object yet */
58 dummy.globals = globals;
59 dummy.style_class = style_class;
61 /* XXX: Extend this with a list of possible standard characters: */
62 /* Especially in non-default coverages, a single standard */
63 /* character may not be available. */
64 ta_get_char_index(&dummy,
65 script_class->standard_char,
66 &glyph_index,
67 &y_offset);
68 if (!glyph_index)
69 return TA_Err_Missing_Glyph;
72 * We now know that HarfBuzz can access the standard character in the
73 * current OpenType feature. However, this doesn't guarantee that there
74 * actually *is* a standard character in the corresponding coverage,
75 * since glyphs shifted with data from the GPOS table are ignored in the
76 * coverage (but neverless used to derive stem widths). For this
77 * reason, search an arbitrary character from the current coverage to
78 * trigger the coverage's metrics computation.
80 if (gstyles[glyph_index] != style_idx)
82 FT_Int i;
85 for (i = 0; i < globals->glyph_count; i++)
87 if (gstyles[i] == style_idx)
88 break;
91 if (i == globals->glyph_count)
92 return TA_Err_Missing_Glyph;
94 glyph_index = i;
98 load_flags = 1 << 29; /* vertical hinting only */
99 error = ta_loader_load_glyph(font, face, glyph_index, load_flags);
101 return error;
105 static FT_Error
106 TA_table_build_cvt(FT_Byte** cvt,
107 FT_ULong* cvt_len,
108 SFNT* sfnt,
109 FONT* font)
111 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
112 glyf_Data* data = (glyf_Data*)glyf_table->data;
114 TA_LatinAxis haxis;
115 TA_LatinAxis vaxis;
117 FT_UInt hwidth_count;
118 FT_UInt vwidth_count;
119 FT_UInt blue_count;
121 FT_UInt i, j, i_max;
122 FT_UInt buf_len;
123 FT_UInt len;
124 FT_Byte* buf;
125 FT_Byte* bufp;
126 FT_UInt cvt_offset;
128 FT_Error error;
131 /* checking multiple styles doesn't make sense for symbol fonts */
132 i_max = font->symbol ? 1 : TA_STYLE_MAX;
134 /* loop over all styles and collect the relevant CVT data */
135 /* to compute the necessary array sizes and meta-information */
136 hwidth_count = 0;
137 vwidth_count = 0;
138 blue_count = 0;
140 data->num_used_styles = 0;
142 for (i = 0; i < i_max; i++)
144 error = TA_sfnt_compute_global_hints(sfnt, font, (TA_Style)i);
145 if (error == TA_Err_Missing_Glyph)
147 TA_FaceGlobals globals = (TA_FaceGlobals)sfnt->face->autohint.data;
148 FT_Byte* gstyles = globals->glyph_styles;
149 FT_Int nn;
152 data->style_ids[i] = 0xFFFFU;
154 /* remove all references to this style; */
155 /* otherwise blue zones are computed later on, which we don't want */
156 for (nn = 0; nn < globals->glyph_count; nn++)
158 if ((gstyles[nn] & ~TA_DIGIT) == i)
160 gstyles[nn] &= ~TA_STYLE_UNASSIGNED;
161 gstyles[nn] |= globals->font->fallback_style;
165 continue;
167 if (error)
168 return error;
170 data->style_ids[i] = data->num_used_styles++;
172 if (i == TA_STYLE_NONE_DFLT)
173 continue;
174 else
176 /* XXX: generalize this to handle other metrics also */
177 haxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[0];
178 vaxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[1];
180 hwidth_count += haxis->width_count;
181 vwidth_count += vaxis->width_count;
182 /* there are two artificial blue zones at the end of the array */
183 /* that are not part of `vaxis->blue_count' */
184 blue_count += vaxis->blue_count + 2;
188 /* exit if the font doesn't contain a single supported style */
189 if (!data->num_used_styles)
190 return TA_Err_Missing_Glyph;
192 buf_len = cvtl_max_runtime /* runtime values 1 */
193 + data->num_used_styles /* runtime values 2 (for scaling) */
194 + 2 * data->num_used_styles /* runtime values 3 (blue data) */
195 + 2 * data->num_used_styles /* vert. and horiz. std. widths */
196 + hwidth_count
197 + vwidth_count
198 + 2 * blue_count; /* round and flat blue zones */
199 buf_len <<= 1; /* we have 16bit values */
201 /* buffer length must be a multiple of four */
202 len = (buf_len + 3) & ~3;
203 buf = (FT_Byte*)malloc(len);
204 if (!buf)
205 return FT_Err_Out_Of_Memory;
207 /* pad end of buffer with zeros */
208 buf[len - 1] = 0x00;
209 buf[len - 2] = 0x00;
210 buf[len - 3] = 0x00;
212 bufp = buf;
215 * some CVT values are initialized (and modified) at runtime:
217 * (1) the `cvtl_xxx' values (see `tabytecode.h')
218 * (2) a scaling value for each style
219 * (3) offset and size of the vertical widths array
220 * (needed by `bci_{smooth,strong}_stem_width') for each style
222 for (i = 0; i < (cvtl_max_runtime
223 + data->num_used_styles
224 + 2 * data->num_used_styles) * 2; i++)
225 *(bufp++) = 0;
227 cvt_offset = bufp - buf;
229 /* loop again over all styles and copy CVT data */
230 for (i = 0; i < i_max; i++)
232 /* collect offsets */
233 data->cvt_offsets[i] = ((FT_UInt)(bufp - buf) - cvt_offset) >> 1;
235 error = TA_sfnt_compute_global_hints(sfnt, font, i);
236 if (error == TA_Err_Missing_Glyph)
237 continue;
238 if (error)
239 return error;
241 if (font->loader->hints.metrics->style_class == &ta_none_dflt_style_class)
243 haxis = NULL;
244 vaxis = NULL;
246 hwidth_count = 0;
247 vwidth_count = 0;
248 blue_count = 0;
250 else
252 haxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[0];
253 vaxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[1];
255 hwidth_count = haxis->width_count;
256 vwidth_count = vaxis->width_count;
257 blue_count = vaxis->blue_count + 2; /* with artificial blue zones */
260 /* horizontal standard width */
261 if (hwidth_count > 0)
263 *(bufp++) = HIGH(haxis->widths[0].org);
264 *(bufp++) = LOW(haxis->widths[0].org);
266 else
268 *(bufp++) = 0;
269 *(bufp++) = 50;
272 for (j = 0; j < hwidth_count; j++)
274 if (haxis->widths[j].org > 0xFFFF)
275 goto Err;
276 *(bufp++) = HIGH(haxis->widths[j].org);
277 *(bufp++) = LOW(haxis->widths[j].org);
280 /* vertical standard width */
281 if (vwidth_count > 0)
283 *(bufp++) = HIGH(vaxis->widths[0].org);
284 *(bufp++) = LOW(vaxis->widths[0].org);
286 else
288 *(bufp++) = 0;
289 *(bufp++) = 50;
292 for (j = 0; j < vwidth_count; j++)
294 if (vaxis->widths[j].org > 0xFFFF)
295 goto Err;
296 *(bufp++) = HIGH(vaxis->widths[j].org);
297 *(bufp++) = LOW(vaxis->widths[j].org);
300 data->cvt_blue_adjustment_offsets[i] = 0xFFFFU;
302 for (j = 0; j < blue_count; j++)
304 if (vaxis->blues[j].ref.org > 0xFFFF)
305 goto Err;
306 *(bufp++) = HIGH(vaxis->blues[j].ref.org);
307 *(bufp++) = LOW(vaxis->blues[j].ref.org);
310 for (j = 0; j < blue_count; j++)
312 if (vaxis->blues[j].shoot.org > 0xFFFF)
313 goto Err;
314 *(bufp++) = HIGH(vaxis->blues[j].shoot.org);
315 *(bufp++) = LOW(vaxis->blues[j].shoot.org);
317 if (vaxis->blues[j].flags & TA_LATIN_BLUE_ADJUSTMENT)
318 data->cvt_blue_adjustment_offsets[i] = j;
321 data->cvt_horz_width_sizes[i] = hwidth_count;
322 data->cvt_vert_width_sizes[i] = vwidth_count;
323 data->cvt_blue_zone_sizes[i] = blue_count;
326 *cvt = buf;
327 *cvt_len = buf_len;
329 return FT_Err_Ok;
331 Err:
332 free(buf);
333 return TA_Err_Hinter_Overflow;
337 FT_Error
338 TA_sfnt_build_cvt_table(SFNT* sfnt,
339 FONT* font)
341 FT_Error error;
343 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
344 glyf_Data* data = (glyf_Data*)glyf_table->data;
346 FT_Byte* cvt_buf;
347 FT_ULong cvt_len;
350 error = TA_sfnt_add_table_info(sfnt);
351 if (error)
352 goto Exit;
354 /* `glyf', `cvt', `fpgm', and `prep' are always used in parallel */
355 if (glyf_table->processed)
357 sfnt->table_infos[sfnt->num_table_infos - 1] = data->cvt_idx;
358 goto Exit;
361 error = TA_table_build_cvt(&cvt_buf, &cvt_len, sfnt, font);
362 if (error)
363 goto Exit;
365 /* in case of success, `cvt_buf' gets linked */
366 /* and is eventually freed in `TA_font_unload' */
367 error = TA_font_add_table(font,
368 &sfnt->table_infos[sfnt->num_table_infos - 1],
369 TTAG_cvt, cvt_len, cvt_buf);
370 if (error)
371 free(cvt_buf);
372 else
373 data->cvt_idx = sfnt->table_infos[sfnt->num_table_infos - 1];
375 Exit:
376 return error;
379 /* end of tacvt.c */