Fix thinko in handling composite glyphs.
[ttfautohint.git] / src / tapost.c
blob0e5d8475a7cfc1dc274186edf2f94d1ace5a15cd
1 /* tapost.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 #define TTFAUTOHINT_GLYPH "\x0C.ttfautohint" /* first byte is string length */
20 #define TTFAUTOHINT_GLYPH_LEN 13
22 FT_Error
23 TA_sfnt_update_post_table(SFNT* sfnt,
24 FONT* font)
26 SFNT_Table* post_table = &font->tables[sfnt->post_idx];
27 FT_Byte* buf = post_table->buf;
28 FT_ULong buf_len = post_table->len;
29 FT_Byte* buf_new;
30 FT_ULong buf_new_len;
32 FT_ULong version;
35 if (post_table->processed)
36 return TA_Err_Ok;
38 version = buf[0] << 24;
39 version += buf[1] << 16;
40 version += buf[2] << 8;
41 version += buf[3];
43 /* since we have to add a non-standard glyph name, */
44 /* we must convert `name' tables in formats 1.0 and 2.5 into format 2.0 */
46 if (version == 0x10000)
48 /* XXX */
50 else if (version == 0x20000)
52 FT_UShort num_glyphs;
53 FT_UShort max_name_idx;
54 FT_ULong len;
55 FT_UShort i;
56 FT_Byte* p;
57 FT_Byte* p_new;
60 num_glyphs = buf[32] << 8;
61 num_glyphs += buf[33];
63 p = buf + 34;
64 max_name_idx = 0;
66 /* get number of glyph names stored in `names' array */
67 for (i = 0; i < num_glyphs; i++)
69 FT_UShort idx;
72 idx = *(p++) << 8;
73 idx += *(p++);
74 if (idx >= 258)
76 idx -= 257;
77 if (idx > max_name_idx)
78 max_name_idx = idx;
82 /* we add one entry to the `glyphNameIndex' array, */
83 /* and append TTFAUTOHINT_GLYPH_LEN bytes to the `names' array */
84 buf_new_len = post_table->len + 2 + TTFAUTOHINT_GLYPH_LEN;
86 /* make the allocated buffer length a multiple of 4 */
87 len = (buf_new_len + 3) & ~3;
88 buf_new = (FT_Byte*)malloc(len);
89 if (!buf_new)
90 return FT_Err_Out_Of_Memory;
92 /* pad end of buffer with zeros */
93 buf_new[len - 1] = 0x00;
94 buf_new[len - 2] = 0x00;
95 buf_new[len - 3] = 0x00;
97 /* copy the table and insert the new data */
98 p = buf;
99 p_new = buf_new;
101 memcpy(p_new, p, 32); /* header */
102 p += 32;
103 p_new += 32;
105 *p_new = HIGH(num_glyphs + 1);
106 *(p_new + 1) = LOW(num_glyphs + 1);
107 p += 2;
108 p_new += 2;
110 memcpy(p_new, p, num_glyphs * 2); /* glyphNameIndex */
111 p += num_glyphs * 2;
112 p_new += num_glyphs * 2;
114 *p_new = HIGH(max_name_idx + 1); /* new entry */
115 *(p_new + 1) = LOW(max_name_idx + 1);
116 p_new += 2;
118 memcpy(p_new, p, buf + buf_len - p); /* names */
119 p_new += buf + buf_len - p;
121 strncpy((char*)p_new, TTFAUTOHINT_GLYPH,
122 TTFAUTOHINT_GLYPH_LEN); /* new entry */
124 free(buf);
125 post_table->buf = buf_new;
126 post_table->len = buf_new_len;
128 else if (version == 0x28000)
130 /* XXX */
132 else
133 goto Done; /* nothing to do for format 3.0 */
135 post_table->checksum = TA_table_compute_checksum(post_table->buf,
136 post_table->len);
137 Done:
138 post_table->processed = 1;
140 return TA_Err_Ok;
143 /* end of tapost.c */