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
);
138 if(cptable
->info
.char_size
!= 1) {
139 /* for double byte charsets we actually want to use cp1252 */
140 cptable
= wine_cp_get_table(1252);
142 fprintf(stderr
, "Can't find codepage 1252\n");
147 ppem
= face
->size
->metrics
.y_ppem
;
148 start
= sizeof(FNT_HEADER
) + sizeof(FONTINFO16
);
150 if(FT_Load_Char(face
, 0xc5, FT_LOAD_DEFAULT
)) {
151 fprintf(stderr
, "Can't find Aring\n");
154 ascent
= face
->glyph
->metrics
.height
>> 6;
155 descent
= ppem
- ascent
;
156 if(FT_Load_Char(face
, 'M', FT_LOAD_DEFAULT
)) {
157 fprintf(stderr
, "Can't find M\n");
160 il
= ascent
- (face
->glyph
->metrics
.height
>> 6);
162 /* Hack: Courier has no internal leading, nor do any Chinese fonts */
163 if(!strcmp(face
->family_name
, "Courier") || enc
== 936 || enc
== 950)
166 first_char
= FT_Get_First_Char(face
, &gi
);
167 if(first_char
== 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
168 first_char
= 32; /* FT_Get_Next_Char for some reason returns too high
169 number in this case */
171 dfCharTable
= malloc((255 + 3) * sizeof(*dfCharTable
));
172 memset(dfCharTable
, 0, (255 + 3) * sizeof(*dfCharTable
));
174 memset(&fi
, 0, sizeof(fi
));
175 fi
.dfFirstChar
= first_char
;
176 fi
.dfLastChar
= 0xff;
177 start
+= ((unsigned char)fi
.dfLastChar
- (unsigned char)fi
.dfFirstChar
+ 3 ) * sizeof(*dfCharTable
);
179 num_names
= FT_Get_Sfnt_Name_Count(face
);
180 for(i
= 0; i
<num_names
; i
++) {
181 FT_Get_Sfnt_Name(face
, i
, &sfntname
);
182 if(sfntname
.platform_id
== 1 && sfntname
.encoding_id
== 0 &&
183 sfntname
.language_id
== 0 && sfntname
.name_id
== 0) {
184 size_t len
= min( sfntname
.string_len
, sizeof(hdr
.dfCopyright
)-1 );
185 memcpy(hdr
.dfCopyright
, sfntname
.string
, len
);
186 hdr
.dfCopyright
[len
] = 0;
190 os2
= FT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
191 for(i
= first_char
; i
< 0x100; i
++) {
192 gi
= FT_Get_Char_Index(face
, cptable
->sbcs
.cp2uni
[i
]);
194 fprintf(stderr
, "Missing glyph for char %04x\n", cptable
->sbcs
.cp2uni
[i
]);
195 if(FT_Load_Char(face
, cptable
->sbcs
.cp2uni
[i
], FT_LOAD_DEFAULT
)) {
196 fprintf(stderr
, "error loading char %d - bad news!\n", i
);
199 dfCharTable
[i
].width
= face
->glyph
->metrics
.horiAdvance
>> 6;
200 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
201 width_bytes
+= ((face
->glyph
->metrics
.horiAdvance
>> 6) + 7) >> 3;
202 if(max_width
< (face
->glyph
->metrics
.horiAdvance
>> 6))
203 max_width
= face
->glyph
->metrics
.horiAdvance
>> 6;
206 space_size
= (ppem
+ 3) / 4;
207 dfCharTable
[i
].width
= space_size
;
208 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
209 width_bytes
+= (space_size
+ 7) >> 3;
211 dfCharTable
[++i
].width
= 0;
212 dfCharTable
[i
].offset
= start
+ (width_bytes
* ppem
);
215 fi
.dfPoints
= ((ppem
- il
) * 72 + dpi
/2) / dpi
;
218 fi
.dfAscent
= ascent
;
219 fi
.dfInternalLeading
= il
;
220 fi
.dfExternalLeading
= 0;
221 fi
.dfItalic
= (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) ? 1 : 0;
224 fi
.dfWeight
= os2
->usWeightClass
;
225 fi
.dfCharSet
= lookup_charset(enc
);
226 fi
.dfPixWidth
= (face
->face_flags
& FT_FACE_FLAG_FIXED_WIDTH
) ?
228 fi
.dfPixHeight
= ppem
;
229 fi
.dfPitchAndFamily
= FT_IS_FIXED_WIDTH(face
) ? 0 : TMPF_FIXED_PITCH
;
230 switch(os2
->panose
[PAN_FAMILYTYPE_INDEX
]) {
231 case PAN_FAMILY_SCRIPT
:
232 fi
.dfPitchAndFamily
|= FF_SCRIPT
;
234 case PAN_FAMILY_DECORATIVE
:
235 case PAN_FAMILY_PICTORIAL
:
236 fi
.dfPitchAndFamily
|= FF_DECORATIVE
;
238 case PAN_FAMILY_TEXT_DISPLAY
:
239 if(fi
.dfPitchAndFamily
== 0) /* fixed */
240 fi
.dfPitchAndFamily
= FF_MODERN
;
242 switch(os2
->panose
[PAN_SERIFSTYLE_INDEX
]) {
243 case PAN_SERIF_NORMAL_SANS
:
244 case PAN_SERIF_OBTUSE_SANS
:
245 case PAN_SERIF_PERP_SANS
:
246 fi
.dfPitchAndFamily
|= FF_SWISS
;
249 fi
.dfPitchAndFamily
|= FF_ROMAN
;
254 fi
.dfPitchAndFamily
|= FF_DONTCARE
;
257 fi
.dfAvgWidth
= avg_width
;
258 fi
.dfMaxWidth
= max_width
;
259 fi
.dfDefaultChar
= def_char
- fi
.dfFirstChar
;
260 fi
.dfBreakChar
= ' ' - fi
.dfFirstChar
;
261 fi
.dfWidthBytes
= (width_bytes
+ 1) & ~1;
263 fi
.dfFace
= start
+ fi
.dfWidthBytes
* ppem
;
264 fi
.dfBitsOffset
= start
;
265 fi
.dfFlags
= 0x10; /* DFF_1COLOR */
266 fi
.dfFlags
|= FT_IS_FIXED_WIDTH(face
) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
268 hdr
.dfVersion
= 0x300;
269 hdr
.dfSize
= start
+ fi
.dfWidthBytes
* ppem
+ strlen(face
->family_name
) + 1;
270 fwrite(&hdr
, sizeof(hdr
), 1, fp
);
271 fwrite(&fi
, sizeof(fi
), 1, fp
);
272 fwrite(dfCharTable
+ fi
.dfFirstChar
, sizeof(*dfCharTable
), ((unsigned char)fi
.dfLastChar
- (unsigned char)fi
.dfFirstChar
) + 3, fp
);
274 for(i
= first_char
; i
< 0x100; i
++) {
275 if(FT_Load_Char(face
, cptable
->sbcs
.cp2uni
[i
], FT_LOAD_DEFAULT
)) {
278 assert(dfCharTable
[i
].width
== face
->glyph
->metrics
.horiAdvance
>> 6);
280 for(x
= 0; x
< ((dfCharTable
[i
].width
+ 7) / 8); x
++) {
281 for(y
= 0; y
< ppem
; y
++) {
282 if(y
< ascent
- face
->glyph
->bitmap_top
||
283 y
>= face
->glyph
->bitmap
.rows
+ ascent
- face
->glyph
->bitmap_top
) {
287 x_off
= face
->glyph
->bitmap_left
/ 8;
288 x_end
= (face
->glyph
->bitmap_left
+ face
->glyph
->bitmap
.width
- 1) / 8;
289 if(x
< x_off
|| x
> x_end
) {
296 left_byte
= face
->glyph
->bitmap
.buffer
[(y
- (ascent
- face
->glyph
->bitmap_top
)) * face
->glyph
->bitmap
.pitch
+ x
- x_off
- 1];
298 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
299 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)))
302 right_byte
= face
->glyph
->bitmap
.buffer
[(y
- (ascent
- face
->glyph
->bitmap_top
)) * face
->glyph
->bitmap
.pitch
+ x
- x_off
];
304 byte
= (left_byte
<< (8 - (face
->glyph
->bitmap_left
& 7))) & 0xff;
305 byte
|= ((right_byte
>> (face
->glyph
->bitmap_left
& 7)) & 0xff);
310 for(x
= 0; x
< (space_size
+ 7) / 8; x
++) {
311 for(y
= 0; y
< ppem
; y
++)
315 if(width_bytes
& 1) {
316 for(y
= 0; y
< ppem
; y
++)
319 fprintf(fp
, "%s", face
->family_name
);
325 int main(int argc
, char **argv
)
331 unsigned int def_char
;
341 ppem
= atoi(argv
[2]);
344 def_char
= atoi(argv
[5]);
345 avg_width
= atoi(argv
[6]);
347 if(FT_Init_FreeType(&lib
)) {
348 fprintf(stderr
, "ft init failure\n");
352 if(FT_New_Face(lib
, argv
[1], 0, &face
)) {
353 fprintf(stderr
, "Can't open face\n");
358 if(FT_Set_Pixel_Sizes(face
, ppem
, ppem
)) {
359 fprintf(stderr
, "Can't set size\n");
364 strcpy(name
, face
->family_name
);
365 /* FIXME: should add a -o option instead */
366 for(cp
= name
; *cp
; cp
++)
368 if(*cp
== ' ') *cp
= '_';
369 else if (*cp
>= 'A' && *cp
<= 'Z') *cp
+= 'a' - 'A';
372 sprintf(output
, "%s-%d-%d-%d.fnt", name
, enc
, dpi
, ppem
);
374 fp
= fopen(output
, "w");
376 fill_fontinfo(face
, enc
, fp
, dpi
, def_char
, avg_width
);
381 #else /* HAVE_FREETYPE */
383 int main(int argc
, char **argv
)
385 fprintf( stderr
, "%s needs to be built with FreeType support\n", argv
[0] );
389 #endif /* HAVE_FREETYPE */