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