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;
32 FT_ULong num_tables
= 0;
37 SFNT_Table
* SFNT_Tables
= NULL
;
45 /*** load font into memory ***/
47 fseek(in
, 0, SEEK_END
);
49 fseek(in
, 0, SEEK_SET
);
51 /* a TTF can never be that small */
53 return FT_Err_Invalid_Argument
;
55 in_buf
= (FT_Byte
*)malloc(in_len
);
57 return FT_Err_Out_Of_Memory
;
59 if (fread(in_buf
, 1, in_len
, in
) != in_len
)
61 error
= FT_Err_Invalid_Stream_Read
;
65 error
= FT_Init_FreeType(&lib
);
69 error
= FT_New_Memory_Face(lib
, in_buf
, in_len
, -1, &face
);
72 num_faces
= face
->num_faces
;
75 error
= FT_New_Memory_Face(lib
, in_buf
, in_len
, 0, &face
);
79 /* check that font is TTF */
80 if (!FT_IS_SFNT(face
))
82 error
= FT_Err_Invalid_Argument
;
86 error
= FT_Sfnt_Table_Info(face
, 0, NULL
, &num_tables
);
90 SFNT_Tables
= (SFNT_Table
*)calloc(1, num_tables
* sizeof (SFNT_Table
));
93 error
= FT_Err_Out_Of_Memory
;
97 /* collect SFNT table data */
98 glyf_idx
= num_tables
;
99 for (i
= 0; i
< num_tables
; i
++)
104 error
= FT_Sfnt_Table_Info(face
, i
, &tag
, &len
);
105 if (error
&& error
!= FT_Err_Table_Missing
)
110 if (tag
== TTAG_glyf
)
113 /* ignore tables which we are going to create by ourselves */
114 if (!(tag
== TTAG_fpgm
118 SFNT_Tables
[i
].tag
= tag
;
119 SFNT_Tables
[i
].len
= len
;
124 /* no (non-empty) `glyf' table; this can't be a TTF with outlines */
125 if (glyf_idx
== num_tables
)
127 error
= FT_Err_Invalid_Argument
;
132 /*** split font into SFNT tables ***/
135 SFNT_Table
* stp
= SFNT_Tables
;
138 for (i
= 0; i
< num_tables
; i
++)
142 stp
->buf
= (FT_Byte
*)malloc(stp
->len
);
145 error
= FT_Err_Out_Of_Memory
;
149 error
= FT_Load_Sfnt_Table(face
, stp
->tag
, 0, stp
->buf
, &stp
->len
);
158 /* compute global hints */
159 /* construct `fpgm' table */
160 /* construct `prep' table */
161 /* construct `cvt ' table */
163 /* split `glyf' table */
164 /* handle all glyphs in a loop */
167 /* construct bytecode */
169 /* construct `glyf' table */
170 /* build font from SFNT tables */
171 /* write font from memory */
176 /* in case of error it is expected that the unallocated pointers */
177 /* are NULL (and counters are zero) */
178 for (i
= 0; i
< num_tables
; i
++)
179 free(SFNT_Tables
[i
].buf
);
182 FT_Done_FreeType(lib
);
188 /* end of ttfautohint.c */