2 * sfnttofnt. Bitmap only ttf to Window fnt file converter
4 * Copyright 2004 Huw Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #ifdef HAVE_FT2BUILD_H
33 #include FT_FREETYPE_H
34 #include FT_SFNT_NAMES_H
35 #include FT_TRUETYPE_TABLES_H
37 #include "wine/unicode.h"
56 INT16 dfInternalLeading
;
57 INT16 dfExternalLeading
;
65 CHAR dfPitchAndFamily
;
78 /* Fields, introduced for Windows 3.x fonts */
85 } FONTINFO16
, *LPFONTINFO16
;
93 void usage(char **argv
)
95 fprintf(stderr
, "%s foo.ttf ppem enc dpi def_char\n", argv
[0]);
99 int lookup_charset(int enc
)
101 /* FIXME: make winelib app and use TranslateCharsetInfo */
106 return RUSSIAN_CHARSET
;
110 return GREEK_CHARSET
;
112 return TURKISH_CHARSET
;
114 return HEBREW_CHARSET
;
116 return ARABIC_CHARSET
;
118 return BALTIC_CHARSET
;
120 return VIETNAMESE_CHARSET
;
140 return SHIFTJIS_CHARSET
;
142 return GB2312_CHARSET
;
144 return HANGUL_CHARSET
;
146 return CHINESEBIG5_CHARSET
;
148 fprintf(stderr
, "Unknown encoding %d - using OEM_CHARSET\n", enc
);
153 static void fill_fontinfo(FT_Face face
, int enc
, FILE *fp
, int dpi
, unsigned char def_char
)
155 int ascent
, il
, ppem
, descent
, avg_width
, width_bytes
= 0, space_size
, max_width
= 0;
158 BYTE left_byte
, right_byte
, byte
;
160 CHAR_TABLE_ENTRY
*dfCharTable
;
161 int i
, x
, y
, x_off
, x_end
, first_char
;
164 const union cptable
*cptable
;
165 FT_SfntName sfntname
;
168 cptable
= wine_cp_get_table(enc
);
170 fprintf(stderr
, "Can't find codepage %d\n", enc
);
173 if(cptable
->info
.char_size
!= 1) {
174 fprintf(stderr
, "Can't cope with double byte codepages\n");
178 ppem
= face
->size
->metrics
.y_ppem
;
179 start
= sizeof(FNT_HEADER
) + sizeof(FONTINFO16
);
181 if(FT_Load_Char(face
, 0xc5, FT_LOAD_DEFAULT
)) {
182 fprintf(stderr
, "Can't find Aring\n");
185 ascent
= face
->glyph
->metrics
.height
>> 6;
186 descent
= ppem
- ascent
;
187 if(FT_Load_Char(face
, 'M', FT_LOAD_DEFAULT
)) {
188 fprintf(stderr
, "Can't find M\n");
191 il
= ascent
- (face
->glyph
->metrics
.height
>> 6);
193 /* Hack: Courier has no internal leading, so we do likewise */
194 if(!strcmp(face
->family_name
, "Wine Courier"))
197 if(FT_Load_Char(face
, 'X', FT_LOAD_DEFAULT
)) {
198 fprintf(stderr
, "Can't find X\n");
201 avg_width
= face
->glyph
->metrics
.horiAdvance
>> 6;
203 first_char
= FT_Get_First_Char(face
, &gi
);
204 if(first_char
== 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
205 first_char
= FT_Get_Next_Char(face
, first_char
, &gi
);
207 dfCharTable
= malloc((255 + 3) * sizeof(*dfCharTable
));
208 memset(&fi
, 0, sizeof(fi
));
209 fi
.dfFirstChar
= first_char
;
210 fi
.dfLastChar
= 0xff;
211 start
+= ((unsigned char)fi
.dfLastChar
- (unsigned char)fi
.dfFirstChar
+ 3 ) * sizeof(*dfCharTable
);
213 num_names
= FT_Get_Sfnt_Name_Count(face
);
214 for(i
= 0; i
<num_names
; i
++) {
215 FT_Get_Sfnt_Name(face
, i
, &sfntname
);
216 strncpy(namebuf
, sfntname
.string
, sfntname
.string_len
);
217 namebuf
[sfntname
.string_len
] = '\0';
218 if(sfntname
.platform_id
== 1 && sfntname
.encoding_id
== 0 && sfntname
.language_id
== 0 && sfntname
.name_id
== 0) {
219 strncpy(hdr
.dfCopyright
, namebuf
, 60);
220 hdr
.dfCopyright
[59] = '\0';
224 os2
= FT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
225 for(i
= first_char
; i
< 0x100; i
++) {
226 gi
= FT_Get_Char_Index(face
, cptable
->sbcs
.cp2uni
[i
]);
228 fprintf(stderr
, "Missing glyph for char %04x\n", cptable
->sbcs
.cp2uni
[i
]);
229 if(FT_Load_Char(face
, cptable
->sbcs
.cp2uni
[i
], FT_LOAD_DEFAULT
)) {
230 fprintf(stderr
, "error loading char %d - bad news!\n", i
);
233 dfCharTable
[i
].width
= face
->glyph
->metrics
.horiAdvance
>> 6;
234 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
235 width_bytes
+= ((face
->glyph
->metrics
.horiAdvance
>> 6) + 7) >> 3;
236 if(max_width
< (face
->glyph
->metrics
.horiAdvance
>> 6))
237 max_width
= face
->glyph
->metrics
.horiAdvance
>> 6;
240 space_size
= (ppem
+ 3) / 4;
241 dfCharTable
[i
].width
= space_size
;
242 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
243 width_bytes
+= (space_size
+ 7) >> 3;
245 dfCharTable
[++i
].width
= 0;
246 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
249 fi
.dfPoints
= ((ppem
- il
) * 72 + dpi
/2) / dpi
;
252 fi
.dfAscent
= ascent
;
253 fi
.dfInternalLeading
= il
;
254 fi
.dfExternalLeading
= 0;
255 fi
.dfItalic
= (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) ? 1 : 0;
258 fi
.dfWeight
= os2
->usWeightClass
;
259 fi
.dfCharSet
= lookup_charset(enc
);
260 fi
.dfPixWidth
= (face
->face_flags
& FT_FACE_FLAG_FIXED_WIDTH
) ?
262 fi
.dfPixHeight
= ppem
;
263 fi
.dfPitchAndFamily
= FT_IS_FIXED_WIDTH(face
) ? 0 : TMPF_FIXED_PITCH
;
264 switch(os2
->panose
[PAN_FAMILYTYPE_INDEX
]) {
265 case PAN_FAMILY_SCRIPT
:
266 fi
.dfPitchAndFamily
|= FF_SCRIPT
;
268 case PAN_FAMILY_DECORATIVE
:
269 case PAN_FAMILY_PICTORIAL
:
270 fi
.dfPitchAndFamily
|= FF_DECORATIVE
;
272 case PAN_FAMILY_TEXT_DISPLAY
:
273 if(fi
.dfPitchAndFamily
== 0) /* fixed */
274 fi
.dfPitchAndFamily
= FF_MODERN
;
276 switch(os2
->panose
[PAN_SERIFSTYLE_INDEX
]) {
277 case PAN_SERIF_NORMAL_SANS
:
278 case PAN_SERIF_OBTUSE_SANS
:
279 case PAN_SERIF_PERP_SANS
:
280 fi
.dfPitchAndFamily
|= FF_SWISS
;
283 fi
.dfPitchAndFamily
|= FF_ROMAN
;
288 fi
.dfPitchAndFamily
|= FF_DONTCARE
;
291 fi
.dfAvgWidth
= avg_width
;
292 fi
.dfMaxWidth
= max_width
;
293 fi
.dfDefaultChar
= def_char
- fi
.dfFirstChar
;
294 fi
.dfBreakChar
= ' ' - fi
.dfFirstChar
;
295 fi
.dfWidthBytes
= (width_bytes
+ 1) & ~1;
297 fi
.dfFace
= start
+ fi
.dfWidthBytes
* ppem
;
298 fi
.dfBitsOffset
= start
;
299 fi
.dfFlags
= 0x10; /* DFF_1COLOR */
300 fi
.dfFlags
|= FT_IS_FIXED_WIDTH(face
) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
302 hdr
.dfVersion
= 0x300;
303 hdr
.dfSize
= start
+ fi
.dfWidthBytes
* ppem
+ strlen(face
->family_name
) + 1;
304 fwrite(&hdr
, sizeof(hdr
), 1, fp
);
305 fwrite(&fi
, sizeof(fi
), 1, fp
);
306 fwrite(dfCharTable
+ fi
.dfFirstChar
, sizeof(*dfCharTable
), ((unsigned char)fi
.dfLastChar
- (unsigned char)fi
.dfFirstChar
) + 3, fp
);
308 for(i
= first_char
; i
< 0x100; i
++) {
309 if(FT_Load_Char(face
, cptable
->sbcs
.cp2uni
[i
], FT_LOAD_DEFAULT
)) {
312 assert(dfCharTable
[i
].width
== face
->glyph
->metrics
.horiAdvance
>> 6);
314 for(x
= 0; x
< ((dfCharTable
[i
].width
+ 7) / 8); x
++) {
315 for(y
= 0; y
< ppem
; y
++) {
316 if(y
< ascent
- face
->glyph
->bitmap_top
||
317 y
>= face
->glyph
->bitmap
.rows
+ ascent
- face
->glyph
->bitmap_top
) {
321 x_off
= face
->glyph
->bitmap_left
/ 8;
322 x_end
= (face
->glyph
->bitmap_left
+ face
->glyph
->bitmap
.width
- 1) / 8;
323 if(x
< x_off
|| x
> x_end
) {
330 left_byte
= face
->glyph
->bitmap
.buffer
[(y
- (ascent
- face
->glyph
->bitmap_top
)) * face
->glyph
->bitmap
.pitch
+ x
- x_off
- 1];
332 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
333 if(x
== x_end
&& (face
->glyph
->bitmap_left
% 8 != 0) && ((face
->glyph
->bitmap
.width
% 8 == 0) || (x
!= (((face
->glyph
->bitmap
.width
) & ~0x7) + face
->glyph
->bitmap_left
) / 8)))
336 right_byte
= face
->glyph
->bitmap
.buffer
[(y
- (ascent
- face
->glyph
->bitmap_top
)) * face
->glyph
->bitmap
.pitch
+ x
- x_off
];
338 byte
= (left_byte
<< (8 - (face
->glyph
->bitmap_left
& 7))) & 0xff;
339 byte
|= ((right_byte
>> (face
->glyph
->bitmap_left
& 7)) & 0xff);
344 for(x
= 0; x
< (space_size
+ 7) / 8; x
++) {
345 for(y
= 0; y
< ppem
; y
++)
349 if(width_bytes
& 1) {
350 for(y
= 0; y
< ppem
; y
++)
353 fprintf(fp
, "%s", face
->family_name
);
359 int main(int argc
, char **argv
)
365 unsigned int def_char
;
375 ppem
= atoi(argv
[2]);
378 def_char
= atoi(argv
[5]);
379 if(FT_Init_FreeType(&lib
)) {
380 fprintf(stderr
, "ft init failure\n");
384 if(FT_New_Face(lib
, argv
[1], 0, &face
)) {
385 fprintf(stderr
, "Can't open face\n");
390 if(FT_Set_Pixel_Sizes(face
, ppem
, ppem
)) {
391 fprintf(stderr
, "Can't set size\n");
396 strcpy(name
, face
->family_name
);
397 /* FIXME: should add a -o option instead */
398 for(cp
= name
; *cp
; cp
++)
400 if(*cp
== ' ') *cp
= '_';
401 else if (*cp
>= 'A' && *cp
<= 'Z') *cp
+= 'a' - 'A';
404 sprintf(output
, "%s-%d-%d-%d.fnt", name
, enc
, dpi
, ppem
);
406 fp
= fopen(output
, "w");
408 fill_fontinfo(face
, enc
, fp
, dpi
, def_char
);
413 #else /* HAVE_FREETYPE */
415 int main(int argc
, char **argv
)
417 fprintf( stderr
, "%s needs to be built with Freetype support\n", argv
[0] );
421 #endif /* HAVE_FREETYPE */