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
22 #include "wine/port.h"
31 #ifdef HAVE_FT2BUILD_H
34 #include FT_FREETYPE_H
35 #include FT_SFNT_NAMES_H
36 #include FT_TRUETYPE_TABLES_H
38 #include "wine/unicode.h"
39 #include "wine/wingdi16.h"
58 static void usage(char **argv
)
60 fprintf(stderr
, "%s foo.ttf ppem enc dpi def_char avg_width\n", argv
[0]);
64 static int lookup_charset(int enc
)
66 /* FIXME: make winelib app and use TranslateCharsetInfo */
71 return RUSSIAN_CHARSET
;
77 return TURKISH_CHARSET
;
79 return HEBREW_CHARSET
;
81 return ARABIC_CHARSET
;
83 return BALTIC_CHARSET
;
85 return VIETNAMESE_CHARSET
;
105 return SHIFTJIS_CHARSET
;
107 return GB2312_CHARSET
;
109 return HANGUL_CHARSET
;
111 return CHINESEBIG5_CHARSET
;
113 fprintf(stderr
, "Unknown encoding %d - using OEM_CHARSET\n", enc
);
118 static void fill_fontinfo(FT_Face face
, int enc
, FILE *fp
, int dpi
, unsigned char def_char
, int avg_width
)
120 int ascent
, il
, ppem
, descent
, width_bytes
= 0, space_size
, max_width
= 0;
123 BYTE left_byte
, right_byte
, byte
;
125 CHAR_TABLE_ENTRY
*dfCharTable
;
126 int i
, x
, y
, x_off
, x_end
, first_char
;
129 const union cptable
*cptable
;
130 FT_SfntName sfntname
;
132 cptable
= wine_cp_get_table(enc
);
134 fprintf(stderr
, "Can't find codepage %d\n", enc
);
137 if(cptable
->info
.char_size
!= 1) {
138 fprintf(stderr
, "Can't cope with double byte codepages\n");
142 ppem
= face
->size
->metrics
.y_ppem
;
143 start
= sizeof(FNT_HEADER
) + sizeof(FONTINFO16
);
145 if(FT_Load_Char(face
, 0xc5, FT_LOAD_DEFAULT
)) {
146 fprintf(stderr
, "Can't find Aring\n");
149 ascent
= face
->glyph
->metrics
.height
>> 6;
150 descent
= ppem
- ascent
;
151 if(FT_Load_Char(face
, 'M', FT_LOAD_DEFAULT
)) {
152 fprintf(stderr
, "Can't find M\n");
155 il
= ascent
- (face
->glyph
->metrics
.height
>> 6);
157 /* Hack: Courier has no internal leading, so we do likewise */
158 if(!strcmp(face
->family_name
, "Wine Courier"))
161 first_char
= FT_Get_First_Char(face
, &gi
);
162 if(first_char
== 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
163 first_char
= 32; /* FT_Get_Next_Char for some reason returns too high
164 number in this case */
166 dfCharTable
= malloc((255 + 3) * sizeof(*dfCharTable
));
167 memset(dfCharTable
, 0, (255 + 3) * sizeof(*dfCharTable
));
169 memset(&fi
, 0, sizeof(fi
));
170 fi
.dfFirstChar
= first_char
;
171 fi
.dfLastChar
= 0xff;
172 start
+= ((unsigned char)fi
.dfLastChar
- (unsigned char)fi
.dfFirstChar
+ 3 ) * sizeof(*dfCharTable
);
174 num_names
= FT_Get_Sfnt_Name_Count(face
);
175 for(i
= 0; i
<num_names
; i
++) {
176 FT_Get_Sfnt_Name(face
, i
, &sfntname
);
177 if(sfntname
.platform_id
== 1 && sfntname
.encoding_id
== 0 &&
178 sfntname
.language_id
== 0 && sfntname
.name_id
== 0) {
179 size_t len
= min( sfntname
.string_len
, sizeof(hdr
.dfCopyright
)-1 );
180 memcpy(hdr
.dfCopyright
, sfntname
.string
, len
);
181 hdr
.dfCopyright
[len
] = 0;
185 os2
= FT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
186 for(i
= first_char
; i
< 0x100; i
++) {
187 gi
= FT_Get_Char_Index(face
, cptable
->sbcs
.cp2uni
[i
]);
189 fprintf(stderr
, "Missing glyph for char %04x\n", cptable
->sbcs
.cp2uni
[i
]);
190 if(FT_Load_Char(face
, cptable
->sbcs
.cp2uni
[i
], FT_LOAD_DEFAULT
)) {
191 fprintf(stderr
, "error loading char %d - bad news!\n", i
);
194 dfCharTable
[i
].width
= face
->glyph
->metrics
.horiAdvance
>> 6;
195 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
196 width_bytes
+= ((face
->glyph
->metrics
.horiAdvance
>> 6) + 7) >> 3;
197 if(max_width
< (face
->glyph
->metrics
.horiAdvance
>> 6))
198 max_width
= face
->glyph
->metrics
.horiAdvance
>> 6;
201 space_size
= (ppem
+ 3) / 4;
202 dfCharTable
[i
].width
= space_size
;
203 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
204 width_bytes
+= (space_size
+ 7) >> 3;
206 dfCharTable
[++i
].width
= 0;
207 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
210 fi
.dfPoints
= ((ppem
- il
) * 72 + dpi
/2) / dpi
;
213 fi
.dfAscent
= ascent
;
214 fi
.dfInternalLeading
= il
;
215 fi
.dfExternalLeading
= 0;
216 fi
.dfItalic
= (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) ? 1 : 0;
219 fi
.dfWeight
= os2
->usWeightClass
;
220 fi
.dfCharSet
= lookup_charset(enc
);
221 fi
.dfPixWidth
= (face
->face_flags
& FT_FACE_FLAG_FIXED_WIDTH
) ?
223 fi
.dfPixHeight
= ppem
;
224 fi
.dfPitchAndFamily
= FT_IS_FIXED_WIDTH(face
) ? 0 : TMPF_FIXED_PITCH
;
225 switch(os2
->panose
[PAN_FAMILYTYPE_INDEX
]) {
226 case PAN_FAMILY_SCRIPT
:
227 fi
.dfPitchAndFamily
|= FF_SCRIPT
;
229 case PAN_FAMILY_DECORATIVE
:
230 case PAN_FAMILY_PICTORIAL
:
231 fi
.dfPitchAndFamily
|= FF_DECORATIVE
;
233 case PAN_FAMILY_TEXT_DISPLAY
:
234 if(fi
.dfPitchAndFamily
== 0) /* fixed */
235 fi
.dfPitchAndFamily
= FF_MODERN
;
237 switch(os2
->panose
[PAN_SERIFSTYLE_INDEX
]) {
238 case PAN_SERIF_NORMAL_SANS
:
239 case PAN_SERIF_OBTUSE_SANS
:
240 case PAN_SERIF_PERP_SANS
:
241 fi
.dfPitchAndFamily
|= FF_SWISS
;
244 fi
.dfPitchAndFamily
|= FF_ROMAN
;
249 fi
.dfPitchAndFamily
|= FF_DONTCARE
;
252 fi
.dfAvgWidth
= avg_width
;
253 fi
.dfMaxWidth
= max_width
;
254 fi
.dfDefaultChar
= def_char
- fi
.dfFirstChar
;
255 fi
.dfBreakChar
= ' ' - fi
.dfFirstChar
;
256 fi
.dfWidthBytes
= (width_bytes
+ 1) & ~1;
258 fi
.dfFace
= start
+ fi
.dfWidthBytes
* ppem
;
259 fi
.dfBitsOffset
= start
;
260 fi
.dfFlags
= 0x10; /* DFF_1COLOR */
261 fi
.dfFlags
|= FT_IS_FIXED_WIDTH(face
) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
263 hdr
.dfVersion
= 0x300;
264 hdr
.dfSize
= start
+ fi
.dfWidthBytes
* ppem
+ strlen(face
->family_name
) + 1;
265 fwrite(&hdr
, sizeof(hdr
), 1, fp
);
266 fwrite(&fi
, sizeof(fi
), 1, fp
);
267 fwrite(dfCharTable
+ fi
.dfFirstChar
, sizeof(*dfCharTable
), ((unsigned char)fi
.dfLastChar
- (unsigned char)fi
.dfFirstChar
) + 3, fp
);
269 for(i
= first_char
; i
< 0x100; i
++) {
270 if(FT_Load_Char(face
, cptable
->sbcs
.cp2uni
[i
], FT_LOAD_DEFAULT
)) {
273 assert(dfCharTable
[i
].width
== face
->glyph
->metrics
.horiAdvance
>> 6);
275 for(x
= 0; x
< ((dfCharTable
[i
].width
+ 7) / 8); x
++) {
276 for(y
= 0; y
< ppem
; y
++) {
277 if(y
< ascent
- face
->glyph
->bitmap_top
||
278 y
>= face
->glyph
->bitmap
.rows
+ ascent
- face
->glyph
->bitmap_top
) {
282 x_off
= face
->glyph
->bitmap_left
/ 8;
283 x_end
= (face
->glyph
->bitmap_left
+ face
->glyph
->bitmap
.width
- 1) / 8;
284 if(x
< x_off
|| x
> x_end
) {
291 left_byte
= face
->glyph
->bitmap
.buffer
[(y
- (ascent
- face
->glyph
->bitmap_top
)) * face
->glyph
->bitmap
.pitch
+ x
- x_off
- 1];
293 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
294 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)))
297 right_byte
= face
->glyph
->bitmap
.buffer
[(y
- (ascent
- face
->glyph
->bitmap_top
)) * face
->glyph
->bitmap
.pitch
+ x
- x_off
];
299 byte
= (left_byte
<< (8 - (face
->glyph
->bitmap_left
& 7))) & 0xff;
300 byte
|= ((right_byte
>> (face
->glyph
->bitmap_left
& 7)) & 0xff);
305 for(x
= 0; x
< (space_size
+ 7) / 8; x
++) {
306 for(y
= 0; y
< ppem
; y
++)
310 if(width_bytes
& 1) {
311 for(y
= 0; y
< ppem
; y
++)
314 fprintf(fp
, "%s", face
->family_name
);
320 int main(int argc
, char **argv
)
326 unsigned int def_char
;
336 ppem
= atoi(argv
[2]);
339 def_char
= atoi(argv
[5]);
340 avg_width
= atoi(argv
[6]);
342 if(FT_Init_FreeType(&lib
)) {
343 fprintf(stderr
, "ft init failure\n");
347 if(FT_New_Face(lib
, argv
[1], 0, &face
)) {
348 fprintf(stderr
, "Can't open face\n");
353 if(FT_Set_Pixel_Sizes(face
, ppem
, ppem
)) {
354 fprintf(stderr
, "Can't set size\n");
359 strcpy(name
, face
->family_name
);
360 /* FIXME: should add a -o option instead */
361 for(cp
= name
; *cp
; cp
++)
363 if(*cp
== ' ') *cp
= '_';
364 else if (*cp
>= 'A' && *cp
<= 'Z') *cp
+= 'a' - 'A';
367 sprintf(output
, "%s-%d-%d-%d.fnt", name
, enc
, dpi
, ppem
);
369 fp
= fopen(output
, "w");
371 fill_fontinfo(face
, enc
, fp
, dpi
, def_char
, avg_width
);
376 #else /* HAVE_FREETYPE */
378 int main(int argc
, char **argv
)
380 fprintf( stderr
, "%s needs to be built with Freetype support\n", argv
[0] );
384 #endif /* HAVE_FREETYPE */