Merge to trunk head (r46631)
[reactos.git] / lib / 3rdparty / freetype / src / winfonts / winfnt.c
1 /***************************************************************************/
2 /* */
3 /* winfnt.c */
4 /* */
5 /* FreeType font driver for Windows FNT/FON files */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2006, 2007 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* Copyright 2003 Huw D M Davies for Codeweavers */
10 /* Copyright 2007 Dmitry Timoshkov for Codeweavers */
11 /* */
12 /* This file is part of the FreeType project, and may only be used, */
13 /* modified, and distributed under the terms of the FreeType project */
14 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
15 /* this file you indicate that you have read the license and */
16 /* understand and accept it fully. */
17 /* */
18 /***************************************************************************/
19
20
21 #include <ft2build.h>
22 #include FT_WINFONTS_H
23 #include FT_INTERNAL_DEBUG_H
24 #include FT_INTERNAL_STREAM_H
25 #include FT_INTERNAL_OBJECTS_H
26
27 #include "winfnt.h"
28 #include "fnterrs.h"
29 #include FT_SERVICE_WINFNT_H
30 #include FT_SERVICE_XFREE86_NAME_H
31
32 /*************************************************************************/
33 /* */
34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
36 /* messages during execution. */
37 /* */
38 #undef FT_COMPONENT
39 #define FT_COMPONENT trace_winfnt
40
41
42 static const FT_Frame_Field winmz_header_fields[] =
43 {
44 #undef FT_STRUCTURE
45 #define FT_STRUCTURE WinMZ_HeaderRec
46
47 FT_FRAME_START( 64 ),
48 FT_FRAME_USHORT_LE ( magic ),
49 FT_FRAME_SKIP_BYTES( 29 * 2 ),
50 FT_FRAME_ULONG_LE ( lfanew ),
51 FT_FRAME_END
52 };
53
54 static const FT_Frame_Field winne_header_fields[] =
55 {
56 #undef FT_STRUCTURE
57 #define FT_STRUCTURE WinNE_HeaderRec
58
59 FT_FRAME_START( 40 ),
60 FT_FRAME_USHORT_LE ( magic ),
61 FT_FRAME_SKIP_BYTES( 34 ),
62 FT_FRAME_USHORT_LE ( resource_tab_offset ),
63 FT_FRAME_USHORT_LE ( rname_tab_offset ),
64 FT_FRAME_END
65 };
66
67 static const FT_Frame_Field winpe32_header_fields[] =
68 {
69 #undef FT_STRUCTURE
70 #define FT_STRUCTURE WinPE32_HeaderRec
71
72 FT_FRAME_START( 248 ),
73 FT_FRAME_ULONG_LE ( magic ), /* PE00 */
74 FT_FRAME_USHORT_LE ( machine ), /* 0x014c - i386 */
75 FT_FRAME_USHORT_LE ( number_of_sections ),
76 FT_FRAME_SKIP_BYTES( 12 ),
77 FT_FRAME_USHORT_LE ( size_of_optional_header ),
78 FT_FRAME_SKIP_BYTES( 2 ),
79 FT_FRAME_USHORT_LE ( magic32 ), /* 0x10b */
80 FT_FRAME_SKIP_BYTES( 110 ),
81 FT_FRAME_ULONG_LE ( rsrc_virtual_address ),
82 FT_FRAME_ULONG_LE ( rsrc_size ),
83 FT_FRAME_SKIP_BYTES( 104 ),
84 FT_FRAME_END
85 };
86
87 static const FT_Frame_Field winpe32_section_fields[] =
88 {
89 #undef FT_STRUCTURE
90 #define FT_STRUCTURE WinPE32_SectionRec
91
92 FT_FRAME_START( 40 ),
93 FT_FRAME_BYTES ( name, 8 ),
94 FT_FRAME_SKIP_BYTES( 4 ),
95 FT_FRAME_ULONG_LE ( virtual_address ),
96 FT_FRAME_ULONG_LE ( size_of_raw_data ),
97 FT_FRAME_ULONG_LE ( pointer_to_raw_data ),
98 FT_FRAME_SKIP_BYTES( 16 ),
99 FT_FRAME_END
100 };
101
102 static const FT_Frame_Field winpe_rsrc_dir_fields[] =
103 {
104 #undef FT_STRUCTURE
105 #define FT_STRUCTURE WinPE_RsrcDirRec
106
107 FT_FRAME_START( 16 ),
108 FT_FRAME_ULONG_LE ( characteristics ),
109 FT_FRAME_ULONG_LE ( time_date_stamp ),
110 FT_FRAME_USHORT_LE( major_version ),
111 FT_FRAME_USHORT_LE( minor_version ),
112 FT_FRAME_USHORT_LE( number_of_named_entries ),
113 FT_FRAME_USHORT_LE( number_of_id_entries ),
114 FT_FRAME_END
115 };
116
117 static const FT_Frame_Field winpe_rsrc_dir_entry_fields[] =
118 {
119 #undef FT_STRUCTURE
120 #define FT_STRUCTURE WinPE_RsrcDirEntryRec
121
122 FT_FRAME_START( 8 ),
123 FT_FRAME_ULONG_LE( name ),
124 FT_FRAME_ULONG_LE( offset ),
125 FT_FRAME_END
126 };
127
128 static const FT_Frame_Field winpe_rsrc_data_entry_fields[] =
129 {
130 #undef FT_STRUCTURE
131 #define FT_STRUCTURE WinPE_RsrcDataEntryRec
132
133 FT_FRAME_START( 16 ),
134 FT_FRAME_ULONG_LE( offset_to_data ),
135 FT_FRAME_ULONG_LE( size ),
136 FT_FRAME_ULONG_LE( code_page ),
137 FT_FRAME_ULONG_LE( reserved ),
138 FT_FRAME_END
139 };
140
141 static const FT_Frame_Field winfnt_header_fields[] =
142 {
143 #undef FT_STRUCTURE
144 #define FT_STRUCTURE FT_WinFNT_HeaderRec
145
146 FT_FRAME_START( 148 ),
147 FT_FRAME_USHORT_LE( version ),
148 FT_FRAME_ULONG_LE ( file_size ),
149 FT_FRAME_BYTES ( copyright, 60 ),
150 FT_FRAME_USHORT_LE( file_type ),
151 FT_FRAME_USHORT_LE( nominal_point_size ),
152 FT_FRAME_USHORT_LE( vertical_resolution ),
153 FT_FRAME_USHORT_LE( horizontal_resolution ),
154 FT_FRAME_USHORT_LE( ascent ),
155 FT_FRAME_USHORT_LE( internal_leading ),
156 FT_FRAME_USHORT_LE( external_leading ),
157 FT_FRAME_BYTE ( italic ),
158 FT_FRAME_BYTE ( underline ),
159 FT_FRAME_BYTE ( strike_out ),
160 FT_FRAME_USHORT_LE( weight ),
161 FT_FRAME_BYTE ( charset ),
162 FT_FRAME_USHORT_LE( pixel_width ),
163 FT_FRAME_USHORT_LE( pixel_height ),
164 FT_FRAME_BYTE ( pitch_and_family ),
165 FT_FRAME_USHORT_LE( avg_width ),
166 FT_FRAME_USHORT_LE( max_width ),
167 FT_FRAME_BYTE ( first_char ),
168 FT_FRAME_BYTE ( last_char ),
169 FT_FRAME_BYTE ( default_char ),
170 FT_FRAME_BYTE ( break_char ),
171 FT_FRAME_USHORT_LE( bytes_per_row ),
172 FT_FRAME_ULONG_LE ( device_offset ),
173 FT_FRAME_ULONG_LE ( face_name_offset ),
174 FT_FRAME_ULONG_LE ( bits_pointer ),
175 FT_FRAME_ULONG_LE ( bits_offset ),
176 FT_FRAME_BYTE ( reserved ),
177 FT_FRAME_ULONG_LE ( flags ),
178 FT_FRAME_USHORT_LE( A_space ),
179 FT_FRAME_USHORT_LE( B_space ),
180 FT_FRAME_USHORT_LE( C_space ),
181 FT_FRAME_ULONG_LE ( color_table_offset ),
182 FT_FRAME_BYTES ( reserved1, 16 ),
183 FT_FRAME_END
184 };
185
186
187 static void
188 fnt_font_done( FNT_Face face )
189 {
190 FT_Memory memory = FT_FACE( face )->memory;
191 FT_Stream stream = FT_FACE( face )->stream;
192 FNT_Font font = face->font;
193
194
195 if ( !font )
196 return;
197
198 if ( font->fnt_frame )
199 FT_FRAME_RELEASE( font->fnt_frame );
200 FT_FREE( font->family_name );
201
202 FT_FREE( font );
203 face->font = 0;
204 }
205
206
207 static FT_Error
208 fnt_font_load( FNT_Font font,
209 FT_Stream stream )
210 {
211 FT_Error error;
212 FT_WinFNT_Header header = &font->header;
213 FT_Bool new_format;
214 FT_UInt size;
215
216
217 /* first of all, read the FNT header */
218 if ( FT_STREAM_SEEK( font->offset ) ||
219 FT_STREAM_READ_FIELDS( winfnt_header_fields, header ) )
220 goto Exit;
221
222 /* check header */
223 if ( header->version != 0x200 &&
224 header->version != 0x300 )
225 {
226 FT_TRACE2(( "[not a valid FNT file]\n" ));
227 error = FNT_Err_Unknown_File_Format;
228 goto Exit;
229 }
230
231 new_format = FT_BOOL( font->header.version == 0x300 );
232 size = new_format ? 148 : 118;
233
234 if ( header->file_size < size )
235 {
236 FT_TRACE2(( "[not a valid FNT file]\n" ));
237 error = FNT_Err_Unknown_File_Format;
238 goto Exit;
239 }
240
241 /* Version 2 doesn't have these fields */
242 if ( header->version == 0x200 )
243 {
244 header->flags = 0;
245 header->A_space = 0;
246 header->B_space = 0;
247 header->C_space = 0;
248
249 header->color_table_offset = 0;
250 }
251
252 if ( header->file_type & 1 )
253 {
254 FT_TRACE2(( "[can't handle vector FNT fonts]\n" ));
255 error = FNT_Err_Unknown_File_Format;
256 goto Exit;
257 }
258
259 /* this is a FNT file/table; extract its frame */
260 if ( FT_STREAM_SEEK( font->offset ) ||
261 FT_FRAME_EXTRACT( header->file_size, font->fnt_frame ) )
262 goto Exit;
263
264 Exit:
265 return error;
266 }
267
268
269 static FT_Error
270 fnt_face_get_dll_font( FNT_Face face,
271 FT_Int face_index )
272 {
273 FT_Error error;
274 FT_Stream stream = FT_FACE( face )->stream;
275 FT_Memory memory = FT_FACE( face )->memory;
276 WinMZ_HeaderRec mz_header;
277
278
279 face->font = 0;
280
281 /* does it begin with an MZ header? */
282 if ( FT_STREAM_SEEK( 0 ) ||
283 FT_STREAM_READ_FIELDS( winmz_header_fields, &mz_header ) )
284 goto Exit;
285
286 error = FNT_Err_Unknown_File_Format;
287 if ( mz_header.magic == WINFNT_MZ_MAGIC )
288 {
289 /* yes, now look for an NE header in the file */
290 WinNE_HeaderRec ne_header;
291
292
293 FT_TRACE2(( "MZ signature found\n" ));
294
295 if ( FT_STREAM_SEEK( mz_header.lfanew ) ||
296 FT_STREAM_READ_FIELDS( winne_header_fields, &ne_header ) )
297 goto Exit;
298
299 error = FNT_Err_Unknown_File_Format;
300 if ( ne_header.magic == WINFNT_NE_MAGIC )
301 {
302 /* good, now look into the resource table for each FNT resource */
303 FT_ULong res_offset = mz_header.lfanew +
304 ne_header.resource_tab_offset;
305 FT_UShort size_shift;
306 FT_UShort font_count = 0;
307 FT_ULong font_offset = 0;
308
309
310 FT_TRACE2(( "NE signature found\n" ));
311
312 if ( FT_STREAM_SEEK( res_offset ) ||
313 FT_FRAME_ENTER( ne_header.rname_tab_offset -
314 ne_header.resource_tab_offset ) )
315 goto Exit;
316
317 size_shift = FT_GET_USHORT_LE();
318
319 for (;;)
320 {
321 FT_UShort type_id, count;
322
323
324 type_id = FT_GET_USHORT_LE();
325 if ( !type_id )
326 break;
327
328 count = FT_GET_USHORT_LE();
329
330 if ( type_id == 0x8008U )
331 {
332 font_count = count;
333 font_offset = (FT_ULong)( FT_STREAM_POS() + 4 +
334 ( stream->cursor - stream->limit ) );
335 break;
336 }
337
338 stream->cursor += 4 + count * 12;
339 }
340
341 FT_FRAME_EXIT();
342
343 if ( !font_count || !font_offset )
344 {
345 FT_TRACE2(( "this file doesn't contain any FNT resources!\n" ));
346 error = FNT_Err_Invalid_File_Format;
347 goto Exit;
348 }
349
350 /* loading `winfnt_header_fields' needs at least 118 bytes; */
351 /* use this as a rough measure to check the expected font size */
352 if ( font_count * 118UL > stream->size )
353 {
354 FT_TRACE2(( "invalid number of faces\n" ));
355 error = FNT_Err_Invalid_File_Format;
356 goto Exit;
357 }
358
359 face->root.num_faces = font_count;
360
361 if ( face_index >= font_count )
362 {
363 error = FNT_Err_Bad_Argument;
364 goto Exit;
365 }
366
367 if ( FT_NEW( face->font ) )
368 goto Exit;
369
370 if ( FT_STREAM_SEEK( font_offset + face_index * 12 ) ||
371 FT_FRAME_ENTER( 12 ) )
372 goto Fail;
373
374 face->font->offset = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
375 face->font->fnt_size = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
376
377 stream->cursor += 8;
378
379 FT_FRAME_EXIT();
380
381 error = fnt_font_load( face->font, stream );
382 }
383 else if ( ne_header.magic == WINFNT_PE_MAGIC )
384 {
385 WinPE32_HeaderRec pe32_header;
386 WinPE32_SectionRec pe32_section;
387 WinPE_RsrcDirRec root_dir, name_dir, lang_dir;
388 WinPE_RsrcDirEntryRec dir_entry1, dir_entry2, dir_entry3;
389 WinPE_RsrcDataEntryRec data_entry;
390
391 FT_Long root_dir_offset, name_dir_offset, lang_dir_offset;
392 FT_UShort i, j, k;
393
394
395 FT_TRACE2(( "PE signature found\n" ));
396
397 if ( FT_STREAM_SEEK( mz_header.lfanew ) ||
398 FT_STREAM_READ_FIELDS( winpe32_header_fields, &pe32_header ) )
399 goto Exit;
400
401 FT_TRACE2(( "magic %04lx, machine %02x, number_of_sections %u, "
402 "size_of_optional_header %02x\n"
403 "magic32 %02x, rsrc_virtual_address %04lx, "
404 "rsrc_size %04lx\n",
405 pe32_header.magic, pe32_header.machine,
406 pe32_header.number_of_sections,
407 pe32_header.size_of_optional_header,
408 pe32_header.magic32, pe32_header.rsrc_virtual_address,
409 pe32_header.rsrc_size ));
410
411 if ( pe32_header.magic != WINFNT_PE_MAGIC /* check full signature */ ||
412 pe32_header.machine != 0x014c /* i386 */ ||
413 pe32_header.size_of_optional_header != 0xe0 /* FIXME */ ||
414 pe32_header.magic32 != 0x10b )
415 {
416 FT_TRACE2(( "this file has an invalid PE header\n" ));
417 error = FNT_Err_Invalid_File_Format;
418 goto Exit;
419 }
420
421 face->root.num_faces = 0;
422
423 for ( i = 0; i < pe32_header.number_of_sections; i++ )
424 {
425 if ( FT_STREAM_READ_FIELDS( winpe32_section_fields,
426 &pe32_section ) )
427 goto Exit;
428
429 FT_TRACE2(( "name %.8s, va %04lx, size %04lx, offset %04lx\n",
430 pe32_section.name, pe32_section.virtual_address,
431 pe32_section.size_of_raw_data,
432 pe32_section.pointer_to_raw_data ));
433
434 if ( pe32_header.rsrc_virtual_address ==
435 pe32_section.virtual_address )
436 goto Found_rsrc_section;
437 }
438
439 FT_TRACE2(( "this file doesn't contain any resources\n" ));
440 error = FNT_Err_Invalid_File_Format;
441 goto Exit;
442
443 Found_rsrc_section:
444 FT_TRACE2(( "found resources section %.8s\n", pe32_section.name ));
445
446 if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data ) ||
447 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &root_dir ) )
448 goto Exit;
449
450 root_dir_offset = pe32_section.pointer_to_raw_data;
451
452 for ( i = 0; i < root_dir.number_of_named_entries +
453 root_dir.number_of_id_entries; i++ )
454 {
455 if ( FT_STREAM_SEEK( root_dir_offset + 16 + i * 8 ) ||
456 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
457 &dir_entry1 ) )
458 goto Exit;
459
460 if ( !(dir_entry1.offset & 0x80000000UL ) /* DataIsDirectory */ )
461 {
462 error = FNT_Err_Invalid_File_Format;
463 goto Exit;
464 }
465
466 dir_entry1.offset &= ~0x80000000UL;
467
468 name_dir_offset = pe32_section.pointer_to_raw_data +
469 dir_entry1.offset;
470
471 if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data +
472 dir_entry1.offset ) ||
473 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &name_dir ) )
474 goto Exit;
475
476 for ( j = 0; j < name_dir.number_of_named_entries +
477 name_dir.number_of_id_entries; j++ )
478 {
479 if ( FT_STREAM_SEEK( name_dir_offset + 16 + j * 8 ) ||
480 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
481 &dir_entry2 ) )
482 goto Exit;
483
484 if ( !(dir_entry2.offset & 0x80000000UL ) /* DataIsDirectory */ )
485 {
486 error = FNT_Err_Invalid_File_Format;
487 goto Exit;
488 }
489
490 dir_entry2.offset &= ~0x80000000UL;
491
492 lang_dir_offset = pe32_section.pointer_to_raw_data +
493 dir_entry2.offset;
494
495 if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data +
496 dir_entry2.offset ) ||
497 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &lang_dir ) )
498 goto Exit;
499
500 for ( k = 0; k < lang_dir.number_of_named_entries +
501 lang_dir.number_of_id_entries; k++ )
502 {
503 if ( FT_STREAM_SEEK( lang_dir_offset + 16 + k * 8 ) ||
504 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
505 &dir_entry3 ) )
506 goto Exit;
507
508 if ( dir_entry2.offset & 0x80000000UL /* DataIsDirectory */ )
509 {
510 error = FNT_Err_Invalid_File_Format;
511 goto Exit;
512 }
513
514 if ( dir_entry1.name == 8 /* RT_FONT */ )
515 {
516 if ( FT_STREAM_SEEK( root_dir_offset + dir_entry3.offset ) ||
517 FT_STREAM_READ_FIELDS( winpe_rsrc_data_entry_fields,
518 &data_entry ) )
519 goto Exit;
520
521 FT_TRACE2(( "found font #%lu, offset %04lx, "
522 "size %04lx, cp %lu\n",
523 dir_entry2.name,
524 pe32_section.pointer_to_raw_data +
525 data_entry.offset_to_data -
526 pe32_section.virtual_address,
527 data_entry.size, data_entry.code_page ));
528
529 if ( face_index == face->root.num_faces )
530 {
531 if ( FT_NEW( face->font ) )
532 goto Exit;
533
534 face->font->offset = pe32_section.pointer_to_raw_data +
535 data_entry.offset_to_data -
536 pe32_section.virtual_address;
537 face->font->fnt_size = data_entry.size;
538
539 error = fnt_font_load( face->font, stream );
540 if ( error )
541 {
542 FT_TRACE2(( "font #%lu load error %d\n",
543 dir_entry2.name, error ));
544 goto Fail;
545 }
546 else
547 FT_TRACE2(( "font #%lu successfully loaded\n",
548 dir_entry2.name ));
549 }
550
551 face->root.num_faces++;
552 }
553 }
554 }
555 }
556 }
557
558 if ( !face->root.num_faces )
559 {
560 FT_TRACE2(( "this file doesn't contain any RT_FONT resources\n" ));
561 error = FNT_Err_Invalid_File_Format;
562 goto Exit;
563 }
564
565 if ( face_index >= face->root.num_faces )
566 {
567 error = FNT_Err_Bad_Argument;
568 goto Exit;
569 }
570 }
571
572 Fail:
573 if ( error )
574 fnt_font_done( face );
575
576 Exit:
577 return error;
578 }
579
580
581 typedef struct FNT_CMapRec_
582 {
583 FT_CMapRec cmap;
584 FT_UInt32 first;
585 FT_UInt32 count;
586
587 } FNT_CMapRec, *FNT_CMap;
588
589
590 static FT_Error
591 fnt_cmap_init( FNT_CMap cmap )
592 {
593 FNT_Face face = (FNT_Face)FT_CMAP_FACE( cmap );
594 FNT_Font font = face->font;
595
596
597 cmap->first = (FT_UInt32) font->header.first_char;
598 cmap->count = (FT_UInt32)( font->header.last_char - cmap->first + 1 );
599
600 return 0;
601 }
602
603
604 static FT_UInt
605 fnt_cmap_char_index( FNT_CMap cmap,
606 FT_UInt32 char_code )
607 {
608 FT_UInt gindex = 0;
609
610
611 char_code -= cmap->first;
612 if ( char_code < cmap->count )
613 gindex = char_code + 1; /* we artificially increase the glyph index; */
614 /* FNT_Load_Glyph reverts to the right one */
615 return gindex;
616 }
617
618
619 static FT_UInt
620 fnt_cmap_char_next( FNT_CMap cmap,
621 FT_UInt32 *pchar_code )
622 {
623 FT_UInt gindex = 0;
624 FT_UInt32 result = 0;
625 FT_UInt32 char_code = *pchar_code + 1;
626
627
628 if ( char_code <= cmap->first )
629 {
630 result = cmap->first;
631 gindex = 1;
632 }
633 else
634 {
635 char_code -= cmap->first;
636 if ( char_code < cmap->count )
637 {
638 result = cmap->first + char_code;
639 gindex = char_code + 1;
640 }
641 }
642
643 *pchar_code = result;
644 return gindex;
645 }
646
647
648 static const FT_CMap_ClassRec fnt_cmap_class_rec =
649 {
650 sizeof ( FNT_CMapRec ),
651
652 (FT_CMap_InitFunc) fnt_cmap_init,
653 (FT_CMap_DoneFunc) NULL,
654 (FT_CMap_CharIndexFunc)fnt_cmap_char_index,
655 (FT_CMap_CharNextFunc) fnt_cmap_char_next
656 };
657
658 static FT_CMap_Class const fnt_cmap_class = &fnt_cmap_class_rec;
659
660
661 static void
662 FNT_Face_Done( FNT_Face face )
663 {
664 if ( face )
665 {
666 FT_Memory memory = FT_FACE_MEMORY( face );
667
668
669 fnt_font_done( face );
670
671 FT_FREE( face->root.available_sizes );
672 face->root.num_fixed_sizes = 0;
673 }
674 }
675
676
677 static FT_Error
678 FNT_Face_Init( FT_Stream stream,
679 FNT_Face face,
680 FT_Int face_index,
681 FT_Int num_params,
682 FT_Parameter* params )
683 {
684 FT_Error error;
685 FT_Memory memory = FT_FACE_MEMORY( face );
686
687 FT_UNUSED( num_params );
688 FT_UNUSED( params );
689
690
691 /* try to load font from a DLL */
692 error = fnt_face_get_dll_font( face, face_index );
693 if ( error == FNT_Err_Unknown_File_Format )
694 {
695 /* this didn't work; try to load a single FNT font */
696 FNT_Font font;
697
698
699 if ( face_index > 0 )
700 {
701 error = FNT_Err_Bad_Argument;
702 goto Exit;
703 }
704
705 if ( FT_NEW( face->font ) )
706 goto Exit;
707
708 face->root.num_faces = 1;
709
710 font = face->font;
711 font->offset = 0;
712 font->fnt_size = stream->size;
713
714 error = fnt_font_load( font, stream );
715 }
716
717 if ( error )
718 goto Fail;
719
720 /* we now need to fill the root FT_Face fields */
721 /* with relevant information */
722 {
723 FT_Face root = FT_FACE( face );
724 FNT_Font font = face->font;
725 FT_PtrDist family_size;
726
727
728 root->face_flags = FT_FACE_FLAG_FIXED_SIZES |
729 FT_FACE_FLAG_HORIZONTAL;
730
731 if ( font->header.avg_width == font->header.max_width )
732 root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
733
734 if ( font->header.italic )
735 root->style_flags |= FT_STYLE_FLAG_ITALIC;
736
737 if ( font->header.weight >= 800 )
738 root->style_flags |= FT_STYLE_FLAG_BOLD;
739
740 /* set up the `fixed_sizes' array */
741 if ( FT_NEW_ARRAY( root->available_sizes, 1 ) )
742 goto Fail;
743
744 root->num_fixed_sizes = 1;
745
746 {
747 FT_Bitmap_Size* bsize = root->available_sizes;
748 FT_UShort x_res, y_res;
749
750
751 bsize->width = font->header.avg_width;
752 bsize->height = (FT_Short)(
753 font->header.pixel_height + font->header.external_leading );
754 bsize->size = font->header.nominal_point_size << 6;
755
756 x_res = font->header.horizontal_resolution;
757 if ( !x_res )
758 x_res = 72;
759
760 y_res = font->header.vertical_resolution;
761 if ( !y_res )
762 y_res = 72;
763
764 bsize->y_ppem = FT_MulDiv( bsize->size, y_res, 72 );
765 bsize->y_ppem = FT_PIX_ROUND( bsize->y_ppem );
766
767 /*
768 * this reads:
769 *
770 * the nominal height is larger than the bbox's height
771 *
772 * => nominal_point_size contains incorrect value;
773 * use pixel_height as the nominal height
774 */
775 if ( bsize->y_ppem > font->header.pixel_height << 6 )
776 {
777 FT_TRACE2(( "use pixel_height as the nominal height\n" ));
778
779 bsize->y_ppem = font->header.pixel_height << 6;
780 bsize->size = FT_MulDiv( bsize->y_ppem, 72, y_res );
781 }
782
783 bsize->x_ppem = FT_MulDiv( bsize->size, x_res, 72 );
784 bsize->x_ppem = FT_PIX_ROUND( bsize->x_ppem );
785 }
786
787 {
788 FT_CharMapRec charmap;
789
790
791 charmap.encoding = FT_ENCODING_NONE;
792 charmap.platform_id = 0;
793 charmap.encoding_id = 0;
794 charmap.face = root;
795
796 if ( font->header.charset == FT_WinFNT_ID_MAC )
797 {
798 charmap.encoding = FT_ENCODING_APPLE_ROMAN;
799 charmap.platform_id = 1;
800 /* charmap.encoding_id = 0; */
801 }
802
803 error = FT_CMap_New( fnt_cmap_class,
804 NULL,
805 &charmap,
806 NULL );
807 if ( error )
808 goto Fail;
809
810 /* Select default charmap */
811 if ( root->num_charmaps )
812 root->charmap = root->charmaps[0];
813 }
814
815 /* setup remaining flags */
816
817 /* reserve one slot for the .notdef glyph at index 0 */
818 root->num_glyphs = font->header.last_char -
819 font->header.first_char + 1 + 1;
820
821 if ( font->header.face_name_offset >= font->header.file_size )
822 {
823 FT_TRACE2(( "invalid family name offset!\n" ));
824 error = FNT_Err_Invalid_File_Format;
825 goto Fail;
826 }
827 family_size = font->header.file_size - font->header.face_name_offset;
828 /* Some broken fonts don't delimit the face name with a final */
829 /* NULL byte -- the frame is erroneously one byte too small. */
830 /* We thus allocate one more byte, setting it explicitly to */
831 /* zero. */
832 if ( FT_ALLOC( font->family_name, family_size + 1 ) )
833 goto Fail;
834
835 FT_MEM_COPY( font->family_name,
836 font->fnt_frame + font->header.face_name_offset,
837 family_size );
838
839 font->family_name[family_size] = '\0';
840
841 if ( FT_REALLOC( font->family_name,
842 family_size,
843 ft_strlen( font->family_name ) + 1 ) )
844 goto Fail;
845
846 root->family_name = font->family_name;
847 root->style_name = (char *)"Regular";
848
849 if ( root->style_flags & FT_STYLE_FLAG_BOLD )
850 {
851 if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
852 root->style_name = (char *)"Bold Italic";
853 else
854 root->style_name = (char *)"Bold";
855 }
856 else if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
857 root->style_name = (char *)"Italic";
858 }
859 goto Exit;
860
861 Fail:
862 FNT_Face_Done( face );
863
864 Exit:
865 return error;
866 }
867
868
869 static FT_Error
870 FNT_Size_Select( FT_Size size )
871 {
872 FNT_Face face = (FNT_Face)size->face;
873 FT_WinFNT_Header header = &face->font->header;
874
875
876 FT_Select_Metrics( size->face, 0 );
877
878 size->metrics.ascender = header->ascent * 64;
879 size->metrics.descender = -( header->pixel_height -
880 header->ascent ) * 64;
881 size->metrics.max_advance = header->max_width * 64;
882
883 return FNT_Err_Ok;
884 }
885
886
887 static FT_Error
888 FNT_Size_Request( FT_Size size,
889 FT_Size_Request req )
890 {
891 FNT_Face face = (FNT_Face)size->face;
892 FT_WinFNT_Header header = &face->font->header;
893 FT_Bitmap_Size* bsize = size->face->available_sizes;
894 FT_Error error = FNT_Err_Invalid_Pixel_Size;
895 FT_Long height;
896
897
898 height = FT_REQUEST_HEIGHT( req );
899 height = ( height + 32 ) >> 6;
900
901 switch ( req->type )
902 {
903 case FT_SIZE_REQUEST_TYPE_NOMINAL:
904 if ( height == ( bsize->y_ppem + 32 ) >> 6 )
905 error = FNT_Err_Ok;
906 break;
907
908 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
909 if ( height == header->pixel_height )
910 error = FNT_Err_Ok;
911 break;
912
913 default:
914 error = FNT_Err_Unimplemented_Feature;
915 break;
916 }
917
918 if ( error )
919 return error;
920 else
921 return FNT_Size_Select( size );
922 }
923
924
925 static FT_Error
926 FNT_Load_Glyph( FT_GlyphSlot slot,
927 FT_Size size,
928 FT_UInt glyph_index,
929 FT_Int32 load_flags )
930 {
931 FNT_Face face = (FNT_Face)FT_SIZE_FACE( size );
932 FNT_Font font = face->font;
933 FT_Error error = FNT_Err_Ok;
934 FT_Byte* p;
935 FT_Int len;
936 FT_Bitmap* bitmap = &slot->bitmap;
937 FT_ULong offset;
938 FT_Bool new_format;
939
940 FT_UNUSED( load_flags );
941
942
943 if ( !face || !font ||
944 glyph_index >= (FT_UInt)( FT_FACE( face )->num_glyphs ) )
945 {
946 error = FNT_Err_Invalid_Argument;
947 goto Exit;
948 }
949
950 if ( glyph_index > 0 )
951 glyph_index--; /* revert to real index */
952 else
953 glyph_index = font->header.default_char; /* the .notdef glyph */
954
955 new_format = FT_BOOL( font->header.version == 0x300 );
956 len = new_format ? 6 : 4;
957
958 /* jump to glyph entry */
959 p = font->fnt_frame + ( new_format ? 148 : 118 ) + len * glyph_index;
960
961 bitmap->width = FT_NEXT_SHORT_LE( p );
962
963 if ( new_format )
964 offset = FT_NEXT_ULONG_LE( p );
965 else
966 offset = FT_NEXT_USHORT_LE( p );
967
968 if ( offset >= font->header.file_size )
969 {
970 FT_TRACE2(( "invalid FNT offset!\n" ));
971 error = FNT_Err_Invalid_File_Format;
972 goto Exit;
973 }
974
975 /* jump to glyph data */
976 p = font->fnt_frame + /* font->header.bits_offset */ + offset;
977
978 /* allocate and build bitmap */
979 {
980 FT_Memory memory = FT_FACE_MEMORY( slot->face );
981 FT_Int pitch = ( bitmap->width + 7 ) >> 3;
982 FT_Byte* column;
983 FT_Byte* write;
984
985
986 bitmap->pitch = pitch;
987 bitmap->rows = font->header.pixel_height;
988 bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
989
990 if ( offset + pitch * bitmap->rows >= font->header.file_size )
991 {
992 FT_TRACE2(( "invalid bitmap width\n" ));
993 error = FNT_Err_Invalid_File_Format;
994 goto Exit;
995 }
996
997 /* note: since glyphs are stored in columns and not in rows we */
998 /* can't use ft_glyphslot_set_bitmap */
999 if ( FT_ALLOC_MULT( bitmap->buffer, pitch, bitmap->rows ) )
1000 goto Exit;
1001
1002 column = (FT_Byte*)bitmap->buffer;
1003
1004 for ( ; pitch > 0; pitch--, column++ )
1005 {
1006 FT_Byte* limit = p + bitmap->rows;
1007
1008
1009 for ( write = column; p < limit; p++, write += bitmap->pitch )
1010 *write = *p;
1011 }
1012 }
1013
1014 slot->internal->flags = FT_GLYPH_OWN_BITMAP;
1015 slot->bitmap_left = 0;
1016 slot->bitmap_top = font->header.ascent;
1017 slot->format = FT_GLYPH_FORMAT_BITMAP;
1018
1019 /* now set up metrics */
1020 slot->metrics.width = bitmap->width << 6;
1021 slot->metrics.height = bitmap->rows << 6;
1022 slot->metrics.horiAdvance = bitmap->width << 6;
1023 slot->metrics.horiBearingX = 0;
1024 slot->metrics.horiBearingY = slot->bitmap_top << 6;
1025
1026 ft_synthesize_vertical_metrics( &slot->metrics,
1027 bitmap->rows << 6 );
1028
1029 Exit:
1030 return error;
1031 }
1032
1033
1034 static FT_Error
1035 winfnt_get_header( FT_Face face,
1036 FT_WinFNT_HeaderRec *aheader )
1037 {
1038 FNT_Font font = ((FNT_Face)face)->font;
1039
1040
1041 *aheader = font->header;
1042
1043 return 0;
1044 }
1045
1046
1047 static const FT_Service_WinFntRec winfnt_service_rec =
1048 {
1049 winfnt_get_header
1050 };
1051
1052 /*
1053 * SERVICE LIST
1054 *
1055 */
1056
1057 static const FT_ServiceDescRec winfnt_services[] =
1058 {
1059 { FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_WINFNT },
1060 { FT_SERVICE_ID_WINFNT, &winfnt_service_rec },
1061 { NULL, NULL }
1062 };
1063
1064
1065 static FT_Module_Interface
1066 winfnt_get_service( FT_Driver driver,
1067 const FT_String* service_id )
1068 {
1069 FT_UNUSED( driver );
1070
1071 return ft_service_list_lookup( winfnt_services, service_id );
1072 }
1073
1074
1075
1076
1077 FT_CALLBACK_TABLE_DEF
1078 const FT_Driver_ClassRec winfnt_driver_class =
1079 {
1080 {
1081 FT_MODULE_FONT_DRIVER |
1082 FT_MODULE_DRIVER_NO_OUTLINES,
1083 sizeof ( FT_DriverRec ),
1084
1085 "winfonts",
1086 0x10000L,
1087 0x20000L,
1088
1089 0,
1090
1091 (FT_Module_Constructor)0,
1092 (FT_Module_Destructor) 0,
1093 (FT_Module_Requester) winfnt_get_service
1094 },
1095
1096 sizeof( FNT_FaceRec ),
1097 sizeof( FT_SizeRec ),
1098 sizeof( FT_GlyphSlotRec ),
1099
1100 (FT_Face_InitFunc) FNT_Face_Init,
1101 (FT_Face_DoneFunc) FNT_Face_Done,
1102 (FT_Size_InitFunc) 0,
1103 (FT_Size_DoneFunc) 0,
1104 (FT_Slot_InitFunc) 0,
1105 (FT_Slot_DoneFunc) 0,
1106
1107 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
1108 ft_stub_set_char_sizes,
1109 ft_stub_set_pixel_sizes,
1110 #endif
1111 (FT_Slot_LoadFunc) FNT_Load_Glyph,
1112
1113 (FT_Face_GetKerningFunc) 0,
1114 (FT_Face_AttachFunc) 0,
1115 (FT_Face_GetAdvancesFunc) 0,
1116
1117 (FT_Size_RequestFunc) FNT_Size_Request,
1118 (FT_Size_SelectFunc) FNT_Size_Select
1119 };
1120
1121
1122 /* END */