[gui] Implement input file watching.
[ttfautohint.git] / lib / taloca.c
blob3a0700478c1bb3c0f979f0af94f9139c84e5e672
1 /* taloca.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 "ta.h"
19 FT_Error
20 TA_sfnt_build_loca_table(SFNT* sfnt,
21 FONT* font)
23 SFNT_Table* loca_table = &font->tables[sfnt->loca_idx];
24 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
25 SFNT_Table* head_table = &font->tables[sfnt->head_idx];
27 glyf_Data* data = (glyf_Data*)glyf_table->data;
28 GLYPH* glyph;
30 FT_ULong offset;
31 FT_Byte loca_format;
32 FT_Byte* buf_new;
33 FT_Byte* p;
34 FT_UShort i;
37 if (loca_table->processed)
38 return TA_Err_Ok;
40 /* get largest offset */
41 offset = 0;
42 glyph = data->glyphs;
44 for (i = 0; i < data->num_glyphs; i++, glyph++)
46 /* glyph records should have offsets which are multiples of 4 */
47 offset = (offset + 3) & ~3;
48 offset += glyph->len1 + glyph->len2 + glyph->ins_len;
49 /* add two bytes for the instructionLength field */
50 if (glyph->len2 || glyph->ins_len)
51 offset += 2;
54 /* to make the short format of the `loca' table always work, */
55 /* the `glyf' table's length is adjusted to an even value */
56 offset = (offset + 1) & ~1;
58 if (offset > 0xFFFF * 2)
59 loca_format = 1;
60 else
61 loca_format = 0;
63 /* fill table */
64 if (loca_format)
66 loca_table->len = (data->num_glyphs + 1) * 4;
67 buf_new = (FT_Byte*)realloc(loca_table->buf, loca_table->len);
68 if (!buf_new)
69 return FT_Err_Out_Of_Memory;
70 else
71 loca_table->buf = buf_new;
73 p = loca_table->buf;
74 offset = 0;
75 glyph = data->glyphs;
77 for (i = 0; i < data->num_glyphs; i++, glyph++)
79 offset = (offset + 3) & ~3;
81 *(p++) = BYTE1(offset);
82 *(p++) = BYTE2(offset);
83 *(p++) = BYTE3(offset);
84 *(p++) = BYTE4(offset);
86 offset += glyph->len1 + glyph->len2 + glyph->ins_len;
87 if (glyph->len2 || glyph->ins_len)
88 offset += 2;
91 /* last element holds the size of the `glyf' table */
92 offset = (offset + 1) & ~1;
93 *(p++) = BYTE1(offset);
94 *(p++) = BYTE2(offset);
95 *(p++) = BYTE3(offset);
96 *(p++) = BYTE4(offset);
98 else
100 loca_table->len = (data->num_glyphs + 1) * 2;
101 buf_new = (FT_Byte*)realloc(loca_table->buf,
102 (loca_table->len + 3) & ~3);
103 if (!buf_new)
104 return FT_Err_Out_Of_Memory;
105 else
106 loca_table->buf = buf_new;
108 p = loca_table->buf;
109 offset = 0;
110 glyph = data->glyphs;
112 for (i = 0; i < data->num_glyphs; i++, glyph++)
114 offset = (offset + 1) & ~1;
116 *(p++) = HIGH(offset);
117 *(p++) = LOW(offset);
119 offset += (glyph->len1 + glyph->len2 + glyph->ins_len + 1) >> 1;
120 if (glyph->len2 || glyph->ins_len)
121 offset += 1;
124 /* last element holds the size of the `glyf' table -- */
125 /* this value must *not* be aligned to a multiple of 4 */
126 *(p++) = HIGH(offset);
127 *(p++) = LOW(offset);
129 /* pad `loca' table to make its length a multiple of 4 */
130 if (loca_table->len % 4 == 2)
132 *(p++) = 0;
133 *(p++) = 0;
137 loca_table->checksum = TA_table_compute_checksum(loca_table->buf,
138 loca_table->len);
139 loca_table->processed = 1;
141 head_table->buf[LOCA_FORMAT_OFFSET] = loca_format;
143 return TA_Err_Ok;
146 /* end of taloca.c */