Synchronize with FreeType.
[ttfautohint.git] / lib / tatables.c
blob9943d1f88e58b83faf2b4d634ea639ca93c6f3ad
1 /* tatables.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 <stdlib.h>
18 #include "ta.h"
21 FT_Error
22 TA_sfnt_add_table_info(SFNT* sfnt)
24 SFNT_Table_Info* table_infos_new;
27 sfnt->num_table_infos++;
28 table_infos_new =
29 (SFNT_Table_Info*)realloc(sfnt->table_infos, sfnt->num_table_infos
30 * sizeof (SFNT_Table_Info));
31 if (!table_infos_new)
33 sfnt->num_table_infos--;
34 return FT_Err_Out_Of_Memory;
36 else
37 sfnt->table_infos = table_infos_new;
39 sfnt->table_infos[sfnt->num_table_infos - 1] = MISSING;
41 return TA_Err_Ok;
45 FT_ULong
46 TA_table_compute_checksum(FT_Byte* buf,
47 FT_ULong len)
49 FT_Byte* end_buf = buf + len;
50 FT_ULong checksum = 0;
53 /* we expect that the length of `buf' is a multiple of 4 */
55 while (buf < end_buf)
57 checksum += *(buf++) << 24;
58 checksum += *(buf++) << 16;
59 checksum += *(buf++) << 8;
60 checksum += *(buf++);
63 return checksum;
67 FT_Error
68 TA_font_add_table(FONT* font,
69 SFNT_Table_Info* table_info,
70 FT_ULong tag,
71 FT_ULong len,
72 FT_Byte* buf)
74 SFNT_Table* tables_new;
75 SFNT_Table* table_last;
78 font->num_tables++;
79 tables_new = (SFNT_Table*)realloc(font->tables,
80 font->num_tables * sizeof (SFNT_Table));
81 if (!tables_new)
83 font->num_tables--;
84 return FT_Err_Out_Of_Memory;
86 else
87 font->tables = tables_new;
89 table_last = &font->tables[font->num_tables - 1];
91 table_last->tag = tag;
92 table_last->len = len;
93 table_last->buf = buf;
94 table_last->checksum = TA_table_compute_checksum(buf, len);
95 table_last->offset = 0; /* set in `TA_font_compute_table_offsets' */
96 table_last->data = NULL;
97 table_last->processed = 0;
99 /* link table and table info */
100 *table_info = font->num_tables - 1;
102 return TA_Err_Ok;
106 void
107 TA_sfnt_sort_table_info(SFNT* sfnt,
108 FONT* font)
110 /* Looking into an arbitrary TTF (with a `DSIG' table), tags */
111 /* starting with an uppercase letter are sorted before lowercase */
112 /* letters. In other words, the alphabetical ordering (as */
113 /* mandated by signing a font) is a simple numeric comparison of */
114 /* the 32bit tag values. */
116 SFNT_Table* tables = font->tables;
118 SFNT_Table_Info* table_infos = sfnt->table_infos;
119 FT_ULong num_table_infos = sfnt->num_table_infos;
121 FT_ULong i;
122 FT_ULong j;
125 /* bubble sort */
126 for (i = 1; i < num_table_infos; i++)
128 for (j = i; j > 0; j--)
130 SFNT_Table_Info swap;
131 FT_ULong tag1;
132 FT_ULong tag2;
135 tag1 = (table_infos[j] == MISSING)
137 : tables[table_infos[j]].tag;
138 tag2 = (table_infos[j - 1] == MISSING)
140 : tables[table_infos[j - 1]].tag;
142 if (tag1 > tag2)
143 break;
145 swap = table_infos[j];
146 table_infos[j] = table_infos[j - 1];
147 table_infos[j - 1] = swap;
153 void
154 TA_font_compute_table_offsets(FONT* font,
155 FT_ULong start)
157 FT_ULong i;
158 FT_ULong offset = start;
161 for (i = 0; i < font->num_tables; i++)
163 SFNT_Table* table = &font->tables[i];
166 table->offset = offset;
168 /* table offsets must be multiples of 4; */
169 /* this also fits the actual buffer lengths */
170 offset += (table->len + 3) & ~3;
174 /* end of tatables.c */