• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  *
3  * winfnt.c
4  *
5  *   FreeType font driver for Windows FNT/FON files
6  *
7  * Copyright 1996-2018 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_FONT_FORMAT_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
fnt_font_done(FNT_Face face)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 = NULL;
205   }
206 
207 
208   static FT_Error
fnt_font_load(FNT_Font font,FT_Stream stream)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
fnt_face_get_dll_font(FNT_Face face,FT_Int face_instance_index)271   fnt_face_get_dll_font( FNT_Face  face,
272                          FT_Int    face_instance_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     FT_Long          face_index;
279 
280 
281     face->font = NULL;
282 
283     face_index = FT_ABS( face_instance_index ) & 0xFFFF;
284 
285     /* does it begin with an MZ header? */
286     if ( FT_STREAM_SEEK( 0 )                                      ||
287          FT_STREAM_READ_FIELDS( winmz_header_fields, &mz_header ) )
288       goto Exit;
289 
290     error = FT_ERR( Unknown_File_Format );
291     if ( mz_header.magic == WINFNT_MZ_MAGIC )
292     {
293       /* yes, now look for an NE header in the file */
294       WinNE_HeaderRec  ne_header;
295 
296 
297       FT_TRACE2(( "MZ signature found\n" ));
298 
299       if ( FT_STREAM_SEEK( mz_header.lfanew )                       ||
300            FT_STREAM_READ_FIELDS( winne_header_fields, &ne_header ) )
301         goto Exit;
302 
303       error = FT_ERR( Unknown_File_Format );
304       if ( ne_header.magic == WINFNT_NE_MAGIC )
305       {
306         /* good, now look into the resource table for each FNT resource */
307         FT_ULong   res_offset  = mz_header.lfanew +
308                                    ne_header.resource_tab_offset;
309         FT_UShort  size_shift;
310         FT_UShort  font_count  = 0;
311         FT_ULong   font_offset = 0;
312 
313 
314         FT_TRACE2(( "NE signature found\n" ));
315 
316         if ( FT_STREAM_SEEK( res_offset )                    ||
317              FT_FRAME_ENTER( ne_header.rname_tab_offset -
318                              ne_header.resource_tab_offset ) )
319           goto Exit;
320 
321         size_shift = FT_GET_USHORT_LE();
322 
323         /* Microsoft's specification of the executable-file header format */
324         /* for `New Executable' (NE) doesn't give a limit for the         */
325         /* alignment shift count; however, in 1985, the year of the       */
326         /* specification release, only 32bit values were supported, thus  */
327         /* anything larger than 16 doesn't make sense in general, given   */
328         /* that file offsets are 16bit values, shifted by the alignment   */
329         /* shift count                                                    */
330         if ( size_shift > 16 )
331         {
332           FT_TRACE2(( "invalid alignment shift count for resource data\n" ));
333           error = FT_THROW( Invalid_File_Format );
334           goto Exit;
335         }
336 
337 
338         for (;;)
339         {
340           FT_UShort  type_id, count;
341 
342 
343           type_id = FT_GET_USHORT_LE();
344           if ( !type_id )
345             break;
346 
347           count = FT_GET_USHORT_LE();
348 
349           if ( type_id == 0x8008U )
350           {
351             font_count  = count;
352             font_offset = FT_STREAM_POS() + 4 +
353                           (FT_ULong)( stream->cursor - stream->limit );
354             break;
355           }
356 
357           stream->cursor += 4 + count * 12;
358         }
359 
360         FT_FRAME_EXIT();
361 
362         if ( !font_count || !font_offset )
363         {
364           FT_TRACE2(( "this file doesn't contain any FNT resources\n" ));
365           error = FT_THROW( Invalid_File_Format );
366           goto Exit;
367         }
368 
369         /* loading `winfnt_header_fields' needs at least 118 bytes;    */
370         /* use this as a rough measure to check the expected font size */
371         if ( font_count * 118UL > stream->size )
372         {
373           FT_TRACE2(( "invalid number of faces\n" ));
374           error = FT_THROW( Invalid_File_Format );
375           goto Exit;
376         }
377 
378         face->root.num_faces = font_count;
379 
380         if ( face_instance_index < 0 )
381           goto Exit;
382 
383         if ( face_index >= font_count )
384         {
385           error = FT_THROW( Invalid_Argument );
386           goto Exit;
387         }
388 
389         if ( FT_NEW( face->font ) )
390           goto Exit;
391 
392         if ( FT_STREAM_SEEK( font_offset + (FT_ULong)face_index * 12 ) ||
393              FT_FRAME_ENTER( 12 )                                      )
394           goto Fail;
395 
396         face->font->offset   = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
397         face->font->fnt_size = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
398 
399         stream->cursor += 8;
400 
401         FT_FRAME_EXIT();
402 
403         error = fnt_font_load( face->font, stream );
404       }
405       else if ( ne_header.magic == WINFNT_PE_MAGIC )
406       {
407         WinPE32_HeaderRec       pe32_header;
408         WinPE32_SectionRec      pe32_section;
409         WinPE_RsrcDirRec        root_dir, name_dir, lang_dir;
410         WinPE_RsrcDirEntryRec   dir_entry1, dir_entry2, dir_entry3;
411         WinPE_RsrcDataEntryRec  data_entry;
412 
413         FT_ULong   root_dir_offset, name_dir_offset, lang_dir_offset;
414         FT_UShort  i, j, k;
415 
416 
417         FT_TRACE2(( "PE signature found\n" ));
418 
419         if ( FT_STREAM_SEEK( mz_header.lfanew )                           ||
420              FT_STREAM_READ_FIELDS( winpe32_header_fields, &pe32_header ) )
421           goto Exit;
422 
423         FT_TRACE2(( "magic %04lx, machine %02x, number_of_sections %u, "
424                     "size_of_optional_header %02x\n"
425                     "magic32 %02x, rsrc_virtual_address %04lx, "
426                     "rsrc_size %04lx\n",
427                     pe32_header.magic, pe32_header.machine,
428                     pe32_header.number_of_sections,
429                     pe32_header.size_of_optional_header,
430                     pe32_header.magic32, pe32_header.rsrc_virtual_address,
431                     pe32_header.rsrc_size ));
432 
433         if ( pe32_header.magic != WINFNT_PE_MAGIC /* check full signature */ ||
434              pe32_header.machine != 0x014C /* i386 */                        ||
435              pe32_header.size_of_optional_header != 0xE0 /* FIXME */         ||
436              pe32_header.magic32 != 0x10B                                    )
437         {
438           FT_TRACE2(( "this file has an invalid PE header\n" ));
439           error = FT_THROW( Invalid_File_Format );
440           goto Exit;
441         }
442 
443         face->root.num_faces = 0;
444 
445         for ( i = 0; i < pe32_header.number_of_sections; i++ )
446         {
447           if ( FT_STREAM_READ_FIELDS( winpe32_section_fields,
448                                       &pe32_section ) )
449             goto Exit;
450 
451           FT_TRACE2(( "name %.8s, va %04lx, size %04lx, offset %04lx\n",
452                       pe32_section.name, pe32_section.virtual_address,
453                       pe32_section.size_of_raw_data,
454                       pe32_section.pointer_to_raw_data ));
455 
456           if ( pe32_header.rsrc_virtual_address ==
457                  pe32_section.virtual_address )
458             goto Found_rsrc_section;
459         }
460 
461         FT_TRACE2(( "this file doesn't contain any resources\n" ));
462         error = FT_THROW( Invalid_File_Format );
463         goto Exit;
464 
465       Found_rsrc_section:
466         FT_TRACE2(( "found resources section %.8s\n", pe32_section.name ));
467 
468         if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data )        ||
469              FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &root_dir ) )
470           goto Exit;
471 
472         root_dir_offset = pe32_section.pointer_to_raw_data;
473 
474         for ( i = 0; i < root_dir.number_of_named_entries +
475                            root_dir.number_of_id_entries; i++ )
476         {
477           if ( FT_STREAM_SEEK( root_dir_offset + 16 + i * 8 )      ||
478                FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
479                                       &dir_entry1 )                )
480             goto Exit;
481 
482           if ( !(dir_entry1.offset & 0x80000000UL ) /* DataIsDirectory */ )
483           {
484             error = FT_THROW( Invalid_File_Format );
485             goto Exit;
486           }
487 
488           dir_entry1.offset &= ~0x80000000UL;
489 
490           name_dir_offset = pe32_section.pointer_to_raw_data +
491                             dir_entry1.offset;
492 
493           if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data +
494                                dir_entry1.offset )                       ||
495                FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &name_dir ) )
496             goto Exit;
497 
498           for ( j = 0; j < name_dir.number_of_named_entries +
499                              name_dir.number_of_id_entries; j++ )
500           {
501             if ( FT_STREAM_SEEK( name_dir_offset + 16 + j * 8 )      ||
502                  FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
503                                         &dir_entry2 )                )
504               goto Exit;
505 
506             if ( !(dir_entry2.offset & 0x80000000UL ) /* DataIsDirectory */ )
507             {
508               error = FT_THROW( Invalid_File_Format );
509               goto Exit;
510             }
511 
512             dir_entry2.offset &= ~0x80000000UL;
513 
514             lang_dir_offset = pe32_section.pointer_to_raw_data +
515                                 dir_entry2.offset;
516 
517             if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data +
518                                    dir_entry2.offset )                     ||
519                  FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &lang_dir ) )
520               goto Exit;
521 
522             for ( k = 0; k < lang_dir.number_of_named_entries +
523                                lang_dir.number_of_id_entries; k++ )
524             {
525               if ( FT_STREAM_SEEK( lang_dir_offset + 16 + k * 8 )      ||
526                    FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
527                                           &dir_entry3 )                )
528                 goto Exit;
529 
530               if ( dir_entry2.offset & 0x80000000UL /* DataIsDirectory */ )
531               {
532                 error = FT_THROW( Invalid_File_Format );
533                 goto Exit;
534               }
535 
536               if ( dir_entry1.name == 8 /* RT_FONT */ )
537               {
538                 if ( FT_STREAM_SEEK( root_dir_offset + dir_entry3.offset ) ||
539                      FT_STREAM_READ_FIELDS( winpe_rsrc_data_entry_fields,
540                                             &data_entry )                  )
541                   goto Exit;
542 
543                 FT_TRACE2(( "found font #%lu, offset %04lx, "
544                             "size %04lx, cp %lu\n",
545                             dir_entry2.name,
546                             pe32_section.pointer_to_raw_data +
547                               data_entry.offset_to_data -
548                               pe32_section.virtual_address,
549                             data_entry.size, data_entry.code_page ));
550 
551                 if ( face_index == face->root.num_faces )
552                 {
553                   if ( FT_NEW( face->font ) )
554                     goto Exit;
555 
556                   face->font->offset   = pe32_section.pointer_to_raw_data +
557                                            data_entry.offset_to_data -
558                                            pe32_section.virtual_address;
559                   face->font->fnt_size = data_entry.size;
560 
561                   error = fnt_font_load( face->font, stream );
562                   if ( error )
563                   {
564                     FT_TRACE2(( "font #%lu load error 0x%x\n",
565                                 dir_entry2.name, error ));
566                     goto Fail;
567                   }
568                   else
569                     FT_TRACE2(( "font #%lu successfully loaded\n",
570                                 dir_entry2.name ));
571                 }
572 
573                 face->root.num_faces++;
574               }
575             }
576           }
577         }
578       }
579 
580       if ( !face->root.num_faces )
581       {
582         FT_TRACE2(( "this file doesn't contain any RT_FONT resources\n" ));
583         error = FT_THROW( Invalid_File_Format );
584         goto Exit;
585       }
586 
587       if ( face_index >= face->root.num_faces )
588       {
589         error = FT_THROW( Invalid_Argument );
590         goto Exit;
591       }
592     }
593 
594   Fail:
595     if ( error )
596       fnt_font_done( face );
597 
598   Exit:
599     return error;
600   }
601 
602 
603   typedef struct  FNT_CMapRec_
604   {
605     FT_CMapRec  cmap;
606     FT_UInt32   first;
607     FT_UInt32   count;
608 
609   } FNT_CMapRec, *FNT_CMap;
610 
611 
612   static FT_Error
fnt_cmap_init(FNT_CMap cmap,FT_Pointer pointer)613   fnt_cmap_init( FNT_CMap    cmap,
614                  FT_Pointer  pointer )
615   {
616     FNT_Face  face = (FNT_Face)FT_CMAP_FACE( cmap );
617     FNT_Font  font = face->font;
618 
619     FT_UNUSED( pointer );
620 
621 
622     cmap->first = (FT_UInt32)  font->header.first_char;
623     cmap->count = (FT_UInt32)( font->header.last_char - cmap->first + 1 );
624 
625     return 0;
626   }
627 
628 
629   static FT_UInt
fnt_cmap_char_index(FNT_CMap cmap,FT_UInt32 char_code)630   fnt_cmap_char_index( FNT_CMap   cmap,
631                        FT_UInt32  char_code )
632   {
633     FT_UInt  gindex = 0;
634 
635 
636     char_code -= cmap->first;
637     if ( char_code < cmap->count )
638       /* we artificially increase the glyph index; */
639       /* FNT_Load_Glyph reverts to the right one   */
640       gindex = (FT_UInt)( char_code + 1 );
641     return gindex;
642   }
643 
644 
645   static FT_UInt32
fnt_cmap_char_next(FNT_CMap cmap,FT_UInt32 * pchar_code)646   fnt_cmap_char_next( FNT_CMap    cmap,
647                       FT_UInt32  *pchar_code )
648   {
649     FT_UInt    gindex = 0;
650     FT_UInt32  result = 0;
651     FT_UInt32  char_code = *pchar_code + 1;
652 
653 
654     if ( char_code <= cmap->first )
655     {
656       result = cmap->first;
657       gindex = 1;
658     }
659     else
660     {
661       char_code -= cmap->first;
662       if ( char_code < cmap->count )
663       {
664         result = cmap->first + char_code;
665         gindex = (FT_UInt)( char_code + 1 );
666       }
667     }
668 
669     *pchar_code = result;
670     return gindex;
671   }
672 
673 
674   static const FT_CMap_ClassRec  fnt_cmap_class_rec =
675   {
676     sizeof ( FNT_CMapRec ),
677 
678     (FT_CMap_InitFunc)     fnt_cmap_init,
679     (FT_CMap_DoneFunc)     NULL,
680     (FT_CMap_CharIndexFunc)fnt_cmap_char_index,
681     (FT_CMap_CharNextFunc) fnt_cmap_char_next,
682 
683     NULL, NULL, NULL, NULL, NULL
684   };
685 
686   static FT_CMap_Class const  fnt_cmap_class = &fnt_cmap_class_rec;
687 
688 
689   static void
FNT_Face_Done(FT_Face fntface)690   FNT_Face_Done( FT_Face  fntface )       /* FNT_Face */
691   {
692     FNT_Face   face = (FNT_Face)fntface;
693     FT_Memory  memory;
694 
695 
696     if ( !face )
697       return;
698 
699     memory = FT_FACE_MEMORY( face );
700 
701     fnt_font_done( face );
702 
703     FT_FREE( fntface->available_sizes );
704     fntface->num_fixed_sizes = 0;
705   }
706 
707 
708   static FT_Error
FNT_Face_Init(FT_Stream stream,FT_Face fntface,FT_Int face_instance_index,FT_Int num_params,FT_Parameter * params)709   FNT_Face_Init( FT_Stream      stream,
710                  FT_Face        fntface,        /* FNT_Face */
711                  FT_Int         face_instance_index,
712                  FT_Int         num_params,
713                  FT_Parameter*  params )
714   {
715     FNT_Face   face   = (FNT_Face)fntface;
716     FT_Error   error;
717     FT_Memory  memory = FT_FACE_MEMORY( face );
718     FT_Int     face_index;
719 
720     FT_UNUSED( num_params );
721     FT_UNUSED( params );
722 
723 
724     FT_TRACE2(( "Windows FNT driver\n" ));
725 
726     face_index = FT_ABS( face_instance_index ) & 0xFFFF;
727 
728     /* try to load font from a DLL */
729     error = fnt_face_get_dll_font( face, face_instance_index );
730     if ( !error && face_instance_index < 0 )
731       goto Exit;
732 
733     if ( FT_ERR_EQ( error, Unknown_File_Format ) )
734     {
735       /* this didn't work; try to load a single FNT font */
736       FNT_Font  font;
737 
738       if ( FT_NEW( face->font ) )
739         goto Exit;
740 
741       fntface->num_faces = 1;
742 
743       font           = face->font;
744       font->offset   = 0;
745       font->fnt_size = stream->size;
746 
747       error = fnt_font_load( font, stream );
748 
749       if ( !error )
750       {
751         if ( face_instance_index < 0 )
752           goto Exit;
753 
754         if ( face_index > 0 )
755           error = FT_THROW( Invalid_Argument );
756       }
757     }
758 
759     if ( error )
760       goto Fail;
761 
762     /* sanity check */
763     if ( !face->font->header.pixel_height )
764     {
765       FT_TRACE2(( "invalid pixel height\n" ));
766       error = FT_THROW( Invalid_File_Format );
767       goto Fail;
768     }
769 
770     /* we now need to fill the root FT_Face fields */
771     /* with relevant information                   */
772     {
773       FT_Face   root = FT_FACE( face );
774       FNT_Font  font = face->font;
775       FT_ULong  family_size;
776 
777 
778       root->face_index = face_index;
779 
780       root->face_flags |= FT_FACE_FLAG_FIXED_SIZES |
781                           FT_FACE_FLAG_HORIZONTAL;
782 
783       if ( font->header.avg_width == font->header.max_width )
784         root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
785 
786       if ( font->header.italic )
787         root->style_flags |= FT_STYLE_FLAG_ITALIC;
788 
789       if ( font->header.weight >= 800 )
790         root->style_flags |= FT_STYLE_FLAG_BOLD;
791 
792       /* set up the `fixed_sizes' array */
793       if ( FT_NEW_ARRAY( root->available_sizes, 1 ) )
794         goto Fail;
795 
796       root->num_fixed_sizes = 1;
797 
798       {
799         FT_Bitmap_Size*  bsize = root->available_sizes;
800         FT_UShort        x_res, y_res;
801 
802 
803         bsize->width  = (FT_Short)font->header.avg_width;
804         bsize->height = (FT_Short)( font->header.pixel_height +
805                                     font->header.external_leading );
806         bsize->size   = font->header.nominal_point_size << 6;
807 
808         x_res = font->header.horizontal_resolution;
809         if ( !x_res )
810           x_res = 72;
811 
812         y_res = font->header.vertical_resolution;
813         if ( !y_res )
814           y_res = 72;
815 
816         bsize->y_ppem = FT_MulDiv( bsize->size, y_res, 72 );
817         bsize->y_ppem = FT_PIX_ROUND( bsize->y_ppem );
818 
819         /*
820          * this reads:
821          *
822          * the nominal height is larger than the bbox's height
823          *
824          * => nominal_point_size contains incorrect value;
825          *    use pixel_height as the nominal height
826          */
827         if ( bsize->y_ppem > ( font->header.pixel_height << 6 ) )
828         {
829           FT_TRACE2(( "use pixel_height as the nominal height\n" ));
830 
831           bsize->y_ppem = font->header.pixel_height << 6;
832           bsize->size   = FT_MulDiv( bsize->y_ppem, 72, y_res );
833         }
834 
835         bsize->x_ppem = FT_MulDiv( bsize->size, x_res, 72 );
836         bsize->x_ppem = FT_PIX_ROUND( bsize->x_ppem );
837       }
838 
839       {
840         FT_CharMapRec  charmap;
841 
842 
843         charmap.encoding    = FT_ENCODING_NONE;
844         /* initial platform/encoding should indicate unset status? */
845         charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;
846         charmap.encoding_id = TT_APPLE_ID_DEFAULT;
847         charmap.face        = root;
848 
849         if ( font->header.charset == FT_WinFNT_ID_MAC )
850         {
851           charmap.encoding    = FT_ENCODING_APPLE_ROMAN;
852           charmap.platform_id = TT_PLATFORM_MACINTOSH;
853 /*        charmap.encoding_id = TT_MAC_ID_ROMAN; */
854         }
855 
856         error = FT_CMap_New( fnt_cmap_class,
857                              NULL,
858                              &charmap,
859                              NULL );
860         if ( error )
861           goto Fail;
862       }
863 
864       /* set up remaining flags */
865 
866       if ( font->header.last_char < font->header.first_char )
867       {
868         FT_TRACE2(( "invalid number of glyphs\n" ));
869         error = FT_THROW( Invalid_File_Format );
870         goto Fail;
871       }
872 
873       /* reserve one slot for the .notdef glyph at index 0 */
874       root->num_glyphs = font->header.last_char -
875                          font->header.first_char + 1 + 1;
876 
877       if ( font->header.face_name_offset >= font->header.file_size )
878       {
879         FT_TRACE2(( "invalid family name offset\n" ));
880         error = FT_THROW( Invalid_File_Format );
881         goto Fail;
882       }
883       family_size = font->header.file_size - font->header.face_name_offset;
884       /* Some broken fonts don't delimit the face name with a final */
885       /* NULL byte -- the frame is erroneously one byte too small.  */
886       /* We thus allocate one more byte, setting it explicitly to   */
887       /* zero.                                                      */
888       if ( FT_ALLOC( font->family_name, family_size + 1 ) )
889         goto Fail;
890 
891       FT_MEM_COPY( font->family_name,
892                    font->fnt_frame + font->header.face_name_offset,
893                    family_size );
894 
895       font->family_name[family_size] = '\0';
896 
897       if ( FT_REALLOC( font->family_name,
898                        family_size,
899                        ft_strlen( font->family_name ) + 1 ) )
900         goto Fail;
901 
902       root->family_name = font->family_name;
903       root->style_name  = (char *)"Regular";
904 
905       if ( root->style_flags & FT_STYLE_FLAG_BOLD )
906       {
907         if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
908           root->style_name = (char *)"Bold Italic";
909         else
910           root->style_name = (char *)"Bold";
911       }
912       else if ( root->style_flags & FT_STYLE_FLAG_ITALIC )
913         root->style_name = (char *)"Italic";
914     }
915     goto Exit;
916 
917   Fail:
918     FNT_Face_Done( fntface );
919 
920   Exit:
921     return error;
922   }
923 
924 
925   static FT_Error
FNT_Size_Select(FT_Size size,FT_ULong strike_index)926   FNT_Size_Select( FT_Size   size,
927                    FT_ULong  strike_index )
928   {
929     FNT_Face          face   = (FNT_Face)size->face;
930     FT_WinFNT_Header  header = &face->font->header;
931 
932     FT_UNUSED( strike_index );
933 
934 
935     FT_Select_Metrics( size->face, 0 );
936 
937     size->metrics.ascender    = header->ascent * 64;
938     size->metrics.descender   = -( header->pixel_height -
939                                    header->ascent ) * 64;
940     size->metrics.max_advance = header->max_width * 64;
941 
942     return FT_Err_Ok;
943   }
944 
945 
946   static FT_Error
FNT_Size_Request(FT_Size size,FT_Size_Request req)947   FNT_Size_Request( FT_Size          size,
948                     FT_Size_Request  req )
949   {
950     FNT_Face          face    = (FNT_Face)size->face;
951     FT_WinFNT_Header  header  = &face->font->header;
952     FT_Bitmap_Size*   bsize   = size->face->available_sizes;
953     FT_Error          error   = FT_ERR( Invalid_Pixel_Size );
954     FT_Long           height;
955 
956 
957     height = FT_REQUEST_HEIGHT( req );
958     height = ( height + 32 ) >> 6;
959 
960     switch ( req->type )
961     {
962     case FT_SIZE_REQUEST_TYPE_NOMINAL:
963       if ( height == ( ( bsize->y_ppem + 32 ) >> 6 ) )
964         error = FT_Err_Ok;
965       break;
966 
967     case FT_SIZE_REQUEST_TYPE_REAL_DIM:
968       if ( height == header->pixel_height )
969         error = FT_Err_Ok;
970       break;
971 
972     default:
973       error = FT_THROW( Unimplemented_Feature );
974       break;
975     }
976 
977     if ( error )
978       return error;
979     else
980       return FNT_Size_Select( size, 0 );
981   }
982 
983 
984   static FT_Error
FNT_Load_Glyph(FT_GlyphSlot slot,FT_Size size,FT_UInt glyph_index,FT_Int32 load_flags)985   FNT_Load_Glyph( FT_GlyphSlot  slot,
986                   FT_Size       size,
987                   FT_UInt       glyph_index,
988                   FT_Int32      load_flags )
989   {
990     FNT_Face    face   = (FNT_Face)FT_SIZE_FACE( size );
991     FNT_Font    font;
992     FT_Error    error  = FT_Err_Ok;
993     FT_Byte*    p;
994     FT_UInt     len;
995     FT_Bitmap*  bitmap = &slot->bitmap;
996     FT_ULong    offset;
997     FT_Bool     new_format;
998 
999 
1000     if ( !face )
1001     {
1002       error = FT_THROW( Invalid_Face_Handle );
1003       goto Exit;
1004     }
1005 
1006     font = face->font;
1007 
1008     if ( !font                                                   ||
1009          glyph_index >= (FT_UInt)( FT_FACE( face )->num_glyphs ) )
1010     {
1011       error = FT_THROW( Invalid_Argument );
1012       goto Exit;
1013     }
1014 
1015     FT_TRACE1(( "FNT_Load_Glyph: glyph index %d\n", glyph_index ));
1016 
1017     if ( glyph_index > 0 )
1018       glyph_index--;                           /* revert to real index */
1019     else
1020       glyph_index = font->header.default_char; /* the `.notdef' glyph  */
1021 
1022     new_format = FT_BOOL( font->header.version == 0x300 );
1023     len        = new_format ? 6 : 4;
1024 
1025     /* get glyph width and offset */
1026     offset = ( new_format ? 148 : 118 ) + len * glyph_index;
1027 
1028     if ( offset >= font->header.file_size - 2 - ( new_format ? 4 : 2 ) )
1029     {
1030       FT_TRACE2(( "invalid FNT offset\n" ));
1031       error = FT_THROW( Invalid_File_Format );
1032       goto Exit;
1033     }
1034 
1035     p = font->fnt_frame + offset;
1036 
1037     bitmap->width = FT_NEXT_USHORT_LE( p );
1038 
1039     /* jump to glyph entry */
1040     if ( new_format )
1041       offset = FT_NEXT_ULONG_LE( p );
1042     else
1043       offset = FT_NEXT_USHORT_LE( p );
1044 
1045     if ( offset >= font->header.file_size )
1046     {
1047       FT_TRACE2(( "invalid FNT offset\n" ));
1048       error = FT_THROW( Invalid_File_Format );
1049       goto Exit;
1050     }
1051 
1052     bitmap->rows       = font->header.pixel_height;
1053     bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
1054 
1055     slot->bitmap_left     = 0;
1056     slot->bitmap_top      = font->header.ascent;
1057     slot->format          = FT_GLYPH_FORMAT_BITMAP;
1058 
1059     /* now set up metrics */
1060     slot->metrics.width        = (FT_Pos)( bitmap->width << 6 );
1061     slot->metrics.height       = (FT_Pos)( bitmap->rows << 6 );
1062     slot->metrics.horiAdvance  = (FT_Pos)( bitmap->width << 6 );
1063     slot->metrics.horiBearingX = 0;
1064     slot->metrics.horiBearingY = slot->bitmap_top << 6;
1065 
1066     ft_synthesize_vertical_metrics( &slot->metrics,
1067                                     (FT_Pos)( bitmap->rows << 6 ) );
1068 
1069     if ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY )
1070       goto Exit;
1071 
1072     /* jump to glyph data */
1073     p = font->fnt_frame + /* font->header.bits_offset */ + offset;
1074 
1075     /* allocate and build bitmap */
1076     {
1077       FT_Memory  memory = FT_FACE_MEMORY( slot->face );
1078       FT_UInt    pitch  = ( bitmap->width + 7 ) >> 3;
1079       FT_Byte*   column;
1080       FT_Byte*   write;
1081 
1082 
1083       bitmap->pitch = (int)pitch;
1084       if ( !pitch                                                 ||
1085            offset + pitch * bitmap->rows > font->header.file_size )
1086       {
1087         FT_TRACE2(( "invalid bitmap width\n" ));
1088         error = FT_THROW( Invalid_File_Format );
1089         goto Exit;
1090       }
1091 
1092       /* note: since glyphs are stored in columns and not in rows we */
1093       /*       can't use ft_glyphslot_set_bitmap                     */
1094       if ( FT_ALLOC_MULT( bitmap->buffer, bitmap->rows, pitch ) )
1095         goto Exit;
1096 
1097       column = (FT_Byte*)bitmap->buffer;
1098 
1099       for ( ; pitch > 0; pitch--, column++ )
1100       {
1101         FT_Byte*  limit = p + bitmap->rows;
1102 
1103 
1104         for ( write = column; p < limit; p++, write += bitmap->pitch )
1105           *write = *p;
1106       }
1107 
1108       slot->internal->flags = FT_GLYPH_OWN_BITMAP;
1109     }
1110 
1111   Exit:
1112     return error;
1113   }
1114 
1115 
1116   static FT_Error
winfnt_get_header(FT_Face face,FT_WinFNT_HeaderRec * aheader)1117   winfnt_get_header( FT_Face               face,
1118                      FT_WinFNT_HeaderRec  *aheader )
1119   {
1120     FNT_Font  font = ((FNT_Face)face)->font;
1121 
1122 
1123     *aheader = font->header;
1124 
1125     return 0;
1126   }
1127 
1128 
1129   static const FT_Service_WinFntRec  winfnt_service_rec =
1130   {
1131     winfnt_get_header       /* get_header */
1132   };
1133 
1134   /*
1135    * SERVICE LIST
1136    *
1137    */
1138 
1139   static const FT_ServiceDescRec  winfnt_services[] =
1140   {
1141     { FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_WINFNT },
1142     { FT_SERVICE_ID_WINFNT,      &winfnt_service_rec },
1143     { NULL, NULL }
1144   };
1145 
1146 
1147   static FT_Module_Interface
winfnt_get_service(FT_Module module,const FT_String * service_id)1148   winfnt_get_service( FT_Module         module,
1149                       const FT_String*  service_id )
1150   {
1151     FT_UNUSED( module );
1152 
1153     return ft_service_list_lookup( winfnt_services, service_id );
1154   }
1155 
1156 
1157 
1158 
1159   FT_CALLBACK_TABLE_DEF
1160   const FT_Driver_ClassRec  winfnt_driver_class =
1161   {
1162     {
1163       FT_MODULE_FONT_DRIVER        |
1164       FT_MODULE_DRIVER_NO_OUTLINES,
1165       sizeof ( FT_DriverRec ),
1166 
1167       "winfonts",
1168       0x10000L,
1169       0x20000L,
1170 
1171       NULL, /* module-specific interface */
1172 
1173       NULL,                     /* FT_Module_Constructor  module_init   */
1174       NULL,                     /* FT_Module_Destructor   module_done   */
1175       winfnt_get_service        /* FT_Module_Requester    get_interface */
1176     },
1177 
1178     sizeof ( FNT_FaceRec ),
1179     sizeof ( FT_SizeRec ),
1180     sizeof ( FT_GlyphSlotRec ),
1181 
1182     FNT_Face_Init,              /* FT_Face_InitFunc  init_face */
1183     FNT_Face_Done,              /* FT_Face_DoneFunc  done_face */
1184     NULL,                       /* FT_Size_InitFunc  init_size */
1185     NULL,                       /* FT_Size_DoneFunc  done_size */
1186     NULL,                       /* FT_Slot_InitFunc  init_slot */
1187     NULL,                       /* FT_Slot_DoneFunc  done_slot */
1188 
1189     FNT_Load_Glyph,             /* FT_Slot_LoadFunc  load_glyph */
1190 
1191     NULL,                       /* FT_Face_GetKerningFunc   get_kerning  */
1192     NULL,                       /* FT_Face_AttachFunc       attach_file  */
1193     NULL,                       /* FT_Face_GetAdvancesFunc  get_advances */
1194 
1195     FNT_Size_Request,           /* FT_Size_RequestFunc  request_size */
1196     FNT_Size_Select             /* FT_Size_SelectFunc   select_size  */
1197   };
1198 
1199 
1200 /* END */
1201