Fix OTS warning about `maxp.maxSizeOfInstructions`.
[ttfautohint.git] / lib / tapost.c
blob1c896f4f173d132db9c568ac294eba81d78aa062
1 /* tapost.c */
3 /*
4 * Copyright (C) 2011-2022 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 = (FT_ULong)(buf[0] << 24
45 | buf[1] << 16
46 | buf[2] << 8
47 | 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 = (FT_UShort)(buf[32] << 8 | buf[33]);
68 p = buf + 34;
69 max_name_idx = 0;
71 /* get number of glyph names stored in `names' array */
72 for (i = 0; i < num_glyphs; i++)
74 FT_UShort idx;
77 idx = NEXT_USHORT(p);
78 if (idx >= 258)
80 idx -= 257;
81 if (idx > max_name_idx)
82 max_name_idx = idx;
86 /* we add one entry to the `glyphNameIndex' array, */
87 /* and append TTFAUTOHINT_GLYPH_LEN bytes to the `names' array */
88 buf_new_len = post_table->len + 2 + TTFAUTOHINT_GLYPH_LEN;
90 /* make the allocated buffer length a multiple of 4 */
91 len = (buf_new_len + 3) & ~3U;
92 buf_new = (FT_Byte*)malloc(len);
93 if (!buf_new)
94 return FT_Err_Out_Of_Memory;
96 /* pad end of buffer with zeros */
97 buf_new[len - 1] = 0x00;
98 buf_new[len - 2] = 0x00;
99 buf_new[len - 3] = 0x00;
101 /* copy the table and insert the new data */
102 p = buf;
103 p_new = buf_new;
105 memcpy(p_new, p, 32); /* header */
106 p += 32;
107 p_new += 32;
109 *p_new = HIGH(num_glyphs + 1);
110 *(p_new + 1) = LOW(num_glyphs + 1);
111 p += 2;
112 p_new += 2;
114 memcpy(p_new, p, num_glyphs * 2); /* glyphNameIndex */
115 p += num_glyphs * 2;
116 p_new += num_glyphs * 2;
118 *p_new = HIGH(max_name_idx + 1 + 257); /* new entry */
119 *(p_new + 1) = LOW(max_name_idx + 1 + 257);
120 p_new += 2;
122 memcpy(p_new, p, (size_t)(buf + buf_len - p)); /* names */
123 p_new += buf + buf_len - p;
125 strncpy((char*)p_new, TTFAUTOHINT_GLYPH_FIRST_BYTE TTFAUTOHINT_GLYPH,
126 TTFAUTOHINT_GLYPH_LEN); /* new entry */
128 free(buf);
129 post_table->buf = buf_new;
130 post_table->len = buf_new_len;
132 else if (version == 0x28000)
134 /* XXX */
136 else
137 goto Done; /* nothing to do for format 3.0 */
139 post_table->checksum = TA_table_compute_checksum(post_table->buf,
140 post_table->len);
141 Done:
142 post_table->processed = 1;
144 return TA_Err_Ok;
147 /* end of tapost.c */