Fix last CVT patch.
[ttfautohint.git] / src / tacvt.c
blobf54c84289ad503d4c80040d49dedca31ba78cea8
1 /* tacvt.c */
3 /*
4 * Copyright (C) 2011 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)
23 FT_Error error;
24 FT_Face face = sfnt->face;
25 FT_UInt enc;
26 FT_UInt idx;
28 static const FT_Encoding latin_encs[] =
30 FT_ENCODING_UNICODE,
31 FT_ENCODING_APPLE_ROMAN,
32 FT_ENCODING_ADOBE_STANDARD,
33 FT_ENCODING_ADOBE_LATIN_1,
35 FT_ENCODING_NONE /* end of list */
39 error = ta_loader_init(font->loader);
40 if (error)
41 return error;
43 /* try to select a latin charmap */
44 for (enc = 0; latin_encs[enc] != FT_ENCODING_NONE; enc++)
46 error = FT_Select_Charmap(face, latin_encs[enc]);
47 if (!error)
48 break;
51 /* load latin glyph `a' to trigger all initializations */
52 idx = FT_Get_Char_Index(face, 'a');
53 error = ta_loader_load_glyph(font->loader, face, idx,
54 font->fallback_script << 30);
56 return error;
60 static FT_Error
61 TA_table_build_cvt(FT_Byte** cvt,
62 FT_ULong* cvt_len,
63 SFNT* sfnt,
64 FONT* font)
66 TA_LatinAxis haxis;
67 TA_LatinAxis vaxis;
69 FT_UInt i;
70 FT_UInt buf_len;
71 FT_UInt len;
72 FT_Byte* buf;
73 FT_Byte* buf_p;
75 FT_Error error;
78 error = TA_sfnt_compute_global_hints(sfnt, font);
79 if (error)
80 return error;
82 haxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[0];
83 vaxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[1];
85 buf_len = 2 * (cvtl_max_runtime /* runtime values */
86 + 2 /* vertical and horizontal standard width */
87 + haxis->width_count
88 + vaxis->width_count
89 + 2 * vaxis->blue_count);
91 /* buffer length must be a multiple of four */
92 len = (buf_len + 3) & ~3;
93 buf = (FT_Byte*)malloc(len);
94 if (!buf)
95 return FT_Err_Out_Of_Memory;
97 /* pad end of buffer with zeros */
98 buf[len - 1] = 0x00;
99 buf[len - 2] = 0x00;
100 buf[len - 3] = 0x00;
102 buf_p = buf;
104 /* some CVT values are initialized (and modified) at runtime; */
105 /* see the `cvtl_xxx' macros in tabytecode.h */
106 for (i = 0; i < cvtl_max_runtime; i++)
108 *(buf_p++) = 0;
109 *(buf_p++) = 0;
112 if (haxis->width_count > 0)
114 *(buf_p++) = HIGH(haxis->widths[0].org);
115 *(buf_p++) = LOW(haxis->widths[0].org);
117 else
119 *(buf_p++) = 0;
120 *(buf_p++) = 50;
122 if (vaxis->width_count > 0)
124 *(buf_p++) = HIGH(vaxis->widths[0].org);
125 *(buf_p++) = LOW(vaxis->widths[0].org);
127 else
129 *(buf_p++) = 0;
130 *(buf_p++) = 50;
133 for (i = 0; i < haxis->width_count; i++)
135 if (haxis->widths[i].org > 0xFFFF)
136 goto Err;
137 *(buf_p++) = HIGH(haxis->widths[i].org);
138 *(buf_p++) = LOW(haxis->widths[i].org);
141 for (i = 0; i < vaxis->width_count; i++)
143 if (vaxis->widths[i].org > 0xFFFF)
144 goto Err;
145 *(buf_p++) = HIGH(vaxis->widths[i].org);
146 *(buf_p++) = LOW(vaxis->widths[i].org);
149 for (i = 0; i < vaxis->blue_count; i++)
151 if (vaxis->blues[i].ref.org > 0xFFFF)
152 goto Err;
153 *(buf_p++) = HIGH(vaxis->blues[i].ref.org);
154 *(buf_p++) = LOW(vaxis->blues[i].ref.org);
157 for (i = 0; i < vaxis->blue_count; i++)
159 if (vaxis->blues[i].shoot.org > 0xFFFF)
160 goto Err;
161 *(buf_p++) = HIGH(vaxis->blues[i].shoot.org);
162 *(buf_p++) = LOW(vaxis->blues[i].shoot.org);
165 #if 0
166 TA_LOG(("--------------------------------------------------\n"));
167 TA_LOG(("glyph %d:\n", idx));
168 ta_glyph_hints_dump_edges(_ta_debug_hints);
169 ta_glyph_hints_dump_segments(_ta_debug_hints);
170 ta_glyph_hints_dump_points(_ta_debug_hints);
171 #endif
173 *cvt = buf;
174 *cvt_len = buf_len;
176 return FT_Err_Ok;
178 Err:
179 free(buf);
180 return TA_Err_Hinter_Overflow;
184 FT_Error
185 TA_sfnt_build_cvt_table(SFNT* sfnt,
186 FONT* font)
188 FT_Error error;
190 FT_Byte* cvt_buf;
191 FT_ULong cvt_len;
194 error = TA_sfnt_add_table_info(sfnt);
195 if (error)
196 return error;
198 error = TA_table_build_cvt(&cvt_buf, &cvt_len, sfnt, font);
199 if (error)
200 return error;
202 /* in case of success, `cvt_buf' gets linked */
203 /* and is eventually freed in `TA_font_unload' */
204 error = TA_font_add_table(font,
205 &sfnt->table_infos[sfnt->num_table_infos - 1],
206 TTAG_cvt, cvt_len, cvt_buf);
207 if (error)
209 free(cvt_buf);
210 return error;
213 return FT_Err_Ok;
216 /* end of tacvt.c */