s/TA_NEXT_/NEXT_/.
[ttfautohint.git] / lib / tapost.c
blob2056aa6cdc854088fccd753ad24b84b0b5fb238a
1 /* tapost.c */
3 /*
4 * Copyright (C) 2011-2015 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 <string.h>
18 #include "ta.h"
21 FT_Error
22 TA_sfnt_update_post_table(SFNT* sfnt,
23 FONT* font)
25 SFNT_Table* post_table;
26 FT_Byte* buf;
27 FT_ULong buf_len;
28 FT_Byte* buf_new;
29 FT_ULong buf_new_len;
31 FT_ULong version;
34 if (sfnt->post_idx == MISSING)
35 return TA_Err_Ok;
37 post_table = &font->tables[sfnt->post_idx];
38 buf = post_table->buf;
39 buf_len = post_table->len;
41 if (post_table->processed)
42 return TA_Err_Ok;
44 version = buf[0] << 24;
45 version += buf[1] << 16;
46 version += buf[2] << 8;
47 version += buf[3];
49 /* since we have to add a non-standard glyph name, */
50 /* we must convert `name' tables in formats 1.0 and 2.5 into format 2.0 */
52 if (version == 0x10000)
54 /* XXX */
56 else if (version == 0x20000)
58 FT_UShort num_glyphs;
59 FT_UShort max_name_idx;
60 FT_ULong len;
61 FT_UShort i;
62 FT_Byte* p;
63 FT_Byte* p_new;
66 num_glyphs = buf[32] << 8;
67 num_glyphs += buf[33];
69 p = buf + 34;
70 max_name_idx = 0;
72 /* get number of glyph names stored in `names' array */
73 for (i = 0; i < num_glyphs; i++)
75 FT_UShort idx;
78 idx = NEXT_USHORT(p);
79 if (idx >= 258)
81 idx -= 257;
82 if (idx > max_name_idx)
83 max_name_idx = idx;
87 /* we add one entry to the `glyphNameIndex' array, */
88 /* and append TTFAUTOHINT_GLYPH_LEN bytes to the `names' array */
89 buf_new_len = post_table->len + 2 + TTFAUTOHINT_GLYPH_LEN;
91 /* make the allocated buffer length a multiple of 4 */
92 len = (buf_new_len + 3) & ~3U;
93 buf_new = (FT_Byte*)malloc(len);
94 if (!buf_new)
95 return FT_Err_Out_Of_Memory;
97 /* pad end of buffer with zeros */
98 buf_new[len - 1] = 0x00;
99 buf_new[len - 2] = 0x00;
100 buf_new[len - 3] = 0x00;
102 /* copy the table and insert the new data */
103 p = buf;
104 p_new = buf_new;
106 memcpy(p_new, p, 32); /* header */
107 p += 32;
108 p_new += 32;
110 *p_new = HIGH(num_glyphs + 1);
111 *(p_new + 1) = LOW(num_glyphs + 1);
112 p += 2;
113 p_new += 2;
115 memcpy(p_new, p, num_glyphs * 2); /* glyphNameIndex */
116 p += num_glyphs * 2;
117 p_new += num_glyphs * 2;
119 *p_new = HIGH(max_name_idx + 1 + 257); /* new entry */
120 *(p_new + 1) = LOW(max_name_idx + 1 + 257);
121 p_new += 2;
123 memcpy(p_new, p, buf + buf_len - p); /* names */
124 p_new += buf + buf_len - p;
126 strncpy((char*)p_new, TTFAUTOHINT_GLYPH_FIRST_BYTE TTFAUTOHINT_GLYPH,
127 TTFAUTOHINT_GLYPH_LEN); /* new entry */
129 free(buf);
130 post_table->buf = buf_new;
131 post_table->len = buf_new_len;
133 else if (version == 0x28000)
135 /* XXX */
137 else
138 goto Done; /* nothing to do for format 3.0 */
140 post_table->checksum = TA_table_compute_checksum(post_table->buf,
141 post_table->len);
142 Done:
143 post_table->processed = 1;
145 return TA_Err_Ok;
148 /* end of tapost.c */