Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / freetype2 / src / src / base / ftobjs.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftobjs.c                                                               */
4 /*                                                                         */
5 /*    The FreeType private base classes (body).                            */
6 /*                                                                         */
7 /*  Copyright 1996-2011 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
10 /*  This file is part of the FreeType project, and may only be used,       */
11 /*  modified, and distributed under the terms of the FreeType project      */
12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
13 /*  this file you indicate that you have read the license and              */
14 /*  understand and accept it fully.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include FT_LIST_H
21 #include FT_OUTLINE_H
22 #include FT_INTERNAL_VALIDATE_H
23 #include FT_INTERNAL_OBJECTS_H
24 #include FT_INTERNAL_DEBUG_H
25 #include FT_INTERNAL_RFORK_H
26 #include FT_INTERNAL_STREAM_H
27 #include FT_INTERNAL_SFNT_H    /* for SFNT_Load_Table_Func */
28 #include FT_TRUETYPE_TABLES_H
29 #include FT_TRUETYPE_TAGS_H
30 #include FT_TRUETYPE_IDS_H
31
32 #include FT_SERVICE_SFNT_H
33 #include FT_SERVICE_POSTSCRIPT_NAME_H
34 #include FT_SERVICE_GLYPH_DICT_H
35 #include FT_SERVICE_TT_CMAP_H
36 #include FT_SERVICE_KERNING_H
37 #include FT_SERVICE_TRUETYPE_ENGINE_H
38
39 #ifdef FT_CONFIG_OPTION_MAC_FONTS
40 #include "ftbase.h"
41 #endif
42
43 #define GRID_FIT_METRICS
44
45
46   FT_BASE_DEF( FT_Pointer )
47   ft_service_list_lookup( FT_ServiceDesc  service_descriptors,
48                           const char*     service_id )
49   {
50     FT_Pointer      result = NULL;
51     FT_ServiceDesc  desc   = service_descriptors;
52
53
54     if ( desc && service_id )
55     {
56       for ( ; desc->serv_id != NULL; desc++ )
57       {
58         if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
59         {
60           result = (FT_Pointer)desc->serv_data;
61           break;
62         }
63       }
64     }
65
66     return result;
67   }
68
69
70   FT_BASE_DEF( void )
71   ft_validator_init( FT_Validator        valid,
72                      const FT_Byte*      base,
73                      const FT_Byte*      limit,
74                      FT_ValidationLevel  level )
75   {
76     valid->base  = base;
77     valid->limit = limit;
78     valid->level = level;
79     valid->error = FT_Err_Ok;
80   }
81
82
83   FT_BASE_DEF( FT_Int )
84   ft_validator_run( FT_Validator  valid )
85   {
86     /* This function doesn't work!  None should call it. */
87     FT_UNUSED( valid );
88
89     return -1;
90   }
91
92
93   FT_BASE_DEF( void )
94   ft_validator_error( FT_Validator  valid,
95                       FT_Error      error )
96   {
97     /* since the cast below also disables the compiler's */
98     /* type check, we introduce a dummy variable, which  */
99     /* will be optimized away                            */
100     volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
101
102
103     valid->error = error;
104
105     /* throw away volatileness; use `jump_buffer' or the  */
106     /* compiler may warn about an unused local variable   */
107     ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
108   }
109
110
111   /*************************************************************************/
112   /*************************************************************************/
113   /*************************************************************************/
114   /****                                                                 ****/
115   /****                                                                 ****/
116   /****                           S T R E A M                           ****/
117   /****                                                                 ****/
118   /****                                                                 ****/
119   /*************************************************************************/
120   /*************************************************************************/
121   /*************************************************************************/
122
123
124   /* create a new input stream from an FT_Open_Args structure */
125   /*                                                          */
126   FT_BASE_DEF( FT_Error )
127   FT_Stream_New( FT_Library           library,
128                  const FT_Open_Args*  args,
129                  FT_Stream           *astream )
130   {
131     FT_Error   error;
132     FT_Memory  memory;
133     FT_Stream  stream = NULL;
134
135
136     *astream = 0;
137
138     if ( !library )
139       return FT_Err_Invalid_Library_Handle;
140
141     if ( !args )
142       return FT_Err_Invalid_Argument;
143
144     memory = library->memory;
145
146     if ( FT_NEW( stream ) )
147       goto Exit;
148
149     stream->memory = memory;
150
151     if ( args->flags & FT_OPEN_MEMORY )
152     {
153       /* create a memory-based stream */
154       FT_Stream_OpenMemory( stream,
155                             (const FT_Byte*)args->memory_base,
156                             args->memory_size );
157     }
158
159 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
160
161     else if ( args->flags & FT_OPEN_PATHNAME )
162     {
163       /* create a normal system stream */
164       error = FT_Stream_Open( stream, args->pathname );
165       stream->pathname.pointer = args->pathname;
166     }
167     else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
168     {
169       /* use an existing, user-provided stream */
170
171       /* in this case, we do not need to allocate a new stream object */
172       /* since the caller is responsible for closing it himself       */
173       FT_FREE( stream );
174       stream = args->stream;
175     }
176
177 #endif
178
179     else
180       error = FT_Err_Invalid_Argument;
181
182     if ( error )
183       FT_FREE( stream );
184     else
185       stream->memory = memory;  /* just to be certain */
186
187     *astream = stream;
188
189   Exit:
190     return error;
191   }
192
193
194   FT_BASE_DEF( void )
195   FT_Stream_Free( FT_Stream  stream,
196                   FT_Int     external )
197   {
198     if ( stream )
199     {
200       FT_Memory  memory = stream->memory;
201
202
203       FT_Stream_Close( stream );
204
205       if ( !external )
206         FT_FREE( stream );
207     }
208   }
209
210
211   /*************************************************************************/
212   /*                                                                       */
213   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
214   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
215   /* messages during execution.                                            */
216   /*                                                                       */
217 #undef  FT_COMPONENT
218 #define FT_COMPONENT  trace_objs
219
220
221   /*************************************************************************/
222   /*************************************************************************/
223   /*************************************************************************/
224   /****                                                                 ****/
225   /****                                                                 ****/
226   /****               FACE, SIZE & GLYPH SLOT OBJECTS                   ****/
227   /****                                                                 ****/
228   /****                                                                 ****/
229   /*************************************************************************/
230   /*************************************************************************/
231   /*************************************************************************/
232
233
234   static FT_Error
235   ft_glyphslot_init( FT_GlyphSlot  slot )
236   {
237     FT_Driver         driver   = slot->face->driver;
238     FT_Driver_Class   clazz    = driver->clazz;
239     FT_Memory         memory   = driver->root.memory;
240     FT_Error          error    = FT_Err_Ok;
241     FT_Slot_Internal  internal = NULL;
242
243
244     slot->library = driver->root.library;
245
246     if ( FT_NEW( internal ) )
247       goto Exit;
248
249     slot->internal = internal;
250
251     if ( FT_DRIVER_USES_OUTLINES( driver ) )
252       error = FT_GlyphLoader_New( memory, &internal->loader );
253
254     if ( !error && clazz->init_slot )
255       error = clazz->init_slot( slot );
256
257   Exit:
258     return error;
259   }
260
261
262   FT_BASE_DEF( void )
263   ft_glyphslot_free_bitmap( FT_GlyphSlot  slot )
264   {
265     if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
266     {
267       FT_Memory  memory = FT_FACE_MEMORY( slot->face );
268
269
270       FT_FREE( slot->bitmap.buffer );
271       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
272     }
273     else
274     {
275       /* assume that the bitmap buffer was stolen or not */
276       /* allocated from the heap                         */
277       slot->bitmap.buffer = NULL;
278     }
279   }
280
281
282   FT_BASE_DEF( void )
283   ft_glyphslot_set_bitmap( FT_GlyphSlot  slot,
284                            FT_Byte*      buffer )
285   {
286     ft_glyphslot_free_bitmap( slot );
287
288     slot->bitmap.buffer = buffer;
289
290     FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
291   }
292
293
294   FT_BASE_DEF( FT_Error )
295   ft_glyphslot_alloc_bitmap( FT_GlyphSlot  slot,
296                              FT_ULong      size )
297   {
298     FT_Memory  memory = FT_FACE_MEMORY( slot->face );
299     FT_Error   error;
300
301
302     if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
303       FT_FREE( slot->bitmap.buffer );
304     else
305       slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
306
307     (void)FT_ALLOC( slot->bitmap.buffer, size );
308     return error;
309   }
310
311
312   static void
313   ft_glyphslot_clear( FT_GlyphSlot  slot )
314   {
315     /* free bitmap if needed */
316     ft_glyphslot_free_bitmap( slot );
317
318     /* clear all public fields in the glyph slot */
319     FT_ZERO( &slot->metrics );
320     FT_ZERO( &slot->outline );
321
322     slot->bitmap.width      = 0;
323     slot->bitmap.rows       = 0;
324     slot->bitmap.pitch      = 0;
325     slot->bitmap.pixel_mode = 0;
326     /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
327
328     slot->bitmap_left   = 0;
329     slot->bitmap_top    = 0;
330     slot->num_subglyphs = 0;
331     slot->subglyphs     = 0;
332     slot->control_data  = 0;
333     slot->control_len   = 0;
334     slot->other         = 0;
335     slot->format        = FT_GLYPH_FORMAT_NONE;
336
337     slot->linearHoriAdvance = 0;
338     slot->linearVertAdvance = 0;
339     slot->lsb_delta         = 0;
340     slot->rsb_delta         = 0;
341   }
342
343
344   static void
345   ft_glyphslot_done( FT_GlyphSlot  slot )
346   {
347     FT_Driver        driver = slot->face->driver;
348     FT_Driver_Class  clazz  = driver->clazz;
349     FT_Memory        memory = driver->root.memory;
350
351
352     if ( clazz->done_slot )
353       clazz->done_slot( slot );
354
355     /* free bitmap buffer if needed */
356     ft_glyphslot_free_bitmap( slot );
357
358     /* slot->internal might be NULL in out-of-memory situations */
359     if ( slot->internal )
360     {
361       /* free glyph loader */
362       if ( FT_DRIVER_USES_OUTLINES( driver ) )
363       {
364         FT_GlyphLoader_Done( slot->internal->loader );
365         slot->internal->loader = 0;
366       }
367
368       FT_FREE( slot->internal );
369     }
370   }
371
372
373   /* documentation is in ftobjs.h */
374
375   FT_BASE_DEF( FT_Error )
376   FT_New_GlyphSlot( FT_Face        face,
377                     FT_GlyphSlot  *aslot )
378   {
379     FT_Error         error;
380     FT_Driver        driver;
381     FT_Driver_Class  clazz;
382     FT_Memory        memory;
383     FT_GlyphSlot     slot = NULL;
384
385
386     if ( !face || !face->driver )
387       return FT_Err_Invalid_Argument;
388
389     driver = face->driver;
390     clazz  = driver->clazz;
391     memory = driver->root.memory;
392
393     FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
394     if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
395     {
396       slot->face = face;
397
398       error = ft_glyphslot_init( slot );
399       if ( error )
400       {
401         ft_glyphslot_done( slot );
402         FT_FREE( slot );
403         goto Exit;
404       }
405
406       slot->next  = face->glyph;
407       face->glyph = slot;
408
409       if ( aslot )
410         *aslot = slot;
411     }
412     else if ( aslot )
413       *aslot = 0;
414
415
416   Exit:
417     FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
418     return error;
419   }
420
421
422   /* documentation is in ftobjs.h */
423
424   FT_BASE_DEF( void )
425   FT_Done_GlyphSlot( FT_GlyphSlot  slot )
426   {
427     if ( slot )
428     {
429       FT_Driver     driver = slot->face->driver;
430       FT_Memory     memory = driver->root.memory;
431       FT_GlyphSlot  prev;
432       FT_GlyphSlot  cur;
433
434
435       /* Remove slot from its parent face's list */
436       prev = NULL;
437       cur  = slot->face->glyph;
438
439       while ( cur )
440       {
441         if ( cur == slot )
442         {
443           if ( !prev )
444             slot->face->glyph = cur->next;
445           else
446             prev->next = cur->next;
447
448           ft_glyphslot_done( slot );
449           FT_FREE( slot );
450           break;
451         }
452         prev = cur;
453         cur  = cur->next;
454       }
455     }
456   }
457
458
459   /* documentation is in freetype.h */
460
461   FT_EXPORT_DEF( void )
462   FT_Set_Transform( FT_Face     face,
463                     FT_Matrix*  matrix,
464                     FT_Vector*  delta )
465   {
466     FT_Face_Internal  internal;
467
468
469     if ( !face )
470       return;
471
472     internal = face->internal;
473
474     internal->transform_flags = 0;
475
476     if ( !matrix )
477     {
478       internal->transform_matrix.xx = 0x10000L;
479       internal->transform_matrix.xy = 0;
480       internal->transform_matrix.yx = 0;
481       internal->transform_matrix.yy = 0x10000L;
482       matrix = &internal->transform_matrix;
483     }
484     else
485       internal->transform_matrix = *matrix;
486
487     /* set transform_flags bit flag 0 if `matrix' isn't the identity */
488     if ( ( matrix->xy | matrix->yx ) ||
489          matrix->xx != 0x10000L      ||
490          matrix->yy != 0x10000L      )
491       internal->transform_flags |= 1;
492
493     if ( !delta )
494     {
495       internal->transform_delta.x = 0;
496       internal->transform_delta.y = 0;
497       delta = &internal->transform_delta;
498     }
499     else
500       internal->transform_delta = *delta;
501
502     /* set transform_flags bit flag 1 if `delta' isn't the null vector */
503     if ( delta->x | delta->y )
504       internal->transform_flags |= 2;
505   }
506
507
508   static FT_Renderer
509   ft_lookup_glyph_renderer( FT_GlyphSlot  slot );
510
511
512 #ifdef GRID_FIT_METRICS
513   static void
514   ft_glyphslot_grid_fit_metrics( FT_GlyphSlot  slot,
515                                  FT_Bool       vertical )
516   {
517     FT_Glyph_Metrics*  metrics = &slot->metrics;
518     FT_Pos             right, bottom;
519
520
521     if ( vertical )
522     {
523       metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
524       metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
525
526       right  = FT_PIX_CEIL( metrics->vertBearingX + metrics->width );
527       bottom = FT_PIX_CEIL( metrics->vertBearingY + metrics->height );
528
529       metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
530       metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
531
532       metrics->width  = right - metrics->vertBearingX;
533       metrics->height = bottom - metrics->vertBearingY;
534     }
535     else
536     {
537       metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
538       metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
539
540       right  = FT_PIX_CEIL ( metrics->horiBearingX + metrics->width );
541       bottom = FT_PIX_FLOOR( metrics->horiBearingY - metrics->height );
542
543       metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
544       metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
545
546       metrics->width  = right - metrics->horiBearingX;
547       metrics->height = metrics->horiBearingY - bottom;
548     }
549
550     metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
551     metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
552   }
553 #endif /* GRID_FIT_METRICS */
554
555
556   /* documentation is in freetype.h */
557
558   FT_EXPORT_DEF( FT_Error )
559   FT_Load_Glyph( FT_Face   face,
560                  FT_UInt   glyph_index,
561                  FT_Int32  load_flags )
562   {
563     FT_Error      error;
564     FT_Driver     driver;
565     FT_GlyphSlot  slot;
566     FT_Library    library;
567     FT_Bool       autohint = FALSE;
568     FT_Module     hinter;
569     TT_Face       ttface = (TT_Face)face;
570
571
572     if ( !face || !face->size || !face->glyph )
573       return FT_Err_Invalid_Face_Handle;
574
575     /* The validity test for `glyph_index' is performed by the */
576     /* font drivers.                                           */
577
578     slot = face->glyph;
579     ft_glyphslot_clear( slot );
580
581     driver  = face->driver;
582     library = driver->root.library;
583     hinter  = library->auto_hinter;
584
585     /* resolve load flags dependencies */
586
587     if ( load_flags & FT_LOAD_NO_RECURSE )
588       load_flags |= FT_LOAD_NO_SCALE         |
589                     FT_LOAD_IGNORE_TRANSFORM;
590
591     if ( load_flags & FT_LOAD_NO_SCALE )
592     {
593       load_flags |= FT_LOAD_NO_HINTING |
594                     FT_LOAD_NO_BITMAP;
595
596       load_flags &= ~FT_LOAD_RENDER;
597     }
598
599     /*
600      * Determine whether we need to auto-hint or not.
601      * The general rules are:
602      *
603      * - Do only auto-hinting if we have a hinter module, a scalable font
604      *   format dealing with outlines, and no transforms except simple
605      *   slants and/or rotations by integer multiples of 90 degrees.
606      *
607      * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
608      *   have a native font hinter.
609      *
610      * - Otherwise, auto-hint for LIGHT hinting mode or if there isn't
611      *   any hinting bytecode in the TrueType/OpenType font.
612      *
613      * - Exception: The font is `tricky' and requires the native hinter to
614      *   load properly.
615      */
616
617     if ( hinter                                           &&
618          !( load_flags & FT_LOAD_NO_HINTING )             &&
619          !( load_flags & FT_LOAD_NO_AUTOHINT )            &&
620          FT_DRIVER_IS_SCALABLE( driver )                  &&
621          FT_DRIVER_USES_OUTLINES( driver )                &&
622          !FT_IS_TRICKY( face )                            &&
623          ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM )    ||
624            ( face->internal->transform_matrix.yx == 0 &&
625              face->internal->transform_matrix.xx != 0 ) ||
626            ( face->internal->transform_matrix.xx == 0 &&
627              face->internal->transform_matrix.yx != 0 ) ) )
628     {
629       if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
630            !FT_DRIVER_HAS_HINTER( driver )         )
631         autohint = TRUE;
632       else
633       {
634         FT_Render_Mode  mode = FT_LOAD_TARGET_MODE( load_flags );
635
636
637         /* the check for `num_locations' assures that we actually    */
638         /* test for instructions in a TTF and not in a CFF-based OTF */
639         if ( mode == FT_RENDER_MODE_LIGHT                       ||
640              face->internal->ignore_unpatented_hinter           ||
641              ( FT_IS_SFNT( face )                             &&
642                ttface->num_locations                          &&
643                ttface->max_profile.maxSizeOfInstructions == 0 ) )
644           autohint = TRUE;
645       }
646     }
647
648     if ( autohint )
649     {
650       FT_AutoHinter_Service  hinting;
651
652
653       /* try to load embedded bitmaps first if available            */
654       /*                                                            */
655       /* XXX: This is really a temporary hack that should disappear */
656       /*      promptly with FreeType 2.1!                           */
657       /*                                                            */
658       if ( FT_HAS_FIXED_SIZES( face )             &&
659           ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
660       {
661         error = driver->clazz->load_glyph( slot, face->size,
662                                            glyph_index,
663                                            load_flags | FT_LOAD_SBITS_ONLY );
664
665         if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
666           goto Load_Ok;
667       }
668
669       {
670         FT_Face_Internal  internal        = face->internal;
671         FT_Int            transform_flags = internal->transform_flags;
672
673
674         /* since the auto-hinter calls FT_Load_Glyph by itself, */
675         /* make sure that glyphs aren't transformed             */
676         internal->transform_flags = 0;
677
678         /* load auto-hinted outline */
679         hinting = (FT_AutoHinter_Service)hinter->clazz->module_interface;
680
681         error   = hinting->load_glyph( (FT_AutoHinter)hinter,
682                                        slot, face->size,
683                                        glyph_index, load_flags );
684
685         internal->transform_flags = transform_flags;
686       }
687     }
688     else
689     {
690       error = driver->clazz->load_glyph( slot,
691                                          face->size,
692                                          glyph_index,
693                                          load_flags );
694       if ( error )
695         goto Exit;
696
697       if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
698       {
699         /* check that the loaded outline is correct */
700         error = FT_Outline_Check( &slot->outline );
701         if ( error )
702           goto Exit;
703
704 #ifdef GRID_FIT_METRICS
705         if ( !( load_flags & FT_LOAD_NO_HINTING ) )
706           ft_glyphslot_grid_fit_metrics( slot,
707               FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
708 #endif
709       }
710     }
711
712   Load_Ok:
713     /* compute the advance */
714     if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
715     {
716       slot->advance.x = 0;
717       slot->advance.y = slot->metrics.vertAdvance;
718     }
719     else
720     {
721       slot->advance.x = slot->metrics.horiAdvance;
722       slot->advance.y = 0;
723     }
724
725     /* compute the linear advance in 16.16 pixels */
726     if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
727          ( FT_IS_SCALABLE( face ) )                  )
728     {
729       FT_Size_Metrics*  metrics = &face->size->metrics;
730
731
732       /* it's tricky! */
733       slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
734                                            metrics->x_scale, 64 );
735
736       slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
737                                            metrics->y_scale, 64 );
738     }
739
740     if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
741     {
742       FT_Face_Internal  internal = face->internal;
743
744
745       /* now, transform the glyph image if needed */
746       if ( internal->transform_flags )
747       {
748         /* get renderer */
749         FT_Renderer  renderer = ft_lookup_glyph_renderer( slot );
750
751
752         if ( renderer )
753           error = renderer->clazz->transform_glyph(
754                                      renderer, slot,
755                                      &internal->transform_matrix,
756                                      &internal->transform_delta );
757         else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
758         {
759           /* apply `standard' transformation if no renderer is available */
760           if ( internal->transform_flags & 1 )
761             FT_Outline_Transform( &slot->outline,
762                                   &internal->transform_matrix );
763
764           if ( internal->transform_flags & 2 )
765             FT_Outline_Translate( &slot->outline,
766                                   internal->transform_delta.x,
767                                   internal->transform_delta.y );
768         }
769
770         /* transform advance */
771         FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
772       }
773     }
774
775     FT_TRACE5(( "  x advance: %d\n" , slot->advance.x ));
776     FT_TRACE5(( "  y advance: %d\n" , slot->advance.y ));
777
778     FT_TRACE5(( "  linear x advance: %d\n" , slot->linearHoriAdvance ));
779     FT_TRACE5(( "  linear y advance: %d\n" , slot->linearVertAdvance ));
780
781     /* do we need to render the image now? */
782     if ( !error                                    &&
783          slot->format != FT_GLYPH_FORMAT_BITMAP    &&
784          slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
785          load_flags & FT_LOAD_RENDER )
786     {
787       FT_Render_Mode  mode = FT_LOAD_TARGET_MODE( load_flags );
788
789
790       if ( mode == FT_RENDER_MODE_NORMAL      &&
791            (load_flags & FT_LOAD_MONOCHROME ) )
792         mode = FT_RENDER_MODE_MONO;
793
794       error = FT_Render_Glyph( slot, mode );
795     }
796
797   Exit:
798     return error;
799   }
800
801
802   /* documentation is in freetype.h */
803
804   FT_EXPORT_DEF( FT_Error )
805   FT_Load_Char( FT_Face   face,
806                 FT_ULong  char_code,
807                 FT_Int32  load_flags )
808   {
809     FT_UInt  glyph_index;
810
811
812     if ( !face )
813       return FT_Err_Invalid_Face_Handle;
814
815     glyph_index = (FT_UInt)char_code;
816     if ( face->charmap )
817       glyph_index = FT_Get_Char_Index( face, char_code );
818
819     return FT_Load_Glyph( face, glyph_index, load_flags );
820   }
821
822
823   /* destructor for sizes list */
824   static void
825   destroy_size( FT_Memory  memory,
826                 FT_Size    size,
827                 FT_Driver  driver )
828   {
829     /* finalize client-specific data */
830     if ( size->generic.finalizer )
831       size->generic.finalizer( size );
832
833     /* finalize format-specific stuff */
834     if ( driver->clazz->done_size )
835       driver->clazz->done_size( size );
836
837     FT_FREE( size->internal );
838     FT_FREE( size );
839   }
840
841
842   static void
843   ft_cmap_done_internal( FT_CMap  cmap );
844
845
846   static void
847   destroy_charmaps( FT_Face    face,
848                     FT_Memory  memory )
849   {
850     FT_Int  n;
851
852
853     if ( !face )
854       return;
855
856     for ( n = 0; n < face->num_charmaps; n++ )
857     {
858       FT_CMap  cmap = FT_CMAP( face->charmaps[n] );
859
860
861       ft_cmap_done_internal( cmap );
862
863       face->charmaps[n] = NULL;
864     }
865
866     FT_FREE( face->charmaps );
867     face->num_charmaps = 0;
868   }
869
870
871   /* destructor for faces list */
872   static void
873   destroy_face( FT_Memory  memory,
874                 FT_Face    face,
875                 FT_Driver  driver )
876   {
877     FT_Driver_Class  clazz = driver->clazz;
878
879
880     /* discard auto-hinting data */
881     if ( face->autohint.finalizer )
882       face->autohint.finalizer( face->autohint.data );
883
884     /* Discard glyph slots for this face.                           */
885     /* Beware!  FT_Done_GlyphSlot() changes the field `face->glyph' */
886     while ( face->glyph )
887       FT_Done_GlyphSlot( face->glyph );
888
889     /* discard all sizes for this face */
890     FT_List_Finalize( &face->sizes_list,
891                       (FT_List_Destructor)destroy_size,
892                       memory,
893                       driver );
894     face->size = 0;
895
896     /* now discard client data */
897     if ( face->generic.finalizer )
898       face->generic.finalizer( face );
899
900     /* discard charmaps */
901     destroy_charmaps( face, memory );
902
903     /* finalize format-specific stuff */
904     if ( clazz->done_face )
905       clazz->done_face( face );
906
907     /* close the stream for this face if needed */
908     FT_Stream_Free(
909       face->stream,
910       ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
911
912     face->stream = 0;
913
914     /* get rid of it */
915     if ( face->internal )
916     {
917       FT_FREE( face->internal );
918     }
919     FT_FREE( face );
920   }
921
922
923   static void
924   Destroy_Driver( FT_Driver  driver )
925   {
926     FT_List_Finalize( &driver->faces_list,
927                       (FT_List_Destructor)destroy_face,
928                       driver->root.memory,
929                       driver );
930
931     /* check whether we need to drop the driver's glyph loader */
932     if ( FT_DRIVER_USES_OUTLINES( driver ) )
933       FT_GlyphLoader_Done( driver->glyph_loader );
934   }
935
936
937   /*************************************************************************/
938   /*                                                                       */
939   /* <Function>                                                            */
940   /*    find_unicode_charmap                                               */
941   /*                                                                       */
942   /* <Description>                                                         */
943   /*    This function finds a Unicode charmap, if there is one.            */
944   /*    And if there is more than one, it tries to favour the more         */
945   /*    extensive one, i.e., one that supports UCS-4 against those which   */
946   /*    are limited to the BMP (said UCS-2 encoding.)                      */
947   /*                                                                       */
948   /*    This function is called from open_face() (just below), and also    */
949   /*    from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ).                */
950   /*                                                                       */
951   static FT_Error
952   find_unicode_charmap( FT_Face  face )
953   {
954     FT_CharMap*  first;
955     FT_CharMap*  cur;
956
957
958     /* caller should have already checked that `face' is valid */
959     FT_ASSERT( face );
960
961     first = face->charmaps;
962
963     if ( !first )
964       return FT_Err_Invalid_CharMap_Handle;
965
966     /*
967      *  The original TrueType specification(s) only specified charmap
968      *  formats that are capable of mapping 8 or 16 bit character codes to
969      *  glyph indices.
970      *
971      *  However, recent updates to the Apple and OpenType specifications
972      *  introduced new formats that are capable of mapping 32-bit character
973      *  codes as well.  And these are already used on some fonts, mainly to
974      *  map non-BMP Asian ideographs as defined in Unicode.
975      *
976      *  For compatibility purposes, these fonts generally come with
977      *  *several* Unicode charmaps:
978      *
979      *   - One of them in the "old" 16-bit format, that cannot access
980      *     all glyphs in the font.
981      *
982      *   - Another one in the "new" 32-bit format, that can access all
983      *     the glyphs.
984      *
985      *  This function has been written to always favor a 32-bit charmap
986      *  when found.  Otherwise, a 16-bit one is returned when found.
987      */
988
989     /* Since the `interesting' table, with IDs (3,10), is normally the */
990     /* last one, we loop backwards.  This loses with type1 fonts with  */
991     /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP  */
992     /* chars (.01% ?), and this is the same about 99.99% of the time!  */
993
994     cur = first + face->num_charmaps;  /* points after the last one */
995
996     for ( ; --cur >= first; )
997     {
998       if ( cur[0]->encoding == FT_ENCODING_UNICODE )
999       {
1000         /* XXX If some new encodings to represent UCS-4 are added, */
1001         /*     they should be added here.                          */
1002         if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
1003                cur[0]->encoding_id == TT_MS_ID_UCS_4        )     ||
1004              ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1005                cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32    ) )
1006         {
1007 #ifdef FT_MAX_CHARMAP_CACHEABLE
1008           if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1009           {
1010             FT_ERROR(( "find_unicode_charmap: UCS-4 cmap is found "
1011                        "at too late position (%d)\n", cur - first ));
1012             continue;
1013           }
1014 #endif
1015           face->charmap = cur[0];
1016           return FT_Err_Ok;
1017         }
1018       }
1019     }
1020
1021     /* We do not have any UCS-4 charmap.                */
1022     /* Do the loop again and search for UCS-2 charmaps. */
1023     cur = first + face->num_charmaps;
1024
1025     for ( ; --cur >= first; )
1026     {
1027       if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1028       {
1029 #ifdef FT_MAX_CHARMAP_CACHEABLE
1030         if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1031         {
1032           FT_ERROR(( "find_unicode_charmap: UCS-2 cmap is found "
1033                      "at too late position (%d)\n", cur - first ));
1034           continue;
1035         }
1036 #endif
1037         face->charmap = cur[0];
1038         return FT_Err_Ok;
1039       }
1040     }
1041
1042     return FT_Err_Invalid_CharMap_Handle;
1043   }
1044
1045
1046   /*************************************************************************/
1047   /*                                                                       */
1048   /* <Function>                                                            */
1049   /*    find_variant_selector_charmap                                      */
1050   /*                                                                       */
1051   /* <Description>                                                         */
1052   /*    This function finds the variant selector charmap, if there is one. */
1053   /*    There can only be one (platform=0, specific=5, format=14).         */
1054   /*                                                                       */
1055   static FT_CharMap
1056   find_variant_selector_charmap( FT_Face  face )
1057   {
1058     FT_CharMap*  first;
1059     FT_CharMap*  end;
1060     FT_CharMap*  cur;
1061
1062
1063     /* caller should have already checked that `face' is valid */
1064     FT_ASSERT( face );
1065
1066     first = face->charmaps;
1067
1068     if ( !first )
1069       return NULL;
1070
1071     end = first + face->num_charmaps;  /* points after the last one */
1072
1073     for ( cur = first; cur < end; ++cur )
1074     {
1075       if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE    &&
1076            cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
1077            FT_Get_CMap_Format( cur[0] ) == 14                  )
1078       {
1079 #ifdef FT_MAX_CHARMAP_CACHEABLE
1080         if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1081         {
1082           FT_ERROR(( "find_unicode_charmap: UVS cmap is found "
1083                      "at too late position (%d)\n", cur - first ));
1084           continue;
1085         }
1086 #endif
1087         return cur[0];
1088       }
1089     }
1090
1091     return NULL;
1092   }
1093
1094
1095   /*************************************************************************/
1096   /*                                                                       */
1097   /* <Function>                                                            */
1098   /*    open_face                                                          */
1099   /*                                                                       */
1100   /* <Description>                                                         */
1101   /*    This function does some work for FT_Open_Face().                   */
1102   /*                                                                       */
1103   static FT_Error
1104   open_face( FT_Driver      driver,
1105              FT_Stream      stream,
1106              FT_Long        face_index,
1107              FT_Int         num_params,
1108              FT_Parameter*  params,
1109              FT_Face       *aface )
1110   {
1111     FT_Memory         memory;
1112     FT_Driver_Class   clazz;
1113     FT_Face           face = 0;
1114     FT_Error          error, error2;
1115     FT_Face_Internal  internal = NULL;
1116
1117
1118     clazz  = driver->clazz;
1119     memory = driver->root.memory;
1120
1121     /* allocate the face object and perform basic initialization */
1122     if ( FT_ALLOC( face, clazz->face_object_size ) )
1123       goto Fail;
1124
1125     if ( FT_NEW( internal ) )
1126       goto Fail;
1127
1128     face->internal = internal;
1129
1130     face->driver   = driver;
1131     face->memory   = memory;
1132     face->stream   = stream;
1133
1134 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1135     {
1136       int  i;
1137
1138
1139       face->internal->incremental_interface = 0;
1140       for ( i = 0; i < num_params && !face->internal->incremental_interface;
1141             i++ )
1142         if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
1143           face->internal->incremental_interface =
1144             (FT_Incremental_Interface)params[i].data;
1145     }
1146 #endif
1147
1148     if ( clazz->init_face )
1149       error = clazz->init_face( stream,
1150                                 face,
1151                                 (FT_Int)face_index,
1152                                 num_params,
1153                                 params );
1154     if ( error )
1155       goto Fail;
1156
1157     /* select Unicode charmap by default */
1158     error2 = find_unicode_charmap( face );
1159
1160     /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
1161     /* is returned.                                                      */
1162
1163     /* no error should happen, but we want to play safe */
1164     if ( error2 && error2 != FT_Err_Invalid_CharMap_Handle )
1165     {
1166       error = error2;
1167       goto Fail;
1168     }
1169
1170     *aface = face;
1171
1172   Fail:
1173     if ( error )
1174     {
1175       destroy_charmaps( face, memory );
1176       if ( clazz->done_face )
1177         clazz->done_face( face );
1178       FT_FREE( internal );
1179       FT_FREE( face );
1180       *aface = 0;
1181     }
1182
1183     return error;
1184   }
1185
1186
1187   /* there's a Mac-specific extended implementation of FT_New_Face() */
1188   /* in src/base/ftmac.c                                             */
1189
1190 #if !defined( FT_MACINTOSH ) || defined( DARWIN_NO_CARBON )
1191
1192   /* documentation is in freetype.h */
1193
1194   FT_EXPORT_DEF( FT_Error )
1195   FT_New_Face( FT_Library   library,
1196                const char*  pathname,
1197                FT_Long      face_index,
1198                FT_Face     *aface )
1199   {
1200     FT_Open_Args  args;
1201
1202
1203     /* test for valid `library' and `aface' delayed to FT_Open_Face() */
1204     if ( !pathname )
1205       return FT_Err_Invalid_Argument;
1206
1207     args.flags    = FT_OPEN_PATHNAME;
1208     args.pathname = (char*)pathname;
1209     args.stream   = NULL;
1210
1211     return FT_Open_Face( library, &args, face_index, aface );
1212   }
1213
1214 #endif  /* defined( FT_MACINTOSH ) && !defined( DARWIN_NO_CARBON ) */
1215
1216
1217   /* documentation is in freetype.h */
1218
1219   FT_EXPORT_DEF( FT_Error )
1220   FT_New_Memory_Face( FT_Library      library,
1221                       const FT_Byte*  file_base,
1222                       FT_Long         file_size,
1223                       FT_Long         face_index,
1224                       FT_Face        *aface )
1225   {
1226     FT_Open_Args  args;
1227
1228
1229     /* test for valid `library' and `face' delayed to FT_Open_Face() */
1230     if ( !file_base )
1231       return FT_Err_Invalid_Argument;
1232
1233     args.flags       = FT_OPEN_MEMORY;
1234     args.memory_base = file_base;
1235     args.memory_size = file_size;
1236     args.stream      = NULL;
1237
1238     return FT_Open_Face( library, &args, face_index, aface );
1239   }
1240
1241
1242 #ifdef FT_CONFIG_OPTION_MAC_FONTS
1243
1244   /* The behavior here is very similar to that in base/ftmac.c, but it     */
1245   /* is designed to work on non-mac systems, so no mac specific calls.     */
1246   /*                                                                       */
1247   /* We look at the file and determine if it is a mac dfont file or a mac  */
1248   /* resource file, or a macbinary file containing a mac resource file.    */
1249   /*                                                                       */
1250   /* Unlike ftmac I'm not going to look at a `FOND'.  I don't really see   */
1251   /* the point, especially since there may be multiple `FOND' resources.   */
1252   /* Instead I'll just look for `sfnt' and `POST' resources, ordered as    */
1253   /* they occur in the file.                                               */
1254   /*                                                                       */
1255   /* Note that multiple `POST' resources do not mean multiple postscript   */
1256   /* fonts; they all get jammed together to make what is essentially a     */
1257   /* pfb file.                                                             */
1258   /*                                                                       */
1259   /* We aren't interested in `NFNT' or `FONT' bitmap resources.            */
1260   /*                                                                       */
1261   /* As soon as we get an `sfnt' load it into memory and pass it off to    */
1262   /* FT_Open_Face.                                                         */
1263   /*                                                                       */
1264   /* If we have a (set of) `POST' resources, massage them into a (memory)  */
1265   /* pfb file and pass that to FT_Open_Face.  (As with ftmac.c I'm not     */
1266   /* going to try to save the kerning info.  After all that lives in the   */
1267   /* `FOND' which isn't in the file containing the `POST' resources so     */
1268   /* we don't really have access to it.                                    */
1269
1270
1271   /* Finalizer for a memory stream; gets called by FT_Done_Face(). */
1272   /* It frees the memory it uses.                                  */
1273   /* From ftmac.c.                                                 */
1274   static void
1275   memory_stream_close( FT_Stream  stream )
1276   {
1277     FT_Memory  memory = stream->memory;
1278
1279
1280     FT_FREE( stream->base );
1281
1282     stream->size  = 0;
1283     stream->base  = 0;
1284     stream->close = 0;
1285   }
1286
1287
1288   /* Create a new memory stream from a buffer and a size. */
1289   /* From ftmac.c.                                        */
1290   static FT_Error
1291   new_memory_stream( FT_Library           library,
1292                      FT_Byte*             base,
1293                      FT_ULong             size,
1294                      FT_Stream_CloseFunc  close,
1295                      FT_Stream           *astream )
1296   {
1297     FT_Error   error;
1298     FT_Memory  memory;
1299     FT_Stream  stream = NULL;
1300
1301
1302     if ( !library )
1303       return FT_Err_Invalid_Library_Handle;
1304
1305     if ( !base )
1306       return FT_Err_Invalid_Argument;
1307
1308     *astream = 0;
1309     memory = library->memory;
1310     if ( FT_NEW( stream ) )
1311       goto Exit;
1312
1313     FT_Stream_OpenMemory( stream, base, size );
1314
1315     stream->close = close;
1316
1317     *astream = stream;
1318
1319   Exit:
1320     return error;
1321   }
1322
1323
1324   /* Create a new FT_Face given a buffer and a driver name. */
1325   /* from ftmac.c */
1326   FT_LOCAL_DEF( FT_Error )
1327   open_face_from_buffer( FT_Library   library,
1328                          FT_Byte*     base,
1329                          FT_ULong     size,
1330                          FT_Long      face_index,
1331                          const char*  driver_name,
1332                          FT_Face     *aface )
1333   {
1334     FT_Open_Args  args;
1335     FT_Error      error;
1336     FT_Stream     stream = NULL;
1337     FT_Memory     memory = library->memory;
1338
1339
1340     error = new_memory_stream( library,
1341                                base,
1342                                size,
1343                                memory_stream_close,
1344                                &stream );
1345     if ( error )
1346     {
1347       FT_FREE( base );
1348       return error;
1349     }
1350
1351     args.flags = FT_OPEN_STREAM;
1352     args.stream = stream;
1353     if ( driver_name )
1354     {
1355       args.flags = args.flags | FT_OPEN_DRIVER;
1356       args.driver = FT_Get_Module( library, driver_name );
1357     }
1358
1359 #ifdef FT_MACINTOSH
1360     /* At this point, face_index has served its purpose;      */
1361     /* whoever calls this function has already used it to     */
1362     /* locate the correct font data.  We should not propagate */
1363     /* this index to FT_Open_Face() (unless it is negative).  */
1364
1365     if ( face_index > 0 )
1366       face_index = 0;
1367 #endif
1368
1369     error = FT_Open_Face( library, &args, face_index, aface );
1370
1371     if ( error == FT_Err_Ok )
1372       (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
1373     else
1374 #ifdef FT_MACINTOSH
1375       FT_Stream_Free( stream, 0 );
1376 #else
1377     {
1378       FT_Stream_Close( stream );
1379       FT_FREE( stream );
1380     }
1381 #endif
1382
1383     return error;
1384   }
1385
1386
1387   /* Look up `TYP1' or `CID ' table from sfnt table directory.       */
1388   /* `offset' and `length' must exclude the binary header in tables. */
1389
1390   /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
1391   /* format too.  Here, since we can't expect that the TrueType font */
1392   /* driver is loaded unconditially, we must parse the font by       */
1393   /* ourselves.  We are only interested in the name of the table and */
1394   /* the offset.                                                     */
1395
1396   static FT_Error
1397   ft_lookup_PS_in_sfnt_stream( FT_Stream  stream,
1398                                FT_Long    face_index,
1399                                FT_ULong*  offset,
1400                                FT_ULong*  length,
1401                                FT_Bool*   is_sfnt_cid )
1402   {
1403     FT_Error   error;
1404     FT_UShort  numTables;
1405     FT_Long    pstable_index;
1406     FT_ULong   tag;
1407     int        i;
1408
1409
1410     *offset = 0;
1411     *length = 0;
1412     *is_sfnt_cid = FALSE;
1413
1414     /* TODO: support for sfnt-wrapped PS/CID in TTC format */
1415
1416     /* version check for 'typ1' (should be ignored?) */
1417     if ( FT_READ_ULONG( tag ) )
1418       return error;
1419     if ( tag != TTAG_typ1 )
1420       return FT_Err_Unknown_File_Format;
1421
1422     if ( FT_READ_USHORT( numTables ) )
1423       return error;
1424     if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
1425       return error;
1426
1427     pstable_index = -1;
1428     *is_sfnt_cid  = FALSE;
1429
1430     for ( i = 0; i < numTables; i++ )
1431     {
1432       if ( FT_READ_ULONG( tag )     || FT_STREAM_SKIP( 4 )      ||
1433            FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
1434         return error;
1435
1436       if ( tag == TTAG_CID )
1437       {
1438         pstable_index++;
1439         *offset += 22;
1440         *length -= 22;
1441         *is_sfnt_cid = TRUE;
1442         if ( face_index < 0 )
1443           return FT_Err_Ok;
1444       }
1445       else if ( tag == TTAG_TYP1 )
1446       {
1447         pstable_index++;
1448         *offset += 24;
1449         *length -= 24;
1450         *is_sfnt_cid = FALSE;
1451         if ( face_index < 0 )
1452           return FT_Err_Ok;
1453       }
1454       if ( face_index >= 0 && pstable_index == face_index )
1455         return FT_Err_Ok;
1456     }
1457     return FT_Err_Table_Missing;
1458   }
1459
1460
1461   FT_LOCAL_DEF( FT_Error )
1462   open_face_PS_from_sfnt_stream( FT_Library     library,
1463                                  FT_Stream      stream,
1464                                  FT_Long        face_index,
1465                                  FT_Int         num_params,
1466                                  FT_Parameter  *params,
1467                                  FT_Face       *aface )
1468   {
1469     FT_Error   error;
1470     FT_Memory  memory = library->memory;
1471     FT_ULong   offset, length;
1472     FT_Long    pos;
1473     FT_Bool    is_sfnt_cid;
1474     FT_Byte*   sfnt_ps = NULL;
1475
1476     FT_UNUSED( num_params );
1477     FT_UNUSED( params );
1478
1479
1480     pos = FT_Stream_Pos( stream );
1481
1482     error = ft_lookup_PS_in_sfnt_stream( stream,
1483                                          face_index,
1484                                          &offset,
1485                                          &length,
1486                                          &is_sfnt_cid );
1487     if ( error )
1488       goto Exit;
1489
1490     if ( FT_Stream_Seek( stream, pos + offset ) )
1491       goto Exit;
1492
1493     if ( FT_ALLOC( sfnt_ps, (FT_Long)length ) )
1494       goto Exit;
1495
1496     error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
1497     if ( error )
1498       goto Exit;
1499
1500     error = open_face_from_buffer( library,
1501                                    sfnt_ps,
1502                                    length,
1503                                    face_index < 0 ? face_index : 0,
1504                                    is_sfnt_cid ? "cid" : "type1",
1505                                    aface );
1506   Exit:
1507     {
1508       FT_Error  error1;
1509
1510
1511       if ( error == FT_Err_Unknown_File_Format )
1512       {
1513         error1 = FT_Stream_Seek( stream, pos );
1514         if ( error1 )
1515           return error1;
1516       }
1517
1518       return error;
1519     }
1520   }
1521
1522
1523 #if !defined( FT_MACINTOSH ) || defined( DARWIN_NO_CARBON )
1524
1525   /* The resource header says we've got resource_cnt `POST' (type1) */
1526   /* resources in this file.  They all need to be coalesced into    */
1527   /* one lump which gets passed on to the type1 driver.             */
1528   /* Here can be only one PostScript font in a file so face_index   */
1529   /* must be 0 (or -1).                                             */
1530   /*                                                                */
1531   static FT_Error
1532   Mac_Read_POST_Resource( FT_Library  library,
1533                           FT_Stream   stream,
1534                           FT_Long    *offsets,
1535                           FT_Long     resource_cnt,
1536                           FT_Long     face_index,
1537                           FT_Face    *aface )
1538   {
1539     FT_Error   error  = FT_Err_Cannot_Open_Resource;
1540     FT_Memory  memory = library->memory;
1541     FT_Byte*   pfb_data = NULL;
1542     int        i, type, flags;
1543     FT_Long    len;
1544     FT_Long    pfb_len, pfb_pos, pfb_lenpos;
1545     FT_Long    rlen, temp;
1546
1547
1548     if ( face_index == -1 )
1549       face_index = 0;
1550     if ( face_index != 0 )
1551       return error;
1552
1553     /* Find the length of all the POST resources, concatenated.  Assume */
1554     /* worst case (each resource in its own section).                   */
1555     pfb_len = 0;
1556     for ( i = 0; i < resource_cnt; ++i )
1557     {
1558       error = FT_Stream_Seek( stream, offsets[i] );
1559       if ( error )
1560         goto Exit;
1561       if ( FT_READ_LONG( temp ) )
1562         goto Exit;
1563       pfb_len += temp + 6;
1564     }
1565
1566     if ( FT_ALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
1567       goto Exit;
1568
1569     pfb_data[0] = 0x80;
1570     pfb_data[1] = 1;            /* Ascii section */
1571     pfb_data[2] = 0;            /* 4-byte length, fill in later */
1572     pfb_data[3] = 0;
1573     pfb_data[4] = 0;
1574     pfb_data[5] = 0;
1575     pfb_pos     = 6;
1576     pfb_lenpos  = 2;
1577
1578     len = 0;
1579     type = 1;
1580     for ( i = 0; i < resource_cnt; ++i )
1581     {
1582       error = FT_Stream_Seek( stream, offsets[i] );
1583       if ( error )
1584         goto Exit2;
1585       if ( FT_READ_LONG( rlen ) )
1586         goto Exit;
1587       if ( FT_READ_USHORT( flags ) )
1588         goto Exit;
1589       FT_TRACE3(( "POST fragment[%d]: offsets=0x%08x, rlen=0x%08x, flags=0x%04x\n",
1590                    i, offsets[i], rlen, flags ));
1591
1592       /* postpone the check of rlen longer than buffer until FT_Stream_Read() */
1593       if ( ( flags >> 8 ) == 0 )        /* Comment, should not be loaded */
1594         continue;
1595
1596       /* the flags are part of the resource, so rlen >= 2.  */
1597       /* but some fonts declare rlen = 0 for empty fragment */
1598       if ( rlen > 2 )
1599         rlen -= 2;
1600       else
1601         rlen = 0;
1602
1603       if ( ( flags >> 8 ) == type )
1604         len += rlen;
1605       else
1606       {
1607         if ( pfb_lenpos + 3 > pfb_len + 2 )
1608           goto Exit2;
1609         pfb_data[pfb_lenpos    ] = (FT_Byte)( len );
1610         pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1611         pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1612         pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1613
1614         if ( ( flags >> 8 ) == 5 )      /* End of font mark */
1615           break;
1616
1617         if ( pfb_pos + 6 > pfb_len + 2 )
1618           goto Exit2;
1619         pfb_data[pfb_pos++] = 0x80;
1620
1621         type = flags >> 8;
1622         len = rlen;
1623
1624         pfb_data[pfb_pos++] = (FT_Byte)type;
1625         pfb_lenpos          = pfb_pos;
1626         pfb_data[pfb_pos++] = 0;        /* 4-byte length, fill in later */
1627         pfb_data[pfb_pos++] = 0;
1628         pfb_data[pfb_pos++] = 0;
1629         pfb_data[pfb_pos++] = 0;
1630       }
1631
1632       error = FT_Err_Cannot_Open_Resource;
1633       if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
1634         goto Exit2;
1635
1636       error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
1637       if ( error )
1638         goto Exit2;
1639       pfb_pos += rlen;
1640     }
1641
1642     if ( pfb_pos + 2 > pfb_len + 2 )
1643       goto Exit2;
1644     pfb_data[pfb_pos++] = 0x80;
1645     pfb_data[pfb_pos++] = 3;
1646
1647     if ( pfb_lenpos + 3 > pfb_len + 2 )
1648       goto Exit2;
1649     pfb_data[pfb_lenpos    ] = (FT_Byte)( len );
1650     pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1651     pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1652     pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1653
1654     return open_face_from_buffer( library,
1655                                   pfb_data,
1656                                   pfb_pos,
1657                                   face_index,
1658                                   "type1",
1659                                   aface );
1660
1661   Exit2:
1662     FT_FREE( pfb_data );
1663
1664   Exit:
1665     return error;
1666   }
1667
1668
1669   /* The resource header says we've got resource_cnt `sfnt'      */
1670   /* (TrueType/OpenType) resources in this file.  Look through   */
1671   /* them for the one indicated by face_index, load it into mem, */
1672   /* pass it on the the truetype driver and return it.           */
1673   /*                                                             */
1674   static FT_Error
1675   Mac_Read_sfnt_Resource( FT_Library  library,
1676                           FT_Stream   stream,
1677                           FT_Long    *offsets,
1678                           FT_Long     resource_cnt,
1679                           FT_Long     face_index,
1680                           FT_Face    *aface )
1681   {
1682     FT_Memory  memory = library->memory;
1683     FT_Byte*   sfnt_data = NULL;
1684     FT_Error   error;
1685     FT_Long    flag_offset;
1686     FT_Long    rlen;
1687     int        is_cff;
1688     FT_Long    face_index_in_resource = 0;
1689
1690
1691     if ( face_index == -1 )
1692       face_index = 0;
1693     if ( face_index >= resource_cnt )
1694       return FT_Err_Cannot_Open_Resource;
1695
1696     flag_offset = offsets[face_index];
1697     error = FT_Stream_Seek( stream, flag_offset );
1698     if ( error )
1699       goto Exit;
1700
1701     if ( FT_READ_LONG( rlen ) )
1702       goto Exit;
1703     if ( rlen == -1 )
1704       return FT_Err_Cannot_Open_Resource;
1705
1706     error = open_face_PS_from_sfnt_stream( library,
1707                                            stream,
1708                                            face_index,
1709                                            0, NULL,
1710                                            aface );
1711     if ( !error )
1712       goto Exit;
1713
1714     /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
1715     if ( FT_Stream_Seek( stream, flag_offset + 4 ) )
1716       goto Exit;
1717
1718     if ( FT_ALLOC( sfnt_data, (FT_Long)rlen ) )
1719       return error;
1720     error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, rlen );
1721     if ( error )
1722       goto Exit;
1723
1724     is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1725     error = open_face_from_buffer( library,
1726                                    sfnt_data,
1727                                    rlen,
1728                                    face_index_in_resource,
1729                                    is_cff ? "cff" : "truetype",
1730                                    aface );
1731
1732   Exit:
1733     return error;
1734   }
1735
1736
1737   /* Check for a valid resource fork header, or a valid dfont    */
1738   /* header.  In a resource fork the first 16 bytes are repeated */
1739   /* at the location specified by bytes 4-7.  In a dfont bytes   */
1740   /* 4-7 point to 16 bytes of zeroes instead.                    */
1741   /*                                                             */
1742   static FT_Error
1743   IsMacResource( FT_Library  library,
1744                  FT_Stream   stream,
1745                  FT_Long     resource_offset,
1746                  FT_Long     face_index,
1747                  FT_Face    *aface )
1748   {
1749     FT_Memory  memory = library->memory;
1750     FT_Error   error;
1751     FT_Long    map_offset, rdara_pos;
1752     FT_Long    *data_offsets;
1753     FT_Long    count;
1754
1755
1756     error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
1757                                        &map_offset, &rdara_pos );
1758     if ( error )
1759       return error;
1760
1761     error = FT_Raccess_Get_DataOffsets( library, stream,
1762                                         map_offset, rdara_pos,
1763                                         TTAG_POST,
1764                                         &data_offsets, &count );
1765     if ( !error )
1766     {
1767       error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
1768                                       face_index, aface );
1769       FT_FREE( data_offsets );
1770       /* POST exists in an LWFN providing a single face */
1771       if ( !error )
1772         (*aface)->num_faces = 1;
1773       return error;
1774     }
1775
1776     error = FT_Raccess_Get_DataOffsets( library, stream,
1777                                         map_offset, rdara_pos,
1778                                         TTAG_sfnt,
1779                                         &data_offsets, &count );
1780     if ( !error )
1781     {
1782       FT_Long  face_index_internal = face_index % count;
1783
1784
1785       error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
1786                                       face_index_internal, aface );
1787       FT_FREE( data_offsets );
1788       if ( !error )
1789         (*aface)->num_faces = count;
1790     }
1791
1792     return error;
1793   }
1794
1795
1796   /* Check for a valid macbinary header, and if we find one   */
1797   /* check that the (flattened) resource fork in it is valid. */
1798   /*                                                          */
1799   static FT_Error
1800   IsMacBinary( FT_Library  library,
1801                FT_Stream   stream,
1802                FT_Long     face_index,
1803                FT_Face    *aface )
1804   {
1805     unsigned char  header[128];
1806     FT_Error       error;
1807     FT_Long        dlen, offset;
1808
1809
1810     if ( NULL == stream )
1811       return FT_Err_Invalid_Stream_Operation;
1812
1813     error = FT_Stream_Seek( stream, 0 );
1814     if ( error )
1815       goto Exit;
1816
1817     error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
1818     if ( error )
1819       goto Exit;
1820
1821     if (            header[ 0] !=  0 ||
1822                     header[74] !=  0 ||
1823                     header[82] !=  0 ||
1824                     header[ 1] ==  0 ||
1825                     header[ 1] >  33 ||
1826                     header[63] !=  0 ||
1827          header[2 + header[1]] !=  0 )
1828       return FT_Err_Unknown_File_Format;
1829
1830     dlen = ( header[0x53] << 24 ) |
1831            ( header[0x54] << 16 ) |
1832            ( header[0x55] <<  8 ) |
1833              header[0x56];
1834 #if 0
1835     rlen = ( header[0x57] << 24 ) |
1836            ( header[0x58] << 16 ) |
1837            ( header[0x59] <<  8 ) |
1838              header[0x5a];
1839 #endif /* 0 */
1840     offset = 128 + ( ( dlen + 127 ) & ~127 );
1841
1842     return IsMacResource( library, stream, offset, face_index, aface );
1843
1844   Exit:
1845     return error;
1846   }
1847
1848
1849   static FT_Error
1850   load_face_in_embedded_rfork( FT_Library           library,
1851                                FT_Stream            stream,
1852                                FT_Long              face_index,
1853                                FT_Face             *aface,
1854                                const FT_Open_Args  *args )
1855   {
1856
1857 #undef  FT_COMPONENT
1858 #define FT_COMPONENT  trace_raccess
1859
1860     FT_Memory  memory = library->memory;
1861     FT_Error   error  = FT_Err_Unknown_File_Format;
1862     int        i;
1863
1864     char *     file_names[FT_RACCESS_N_RULES];
1865     FT_Long    offsets[FT_RACCESS_N_RULES];
1866     FT_Error   errors[FT_RACCESS_N_RULES];
1867     FT_Bool    is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */
1868
1869     FT_Open_Args  args2;
1870     FT_Stream     stream2 = 0;
1871
1872
1873     FT_Raccess_Guess( library, stream,
1874                       args->pathname, file_names, offsets, errors );
1875
1876     for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
1877     {
1878       is_darwin_vfs = raccess_rule_by_darwin_vfs( i );
1879       if ( is_darwin_vfs && vfs_rfork_has_no_font )
1880       {
1881         FT_TRACE3(( "Skip rule %d: darwin vfs resource fork"
1882                     " is already checked and"
1883                     " no font is found\n", i ));
1884         continue;
1885       }
1886
1887       if ( errors[i] )
1888       {
1889         FT_TRACE3(( "Error[%d] has occurred in rule %d\n", errors[i], i ));
1890         continue;
1891       }
1892
1893       args2.flags    = FT_OPEN_PATHNAME;
1894       args2.pathname = file_names[i] ? file_names[i] : args->pathname;
1895
1896       FT_TRACE3(( "Try rule %d: %s (offset=%d) ...",
1897                   i, args2.pathname, offsets[i] ));
1898
1899       error = FT_Stream_New( library, &args2, &stream2 );
1900       if ( is_darwin_vfs && error == FT_Err_Cannot_Open_Stream )
1901         vfs_rfork_has_no_font = TRUE;
1902
1903       if ( error )
1904       {
1905         FT_TRACE3(( "failed\n" ));
1906         continue;
1907       }
1908
1909       error = IsMacResource( library, stream2, offsets[i],
1910                              face_index, aface );
1911       FT_Stream_Free( stream2, 0 );
1912
1913       FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
1914
1915       if ( !error )
1916           break;
1917       else if ( is_darwin_vfs )
1918           vfs_rfork_has_no_font = TRUE;
1919     }
1920
1921     for (i = 0; i < FT_RACCESS_N_RULES; i++)
1922     {
1923       if ( file_names[i] )
1924         FT_FREE( file_names[i] );
1925     }
1926
1927     /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
1928     if ( error )
1929       error = FT_Err_Unknown_File_Format;
1930
1931     return error;
1932
1933 #undef  FT_COMPONENT
1934 #define FT_COMPONENT  trace_objs
1935
1936   }
1937
1938
1939   /* Check for some macintosh formats without Carbon framework.    */
1940   /* Is this a macbinary file?  If so look at the resource fork.   */
1941   /* Is this a mac dfont file?                                     */
1942   /* Is this an old style resource fork? (in data)                 */
1943   /* Else call load_face_in_embedded_rfork to try extra rules      */
1944   /* (defined in `ftrfork.c').                                     */
1945   /*                                                               */
1946   static FT_Error
1947   load_mac_face( FT_Library           library,
1948                  FT_Stream            stream,
1949                  FT_Long              face_index,
1950                  FT_Face             *aface,
1951                  const FT_Open_Args  *args )
1952   {
1953     FT_Error error;
1954     FT_UNUSED( args );
1955
1956
1957     error = IsMacBinary( library, stream, face_index, aface );
1958     if ( FT_ERROR_BASE( error ) == FT_Err_Unknown_File_Format )
1959     {
1960
1961 #undef  FT_COMPONENT
1962 #define FT_COMPONENT  trace_raccess
1963
1964       FT_TRACE3(( "Try as dfont: %s ...", args->pathname ));
1965
1966       error = IsMacResource( library, stream, 0, face_index, aface );
1967
1968       FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
1969
1970 #undef  FT_COMPONENT
1971 #define FT_COMPONENT  trace_objs
1972
1973     }
1974
1975     if ( ( FT_ERROR_BASE( error ) == FT_Err_Unknown_File_Format      ||
1976            FT_ERROR_BASE( error ) == FT_Err_Invalid_Stream_Operation ) &&
1977          ( args->flags & FT_OPEN_PATHNAME )                            )
1978       error = load_face_in_embedded_rfork( library, stream,
1979                                            face_index, aface, args );
1980     return error;
1981   }
1982 #endif
1983
1984 #endif  /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
1985
1986
1987   /* documentation is in freetype.h */
1988
1989   FT_EXPORT_DEF( FT_Error )
1990   FT_Open_Face( FT_Library           library,
1991                 const FT_Open_Args*  args,
1992                 FT_Long              face_index,
1993                 FT_Face             *aface )
1994   {
1995     FT_Error     error;
1996     FT_Driver    driver;
1997     FT_Memory    memory;
1998     FT_Stream    stream = NULL;
1999     FT_Face      face   = NULL;
2000     FT_ListNode  node   = NULL;
2001     FT_Bool      external_stream;
2002     FT_Module*   cur;
2003     FT_Module*   limit;
2004
2005
2006     /* test for valid `library' delayed to */
2007     /* FT_Stream_New()                     */
2008
2009     if ( ( !aface && face_index >= 0 ) || !args )
2010       return FT_Err_Invalid_Argument;
2011
2012     external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
2013                                args->stream                     );
2014
2015     /* create input stream */
2016     error = FT_Stream_New( library, args, &stream );
2017     if ( error )
2018       goto Fail3;
2019
2020     memory = library->memory;
2021
2022     /* If the font driver is specified in the `args' structure, use */
2023     /* it.  Otherwise, we scan the list of registered drivers.      */
2024     if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
2025     {
2026       driver = FT_DRIVER( args->driver );
2027
2028       /* not all modules are drivers, so check... */
2029       if ( FT_MODULE_IS_DRIVER( driver ) )
2030       {
2031         FT_Int         num_params = 0;
2032         FT_Parameter*  params     = 0;
2033
2034
2035         if ( args->flags & FT_OPEN_PARAMS )
2036         {
2037           num_params = args->num_params;
2038           params     = args->params;
2039         }
2040
2041         error = open_face( driver, stream, face_index,
2042                            num_params, params, &face );
2043         if ( !error )
2044           goto Success;
2045       }
2046       else
2047         error = FT_Err_Invalid_Handle;
2048
2049       FT_Stream_Free( stream, external_stream );
2050       goto Fail;
2051     }
2052     else
2053     {
2054       /* check each font driver for an appropriate format */
2055       cur   = library->modules;
2056       limit = cur + library->num_modules;
2057
2058
2059       for ( ; cur < limit; cur++ )
2060       {
2061         /* not all modules are font drivers, so check... */
2062         if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2063         {
2064           FT_Int         num_params = 0;
2065           FT_Parameter*  params     = 0;
2066
2067
2068           driver = FT_DRIVER( cur[0] );
2069
2070           if ( args->flags & FT_OPEN_PARAMS )
2071           {
2072             num_params = args->num_params;
2073             params     = args->params;
2074           }
2075
2076           error = open_face( driver, stream, face_index,
2077                              num_params, params, &face );
2078           if ( !error )
2079             goto Success;
2080
2081 #ifdef FT_CONFIG_OPTION_MAC_FONTS
2082           if ( ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2083                FT_ERROR_BASE( error ) == FT_Err_Table_Missing           )
2084           {
2085             /* TrueType but essential tables are missing */
2086             if ( FT_Stream_Seek( stream, 0 ) )
2087               break;
2088
2089             error = open_face_PS_from_sfnt_stream( library,
2090                                                    stream,
2091                                                    face_index,
2092                                                    num_params,
2093                                                    params,
2094                                                    aface );
2095             if ( !error )
2096             {
2097               FT_Stream_Free( stream, external_stream );
2098               return error;
2099             }
2100           }
2101 #endif
2102
2103           if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
2104             goto Fail3;
2105         }
2106       }
2107
2108   Fail3:
2109     /* If we are on the mac, and we get an FT_Err_Invalid_Stream_Operation */
2110     /* it may be because we have an empty data fork, so we need to check   */
2111     /* the resource fork.                                                  */
2112     if ( FT_ERROR_BASE( error ) != FT_Err_Cannot_Open_Stream       &&
2113          FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format      &&
2114          FT_ERROR_BASE( error ) != FT_Err_Invalid_Stream_Operation )
2115       goto Fail2;
2116
2117 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2118     error = load_mac_face( library, stream, face_index, aface, args );
2119     if ( !error )
2120     {
2121       /* We don't want to go to Success here.  We've already done that. */
2122       /* On the other hand, if we succeeded we still need to close this */
2123       /* stream (we opened a different stream which extracted the       */
2124       /* interesting information out of this stream here.  That stream  */
2125       /* will still be open and the face will point to it).             */
2126       FT_Stream_Free( stream, external_stream );
2127       return error;
2128     }
2129
2130     if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
2131       goto Fail2;
2132 #endif  /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2133
2134       /* no driver is able to handle this format */
2135       error = FT_Err_Unknown_File_Format;
2136
2137   Fail2:
2138       FT_Stream_Free( stream, external_stream );
2139       goto Fail;
2140     }
2141
2142   Success:
2143     FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2144
2145     /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
2146     if ( external_stream )
2147       face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
2148
2149     /* add the face object to its driver's list */
2150     if ( FT_NEW( node ) )
2151       goto Fail;
2152
2153     node->data = face;
2154     /* don't assume driver is the same as face->driver, so use */
2155     /* face->driver instead.                                   */
2156     FT_List_Add( &face->driver->faces_list, node );
2157
2158     /* now allocate a glyph slot object for the face */
2159     FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2160
2161     if ( face_index >= 0 )
2162     {
2163       error = FT_New_GlyphSlot( face, NULL );
2164       if ( error )
2165         goto Fail;
2166
2167       /* finally, allocate a size object for the face */
2168       {
2169         FT_Size  size;
2170
2171
2172         FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2173
2174         error = FT_New_Size( face, &size );
2175         if ( error )
2176           goto Fail;
2177
2178         face->size = size;
2179       }
2180     }
2181
2182     /* some checks */
2183
2184     if ( FT_IS_SCALABLE( face ) )
2185     {
2186       if ( face->height < 0 )
2187         face->height = (FT_Short)-face->height;
2188
2189       if ( !FT_HAS_VERTICAL( face ) )
2190         face->max_advance_height = (FT_Short)face->height;
2191     }
2192
2193     if ( FT_HAS_FIXED_SIZES( face ) )
2194     {
2195       FT_Int  i;
2196
2197
2198       for ( i = 0; i < face->num_fixed_sizes; i++ )
2199       {
2200         FT_Bitmap_Size*  bsize = face->available_sizes + i;
2201
2202
2203         if ( bsize->height < 0 )
2204           bsize->height = (FT_Short)-bsize->height;
2205         if ( bsize->x_ppem < 0 )
2206           bsize->x_ppem = (FT_Short)-bsize->x_ppem;
2207         if ( bsize->y_ppem < 0 )
2208           bsize->y_ppem = -bsize->y_ppem;
2209       }
2210     }
2211
2212     /* initialize internal face data */
2213     {
2214       FT_Face_Internal  internal = face->internal;
2215
2216
2217       internal->transform_matrix.xx = 0x10000L;
2218       internal->transform_matrix.xy = 0;
2219       internal->transform_matrix.yx = 0;
2220       internal->transform_matrix.yy = 0x10000L;
2221
2222       internal->transform_delta.x = 0;
2223       internal->transform_delta.y = 0;
2224
2225       internal->refcount = 1;
2226     }
2227
2228     if ( aface )
2229       *aface = face;
2230     else
2231       FT_Done_Face( face );
2232
2233     goto Exit;
2234
2235   Fail:
2236     FT_Done_Face( face );
2237
2238   Exit:
2239     FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
2240
2241     return error;
2242   }
2243
2244
2245   /* documentation is in freetype.h */
2246
2247   FT_EXPORT_DEF( FT_Error )
2248   FT_Attach_File( FT_Face      face,
2249                   const char*  filepathname )
2250   {
2251     FT_Open_Args  open;
2252
2253
2254     /* test for valid `face' delayed to FT_Attach_Stream() */
2255
2256     if ( !filepathname )
2257       return FT_Err_Invalid_Argument;
2258
2259     open.stream   = NULL;
2260     open.flags    = FT_OPEN_PATHNAME;
2261     open.pathname = (char*)filepathname;
2262
2263     return FT_Attach_Stream( face, &open );
2264   }
2265
2266
2267   /* documentation is in freetype.h */
2268
2269   FT_EXPORT_DEF( FT_Error )
2270   FT_Attach_Stream( FT_Face        face,
2271                     FT_Open_Args*  parameters )
2272   {
2273     FT_Stream  stream;
2274     FT_Error   error;
2275     FT_Driver  driver;
2276
2277     FT_Driver_Class  clazz;
2278
2279
2280     /* test for valid `parameters' delayed to FT_Stream_New() */
2281
2282     if ( !face )
2283       return FT_Err_Invalid_Face_Handle;
2284
2285     driver = face->driver;
2286     if ( !driver )
2287       return FT_Err_Invalid_Driver_Handle;
2288
2289     error = FT_Stream_New( driver->root.library, parameters, &stream );
2290     if ( error )
2291       goto Exit;
2292
2293     /* we implement FT_Attach_Stream in each driver through the */
2294     /* `attach_file' interface                                  */
2295
2296     error = FT_Err_Unimplemented_Feature;
2297     clazz = driver->clazz;
2298     if ( clazz->attach_file )
2299       error = clazz->attach_file( face, stream );
2300
2301     /* close the attached stream */
2302     FT_Stream_Free( stream,
2303                     (FT_Bool)( parameters->stream &&
2304                                ( parameters->flags & FT_OPEN_STREAM ) ) );
2305
2306   Exit:
2307     return error;
2308   }
2309
2310
2311   /* documentation is in freetype.h */
2312
2313   FT_EXPORT_DEF( FT_Error )
2314   FT_Reference_Face( FT_Face  face )
2315   {
2316     face->internal->refcount++;
2317
2318     return FT_Err_Ok;
2319   }
2320
2321
2322   /* documentation is in freetype.h */
2323
2324   FT_EXPORT_DEF( FT_Error )
2325   FT_Done_Face( FT_Face  face )
2326   {
2327     FT_Error     error;
2328     FT_Driver    driver;
2329     FT_Memory    memory;
2330     FT_ListNode  node;
2331
2332
2333     error = FT_Err_Invalid_Face_Handle;
2334     if ( face && face->driver )
2335     {
2336       face->internal->refcount--;
2337       if ( face->internal->refcount > 0 )
2338         error = FT_Err_Ok;
2339       else
2340       {
2341         driver = face->driver;
2342         memory = driver->root.memory;
2343
2344         /* find face in driver's list */
2345         node = FT_List_Find( &driver->faces_list, face );
2346         if ( node )
2347         {
2348           /* remove face object from the driver's list */
2349           FT_List_Remove( &driver->faces_list, node );
2350           FT_FREE( node );
2351
2352           /* now destroy the object proper */
2353           destroy_face( memory, face, driver );
2354           error = FT_Err_Ok;
2355         }
2356       }
2357     }
2358
2359     return error;
2360   }
2361
2362
2363   /* documentation is in ftobjs.h */
2364
2365   FT_EXPORT_DEF( FT_Error )
2366   FT_New_Size( FT_Face   face,
2367                FT_Size  *asize )
2368   {
2369     FT_Error         error;
2370     FT_Memory        memory;
2371     FT_Driver        driver;
2372     FT_Driver_Class  clazz;
2373
2374     FT_Size          size = 0;
2375     FT_ListNode      node = 0;
2376
2377
2378     if ( !face )
2379       return FT_Err_Invalid_Face_Handle;
2380
2381     if ( !asize )
2382       return FT_Err_Invalid_Size_Handle;
2383
2384     if ( !face->driver )
2385       return FT_Err_Invalid_Driver_Handle;
2386
2387     *asize = 0;
2388
2389     driver = face->driver;
2390     clazz  = driver->clazz;
2391     memory = face->memory;
2392
2393     /* Allocate new size object and perform basic initialisation */
2394     if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
2395       goto Exit;
2396
2397     size->face = face;
2398
2399     /* for now, do not use any internal fields in size objects */
2400     size->internal = 0;
2401
2402     if ( clazz->init_size )
2403       error = clazz->init_size( size );
2404
2405     /* in case of success, add to the face's list */
2406     if ( !error )
2407     {
2408       *asize     = size;
2409       node->data = size;
2410       FT_List_Add( &face->sizes_list, node );
2411     }
2412
2413   Exit:
2414     if ( error )
2415     {
2416       FT_FREE( node );
2417       FT_FREE( size );
2418     }
2419
2420     return error;
2421   }
2422
2423
2424   /* documentation is in ftobjs.h */
2425
2426   FT_EXPORT_DEF( FT_Error )
2427   FT_Done_Size( FT_Size  size )
2428   {
2429     FT_Error     error;
2430     FT_Driver    driver;
2431     FT_Memory    memory;
2432     FT_Face      face;
2433     FT_ListNode  node;
2434
2435
2436     if ( !size )
2437       return FT_Err_Invalid_Size_Handle;
2438
2439     face = size->face;
2440     if ( !face )
2441       return FT_Err_Invalid_Face_Handle;
2442
2443     driver = face->driver;
2444     if ( !driver )
2445       return FT_Err_Invalid_Driver_Handle;
2446
2447     memory = driver->root.memory;
2448
2449     error = FT_Err_Ok;
2450     node  = FT_List_Find( &face->sizes_list, size );
2451     if ( node )
2452     {
2453       FT_List_Remove( &face->sizes_list, node );
2454       FT_FREE( node );
2455
2456       if ( face->size == size )
2457       {
2458         face->size = 0;
2459         if ( face->sizes_list.head )
2460           face->size = (FT_Size)(face->sizes_list.head->data);
2461       }
2462
2463       destroy_size( memory, size, driver );
2464     }
2465     else
2466       error = FT_Err_Invalid_Size_Handle;
2467
2468     return error;
2469   }
2470
2471
2472   /* documentation is in ftobjs.h */
2473
2474   FT_BASE_DEF( FT_Error )
2475   FT_Match_Size( FT_Face          face,
2476                  FT_Size_Request  req,
2477                  FT_Bool          ignore_width,
2478                  FT_ULong*        size_index )
2479   {
2480     FT_Int   i;
2481     FT_Long  w, h;
2482
2483
2484     if ( !FT_HAS_FIXED_SIZES( face ) )
2485       return FT_Err_Invalid_Face_Handle;
2486
2487     /* FT_Bitmap_Size doesn't provide enough info... */
2488     if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2489       return FT_Err_Unimplemented_Feature;
2490
2491     w = FT_REQUEST_WIDTH ( req );
2492     h = FT_REQUEST_HEIGHT( req );
2493
2494     if ( req->width && !req->height )
2495       h = w;
2496     else if ( !req->width && req->height )
2497       w = h;
2498
2499     w = FT_PIX_ROUND( w );
2500     h = FT_PIX_ROUND( h );
2501
2502     for ( i = 0; i < face->num_fixed_sizes; i++ )
2503     {
2504       FT_Bitmap_Size*  bsize = face->available_sizes + i;
2505
2506
2507       if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
2508         continue;
2509
2510       if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
2511       {
2512         if ( size_index )
2513           *size_index = (FT_ULong)i;
2514
2515         return FT_Err_Ok;
2516       }
2517     }
2518
2519     return FT_Err_Invalid_Pixel_Size;
2520   }
2521
2522
2523   /* documentation is in ftobjs.h */
2524
2525   FT_BASE_DEF( void )
2526   ft_synthesize_vertical_metrics( FT_Glyph_Metrics*  metrics,
2527                                   FT_Pos             advance )
2528   {
2529     FT_Pos  height = metrics->height;
2530
2531
2532     /* compensate for glyph with bbox above/below the baseline */
2533     if ( metrics->horiBearingY < 0 )
2534     {
2535       if ( height < metrics->horiBearingY )
2536         height = metrics->horiBearingY;
2537     }
2538     else if ( metrics->horiBearingY > 0 )
2539       height -= metrics->horiBearingY;
2540
2541     /* the factor 1.2 is a heuristical value */
2542     if ( !advance )
2543       advance = height * 12 / 10;
2544
2545     metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
2546     metrics->vertBearingY = ( advance - height ) / 2;
2547     metrics->vertAdvance  = advance;
2548   }
2549
2550
2551   static void
2552   ft_recompute_scaled_metrics( FT_Face           face,
2553                                FT_Size_Metrics*  metrics )
2554   {
2555     /* Compute root ascender, descender, test height, and max_advance */
2556
2557 #ifdef GRID_FIT_METRICS
2558     metrics->ascender    = FT_PIX_CEIL( FT_MulFix( face->ascender,
2559                                                    metrics->y_scale ) );
2560
2561     metrics->descender   = FT_PIX_FLOOR( FT_MulFix( face->descender,
2562                                                     metrics->y_scale ) );
2563
2564     metrics->height      = FT_PIX_ROUND( FT_MulFix( face->height,
2565                                                     metrics->y_scale ) );
2566
2567     metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
2568                                                     metrics->x_scale ) );
2569 #else /* !GRID_FIT_METRICS */
2570     metrics->ascender    = FT_MulFix( face->ascender,
2571                                       metrics->y_scale );
2572
2573     metrics->descender   = FT_MulFix( face->descender,
2574                                       metrics->y_scale );
2575
2576     metrics->height      = FT_MulFix( face->height,
2577                                       metrics->y_scale );
2578
2579     metrics->max_advance = FT_MulFix( face->max_advance_width,
2580                                       metrics->x_scale );
2581 #endif /* !GRID_FIT_METRICS */
2582   }
2583
2584
2585   FT_BASE_DEF( void )
2586   FT_Select_Metrics( FT_Face   face,
2587                      FT_ULong  strike_index )
2588   {
2589     FT_Size_Metrics*  metrics;
2590     FT_Bitmap_Size*   bsize;
2591
2592
2593     metrics = &face->size->metrics;
2594     bsize   = face->available_sizes + strike_index;
2595
2596     metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
2597     metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
2598
2599     if ( FT_IS_SCALABLE( face ) )
2600     {
2601       metrics->x_scale = FT_DivFix( bsize->x_ppem,
2602                                     face->units_per_EM );
2603       metrics->y_scale = FT_DivFix( bsize->y_ppem,
2604                                     face->units_per_EM );
2605
2606       ft_recompute_scaled_metrics( face, metrics );
2607     }
2608     else
2609     {
2610       metrics->x_scale     = 1L << 16;
2611       metrics->y_scale     = 1L << 16;
2612       metrics->ascender    = bsize->y_ppem;
2613       metrics->descender   = 0;
2614       metrics->height      = bsize->height << 6;
2615       metrics->max_advance = bsize->x_ppem;
2616     }
2617
2618     FT_TRACE5(( "FT_Select_Metrics:\n" ));
2619     FT_TRACE5(( "  x scale: %d (%f)\n",
2620                 metrics->x_scale, metrics->x_scale / 65536.0 ));
2621     FT_TRACE5(( "  y scale: %d (%f)\n",
2622                 metrics->y_scale, metrics->y_scale / 65536.0 ));
2623     FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
2624     FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
2625     FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
2626     FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
2627     FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
2628     FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
2629   }
2630
2631
2632   FT_BASE_DEF( void )
2633   FT_Request_Metrics( FT_Face          face,
2634                       FT_Size_Request  req )
2635   {
2636     FT_Size_Metrics*  metrics;
2637
2638
2639     metrics = &face->size->metrics;
2640
2641     if ( FT_IS_SCALABLE( face ) )
2642     {
2643       FT_Long  w = 0, h = 0, scaled_w = 0, scaled_h = 0;
2644
2645
2646       switch ( req->type )
2647       {
2648       case FT_SIZE_REQUEST_TYPE_NOMINAL:
2649         w = h = face->units_per_EM;
2650         break;
2651
2652       case FT_SIZE_REQUEST_TYPE_REAL_DIM:
2653         w = h = face->ascender - face->descender;
2654         break;
2655
2656       case FT_SIZE_REQUEST_TYPE_BBOX:
2657         w = face->bbox.xMax - face->bbox.xMin;
2658         h = face->bbox.yMax - face->bbox.yMin;
2659         break;
2660
2661       case FT_SIZE_REQUEST_TYPE_CELL:
2662         w = face->max_advance_width;
2663         h = face->ascender - face->descender;
2664         break;
2665
2666       case FT_SIZE_REQUEST_TYPE_SCALES:
2667         metrics->x_scale = (FT_Fixed)req->width;
2668         metrics->y_scale = (FT_Fixed)req->height;
2669         if ( !metrics->x_scale )
2670           metrics->x_scale = metrics->y_scale;
2671         else if ( !metrics->y_scale )
2672           metrics->y_scale = metrics->x_scale;
2673         goto Calculate_Ppem;
2674
2675       case FT_SIZE_REQUEST_TYPE_MAX:
2676         break;
2677       }
2678
2679       /* to be on the safe side */
2680       if ( w < 0 )
2681         w = -w;
2682
2683       if ( h < 0 )
2684         h = -h;
2685
2686       scaled_w = FT_REQUEST_WIDTH ( req );
2687       scaled_h = FT_REQUEST_HEIGHT( req );
2688
2689       /* determine scales */
2690       if ( req->width )
2691       {
2692         metrics->x_scale = FT_DivFix( scaled_w, w );
2693
2694         if ( req->height )
2695         {
2696           metrics->y_scale = FT_DivFix( scaled_h, h );
2697
2698           if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
2699           {
2700             if ( metrics->y_scale > metrics->x_scale )
2701               metrics->y_scale = metrics->x_scale;
2702             else
2703               metrics->x_scale = metrics->y_scale;
2704           }
2705         }
2706         else
2707         {
2708           metrics->y_scale = metrics->x_scale;
2709           scaled_h = FT_MulDiv( scaled_w, h, w );
2710         }
2711       }
2712       else
2713       {
2714         metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
2715         scaled_w = FT_MulDiv( scaled_h, w, h );
2716       }
2717
2718   Calculate_Ppem:
2719       /* calculate the ppems */
2720       if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2721       {
2722         scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
2723         scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
2724       }
2725
2726       metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 );
2727       metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 );
2728
2729       ft_recompute_scaled_metrics( face, metrics );
2730     }
2731     else
2732     {
2733       FT_ZERO( metrics );
2734       metrics->x_scale = 1L << 16;
2735       metrics->y_scale = 1L << 16;
2736     }
2737
2738     FT_TRACE5(( "FT_Request_Metrics:\n" ));
2739     FT_TRACE5(( "  x scale: %d (%f)\n",
2740                 metrics->x_scale, metrics->x_scale / 65536.0 ));
2741     FT_TRACE5(( "  y scale: %d (%f)\n",
2742                 metrics->y_scale, metrics->y_scale / 65536.0 ));
2743     FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
2744     FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
2745     FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
2746     FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
2747     FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
2748     FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
2749   }
2750
2751
2752   /* documentation is in freetype.h */
2753
2754   FT_EXPORT_DEF( FT_Error )
2755   FT_Select_Size( FT_Face  face,
2756                   FT_Int   strike_index )
2757   {
2758     FT_Driver_Class  clazz;
2759
2760
2761     if ( !face || !FT_HAS_FIXED_SIZES( face ) )
2762       return FT_Err_Invalid_Face_Handle;
2763
2764     if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
2765       return FT_Err_Invalid_Argument;
2766
2767     clazz = face->driver->clazz;
2768
2769     if ( clazz->select_size )
2770     {
2771       FT_Error  error;
2772
2773
2774       error = clazz->select_size( face->size, (FT_ULong)strike_index );
2775
2776 #ifdef FT_DEBUG_LEVEL_TRACE
2777       {
2778         FT_Size_Metrics*  metrics = &face->size->metrics;
2779
2780
2781         FT_TRACE5(( "FT_Select_Size (font driver's `select_size'):\n" ));
2782         FT_TRACE5(( "  x scale: %d (%f)\n",
2783                     metrics->x_scale, metrics->x_scale / 65536.0 ));
2784         FT_TRACE5(( "  y scale: %d (%f)\n",
2785                     metrics->y_scale, metrics->y_scale / 65536.0 ));
2786         FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
2787         FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
2788         FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
2789         FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
2790         FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
2791         FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
2792       }
2793 #endif
2794
2795       return error;
2796     }
2797
2798     FT_Select_Metrics( face, (FT_ULong)strike_index );
2799
2800     return FT_Err_Ok;
2801   }
2802
2803
2804   /* documentation is in freetype.h */
2805
2806   FT_EXPORT_DEF( FT_Error )
2807   FT_Request_Size( FT_Face          face,
2808                    FT_Size_Request  req )
2809   {
2810     FT_Driver_Class  clazz;
2811     FT_ULong         strike_index;
2812
2813
2814     if ( !face )
2815       return FT_Err_Invalid_Face_Handle;
2816
2817     if ( !req || req->width < 0 || req->height < 0 ||
2818          req->type >= FT_SIZE_REQUEST_TYPE_MAX )
2819       return FT_Err_Invalid_Argument;
2820
2821     clazz = face->driver->clazz;
2822
2823     if ( clazz->request_size )
2824     {
2825       FT_Error  error;
2826
2827
2828       error = clazz->request_size( face->size, req );
2829
2830 #ifdef FT_DEBUG_LEVEL_TRACE
2831       {
2832         FT_Size_Metrics*  metrics = &face->size->metrics;
2833
2834
2835         FT_TRACE5(( "FT_Request_Size (font driver's `request_size'):\n" ));
2836         FT_TRACE5(( "  x scale: %d (%f)\n",
2837                     metrics->x_scale, metrics->x_scale / 65536.0 ));
2838         FT_TRACE5(( "  y scale: %d (%f)\n",
2839                     metrics->y_scale, metrics->y_scale / 65536.0 ));
2840         FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
2841         FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
2842         FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
2843         FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
2844         FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
2845         FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
2846       }
2847 #endif
2848
2849       return error;
2850     }
2851
2852     /*
2853      * The reason that a driver doesn't have `request_size' defined is
2854      * either that the scaling here suffices or that the supported formats
2855      * are bitmap-only and size matching is not implemented.
2856      *
2857      * In the latter case, a simple size matching is done.
2858      */
2859     if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
2860     {
2861       FT_Error  error;
2862
2863
2864       error = FT_Match_Size( face, req, 0, &strike_index );
2865       if ( error )
2866         return error;
2867
2868       FT_TRACE3(( "FT_Request_Size: bitmap strike %lu matched\n",
2869                   strike_index ));
2870
2871       return FT_Select_Size( face, (FT_Int)strike_index );
2872     }
2873
2874     FT_Request_Metrics( face, req );
2875
2876     return FT_Err_Ok;
2877   }
2878
2879
2880   /* documentation is in freetype.h */
2881
2882   FT_EXPORT_DEF( FT_Error )
2883   FT_Set_Char_Size( FT_Face     face,
2884                     FT_F26Dot6  char_width,
2885                     FT_F26Dot6  char_height,
2886                     FT_UInt     horz_resolution,
2887                     FT_UInt     vert_resolution )
2888   {
2889     FT_Size_RequestRec  req;
2890
2891
2892     if ( !char_width )
2893       char_width = char_height;
2894     else if ( !char_height )
2895       char_height = char_width;
2896
2897     if ( !horz_resolution )
2898       horz_resolution = vert_resolution;
2899     else if ( !vert_resolution )
2900       vert_resolution = horz_resolution;
2901
2902     if ( char_width  < 1 * 64 )
2903       char_width  = 1 * 64;
2904     if ( char_height < 1 * 64 )
2905       char_height = 1 * 64;
2906
2907     if ( !horz_resolution )
2908       horz_resolution = vert_resolution = 72;
2909
2910     req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
2911     req.width          = char_width;
2912     req.height         = char_height;
2913     req.horiResolution = horz_resolution;
2914     req.vertResolution = vert_resolution;
2915
2916     return FT_Request_Size( face, &req );
2917   }
2918
2919
2920   /* documentation is in freetype.h */
2921
2922   FT_EXPORT_DEF( FT_Error )
2923   FT_Set_Pixel_Sizes( FT_Face  face,
2924                       FT_UInt  pixel_width,
2925                       FT_UInt  pixel_height )
2926   {
2927     FT_Size_RequestRec  req;
2928
2929
2930     if ( pixel_width == 0 )
2931       pixel_width = pixel_height;
2932     else if ( pixel_height == 0 )
2933       pixel_height = pixel_width;
2934
2935     if ( pixel_width  < 1 )
2936       pixel_width  = 1;
2937     if ( pixel_height < 1 )
2938       pixel_height = 1;
2939
2940     /* use `>=' to avoid potential compiler warning on 16bit platforms */
2941     if ( pixel_width  >= 0xFFFFU )
2942       pixel_width  = 0xFFFFU;
2943     if ( pixel_height >= 0xFFFFU )
2944       pixel_height = 0xFFFFU;
2945
2946     req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
2947     req.width          = pixel_width << 6;
2948     req.height         = pixel_height << 6;
2949     req.horiResolution = 0;
2950     req.vertResolution = 0;
2951
2952     return FT_Request_Size( face, &req );
2953   }
2954
2955
2956   /* documentation is in freetype.h */
2957
2958   FT_EXPORT_DEF( FT_Error )
2959   FT_Get_Kerning( FT_Face     face,
2960                   FT_UInt     left_glyph,
2961                   FT_UInt     right_glyph,
2962                   FT_UInt     kern_mode,
2963                   FT_Vector  *akerning )
2964   {
2965     FT_Error   error = FT_Err_Ok;
2966     FT_Driver  driver;
2967
2968
2969     if ( !face )
2970       return FT_Err_Invalid_Face_Handle;
2971
2972     if ( !akerning )
2973       return FT_Err_Invalid_Argument;
2974
2975     driver = face->driver;
2976
2977     akerning->x = 0;
2978     akerning->y = 0;
2979
2980     if ( driver->clazz->get_kerning )
2981     {
2982       error = driver->clazz->get_kerning( face,
2983                                           left_glyph,
2984                                           right_glyph,
2985                                           akerning );
2986       if ( !error )
2987       {
2988         if ( kern_mode != FT_KERNING_UNSCALED )
2989         {
2990           akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
2991           akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
2992
2993           if ( kern_mode != FT_KERNING_UNFITTED )
2994           {
2995             /* we scale down kerning values for small ppem values */
2996             /* to avoid that rounding makes them too big.         */
2997             /* `25' has been determined heuristically.            */
2998             if ( face->size->metrics.x_ppem < 25 )
2999               akerning->x = FT_MulDiv( akerning->x,
3000                                        face->size->metrics.x_ppem, 25 );
3001             if ( face->size->metrics.y_ppem < 25 )
3002               akerning->y = FT_MulDiv( akerning->y,
3003                                        face->size->metrics.y_ppem, 25 );
3004
3005             akerning->x = FT_PIX_ROUND( akerning->x );
3006             akerning->y = FT_PIX_ROUND( akerning->y );
3007           }
3008         }
3009       }
3010     }
3011
3012     return error;
3013   }
3014
3015
3016   /* documentation is in freetype.h */
3017
3018   FT_EXPORT_DEF( FT_Error )
3019   FT_Get_Track_Kerning( FT_Face    face,
3020                         FT_Fixed   point_size,
3021                         FT_Int     degree,
3022                         FT_Fixed*  akerning )
3023   {
3024     FT_Service_Kerning  service;
3025     FT_Error            error = FT_Err_Ok;
3026
3027
3028     if ( !face )
3029       return FT_Err_Invalid_Face_Handle;
3030
3031     if ( !akerning )
3032       return FT_Err_Invalid_Argument;
3033
3034     FT_FACE_FIND_SERVICE( face, service, KERNING );
3035     if ( !service )
3036       return FT_Err_Unimplemented_Feature;
3037
3038     error = service->get_track( face,
3039                                 point_size,
3040                                 degree,
3041                                 akerning );
3042
3043     return error;
3044   }
3045
3046
3047   /* documentation is in freetype.h */
3048
3049   FT_EXPORT_DEF( FT_Error )
3050   FT_Select_Charmap( FT_Face      face,
3051                      FT_Encoding  encoding )
3052   {
3053     FT_CharMap*  cur;
3054     FT_CharMap*  limit;
3055
3056
3057     if ( !face )
3058       return FT_Err_Invalid_Face_Handle;
3059
3060     if ( encoding == FT_ENCODING_NONE )
3061       return FT_Err_Invalid_Argument;
3062
3063     /* FT_ENCODING_UNICODE is special.  We try to find the `best' Unicode */
3064     /* charmap available, i.e., one with UCS-4 characters, if possible.   */
3065     /*                                                                    */
3066     /* This is done by find_unicode_charmap() above, to share code.       */
3067     if ( encoding == FT_ENCODING_UNICODE )
3068       return find_unicode_charmap( face );
3069
3070     cur = face->charmaps;
3071     if ( !cur )
3072       return FT_Err_Invalid_CharMap_Handle;
3073
3074     limit = cur + face->num_charmaps;
3075
3076     for ( ; cur < limit; cur++ )
3077     {
3078       if ( cur[0]->encoding == encoding )
3079       {
3080 #ifdef FT_MAX_CHARMAP_CACHEABLE
3081         if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3082         {
3083           FT_ERROR(( "FT_Select_Charmap: requested charmap is found (%d), "
3084                      "but in too late position to cache\n",
3085                      cur - face->charmaps ));
3086           continue;
3087         }
3088 #endif
3089         face->charmap = cur[0];
3090         return 0;
3091       }
3092     }
3093
3094     return FT_Err_Invalid_Argument;
3095   }
3096
3097
3098   /* documentation is in freetype.h */
3099
3100   FT_EXPORT_DEF( FT_Error )
3101   FT_Set_Charmap( FT_Face     face,
3102                   FT_CharMap  charmap )
3103   {
3104     FT_CharMap*  cur;
3105     FT_CharMap*  limit;
3106
3107
3108     if ( !face )
3109       return FT_Err_Invalid_Face_Handle;
3110
3111     cur = face->charmaps;
3112     if ( !cur )
3113       return FT_Err_Invalid_CharMap_Handle;
3114     if ( FT_Get_CMap_Format( charmap ) == 14 )
3115       return FT_Err_Invalid_Argument;
3116
3117     limit = cur + face->num_charmaps;
3118
3119     for ( ; cur < limit; cur++ )
3120     {
3121       if ( cur[0] == charmap )
3122       {
3123 #ifdef FT_MAX_CHARMAP_CACHEABLE
3124         if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3125         {
3126           FT_ERROR(( "FT_Set_Charmap: requested charmap is found (%d), "
3127                      "but in too late position to cache\n",
3128                      cur - face->charmaps ));
3129           continue;
3130         }
3131 #endif
3132         face->charmap = cur[0];
3133         return 0;
3134       }
3135     }
3136     return FT_Err_Invalid_Argument;
3137   }
3138
3139
3140   /* documentation is in freetype.h */
3141
3142   FT_EXPORT_DEF( FT_Int )
3143   FT_Get_Charmap_Index( FT_CharMap  charmap )
3144   {
3145     FT_Int  i;
3146
3147
3148     if ( !charmap || !charmap->face )
3149       return -1;
3150
3151     for ( i = 0; i < charmap->face->num_charmaps; i++ )
3152       if ( charmap->face->charmaps[i] == charmap )
3153         break;
3154
3155     FT_ASSERT( i < charmap->face->num_charmaps );
3156
3157 #ifdef FT_MAX_CHARMAP_CACHEABLE
3158     if ( i > FT_MAX_CHARMAP_CACHEABLE )
3159     {
3160       FT_ERROR(( "FT_Get_Charmap_Index: requested charmap is found (%d), "
3161                  "but in too late position to cache\n",
3162                  i ));
3163       return -i;
3164     }
3165 #endif
3166     return i;
3167   }
3168
3169
3170   static void
3171   ft_cmap_done_internal( FT_CMap  cmap )
3172   {
3173     FT_CMap_Class  clazz  = cmap->clazz;
3174     FT_Face        face   = cmap->charmap.face;
3175     FT_Memory      memory = FT_FACE_MEMORY(face);
3176
3177
3178     if ( clazz->done )
3179       clazz->done( cmap );
3180
3181     FT_FREE( cmap );
3182   }
3183
3184
3185   FT_BASE_DEF( void )
3186   FT_CMap_Done( FT_CMap  cmap )
3187   {
3188     if ( cmap )
3189     {
3190       FT_Face    face   = cmap->charmap.face;
3191       FT_Memory  memory = FT_FACE_MEMORY( face );
3192       FT_Error   error;
3193       FT_Int     i, j;
3194
3195
3196       for ( i = 0; i < face->num_charmaps; i++ )
3197       {
3198         if ( (FT_CMap)face->charmaps[i] == cmap )
3199         {
3200           FT_CharMap  last_charmap = face->charmaps[face->num_charmaps - 1];
3201
3202
3203           if ( FT_RENEW_ARRAY( face->charmaps,
3204                                face->num_charmaps,
3205                                face->num_charmaps - 1 ) )
3206             return;
3207
3208           /* remove it from our list of charmaps */
3209           for ( j = i + 1; j < face->num_charmaps; j++ )
3210           {
3211             if ( j == face->num_charmaps - 1 )
3212               face->charmaps[j - 1] = last_charmap;
3213             else
3214               face->charmaps[j - 1] = face->charmaps[j];
3215           }
3216
3217           face->num_charmaps--;
3218
3219           if ( (FT_CMap)face->charmap == cmap )
3220             face->charmap = NULL;
3221
3222           ft_cmap_done_internal( cmap );
3223
3224           break;
3225         }
3226       }
3227     }
3228   }
3229
3230
3231   FT_BASE_DEF( FT_Error )
3232   FT_CMap_New( FT_CMap_Class  clazz,
3233                FT_Pointer     init_data,
3234                FT_CharMap     charmap,
3235                FT_CMap       *acmap )
3236   {
3237     FT_Error   error = FT_Err_Ok;
3238     FT_Face    face;
3239     FT_Memory  memory;
3240     FT_CMap    cmap = NULL;
3241
3242
3243     if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
3244       return FT_Err_Invalid_Argument;
3245
3246     face   = charmap->face;
3247     memory = FT_FACE_MEMORY( face );
3248
3249     if ( !FT_ALLOC( cmap, clazz->size ) )
3250     {
3251       cmap->charmap = *charmap;
3252       cmap->clazz   = clazz;
3253
3254       if ( clazz->init )
3255       {
3256         error = clazz->init( cmap, init_data );
3257         if ( error )
3258           goto Fail;
3259       }
3260
3261       /* add it to our list of charmaps */
3262       if ( FT_RENEW_ARRAY( face->charmaps,
3263                            face->num_charmaps,
3264                            face->num_charmaps + 1 ) )
3265         goto Fail;
3266
3267       face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3268     }
3269
3270   Exit:
3271     if ( acmap )
3272       *acmap = cmap;
3273
3274     return error;
3275
3276   Fail:
3277     ft_cmap_done_internal( cmap );
3278     cmap = NULL;
3279     goto Exit;
3280   }
3281
3282
3283   /* documentation is in freetype.h */
3284
3285   FT_EXPORT_DEF( FT_UInt )
3286   FT_Get_Char_Index( FT_Face   face,
3287                      FT_ULong  charcode )
3288   {
3289     FT_UInt  result = 0;
3290
3291
3292     if ( face && face->charmap )
3293     {
3294       FT_CMap  cmap = FT_CMAP( face->charmap );
3295
3296
3297       if ( charcode > 0xFFFFFFFFUL )
3298       {
3299         FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3300         FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3301       }
3302       result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3303     }
3304     return result;
3305   }
3306
3307
3308   /* documentation is in freetype.h */
3309
3310   FT_EXPORT_DEF( FT_ULong )
3311   FT_Get_First_Char( FT_Face   face,
3312                      FT_UInt  *agindex )
3313   {
3314     FT_ULong  result = 0;
3315     FT_UInt   gindex = 0;
3316
3317
3318     if ( face && face->charmap && face->num_glyphs )
3319     {
3320       gindex = FT_Get_Char_Index( face, 0 );
3321       if ( gindex == 0 || gindex >= (FT_UInt)face->num_glyphs )
3322         result = FT_Get_Next_Char( face, 0, &gindex );
3323     }
3324
3325     if ( agindex )
3326       *agindex = gindex;
3327
3328     return result;
3329   }
3330
3331
3332   /* documentation is in freetype.h */
3333
3334   FT_EXPORT_DEF( FT_ULong )
3335   FT_Get_Next_Char( FT_Face   face,
3336                     FT_ULong  charcode,
3337                     FT_UInt  *agindex )
3338   {
3339     FT_ULong  result = 0;
3340     FT_UInt   gindex = 0;
3341
3342
3343     if ( face && face->charmap && face->num_glyphs )
3344     {
3345       FT_UInt32  code = (FT_UInt32)charcode;
3346       FT_CMap    cmap = FT_CMAP( face->charmap );
3347
3348
3349       do {
3350         gindex = cmap->clazz->char_next( cmap, &code );
3351       } while ( gindex >= (FT_UInt)face->num_glyphs );
3352
3353       result = ( gindex == 0 ) ? 0 : code;
3354     }
3355
3356     if ( agindex )
3357       *agindex = gindex;
3358
3359     return result;
3360   }
3361
3362
3363   /* documentation is in freetype.h */
3364
3365   FT_EXPORT_DEF( FT_UInt )
3366   FT_Face_GetCharVariantIndex( FT_Face   face,
3367                                FT_ULong  charcode,
3368                                FT_ULong  variantSelector )
3369   {
3370     FT_UInt  result = 0;
3371
3372
3373     if ( face && face->charmap &&
3374         face->charmap->encoding == FT_ENCODING_UNICODE )
3375     {
3376       FT_CharMap  charmap = find_variant_selector_charmap( face );
3377       FT_CMap     ucmap = FT_CMAP( face->charmap );
3378
3379
3380       if ( charmap != NULL )
3381       {
3382         FT_CMap  vcmap = FT_CMAP( charmap );
3383
3384
3385         if ( charcode > 0xFFFFFFFFUL )
3386         {
3387           FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3388           FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3389         }
3390         if ( variantSelector > 0xFFFFFFFFUL )
3391         {
3392           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3393           FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3394         }
3395
3396         result = vcmap->clazz->char_var_index( vcmap, ucmap,
3397                                                (FT_UInt32)charcode,
3398                                                (FT_UInt32)variantSelector );
3399       }
3400     }
3401
3402     return result;
3403   }
3404
3405
3406   /* documentation is in freetype.h */
3407
3408   FT_EXPORT_DEF( FT_Int )
3409   FT_Face_GetCharVariantIsDefault( FT_Face   face,
3410                                    FT_ULong  charcode,
3411                                    FT_ULong  variantSelector )
3412   {
3413     FT_Int  result = -1;
3414
3415
3416     if ( face )
3417     {
3418       FT_CharMap  charmap = find_variant_selector_charmap( face );
3419
3420
3421       if ( charmap != NULL )
3422       {
3423         FT_CMap  vcmap = FT_CMAP( charmap );
3424
3425
3426         if ( charcode > 0xFFFFFFFFUL )
3427         {
3428           FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3429           FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3430         }
3431         if ( variantSelector > 0xFFFFFFFFUL )
3432         {
3433           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3434           FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3435         }
3436
3437         result = vcmap->clazz->char_var_default( vcmap,
3438                                                  (FT_UInt32)charcode,
3439                                                  (FT_UInt32)variantSelector );
3440       }
3441     }
3442
3443     return result;
3444   }
3445
3446
3447   /* documentation is in freetype.h */
3448
3449   FT_EXPORT_DEF( FT_UInt32* )
3450   FT_Face_GetVariantSelectors( FT_Face  face )
3451   {
3452     FT_UInt32  *result = NULL;
3453
3454
3455     if ( face )
3456     {
3457       FT_CharMap  charmap = find_variant_selector_charmap( face );
3458
3459
3460       if ( charmap != NULL )
3461       {
3462         FT_CMap    vcmap  = FT_CMAP( charmap );
3463         FT_Memory  memory = FT_FACE_MEMORY( face );
3464
3465
3466         result = vcmap->clazz->variant_list( vcmap, memory );
3467       }
3468     }
3469
3470     return result;
3471   }
3472
3473
3474   /* documentation is in freetype.h */
3475
3476   FT_EXPORT_DEF( FT_UInt32* )
3477   FT_Face_GetVariantsOfChar( FT_Face   face,
3478                              FT_ULong  charcode )
3479   {
3480     FT_UInt32  *result = NULL;
3481
3482
3483     if ( face )
3484     {
3485       FT_CharMap  charmap = find_variant_selector_charmap( face );
3486
3487
3488       if ( charmap != NULL )
3489       {
3490         FT_CMap    vcmap  = FT_CMAP( charmap );
3491         FT_Memory  memory = FT_FACE_MEMORY( face );
3492
3493
3494         if ( charcode > 0xFFFFFFFFUL )
3495         {
3496           FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3497           FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3498         }
3499
3500         result = vcmap->clazz->charvariant_list( vcmap, memory,
3501                                                  (FT_UInt32)charcode );
3502       }
3503     }
3504     return result;
3505   }
3506
3507
3508   /* documentation is in freetype.h */
3509
3510   FT_EXPORT_DEF( FT_UInt32* )
3511   FT_Face_GetCharsOfVariant( FT_Face   face,
3512                              FT_ULong  variantSelector )
3513   {
3514     FT_UInt32  *result = NULL;
3515
3516
3517     if ( face )
3518     {
3519       FT_CharMap  charmap = find_variant_selector_charmap( face );
3520
3521
3522       if ( charmap != NULL )
3523       {
3524         FT_CMap    vcmap  = FT_CMAP( charmap );
3525         FT_Memory  memory = FT_FACE_MEMORY( face );
3526
3527
3528         if ( variantSelector > 0xFFFFFFFFUL )
3529         {
3530           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3531           FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3532         }
3533
3534         result = vcmap->clazz->variantchar_list( vcmap, memory,
3535                                                  (FT_UInt32)variantSelector );
3536       }
3537     }
3538
3539     return result;
3540   }
3541
3542
3543   /* documentation is in freetype.h */
3544
3545   FT_EXPORT_DEF( FT_UInt )
3546   FT_Get_Name_Index( FT_Face     face,
3547                      FT_String*  glyph_name )
3548   {
3549     FT_UInt  result = 0;
3550
3551
3552     if ( face && FT_HAS_GLYPH_NAMES( face ) )
3553     {
3554       FT_Service_GlyphDict  service;
3555
3556
3557       FT_FACE_LOOKUP_SERVICE( face,
3558                               service,
3559                               GLYPH_DICT );
3560
3561       if ( service && service->name_index )
3562         result = service->name_index( face, glyph_name );
3563     }
3564
3565     return result;
3566   }
3567
3568
3569   /* documentation is in freetype.h */
3570
3571   FT_EXPORT_DEF( FT_Error )
3572   FT_Get_Glyph_Name( FT_Face     face,
3573                      FT_UInt     glyph_index,
3574                      FT_Pointer  buffer,
3575                      FT_UInt     buffer_max )
3576   {
3577     FT_Error  error = FT_Err_Invalid_Argument;
3578
3579
3580     /* clean up buffer */
3581     if ( buffer && buffer_max > 0 )
3582       ((FT_Byte*)buffer)[0] = 0;
3583
3584     if ( face                                     &&
3585          (FT_Long)glyph_index <= face->num_glyphs &&
3586          FT_HAS_GLYPH_NAMES( face )               )
3587     {
3588       FT_Service_GlyphDict  service;
3589
3590
3591       FT_FACE_LOOKUP_SERVICE( face,
3592                               service,
3593                               GLYPH_DICT );
3594
3595       if ( service && service->get_name )
3596         error = service->get_name( face, glyph_index, buffer, buffer_max );
3597     }
3598
3599     return error;
3600   }
3601
3602
3603   /* documentation is in freetype.h */
3604
3605   FT_EXPORT_DEF( const char* )
3606   FT_Get_Postscript_Name( FT_Face  face )
3607   {
3608     const char*  result = NULL;
3609
3610
3611     if ( !face )
3612       goto Exit;
3613
3614     if ( !result )
3615     {
3616       FT_Service_PsFontName  service;
3617
3618
3619       FT_FACE_LOOKUP_SERVICE( face,
3620                               service,
3621                               POSTSCRIPT_FONT_NAME );
3622
3623       if ( service && service->get_ps_font_name )
3624         result = service->get_ps_font_name( face );
3625     }
3626
3627   Exit:
3628     return result;
3629   }
3630
3631
3632   /* documentation is in tttables.h */
3633
3634   FT_EXPORT_DEF( void* )
3635   FT_Get_Sfnt_Table( FT_Face      face,
3636                      FT_Sfnt_Tag  tag )
3637   {
3638     void*                  table = 0;
3639     FT_Service_SFNT_Table  service;
3640
3641
3642     if ( face && FT_IS_SFNT( face ) )
3643     {
3644       FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3645       if ( service != NULL )
3646         table = service->get_table( face, tag );
3647     }
3648
3649     return table;
3650   }
3651
3652
3653   /* documentation is in tttables.h */
3654
3655   FT_EXPORT_DEF( FT_Error )
3656   FT_Load_Sfnt_Table( FT_Face    face,
3657                       FT_ULong   tag,
3658                       FT_Long    offset,
3659                       FT_Byte*   buffer,
3660                       FT_ULong*  length )
3661   {
3662     FT_Service_SFNT_Table  service;
3663
3664
3665     if ( !face || !FT_IS_SFNT( face ) )
3666       return FT_Err_Invalid_Face_Handle;
3667
3668     FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3669     if ( service == NULL )
3670       return FT_Err_Unimplemented_Feature;
3671
3672     return service->load_table( face, tag, offset, buffer, length );
3673   }
3674
3675
3676   /* documentation is in tttables.h */
3677
3678   FT_EXPORT_DEF( FT_Error )
3679   FT_Sfnt_Table_Info( FT_Face    face,
3680                       FT_UInt    table_index,
3681                       FT_ULong  *tag,
3682                       FT_ULong  *length )
3683   {
3684     FT_Service_SFNT_Table  service;
3685     FT_ULong               offset;
3686
3687
3688     if ( !face || !FT_IS_SFNT( face ) )
3689       return FT_Err_Invalid_Face_Handle;
3690
3691     FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3692     if ( service == NULL )
3693       return FT_Err_Unimplemented_Feature;
3694
3695     return service->table_info( face, table_index, tag, &offset, length );
3696   }
3697
3698
3699   /* documentation is in tttables.h */
3700
3701   FT_EXPORT_DEF( FT_ULong )
3702   FT_Get_CMap_Language_ID( FT_CharMap  charmap )
3703   {
3704     FT_Service_TTCMaps  service;
3705     FT_Face             face;
3706     TT_CMapInfo         cmap_info;
3707
3708
3709     if ( !charmap || !charmap->face )
3710       return 0;
3711
3712     face = charmap->face;
3713     FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3714     if ( service == NULL )
3715       return 0;
3716     if ( service->get_cmap_info( charmap, &cmap_info ))
3717       return 0;
3718
3719     return cmap_info.language;
3720   }
3721
3722
3723   /* documentation is in tttables.h */
3724
3725   FT_EXPORT_DEF( FT_Long )
3726   FT_Get_CMap_Format( FT_CharMap  charmap )
3727   {
3728     FT_Service_TTCMaps  service;
3729     FT_Face             face;
3730     TT_CMapInfo         cmap_info;
3731
3732
3733     if ( !charmap || !charmap->face )
3734       return -1;
3735
3736     face = charmap->face;
3737     FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3738     if ( service == NULL )
3739       return -1;
3740     if ( service->get_cmap_info( charmap, &cmap_info ))
3741       return -1;
3742
3743     return cmap_info.format;
3744   }
3745
3746
3747   /* documentation is in ftsizes.h */
3748
3749   FT_EXPORT_DEF( FT_Error )
3750   FT_Activate_Size( FT_Size  size )
3751   {
3752     FT_Face  face;
3753
3754
3755     if ( size == NULL )
3756       return FT_Err_Invalid_Argument;
3757
3758     face = size->face;
3759     if ( face == NULL || face->driver == NULL )
3760       return FT_Err_Invalid_Argument;
3761
3762     /* we don't need anything more complex than that; all size objects */
3763     /* are already listed by the face                                  */
3764     face->size = size;
3765
3766     return FT_Err_Ok;
3767   }
3768
3769
3770   /*************************************************************************/
3771   /*************************************************************************/
3772   /*************************************************************************/
3773   /****                                                                 ****/
3774   /****                                                                 ****/
3775   /****                        R E N D E R E R S                        ****/
3776   /****                                                                 ****/
3777   /****                                                                 ****/
3778   /*************************************************************************/
3779   /*************************************************************************/
3780   /*************************************************************************/
3781
3782   /* lookup a renderer by glyph format in the library's list */
3783   FT_BASE_DEF( FT_Renderer )
3784   FT_Lookup_Renderer( FT_Library       library,
3785                       FT_Glyph_Format  format,
3786                       FT_ListNode*     node )
3787   {
3788     FT_ListNode  cur;
3789     FT_Renderer  result = 0;
3790
3791
3792     if ( !library )
3793       goto Exit;
3794
3795     cur = library->renderers.head;
3796
3797     if ( node )
3798     {
3799       if ( *node )
3800         cur = (*node)->next;
3801       *node = 0;
3802     }
3803
3804     while ( cur )
3805     {
3806       FT_Renderer  renderer = FT_RENDERER( cur->data );
3807
3808
3809       if ( renderer->glyph_format == format )
3810       {
3811         if ( node )
3812           *node = cur;
3813
3814         result = renderer;
3815         break;
3816       }
3817       cur = cur->next;
3818     }
3819
3820   Exit:
3821     return result;
3822   }
3823
3824
3825   static FT_Renderer
3826   ft_lookup_glyph_renderer( FT_GlyphSlot  slot )
3827   {
3828     FT_Face      face    = slot->face;
3829     FT_Library   library = FT_FACE_LIBRARY( face );
3830     FT_Renderer  result  = library->cur_renderer;
3831
3832
3833     if ( !result || result->glyph_format != slot->format )
3834       result = FT_Lookup_Renderer( library, slot->format, 0 );
3835
3836     return result;
3837   }
3838
3839
3840   static void
3841   ft_set_current_renderer( FT_Library  library )
3842   {
3843     FT_Renderer  renderer;
3844
3845
3846     renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
3847     library->cur_renderer = renderer;
3848   }
3849
3850
3851   static FT_Error
3852   ft_add_renderer( FT_Module  module )
3853   {
3854     FT_Library   library = module->library;
3855     FT_Memory    memory  = library->memory;
3856     FT_Error     error;
3857     FT_ListNode  node    = NULL;
3858
3859
3860     if ( FT_NEW( node ) )
3861       goto Exit;
3862
3863     {
3864       FT_Renderer         render = FT_RENDERER( module );
3865       FT_Renderer_Class*  clazz  = (FT_Renderer_Class*)module->clazz;
3866
3867
3868       render->clazz        = clazz;
3869       render->glyph_format = clazz->glyph_format;
3870
3871       /* allocate raster object if needed */
3872       if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3873            clazz->raster_class->raster_new                )
3874       {
3875         error = clazz->raster_class->raster_new( memory, &render->raster );
3876         if ( error )
3877           goto Fail;
3878
3879         render->raster_render = clazz->raster_class->raster_render;
3880         render->render        = clazz->render_glyph;
3881       }
3882
3883       /* add to list */
3884       node->data = module;
3885       FT_List_Add( &library->renderers, node );
3886
3887       ft_set_current_renderer( library );
3888     }
3889
3890   Fail:
3891     if ( error )
3892       FT_FREE( node );
3893
3894   Exit:
3895     return error;
3896   }
3897
3898
3899   static void
3900   ft_remove_renderer( FT_Module  module )
3901   {
3902     FT_Library   library = module->library;
3903     FT_Memory    memory  = library->memory;
3904     FT_ListNode  node;
3905
3906
3907     node = FT_List_Find( &library->renderers, module );
3908     if ( node )
3909     {
3910       FT_Renderer  render = FT_RENDERER( module );
3911
3912
3913       /* release raster object, if any */
3914       if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3915            render->raster                                         )
3916         render->clazz->raster_class->raster_done( render->raster );
3917
3918       /* remove from list */
3919       FT_List_Remove( &library->renderers, node );
3920       FT_FREE( node );
3921
3922       ft_set_current_renderer( library );
3923     }
3924   }
3925
3926
3927   /* documentation is in ftrender.h */
3928
3929   FT_EXPORT_DEF( FT_Renderer )
3930   FT_Get_Renderer( FT_Library       library,
3931                    FT_Glyph_Format  format )
3932   {
3933     /* test for valid `library' delayed to FT_Lookup_Renderer() */
3934
3935     return FT_Lookup_Renderer( library, format, 0 );
3936   }
3937
3938
3939   /* documentation is in ftrender.h */
3940
3941   FT_EXPORT_DEF( FT_Error )
3942   FT_Set_Renderer( FT_Library     library,
3943                    FT_Renderer    renderer,
3944                    FT_UInt        num_params,
3945                    FT_Parameter*  parameters )
3946   {
3947     FT_ListNode  node;
3948     FT_Error     error = FT_Err_Ok;
3949
3950
3951     if ( !library )
3952       return FT_Err_Invalid_Library_Handle;
3953
3954     if ( !renderer )
3955       return FT_Err_Invalid_Argument;
3956
3957     node = FT_List_Find( &library->renderers, renderer );
3958     if ( !node )
3959     {
3960       error = FT_Err_Invalid_Argument;
3961       goto Exit;
3962     }
3963
3964     FT_List_Up( &library->renderers, node );
3965
3966     if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
3967       library->cur_renderer = renderer;
3968
3969     if ( num_params > 0 )
3970     {
3971       FT_Renderer_SetModeFunc  set_mode = renderer->clazz->set_mode;
3972
3973
3974       for ( ; num_params > 0; num_params-- )
3975       {
3976         error = set_mode( renderer, parameters->tag, parameters->data );
3977         if ( error )
3978           break;
3979         parameters++;
3980       }
3981     }
3982
3983   Exit:
3984     return error;
3985   }
3986
3987
3988   FT_BASE_DEF( FT_Error )
3989   FT_Render_Glyph_Internal( FT_Library      library,
3990                             FT_GlyphSlot    slot,
3991                             FT_Render_Mode  render_mode )
3992   {
3993     FT_Error     error = FT_Err_Ok;
3994     FT_Renderer  renderer;
3995
3996
3997     /* if it is already a bitmap, no need to do anything */
3998     switch ( slot->format )
3999     {
4000     case FT_GLYPH_FORMAT_BITMAP:   /* already a bitmap, don't do anything */
4001       break;
4002
4003     default:
4004       {
4005         FT_ListNode  node   = 0;
4006         FT_Bool      update = 0;
4007
4008
4009         /* small shortcut for the very common case */
4010         if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
4011         {
4012           renderer = library->cur_renderer;
4013           node     = library->renderers.head;
4014         }
4015         else
4016           renderer = FT_Lookup_Renderer( library, slot->format, &node );
4017
4018         error = FT_Err_Unimplemented_Feature;
4019         while ( renderer )
4020         {
4021           error = renderer->render( renderer, slot, render_mode, NULL );
4022           if ( !error                                               ||
4023                FT_ERROR_BASE( error ) != FT_Err_Cannot_Render_Glyph )
4024             break;
4025
4026           /* FT_Err_Cannot_Render_Glyph is returned if the render mode   */
4027           /* is unsupported by the current renderer for this glyph image */
4028           /* format.                                                     */
4029
4030           /* now, look for another renderer that supports the same */
4031           /* format.                                               */
4032           renderer = FT_Lookup_Renderer( library, slot->format, &node );
4033           update   = 1;
4034         }
4035
4036         /* if we changed the current renderer for the glyph image format */
4037         /* we need to select it as the next current one                  */
4038         if ( !error && update && renderer )
4039           FT_Set_Renderer( library, renderer, 0, 0 );
4040       }
4041     }
4042
4043     return error;
4044   }
4045
4046
4047   /* documentation is in freetype.h */
4048
4049   FT_EXPORT_DEF( FT_Error )
4050   FT_Render_Glyph( FT_GlyphSlot    slot,
4051                    FT_Render_Mode  render_mode )
4052   {
4053     FT_Library  library;
4054
4055
4056     if ( !slot || !slot->face )
4057       return FT_Err_Invalid_Argument;
4058
4059     library = FT_FACE_LIBRARY( slot->face );
4060
4061     return FT_Render_Glyph_Internal( library, slot, render_mode );
4062   }
4063
4064
4065   /*************************************************************************/
4066   /*************************************************************************/
4067   /*************************************************************************/
4068   /****                                                                 ****/
4069   /****                                                                 ****/
4070   /****                         M O D U L E S                           ****/
4071   /****                                                                 ****/
4072   /****                                                                 ****/
4073   /*************************************************************************/
4074   /*************************************************************************/
4075   /*************************************************************************/
4076
4077
4078   /*************************************************************************/
4079   /*                                                                       */
4080   /* <Function>                                                            */
4081   /*    Destroy_Module                                                     */
4082   /*                                                                       */
4083   /* <Description>                                                         */
4084   /*    Destroys a given module object.  For drivers, this also destroys   */
4085   /*    all child faces.                                                   */
4086   /*                                                                       */
4087   /* <InOut>                                                               */
4088   /*     module :: A handle to the target driver object.                   */
4089   /*                                                                       */
4090   /* <Note>                                                                */
4091   /*     The driver _must_ be LOCKED!                                      */
4092   /*                                                                       */
4093   static void
4094   Destroy_Module( FT_Module  module )
4095   {
4096     FT_Memory         memory  = module->memory;
4097     FT_Module_Class*  clazz   = module->clazz;
4098     FT_Library        library = module->library;
4099
4100
4101     /* finalize client-data - before anything else */
4102     if ( module->generic.finalizer )
4103       module->generic.finalizer( module );
4104
4105     if ( library && library->auto_hinter == module )
4106       library->auto_hinter = 0;
4107
4108     /* if the module is a renderer */
4109     if ( FT_MODULE_IS_RENDERER( module ) )
4110       ft_remove_renderer( module );
4111
4112     /* if the module is a font driver, add some steps */
4113     if ( FT_MODULE_IS_DRIVER( module ) )
4114       Destroy_Driver( FT_DRIVER( module ) );
4115
4116     /* finalize the module object */
4117     if ( clazz->module_done )
4118       clazz->module_done( module );
4119
4120     /* discard it */
4121     FT_FREE( module );
4122   }
4123
4124
4125   /* documentation is in ftmodapi.h */
4126
4127   FT_EXPORT_DEF( FT_Error )
4128   FT_Add_Module( FT_Library              library,
4129                  const FT_Module_Class*  clazz )
4130   {
4131     FT_Error   error;
4132     FT_Memory  memory;
4133     FT_Module  module;
4134     FT_UInt    nn;
4135
4136
4137 #define FREETYPE_VER_FIXED  ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
4138                                 FREETYPE_MINOR                  )
4139
4140     if ( !library )
4141       return FT_Err_Invalid_Library_Handle;
4142
4143     if ( !clazz )
4144       return FT_Err_Invalid_Argument;
4145
4146     /* check freetype version */
4147     if ( clazz->module_requires > FREETYPE_VER_FIXED )
4148       return FT_Err_Invalid_Version;
4149
4150     /* look for a module with the same name in the library's table */
4151     for ( nn = 0; nn < library->num_modules; nn++ )
4152     {
4153       module = library->modules[nn];
4154       if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
4155       {
4156         /* this installed module has the same name, compare their versions */
4157         if ( clazz->module_version <= module->clazz->module_version )
4158           return FT_Err_Lower_Module_Version;
4159
4160         /* remove the module from our list, then exit the loop to replace */
4161         /* it by our new version..                                        */
4162         FT_Remove_Module( library, module );
4163         break;
4164       }
4165     }
4166
4167     memory = library->memory;
4168     error  = FT_Err_Ok;
4169
4170     if ( library->num_modules >= FT_MAX_MODULES )
4171     {
4172       error = FT_Err_Too_Many_Drivers;
4173       goto Exit;
4174     }
4175
4176     /* allocate module object */
4177     if ( FT_ALLOC( module, clazz->module_size ) )
4178       goto Exit;
4179
4180     /* base initialization */
4181     module->library = library;
4182     module->memory  = memory;
4183     module->clazz   = (FT_Module_Class*)clazz;
4184
4185     /* check whether the module is a renderer - this must be performed */
4186     /* before the normal module initialization                         */
4187     if ( FT_MODULE_IS_RENDERER( module ) )
4188     {
4189       /* add to the renderers list */
4190       error = ft_add_renderer( module );
4191       if ( error )
4192         goto Fail;
4193     }
4194
4195     /* is the module a auto-hinter? */
4196     if ( FT_MODULE_IS_HINTER( module ) )
4197       library->auto_hinter = module;
4198
4199     /* if the module is a font driver */
4200     if ( FT_MODULE_IS_DRIVER( module ) )
4201     {
4202       /* allocate glyph loader if needed */
4203       FT_Driver  driver = FT_DRIVER( module );
4204
4205
4206       driver->clazz = (FT_Driver_Class)module->clazz;
4207       if ( FT_DRIVER_USES_OUTLINES( driver ) )
4208       {
4209         error = FT_GlyphLoader_New( memory, &driver->glyph_loader );
4210         if ( error )
4211           goto Fail;
4212       }
4213     }
4214
4215     if ( clazz->module_init )
4216     {
4217       error = clazz->module_init( module );
4218       if ( error )
4219         goto Fail;
4220     }
4221
4222     /* add module to the library's table */
4223     library->modules[library->num_modules++] = module;
4224
4225   Exit:
4226     return error;
4227
4228   Fail:
4229     if ( FT_MODULE_IS_DRIVER( module ) )
4230     {
4231       FT_Driver  driver = FT_DRIVER( module );
4232
4233
4234       if ( FT_DRIVER_USES_OUTLINES( driver ) )
4235         FT_GlyphLoader_Done( driver->glyph_loader );
4236     }
4237
4238     if ( FT_MODULE_IS_RENDERER( module ) )
4239     {
4240       FT_Renderer  renderer = FT_RENDERER( module );
4241
4242
4243       if ( renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
4244            renderer->raster                                         )
4245         renderer->clazz->raster_class->raster_done( renderer->raster );
4246     }
4247
4248     FT_FREE( module );
4249     goto Exit;
4250   }
4251
4252
4253   /* documentation is in ftmodapi.h */
4254
4255   FT_EXPORT_DEF( FT_Module )
4256   FT_Get_Module( FT_Library   library,
4257                  const char*  module_name )
4258   {
4259     FT_Module   result = 0;
4260     FT_Module*  cur;
4261     FT_Module*  limit;
4262
4263
4264     if ( !library || !module_name )
4265       return result;
4266
4267     cur   = library->modules;
4268     limit = cur + library->num_modules;
4269
4270     for ( ; cur < limit; cur++ )
4271       if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
4272       {
4273         result = cur[0];
4274         break;
4275       }
4276
4277     return result;
4278   }
4279
4280
4281   /* documentation is in ftobjs.h */
4282
4283   FT_BASE_DEF( const void* )
4284   FT_Get_Module_Interface( FT_Library   library,
4285                            const char*  mod_name )
4286   {
4287     FT_Module  module;
4288
4289
4290     /* test for valid `library' delayed to FT_Get_Module() */
4291
4292     module = FT_Get_Module( library, mod_name );
4293
4294     return module ? module->clazz->module_interface : 0;
4295   }
4296
4297
4298   FT_BASE_DEF( FT_Pointer )
4299   ft_module_get_service( FT_Module    module,
4300                          const char*  service_id )
4301   {
4302     FT_Pointer  result = NULL;
4303
4304     if ( module )
4305     {
4306       FT_ASSERT( module->clazz && module->clazz->get_interface );
4307
4308      /* first, look for the service in the module
4309       */
4310       if ( module->clazz->get_interface )
4311         result = module->clazz->get_interface( module, service_id );
4312
4313       if ( result == NULL )
4314       {
4315        /* we didn't find it, look in all other modules then
4316         */
4317         FT_Library  library = module->library;
4318         FT_Module*  cur     = library->modules;
4319         FT_Module*  limit   = cur + library->num_modules;
4320
4321         for ( ; cur < limit; cur++ )
4322         {
4323           if ( cur[0] != module )
4324           {
4325             FT_ASSERT( cur[0]->clazz );
4326
4327             if ( cur[0]->clazz->get_interface )
4328             {
4329               result = cur[0]->clazz->get_interface( cur[0], service_id );
4330               if ( result != NULL )
4331                 break;
4332             }
4333           }
4334         }
4335       }
4336     }
4337
4338     return result;
4339   }
4340
4341
4342   /* documentation is in ftmodapi.h */
4343
4344   FT_EXPORT_DEF( FT_Error )
4345   FT_Remove_Module( FT_Library  library,
4346                     FT_Module   module )
4347   {
4348     /* try to find the module from the table, then remove it from there */
4349
4350     if ( !library )
4351       return FT_Err_Invalid_Library_Handle;
4352
4353     if ( module )
4354     {
4355       FT_Module*  cur   = library->modules;
4356       FT_Module*  limit = cur + library->num_modules;
4357
4358
4359       for ( ; cur < limit; cur++ )
4360       {
4361         if ( cur[0] == module )
4362         {
4363           /* remove it from the table */
4364           library->num_modules--;
4365           limit--;
4366           while ( cur < limit )
4367           {
4368             cur[0] = cur[1];
4369             cur++;
4370           }
4371           limit[0] = 0;
4372
4373           /* destroy the module */
4374           Destroy_Module( module );
4375
4376           return FT_Err_Ok;
4377         }
4378       }
4379     }
4380     return FT_Err_Invalid_Driver_Handle;
4381   }
4382
4383
4384   /*************************************************************************/
4385   /*************************************************************************/
4386   /*************************************************************************/
4387   /****                                                                 ****/
4388   /****                                                                 ****/
4389   /****                         L I B R A R Y                           ****/
4390   /****                                                                 ****/
4391   /****                                                                 ****/
4392   /*************************************************************************/
4393   /*************************************************************************/
4394   /*************************************************************************/
4395
4396
4397   /* documentation is in ftmodapi.h */
4398
4399   FT_EXPORT_DEF( FT_Error )
4400   FT_Reference_Library( FT_Library  library )
4401   {
4402     library->refcount++;
4403
4404     return FT_Err_Ok;
4405   }
4406
4407
4408   /* documentation is in ftmodapi.h */
4409
4410   FT_EXPORT_DEF( FT_Error )
4411   FT_New_Library( FT_Memory    memory,
4412                   FT_Library  *alibrary )
4413   {
4414     FT_Library  library = NULL;
4415     FT_Error    error;
4416
4417
4418     if ( !memory )
4419       return FT_Err_Invalid_Argument;
4420
4421 #ifdef FT_DEBUG_LEVEL_ERROR
4422     /* init debugging support */
4423     ft_debug_init();
4424 #endif
4425
4426     /* first of all, allocate the library object */
4427     if ( FT_NEW( library ) )
4428       return error;
4429
4430     library->memory = memory;
4431
4432 #ifdef FT_CONFIG_OPTION_PIC
4433     /* initialize position independent code containers */
4434     error = ft_pic_container_init( library );
4435     if ( error )
4436       goto Fail;
4437 #endif
4438
4439     /* allocate the render pool */
4440     library->raster_pool_size = FT_RENDER_POOL_SIZE;
4441 #if FT_RENDER_POOL_SIZE > 0
4442     if ( FT_ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) )
4443       goto Fail;
4444 #endif
4445
4446     library->version_major = FREETYPE_MAJOR;
4447     library->version_minor = FREETYPE_MINOR;
4448     library->version_patch = FREETYPE_PATCH;
4449
4450     library->refcount = 1;
4451
4452     /* That's ok now */
4453     *alibrary = library;
4454
4455     return FT_Err_Ok;
4456
4457   Fail:
4458 #ifdef FT_CONFIG_OPTION_PIC
4459     ft_pic_container_destroy( library );
4460 #endif
4461     FT_FREE( library );
4462     return error;
4463   }
4464
4465
4466   /* documentation is in freetype.h */
4467
4468   FT_EXPORT_DEF( void )
4469   FT_Library_Version( FT_Library   library,
4470                       FT_Int      *amajor,
4471                       FT_Int      *aminor,
4472                       FT_Int      *apatch )
4473   {
4474     FT_Int  major = 0;
4475     FT_Int  minor = 0;
4476     FT_Int  patch = 0;
4477
4478
4479     if ( library )
4480     {
4481       major = library->version_major;
4482       minor = library->version_minor;
4483       patch = library->version_patch;
4484     }
4485
4486     if ( amajor )
4487       *amajor = major;
4488
4489     if ( aminor )
4490       *aminor = minor;
4491
4492     if ( apatch )
4493       *apatch = patch;
4494   }
4495
4496
4497   /* documentation is in ftmodapi.h */
4498
4499   FT_EXPORT_DEF( FT_Error )
4500   FT_Done_Library( FT_Library  library )
4501   {
4502     FT_Memory  memory;
4503
4504
4505     if ( !library )
4506       return FT_Err_Invalid_Library_Handle;
4507
4508     library->refcount--;
4509     if ( library->refcount > 0 )
4510       goto Exit;
4511
4512     memory = library->memory;
4513
4514     /* Discard client-data */
4515     if ( library->generic.finalizer )
4516       library->generic.finalizer( library );
4517
4518     /*
4519      * Close all faces in the library.  If we don't do this, we can have
4520      * some subtle memory leaks.
4521      *
4522      * Example:
4523      *
4524      *  - the cff font driver uses the pshinter module in cff_size_done
4525      *  - if the pshinter module is destroyed before the cff font driver,
4526      *    opened FT_Face objects managed by the driver are not properly
4527      *    destroyed, resulting in a memory leak
4528      *
4529      * Some faces are dependent on other faces, like Type42 faces that
4530      * depend on TrueType faces synthesized internally.
4531      *
4532      * The order of drivers should be specified in driver_name[].
4533      */
4534     {
4535       FT_UInt      m, n;
4536       const char*  driver_name[] = { "type42", NULL };
4537
4538
4539       for ( m = 0;
4540             m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
4541             m++ )
4542       {
4543         for ( n = 0; n < library->num_modules; n++ )
4544         {
4545           FT_Module    module      = library->modules[n];
4546           const char*  module_name = module->clazz->module_name;
4547           FT_List      faces;
4548
4549
4550           if ( driver_name[m]                                &&
4551                ft_strcmp( module_name, driver_name[m] ) != 0 )
4552             continue;
4553
4554           if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
4555             continue;
4556
4557           FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
4558
4559           faces = &FT_DRIVER( module )->faces_list;
4560           while ( faces->head )
4561           {
4562             FT_Done_Face( FT_FACE( faces->head->data ) );
4563             if ( faces->head )
4564               FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
4565           }
4566         }
4567       }
4568     }
4569
4570     /* Close all other modules in the library */
4571 #if 1
4572     /* XXX Modules are removed in the reversed order so that  */
4573     /* type42 module is removed before truetype module.  This */
4574     /* avoids double free in some occasions.  It is a hack.   */
4575     while ( library->num_modules > 0 )
4576       FT_Remove_Module( library,
4577                         library->modules[library->num_modules - 1] );
4578 #else
4579     {
4580       FT_UInt  n;
4581
4582
4583       for ( n = 0; n < library->num_modules; n++ )
4584       {
4585         FT_Module  module = library->modules[n];
4586
4587
4588         if ( module )
4589         {
4590           Destroy_Module( module );
4591           library->modules[n] = 0;
4592         }
4593       }
4594     }
4595 #endif
4596
4597     /* Destroy raster objects */
4598     FT_FREE( library->raster_pool );
4599     library->raster_pool_size = 0;
4600
4601 #ifdef FT_CONFIG_OPTION_PIC
4602     /* Destroy pic container contents */
4603     ft_pic_container_destroy( library );
4604 #endif
4605
4606     FT_FREE( library );
4607
4608   Exit:
4609     return FT_Err_Ok;
4610   }
4611
4612
4613   /* documentation is in ftmodapi.h */
4614
4615   FT_EXPORT_DEF( void )
4616   FT_Set_Debug_Hook( FT_Library         library,
4617                      FT_UInt            hook_index,
4618                      FT_DebugHook_Func  debug_hook )
4619   {
4620     if ( library && debug_hook &&
4621          hook_index <
4622            ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
4623       library->debug_hooks[hook_index] = debug_hook;
4624   }
4625
4626
4627   /* documentation is in ftmodapi.h */
4628
4629   FT_EXPORT_DEF( FT_TrueTypeEngineType )
4630   FT_Get_TrueType_Engine_Type( FT_Library  library )
4631   {
4632     FT_TrueTypeEngineType  result = FT_TRUETYPE_ENGINE_TYPE_NONE;
4633
4634
4635     if ( library )
4636     {
4637       FT_Module  module = FT_Get_Module( library, "truetype" );
4638
4639
4640       if ( module )
4641       {
4642         FT_Service_TrueTypeEngine  service;
4643
4644
4645         service = (FT_Service_TrueTypeEngine)
4646                     ft_module_get_service( module,
4647                                            FT_SERVICE_ID_TRUETYPE_ENGINE );
4648         if ( service )
4649           result = service->engine_type;
4650       }
4651     }
4652
4653     return result;
4654   }
4655
4656
4657 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
4658
4659   FT_BASE_DEF( FT_Error )
4660   ft_stub_set_char_sizes( FT_Size     size,
4661                           FT_F26Dot6  width,
4662                           FT_F26Dot6  height,
4663                           FT_UInt     horz_res,
4664                           FT_UInt     vert_res )
4665   {
4666     FT_Size_RequestRec  req;
4667     FT_Driver           driver = size->face->driver;
4668
4669
4670     if ( driver->clazz->request_size )
4671     {
4672       req.type   = FT_SIZE_REQUEST_TYPE_NOMINAL;
4673       req.width  = width;
4674       req.height = height;
4675
4676       if ( horz_res == 0 )
4677         horz_res = vert_res;
4678
4679       if ( vert_res == 0 )
4680         vert_res = horz_res;
4681
4682       if ( horz_res == 0 )
4683         horz_res = vert_res = 72;
4684
4685       req.horiResolution = horz_res;
4686       req.vertResolution = vert_res;
4687
4688       return driver->clazz->request_size( size, &req );
4689     }
4690
4691     return 0;
4692   }
4693
4694
4695   FT_BASE_DEF( FT_Error )
4696   ft_stub_set_pixel_sizes( FT_Size  size,
4697                            FT_UInt  width,
4698                            FT_UInt  height )
4699   {
4700     FT_Size_RequestRec  req;
4701     FT_Driver           driver = size->face->driver;
4702
4703
4704     if ( driver->clazz->request_size )
4705     {
4706       req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
4707       req.width          = width  << 6;
4708       req.height         = height << 6;
4709       req.horiResolution = 0;
4710       req.vertResolution = 0;
4711
4712       return driver->clazz->request_size( size, &req );
4713     }
4714
4715     return 0;
4716   }
4717
4718 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
4719
4720
4721   /* documentation is in freetype.h */
4722
4723   FT_EXPORT_DEF( FT_Error )
4724   FT_Get_SubGlyph_Info( FT_GlyphSlot  glyph,
4725                         FT_UInt       sub_index,
4726                         FT_Int       *p_index,
4727                         FT_UInt      *p_flags,
4728                         FT_Int       *p_arg1,
4729                         FT_Int       *p_arg2,
4730                         FT_Matrix    *p_transform )
4731   {
4732     FT_Error  error = FT_Err_Invalid_Argument;
4733
4734
4735     if ( glyph                                      &&
4736          glyph->subglyphs                           &&
4737          glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
4738          sub_index < glyph->num_subglyphs           )
4739     {
4740       FT_SubGlyph  subg = glyph->subglyphs + sub_index;
4741
4742
4743       *p_index     = subg->index;
4744       *p_flags     = subg->flags;
4745       *p_arg1      = subg->arg1;
4746       *p_arg2      = subg->arg2;
4747       *p_transform = subg->transform;
4748     }
4749
4750     return error;
4751   }
4752
4753
4754 /* END */