3 /* written 2011 by Werner Lemberg <wl@gnu.org> */
5 /* This file needs FreeType 2.4.5 or newer. */
12 #include FT_FREETYPE_H
13 #include FT_TRUETYPE_TAGS_H
15 #include "ttfautohint.h"
18 typedef struct SFNT_Table_
{
26 TTF_autohint(FILE *in
,
29 FT_Library lib
= NULL
;
31 FT_Long num_faces
= 0;
36 SFNT_Table
* SFNT_Table_Infos
= NULL
;
37 FT_ULong num_table_infos
= 0;
46 /*** load font into memory ***/
48 fseek(in
, 0, SEEK_END
);
50 fseek(in
, 0, SEEK_SET
);
52 /* a TTF can never be that small */
54 return FT_Err_Invalid_Argument
;
56 in_buf
= (FT_Byte
*)malloc(in_len
);
58 return FT_Err_Out_Of_Memory
;
60 if (fread(in_buf
, 1, in_len
, in
) != in_len
)
62 error
= FT_Err_Invalid_Stream_Read
;
66 error
= FT_Init_FreeType(&lib
);
70 error
= FT_New_Memory_Face(lib
, in_buf
, in_len
, -1, &face
);
73 num_faces
= face
->num_faces
;
76 error
= FT_New_Memory_Face(lib
, in_buf
, in_len
, 0, &face
);
80 /* check that font is TTF */
81 if (!FT_IS_SFNT(face
))
83 error
= FT_Err_Invalid_Argument
;
87 error
= FT_Sfnt_Table_Info(face
, 0, NULL
, &num_table_infos
);
92 (SFNT_Table
*)calloc(1, num_table_infos
* sizeof (SFNT_Table
));
93 if (!SFNT_Table_Infos
)
95 error
= FT_Err_Out_Of_Memory
;
99 /* collect SFNT table data */
100 glyf_idx
= num_table_infos
;
101 for (j
= 0; j
< num_table_infos
; j
++)
106 error
= FT_Sfnt_Table_Info(face
, j
, &tag
, &len
);
107 if (error
&& error
!= FT_Err_Table_Missing
)
112 if (tag
== TTAG_glyf
)
115 /* ignore tables which we are going to create by ourselves */
116 if (!(tag
== TTAG_fpgm
120 SFNT_Table_Infos
[j
].tag
= tag
;
121 SFNT_Table_Infos
[j
].len
= len
;
126 /* no (non-empty) `glyf' table; this can't be a TTF with outlines */
127 if (glyf_idx
== num_table_infos
)
129 error
= FT_Err_Invalid_Argument
;
134 /*** split font into SFNT tables ***/
137 SFNT_Table
* sti_p
= SFNT_Table_Infos
;
140 for (j
= 0; j
< num_table_infos
; j
++)
144 sti_p
->buf
= (FT_Byte
*)malloc(sti_p
->len
);
147 error
= FT_Err_Out_Of_Memory
;
151 error
= FT_Load_Sfnt_Table(face
, sti_p
->tag
, 0,
152 sti_p
->buf
, &sti_p
->len
);
161 /* compute global hints */
162 /* construct `fpgm' table */
163 /* construct `prep' table */
164 /* construct `cvt ' table */
166 /* split `glyf' table */
167 /* handle all glyphs in a loop */
170 /* construct bytecode */
172 /* construct `glyf' table */
173 /* build font from SFNT tables */
174 /* write font from memory */
179 /* in case of error it is expected that the unallocated pointers */
180 /* are NULL (and counters are zero) */
181 if (SFNT_Table_Infos
)
183 for (j
= 0; j
< num_table_infos
; j
++)
184 free(SFNT_Table_Infos
[j
].buf
);
185 free(SFNT_Table_Infos
);
188 FT_Done_FreeType(lib
);
194 /* end of ttfautohint.c */