Split `tabytecode.c' into `tacvt.c', `tafpgm.c', and `taprep.c'.
[ttfautohint.git] / src / tacvt.c
blobbb3739cd0604d248fadbaa6b5eaeae8d73f2bf92
1 /* tacvt.c */
3 /* written 2011 by Werner Lemberg <wl@gnu.org> */
5 #include "ta.h"
8 static FT_Error
9 TA_sfnt_compute_global_hints(SFNT* sfnt,
10 FONT* font)
12 FT_Error error;
13 FT_Face face = sfnt->face;
14 FT_UInt enc;
15 FT_UInt idx;
17 static const FT_Encoding latin_encs[] =
19 FT_ENCODING_UNICODE,
20 FT_ENCODING_APPLE_ROMAN,
21 FT_ENCODING_ADOBE_STANDARD,
22 FT_ENCODING_ADOBE_LATIN_1,
24 FT_ENCODING_NONE /* end of list */
28 error = ta_loader_init(font->loader);
29 if (error)
30 return error;
32 /* try to select a latin charmap */
33 for (enc = 0; latin_encs[enc] != FT_ENCODING_NONE; enc++)
35 error = FT_Select_Charmap(face, latin_encs[enc]);
36 if (!error)
37 break;
40 /* load latin glyph `a' to trigger all initializations */
41 idx = FT_Get_Char_Index(face, 'a');
42 error = ta_loader_load_glyph(font->loader, face, idx, 0);
44 return error;
48 static FT_Error
49 TA_table_build_cvt(FT_Byte** cvt,
50 FT_ULong* cvt_len,
51 SFNT* sfnt,
52 FONT* font)
54 TA_LatinAxis haxis;
55 TA_LatinAxis vaxis;
57 FT_UInt i;
58 FT_UInt buf_len;
59 FT_UInt len;
60 FT_Byte* buf;
61 FT_Byte* buf_p;
63 FT_Error error;
66 error = TA_sfnt_compute_global_hints(sfnt, font);
67 if (error)
68 return error;
70 haxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[0];
71 vaxis = &((TA_LatinMetrics)font->loader->hints.metrics)->axis[1];
73 buf_len = 2 * (2
74 + haxis->width_count
75 + vaxis->width_count
76 + 2 * vaxis->blue_count);
78 /* buffer length must be a multiple of four */
79 len = (buf_len + 3) & ~3;
80 buf = (FT_Byte*)malloc(len);
81 if (!buf)
82 return FT_Err_Out_Of_Memory;
84 /* pad end of buffer with zeros */
85 buf[len - 1] = 0x00;
86 buf[len - 2] = 0x00;
87 buf[len - 3] = 0x00;
89 buf_p = buf;
91 if (haxis->width_count > 0)
93 *(buf_p++) = HIGH(haxis->widths[0].org);
94 *(buf_p++) = LOW(haxis->widths[0].org);
96 else
98 *(buf_p++) = 0;
99 *(buf_p++) = 50;
101 if (vaxis->width_count > 0)
103 *(buf_p++) = HIGH(vaxis->widths[0].org);
104 *(buf_p++) = LOW(vaxis->widths[0].org);
106 else
108 *(buf_p++) = 0;
109 *(buf_p++) = 50;
112 for (i = 0; i < haxis->width_count; i++)
114 if (haxis->widths[i].org > 0xFFFF)
115 goto Err;
116 *(buf_p++) = HIGH(haxis->widths[i].org);
117 *(buf_p++) = LOW(haxis->widths[i].org);
120 for (i = 0; i < vaxis->width_count; i++)
122 if (vaxis->widths[i].org > 0xFFFF)
123 goto Err;
124 *(buf_p++) = HIGH(vaxis->widths[i].org);
125 *(buf_p++) = LOW(vaxis->widths[i].org);
128 for (i = 0; i < vaxis->blue_count; i++)
130 if (vaxis->blues[i].ref.org > 0xFFFF)
131 goto Err;
132 *(buf_p++) = HIGH(vaxis->blues[i].ref.org);
133 *(buf_p++) = LOW(vaxis->blues[i].ref.org);
136 for (i = 0; i < vaxis->blue_count; i++)
138 if (vaxis->blues[i].shoot.org > 0xFFFF)
139 goto Err;
140 *(buf_p++) = HIGH(vaxis->blues[i].shoot.org);
141 *(buf_p++) = LOW(vaxis->blues[i].shoot.org);
144 #if 0
145 TA_LOG(("--------------------------------------------------\n"));
146 TA_LOG(("glyph %d:\n", idx));
147 ta_glyph_hints_dump_edges(_ta_debug_hints);
148 ta_glyph_hints_dump_segments(_ta_debug_hints);
149 ta_glyph_hints_dump_points(_ta_debug_hints);
150 #endif
152 *cvt = buf;
153 *cvt_len = buf_len;
155 return FT_Err_Ok;
157 Err:
158 free(buf);
159 return TA_Err_Hinter_Overflow;
163 FT_Error
164 TA_sfnt_build_cvt_table(SFNT* sfnt,
165 FONT* font)
167 FT_Error error;
169 FT_Byte* cvt_buf;
170 FT_ULong cvt_len;
173 error = TA_sfnt_add_table_info(sfnt);
174 if (error)
175 return error;
177 error = TA_table_build_cvt(&cvt_buf, &cvt_len, sfnt, font);
178 if (error)
179 return error;
181 /* in case of success, `cvt_buf' gets linked */
182 /* and is eventually freed in `TA_font_unload' */
183 error = TA_font_add_table(font,
184 &sfnt->table_infos[sfnt->num_table_infos - 1],
185 TTAG_cvt, cvt_len, cvt_buf);
186 if (error)
188 free(cvt_buf);
189 return error;
192 return FT_Err_Ok;
195 /* end of tacvt.c */