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