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