Fix OTS warning about `maxp.maxSizeOfInstructions`.
[ttfautohint.git] / lib / tafont.c
blob675fd21d9d270be9af000841f4ed914131d0a4fa
1 /* tafont.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 "ta.h"
19 FT_Error
20 TA_font_init(FONT* font)
22 FT_Error error;
23 FT_Face f;
24 FT_Int major, minor, patch;
27 error = FT_Init_FreeType(&font->lib);
28 if (error)
29 return error;
31 /* assure correct FreeType version to avoid using the wrong DLL */
32 FT_Library_Version(font->lib, &major, &minor, &patch);
33 if (((major*1000 + minor)*1000 + patch) < 2004005)
34 return TA_Err_Invalid_FreeType_Version;
36 /* get number of faces (i.e. subfonts) */
37 error = FT_New_Memory_Face(font->lib,
38 font->in_buf,
39 (FT_Long)font->in_len,
40 -1,
41 &f);
42 if (error)
43 return error;
44 font->num_sfnts = f->num_faces;
45 FT_Done_Face(f);
47 /* it is a TTC if we have more than a single subfont */
48 font->sfnts = (SFNT*)calloc(1, (size_t)font->num_sfnts * sizeof (SFNT));
49 if (!font->sfnts)
50 return FT_Err_Out_Of_Memory;
52 if (font->reference_buf)
54 error = FT_New_Memory_Face(font->lib,
55 font->reference_buf,
56 (FT_Long)font->reference_len,
57 font->reference_index,
58 &font->reference);
59 if (error)
60 return error + 0x300;
63 return TA_Err_Ok;
67 void
68 TA_font_unload(FONT* font,
69 const char* in_buf,
70 char** out_bufp,
71 const char* control_buf,
72 const char* reference_buf)
74 /* in case of error it is expected that unallocated pointers */
75 /* are NULL (and counters are zero) */
77 if (!font)
78 return;
80 ta_loader_done(font);
82 if (font->tables)
84 FT_ULong i;
87 for (i = 0; i < font->num_tables; i++)
89 free(font->tables[i].buf);
90 if (font->tables[i].data)
92 if (font->tables[i].tag == TTAG_glyf)
94 glyf_Data* data = (glyf_Data*)font->tables[i].data;
95 FT_UShort j;
98 for (j = 0; j < data->num_glyphs; j++)
100 free(data->glyphs[j].buf);
101 free(data->glyphs[j].ins_buf);
102 free(data->glyphs[j].ins_extra_buf);
103 free(data->glyphs[j].components);
104 free(data->glyphs[j].pointsums);
106 free(data->glyphs);
107 free(data);
111 free(font->tables);
114 if (font->sfnts)
116 FT_Long i;
119 for (i = 0; i < font->num_sfnts; i++)
121 FT_Done_Face(font->sfnts[i].face);
122 free(font->sfnts[i].table_infos);
124 free(font->sfnts);
127 FT_Done_Face(font->reference);
129 number_set_free(font->x_height_snapping_exceptions);
131 FT_Done_FreeType(font->lib);
133 /* in case the user provided file handles, */
134 /* free the allocated buffers for the file contents */
135 if (!in_buf)
136 free(font->in_buf);
137 if (!out_bufp)
138 font->deallocate(font->out_buf);
139 if (!control_buf)
140 free(font->control_buf);
141 if (!reference_buf)
142 free(font->reference_buf);
144 free(font);
147 /* end of tafont.c */