Synchronize with FreeType.
[ttfautohint.git] / lib / tapost.c
blob794c7187bfad3f3a11bf6655d27d7beaad1b71ec
1 /* tapost.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 <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 = *(p++) << 8;
79 idx += *(p++);
80 if (idx >= 258)
82 idx -= 257;
83 if (idx > max_name_idx)
84 max_name_idx = idx;
88 /* we add one entry to the `glyphNameIndex' array, */
89 /* and append TTFAUTOHINT_GLYPH_LEN bytes to the `names' array */
90 buf_new_len = post_table->len + 2 + TTFAUTOHINT_GLYPH_LEN;
92 /* make the allocated buffer length a multiple of 4 */
93 len = (buf_new_len + 3) & ~3;
94 buf_new = (FT_Byte*)malloc(len);
95 if (!buf_new)
96 return FT_Err_Out_Of_Memory;
98 /* pad end of buffer with zeros */
99 buf_new[len - 1] = 0x00;
100 buf_new[len - 2] = 0x00;
101 buf_new[len - 3] = 0x00;
103 /* copy the table and insert the new data */
104 p = buf;
105 p_new = buf_new;
107 memcpy(p_new, p, 32); /* header */
108 p += 32;
109 p_new += 32;
111 *p_new = HIGH(num_glyphs + 1);
112 *(p_new + 1) = LOW(num_glyphs + 1);
113 p += 2;
114 p_new += 2;
116 memcpy(p_new, p, num_glyphs * 2); /* glyphNameIndex */
117 p += num_glyphs * 2;
118 p_new += num_glyphs * 2;
120 *p_new = HIGH(max_name_idx + 1 + 257); /* new entry */
121 *(p_new + 1) = LOW(max_name_idx + 1 + 257);
122 p_new += 2;
124 memcpy(p_new, p, buf + buf_len - p); /* names */
125 p_new += buf + buf_len - p;
127 strncpy((char*)p_new, TTFAUTOHINT_GLYPH_FIRST_BYTE TTFAUTOHINT_GLYPH,
128 TTFAUTOHINT_GLYPH_LEN); /* new entry */
130 free(buf);
131 post_table->buf = buf_new;
132 post_table->len = buf_new_len;
134 else if (version == 0x28000)
136 /* XXX */
138 else
139 goto Done; /* nothing to do for format 3.0 */
141 post_table->checksum = TA_table_compute_checksum(post_table->buf,
142 post_table->len);
143 Done:
144 post_table->processed = 1;
146 return TA_Err_Ok;
149 /* end of tapost.c */