• 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 
2454     /* only use lower 31 bits together with sign bit */
2455     if ( face_index > 0 )
2456       face_index &= 0x7FFFFFFFL;
2457     else
2458     {
2459       face_index &= 0x7FFFFFFFL;
2460       face_index  = -face_index;
2461     }
2462 
2463 #ifdef FT_DEBUG_LEVEL_TRACE
2464     FT_TRACE3(( "FT_Open_Face: " ));
2465     if ( face_index < 0 )
2466       FT_TRACE3(( "Requesting number of faces and named instances\n"));
2467     else
2468     {
2469       FT_TRACE3(( "Requesting face %ld", face_index & 0xFFFFL ));
2470       if ( face_index & 0x7FFF0000L )
2471         FT_TRACE3(( ", named instance %ld", face_index >> 16 ));
2472       FT_TRACE3(( "\n" ));
2473     }
2474 #endif
2475 
2476     /* test for valid `library' delayed to `FT_Stream_New' */
2477 
2478     if ( ( !aface && face_index >= 0 ) || !args )
2479       return FT_THROW( Invalid_Argument );
2480 
2481     external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
2482                                args->stream                     );
2483 
2484     /* create input stream */
2485     error = FT_Stream_New( library, args, &stream );
2486     if ( error )
2487       goto Fail3;
2488 
2489     memory = library->memory;
2490 
2491     /* If the font driver is specified in the `args' structure, use */
2492     /* it.  Otherwise, we scan the list of registered drivers.      */
2493     if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
2494     {
2495       driver = FT_DRIVER( args->driver );
2496 
2497       /* not all modules are drivers, so check... */
2498       if ( FT_MODULE_IS_DRIVER( driver ) )
2499       {
2500         FT_Int         num_params = 0;
2501         FT_Parameter*  params     = NULL;
2502 
2503 
2504         if ( args->flags & FT_OPEN_PARAMS )
2505         {
2506           num_params = args->num_params;
2507           params     = args->params;
2508         }
2509 
2510         error = open_face( driver, &stream, external_stream, face_index,
2511                            num_params, params, &face );
2512         if ( !error )
2513           goto Success;
2514       }
2515       else
2516         error = FT_THROW( Invalid_Handle );
2517 
2518       FT_Stream_Free( stream, external_stream );
2519       goto Fail;
2520     }
2521     else
2522     {
2523       error = FT_ERR( Missing_Module );
2524 
2525       /* check each font driver for an appropriate format */
2526       cur   = library->modules;
2527       limit = cur + library->num_modules;
2528 
2529       for ( ; cur < limit; cur++ )
2530       {
2531         /* not all modules are font drivers, so check... */
2532         if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2533         {
2534           FT_Int         num_params = 0;
2535           FT_Parameter*  params     = NULL;
2536 
2537 
2538           driver = FT_DRIVER( cur[0] );
2539 
2540           if ( args->flags & FT_OPEN_PARAMS )
2541           {
2542             num_params = args->num_params;
2543             params     = args->params;
2544           }
2545 
2546           error = open_face( driver, &stream, external_stream, face_index,
2547                              num_params, params, &face );
2548           if ( !error )
2549             goto Success;
2550 
2551 #ifdef FT_CONFIG_OPTION_MAC_FONTS
2552           if ( test_mac_fonts                                           &&
2553                ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2554                FT_ERR_EQ( error, Table_Missing )                        )
2555           {
2556             /* TrueType but essential tables are missing */
2557             error = FT_Stream_Seek( stream, 0 );
2558             if ( error )
2559               break;
2560 
2561             error = open_face_PS_from_sfnt_stream( library,
2562                                                    stream,
2563                                                    face_index,
2564                                                    num_params,
2565                                                    params,
2566                                                    aface );
2567             if ( !error )
2568             {
2569               FT_Stream_Free( stream, external_stream );
2570               return error;
2571             }
2572           }
2573 #endif
2574 
2575           if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2576             goto Fail3;
2577         }
2578       }
2579 
2580     Fail3:
2581       /* If we are on the mac, and we get an                          */
2582       /* FT_Err_Invalid_Stream_Operation it may be because we have an */
2583       /* empty data fork, so we need to check the resource fork.      */
2584       if ( FT_ERR_NEQ( error, Cannot_Open_Stream )       &&
2585            FT_ERR_NEQ( error, Unknown_File_Format )      &&
2586            FT_ERR_NEQ( error, Invalid_Stream_Operation ) )
2587         goto Fail2;
2588 
2589 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2590       if ( test_mac_fonts )
2591       {
2592         error = load_mac_face( library, stream, face_index, aface, args );
2593         if ( !error )
2594         {
2595           /* We don't want to go to Success here.  We've already done   */
2596           /* that.  On the other hand, if we succeeded we still need to */
2597           /* close this stream (we opened a different stream which      */
2598           /* extracted the interesting information out of this stream   */
2599           /* here.  That stream will still be open and the face will    */
2600           /* point to it).                                              */
2601           FT_Stream_Free( stream, external_stream );
2602           return error;
2603         }
2604       }
2605 
2606       if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2607         goto Fail2;
2608 #endif  /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2609 
2610       /* no driver is able to handle this format */
2611       error = FT_THROW( Unknown_File_Format );
2612 
2613   Fail2:
2614       FT_Stream_Free( stream, external_stream );
2615       goto Fail;
2616     }
2617 
2618   Success:
2619     FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2620 
2621     /* add the face object to its driver's list */
2622     if ( FT_QNEW( node ) )
2623       goto Fail;
2624 
2625     node->data = face;
2626     /* don't assume driver is the same as face->driver, so use */
2627     /* face->driver instead.                                   */
2628     FT_List_Add( &face->driver->faces_list, node );
2629 
2630     /* now allocate a glyph slot object for the face */
2631     FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2632 
2633     if ( face_index >= 0 )
2634     {
2635       error = FT_New_GlyphSlot( face, NULL );
2636       if ( error )
2637         goto Fail;
2638 
2639       /* finally, allocate a size object for the face */
2640       {
2641         FT_Size  size;
2642 
2643 
2644         FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2645 
2646         error = FT_New_Size( face, &size );
2647         if ( error )
2648           goto Fail;
2649 
2650         face->size = size;
2651       }
2652     }
2653 
2654     /* some checks */
2655 
2656     if ( FT_IS_SCALABLE( face ) )
2657     {
2658       if ( face->height < 0 )
2659         face->height = (FT_Short)-face->height;
2660 
2661       if ( !FT_HAS_VERTICAL( face ) )
2662         face->max_advance_height = (FT_Short)face->height;
2663     }
2664 
2665     if ( FT_HAS_FIXED_SIZES( face ) )
2666     {
2667       FT_Int  i;
2668 
2669 
2670       for ( i = 0; i < face->num_fixed_sizes; i++ )
2671       {
2672         FT_Bitmap_Size*  bsize = face->available_sizes + i;
2673 
2674 
2675         if ( bsize->height < 0 )
2676           bsize->height = -bsize->height;
2677         if ( bsize->x_ppem < 0 )
2678           bsize->x_ppem = -bsize->x_ppem;
2679         if ( bsize->y_ppem < 0 )
2680           bsize->y_ppem = -bsize->y_ppem;
2681 
2682         /* check whether negation actually has worked */
2683         if ( bsize->height < 0 || bsize->x_ppem < 0 || bsize->y_ppem < 0 )
2684         {
2685           FT_TRACE0(( "FT_Open_Face:"
2686                       " Invalid bitmap dimensions for strike %d,"
2687                       " now disabled\n", i ));
2688           bsize->width  = 0;
2689           bsize->height = 0;
2690           bsize->size   = 0;
2691           bsize->x_ppem = 0;
2692           bsize->y_ppem = 0;
2693         }
2694       }
2695     }
2696 
2697     /* initialize internal face data */
2698     {
2699       FT_Face_Internal  internal = face->internal;
2700 
2701 
2702       internal->transform_matrix.xx = 0x10000L;
2703       internal->transform_matrix.xy = 0;
2704       internal->transform_matrix.yx = 0;
2705       internal->transform_matrix.yy = 0x10000L;
2706 
2707       internal->transform_delta.x = 0;
2708       internal->transform_delta.y = 0;
2709 
2710       internal->refcount = 1;
2711 
2712       internal->no_stem_darkening = -1;
2713 
2714 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
2715       /* Per-face filtering can only be set up by FT_Face_Properties */
2716       internal->lcd_filter_func = NULL;
2717 #endif
2718     }
2719 
2720     if ( aface )
2721       *aface = face;
2722     else
2723       FT_Done_Face( face );
2724 
2725     goto Exit;
2726 
2727   Fail:
2728     if ( node )
2729       FT_Done_Face( face );    /* face must be in the driver's list */
2730     else if ( face )
2731       destroy_face( memory, face, driver );
2732 
2733   Exit:
2734 #ifdef FT_DEBUG_LEVEL_TRACE
2735     if ( !error && face_index < 0 )
2736     {
2737       FT_TRACE3(( "FT_Open_Face: The font has %ld face%s\n",
2738                   face->num_faces,
2739                   face->num_faces == 1 ? "" : "s" ));
2740       FT_TRACE3(( "              and %ld named instance%s for face %ld\n",
2741                   face->style_flags >> 16,
2742                   ( face->style_flags >> 16 ) == 1 ? "" : "s",
2743                   -face_index - 1 ));
2744     }
2745 #endif
2746 
2747     FT_TRACE4(( "FT_Open_Face: Return 0x%x\n", error ));
2748 
2749     return error;
2750   }
2751 
2752 
2753   /* documentation is in freetype.h */
2754 
2755   FT_EXPORT_DEF( FT_Error )
FT_Attach_File(FT_Face face,const char * filepathname)2756   FT_Attach_File( FT_Face      face,
2757                   const char*  filepathname )
2758   {
2759     FT_Open_Args  open;
2760 
2761 
2762     /* test for valid `face' delayed to `FT_Attach_Stream' */
2763 
2764     if ( !filepathname )
2765       return FT_THROW( Invalid_Argument );
2766 
2767     open.stream   = NULL;
2768     open.flags    = FT_OPEN_PATHNAME;
2769     open.pathname = (char*)filepathname;
2770 
2771     return FT_Attach_Stream( face, &open );
2772   }
2773 
2774 
2775   /* documentation is in freetype.h */
2776 
2777   FT_EXPORT_DEF( FT_Error )
FT_Attach_Stream(FT_Face face,FT_Open_Args * parameters)2778   FT_Attach_Stream( FT_Face        face,
2779                     FT_Open_Args*  parameters )
2780   {
2781     FT_Stream  stream;
2782     FT_Error   error;
2783     FT_Driver  driver;
2784 
2785     FT_Driver_Class  clazz;
2786 
2787 
2788     /* test for valid `parameters' delayed to `FT_Stream_New' */
2789 
2790     if ( !face )
2791       return FT_THROW( Invalid_Face_Handle );
2792 
2793     driver = face->driver;
2794     if ( !driver )
2795       return FT_THROW( Invalid_Driver_Handle );
2796 
2797     error = FT_Stream_New( driver->root.library, parameters, &stream );
2798     if ( error )
2799       goto Exit;
2800 
2801     /* we implement FT_Attach_Stream in each driver through the */
2802     /* `attach_file' interface                                  */
2803 
2804     error = FT_ERR( Unimplemented_Feature );
2805     clazz = driver->clazz;
2806     if ( clazz->attach_file )
2807       error = clazz->attach_file( face, stream );
2808 
2809     /* close the attached stream */
2810     FT_Stream_Free( stream,
2811                     FT_BOOL( parameters->stream                     &&
2812                              ( parameters->flags & FT_OPEN_STREAM ) ) );
2813 
2814   Exit:
2815     return error;
2816   }
2817 
2818 
2819   /* documentation is in freetype.h */
2820 
2821   FT_EXPORT_DEF( FT_Error )
FT_Reference_Face(FT_Face face)2822   FT_Reference_Face( FT_Face  face )
2823   {
2824     if ( !face )
2825       return FT_THROW( Invalid_Face_Handle );
2826 
2827     face->internal->refcount++;
2828 
2829     return FT_Err_Ok;
2830   }
2831 
2832 
2833   /* documentation is in freetype.h */
2834 
2835   FT_EXPORT_DEF( FT_Error )
FT_Done_Face(FT_Face face)2836   FT_Done_Face( FT_Face  face )
2837   {
2838     FT_Error     error;
2839     FT_Driver    driver;
2840     FT_Memory    memory;
2841     FT_ListNode  node;
2842 
2843 
2844     error = FT_ERR( Invalid_Face_Handle );
2845     if ( face && face->driver )
2846     {
2847       face->internal->refcount--;
2848       if ( face->internal->refcount > 0 )
2849         error = FT_Err_Ok;
2850       else
2851       {
2852         driver = face->driver;
2853         memory = driver->root.memory;
2854 
2855         /* find face in driver's list */
2856         node = FT_List_Find( &driver->faces_list, face );
2857         if ( node )
2858         {
2859           /* remove face object from the driver's list */
2860           FT_List_Remove( &driver->faces_list, node );
2861           FT_FREE( node );
2862 
2863           /* now destroy the object proper */
2864           destroy_face( memory, face, driver );
2865           error = FT_Err_Ok;
2866         }
2867       }
2868     }
2869 
2870     return error;
2871   }
2872 
2873 
2874   /* documentation is in ftobjs.h */
2875 
2876   FT_EXPORT_DEF( FT_Error )
FT_New_Size(FT_Face face,FT_Size * asize)2877   FT_New_Size( FT_Face   face,
2878                FT_Size  *asize )
2879   {
2880     FT_Error         error;
2881     FT_Memory        memory;
2882     FT_Driver        driver;
2883     FT_Driver_Class  clazz;
2884 
2885     FT_Size          size = NULL;
2886     FT_ListNode      node = NULL;
2887 
2888     FT_Size_Internal  internal = NULL;
2889 
2890 
2891     if ( !face )
2892       return FT_THROW( Invalid_Face_Handle );
2893 
2894     if ( !asize )
2895       return FT_THROW( Invalid_Argument );
2896 
2897     if ( !face->driver )
2898       return FT_THROW( Invalid_Driver_Handle );
2899 
2900     *asize = NULL;
2901 
2902     driver = face->driver;
2903     clazz  = driver->clazz;
2904     memory = face->memory;
2905 
2906     /* Allocate new size object and perform basic initialisation */
2907     if ( FT_ALLOC( size, clazz->size_object_size ) || FT_QNEW( node ) )
2908       goto Exit;
2909 
2910     size->face = face;
2911 
2912     if ( FT_NEW( internal ) )
2913       goto Exit;
2914 
2915     size->internal = internal;
2916 
2917     if ( clazz->init_size )
2918       error = clazz->init_size( size );
2919 
2920     /* in case of success, add to the face's list */
2921     if ( !error )
2922     {
2923       *asize     = size;
2924       node->data = size;
2925       FT_List_Add( &face->sizes_list, node );
2926     }
2927 
2928   Exit:
2929     if ( error )
2930     {
2931       FT_FREE( node );
2932       if ( size )
2933         FT_FREE( size->internal );
2934       FT_FREE( size );
2935     }
2936 
2937     return error;
2938   }
2939 
2940 
2941   /* documentation is in ftobjs.h */
2942 
2943   FT_EXPORT_DEF( FT_Error )
FT_Done_Size(FT_Size size)2944   FT_Done_Size( FT_Size  size )
2945   {
2946     FT_Error     error;
2947     FT_Driver    driver;
2948     FT_Memory    memory;
2949     FT_Face      face;
2950     FT_ListNode  node;
2951 
2952 
2953     if ( !size )
2954       return FT_THROW( Invalid_Size_Handle );
2955 
2956     face = size->face;
2957     if ( !face )
2958       return FT_THROW( Invalid_Face_Handle );
2959 
2960     driver = face->driver;
2961     if ( !driver )
2962       return FT_THROW( Invalid_Driver_Handle );
2963 
2964     memory = driver->root.memory;
2965 
2966     error = FT_Err_Ok;
2967     node  = FT_List_Find( &face->sizes_list, size );
2968     if ( node )
2969     {
2970       FT_List_Remove( &face->sizes_list, node );
2971       FT_FREE( node );
2972 
2973       if ( face->size == size )
2974       {
2975         face->size = NULL;
2976         if ( face->sizes_list.head )
2977           face->size = (FT_Size)(face->sizes_list.head->data);
2978       }
2979 
2980       destroy_size( memory, size, driver );
2981     }
2982     else
2983       error = FT_THROW( Invalid_Size_Handle );
2984 
2985     return error;
2986   }
2987 
2988 
2989   /* documentation is in ftobjs.h */
2990 
2991   FT_BASE_DEF( FT_Error )
FT_Match_Size(FT_Face face,FT_Size_Request req,FT_Bool ignore_width,FT_ULong * size_index)2992   FT_Match_Size( FT_Face          face,
2993                  FT_Size_Request  req,
2994                  FT_Bool          ignore_width,
2995                  FT_ULong*        size_index )
2996   {
2997     FT_Int   i;
2998     FT_Long  w, h;
2999 
3000 
3001     if ( !FT_HAS_FIXED_SIZES( face ) )
3002       return FT_THROW( Invalid_Face_Handle );
3003 
3004     /* FT_Bitmap_Size doesn't provide enough info... */
3005     if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
3006       return FT_THROW( Unimplemented_Feature );
3007 
3008     w = FT_REQUEST_WIDTH ( req );
3009     h = FT_REQUEST_HEIGHT( req );
3010 
3011     if ( req->width && !req->height )
3012       h = w;
3013     else if ( !req->width && req->height )
3014       w = h;
3015 
3016     w = FT_PIX_ROUND( w );
3017     h = FT_PIX_ROUND( h );
3018 
3019     if ( !w || !h )
3020       return FT_THROW( Invalid_Pixel_Size );
3021 
3022     for ( i = 0; i < face->num_fixed_sizes; i++ )
3023     {
3024       FT_Bitmap_Size*  bsize = face->available_sizes + i;
3025 
3026 
3027       if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
3028         continue;
3029 
3030       if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
3031       {
3032         FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i ));
3033 
3034         if ( size_index )
3035           *size_index = (FT_ULong)i;
3036 
3037         return FT_Err_Ok;
3038       }
3039     }
3040 
3041     FT_TRACE3(( "FT_Match_Size: no matching bitmap strike\n" ));
3042 
3043     return FT_THROW( Invalid_Pixel_Size );
3044   }
3045 
3046 
3047   /* documentation is in ftobjs.h */
3048 
3049   FT_BASE_DEF( void )
ft_synthesize_vertical_metrics(FT_Glyph_Metrics * metrics,FT_Pos advance)3050   ft_synthesize_vertical_metrics( FT_Glyph_Metrics*  metrics,
3051                                   FT_Pos             advance )
3052   {
3053     FT_Pos  height = metrics->height;
3054 
3055 
3056     /* compensate for glyph with bbox above/below the baseline */
3057     if ( metrics->horiBearingY < 0 )
3058     {
3059       if ( height < metrics->horiBearingY )
3060         height = metrics->horiBearingY;
3061     }
3062     else if ( metrics->horiBearingY > 0 )
3063       height -= metrics->horiBearingY;
3064 
3065     /* the factor 1.2 is a heuristical value */
3066     if ( !advance )
3067       advance = height * 12 / 10;
3068 
3069     metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
3070     metrics->vertBearingY = ( advance - height ) / 2;
3071     metrics->vertAdvance  = advance;
3072   }
3073 
3074 
3075   static void
ft_recompute_scaled_metrics(FT_Face face,FT_Size_Metrics * metrics)3076   ft_recompute_scaled_metrics( FT_Face           face,
3077                                FT_Size_Metrics*  metrics )
3078   {
3079     /* Compute root ascender, descender, test height, and max_advance */
3080 
3081 #ifdef GRID_FIT_METRICS
3082     metrics->ascender    = FT_PIX_CEIL( FT_MulFix( face->ascender,
3083                                                    metrics->y_scale ) );
3084 
3085     metrics->descender   = FT_PIX_FLOOR( FT_MulFix( face->descender,
3086                                                     metrics->y_scale ) );
3087 
3088     metrics->height      = FT_PIX_ROUND( FT_MulFix( face->height,
3089                                                     metrics->y_scale ) );
3090 
3091     metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
3092                                                     metrics->x_scale ) );
3093 #else /* !GRID_FIT_METRICS */
3094     metrics->ascender    = FT_MulFix( face->ascender,
3095                                       metrics->y_scale );
3096 
3097     metrics->descender   = FT_MulFix( face->descender,
3098                                       metrics->y_scale );
3099 
3100     metrics->height      = FT_MulFix( face->height,
3101                                       metrics->y_scale );
3102 
3103     metrics->max_advance = FT_MulFix( face->max_advance_width,
3104                                       metrics->x_scale );
3105 #endif /* !GRID_FIT_METRICS */
3106   }
3107 
3108 
3109   FT_BASE_DEF( void )
FT_Select_Metrics(FT_Face face,FT_ULong strike_index)3110   FT_Select_Metrics( FT_Face   face,
3111                      FT_ULong  strike_index )
3112   {
3113     FT_Size_Metrics*  metrics;
3114     FT_Bitmap_Size*   bsize;
3115 
3116 
3117     metrics = &face->size->metrics;
3118     bsize   = face->available_sizes + strike_index;
3119 
3120     metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
3121     metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
3122 
3123     if ( FT_IS_SCALABLE( face ) )
3124     {
3125       metrics->x_scale = FT_DivFix( bsize->x_ppem,
3126                                     face->units_per_EM );
3127       metrics->y_scale = FT_DivFix( bsize->y_ppem,
3128                                     face->units_per_EM );
3129 
3130       ft_recompute_scaled_metrics( face, metrics );
3131     }
3132     else
3133     {
3134       metrics->x_scale     = 1L << 16;
3135       metrics->y_scale     = 1L << 16;
3136       metrics->ascender    = bsize->y_ppem;
3137       metrics->descender   = 0;
3138       metrics->height      = bsize->height << 6;
3139       metrics->max_advance = bsize->x_ppem;
3140     }
3141   }
3142 
3143 
3144   FT_BASE_DEF( FT_Error )
FT_Request_Metrics(FT_Face face,FT_Size_Request req)3145   FT_Request_Metrics( FT_Face          face,
3146                       FT_Size_Request  req )
3147   {
3148     FT_Error  error = FT_Err_Ok;
3149 
3150     FT_Size_Metrics*  metrics;
3151 
3152 
3153     metrics = &face->size->metrics;
3154 
3155     if ( FT_IS_SCALABLE( face ) )
3156     {
3157       FT_Long  w = 0, h = 0, scaled_w = 0, scaled_h = 0;
3158 
3159 
3160       switch ( req->type )
3161       {
3162       case FT_SIZE_REQUEST_TYPE_NOMINAL:
3163         w = h = face->units_per_EM;
3164         break;
3165 
3166       case FT_SIZE_REQUEST_TYPE_REAL_DIM:
3167         w = h = face->ascender - face->descender;
3168         break;
3169 
3170       case FT_SIZE_REQUEST_TYPE_BBOX:
3171         w = face->bbox.xMax - face->bbox.xMin;
3172         h = face->bbox.yMax - face->bbox.yMin;
3173         break;
3174 
3175       case FT_SIZE_REQUEST_TYPE_CELL:
3176         w = face->max_advance_width;
3177         h = face->ascender - face->descender;
3178         break;
3179 
3180       case FT_SIZE_REQUEST_TYPE_SCALES:
3181         metrics->x_scale = (FT_Fixed)req->width;
3182         metrics->y_scale = (FT_Fixed)req->height;
3183         if ( !metrics->x_scale )
3184           metrics->x_scale = metrics->y_scale;
3185         else if ( !metrics->y_scale )
3186           metrics->y_scale = metrics->x_scale;
3187         goto Calculate_Ppem;
3188 
3189       case FT_SIZE_REQUEST_TYPE_MAX:
3190         break;
3191       }
3192 
3193       /* to be on the safe side */
3194       if ( w < 0 )
3195         w = -w;
3196 
3197       if ( h < 0 )
3198         h = -h;
3199 
3200       scaled_w = FT_REQUEST_WIDTH ( req );
3201       scaled_h = FT_REQUEST_HEIGHT( req );
3202 
3203       /* determine scales */
3204       if ( req->width )
3205       {
3206         metrics->x_scale = FT_DivFix( scaled_w, w );
3207 
3208         if ( req->height )
3209         {
3210           metrics->y_scale = FT_DivFix( scaled_h, h );
3211 
3212           if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
3213           {
3214             if ( metrics->y_scale > metrics->x_scale )
3215               metrics->y_scale = metrics->x_scale;
3216             else
3217               metrics->x_scale = metrics->y_scale;
3218           }
3219         }
3220         else
3221         {
3222           metrics->y_scale = metrics->x_scale;
3223           scaled_h = FT_MulDiv( scaled_w, h, w );
3224         }
3225       }
3226       else
3227       {
3228         metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
3229         scaled_w = FT_MulDiv( scaled_h, w, h );
3230       }
3231 
3232   Calculate_Ppem:
3233       /* calculate the ppems */
3234       if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
3235       {
3236         scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
3237         scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
3238       }
3239 
3240       scaled_w = ( scaled_w + 32 ) >> 6;
3241       scaled_h = ( scaled_h + 32 ) >> 6;
3242       if ( scaled_w > (FT_Long)FT_USHORT_MAX ||
3243            scaled_h > (FT_Long)FT_USHORT_MAX )
3244       {
3245         FT_ERROR(( "FT_Request_Metrics: Resulting ppem size too large\n" ));
3246         error = FT_ERR( Invalid_Pixel_Size );
3247         goto Exit;
3248       }
3249 
3250       metrics->x_ppem = (FT_UShort)scaled_w;
3251       metrics->y_ppem = (FT_UShort)scaled_h;
3252 
3253       ft_recompute_scaled_metrics( face, metrics );
3254     }
3255     else
3256     {
3257       FT_ZERO( metrics );
3258       metrics->x_scale = 1L << 16;
3259       metrics->y_scale = 1L << 16;
3260     }
3261 
3262   Exit:
3263     return error;
3264   }
3265 
3266 
3267   /* documentation is in freetype.h */
3268 
3269   FT_EXPORT_DEF( FT_Error )
FT_Select_Size(FT_Face face,FT_Int strike_index)3270   FT_Select_Size( FT_Face  face,
3271                   FT_Int   strike_index )
3272   {
3273     FT_Error         error = FT_Err_Ok;
3274     FT_Driver_Class  clazz;
3275 
3276 
3277     if ( !face || !FT_HAS_FIXED_SIZES( face ) )
3278       return FT_THROW( Invalid_Face_Handle );
3279 
3280     if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
3281       return FT_THROW( Invalid_Argument );
3282 
3283     clazz = face->driver->clazz;
3284 
3285     if ( clazz->select_size )
3286     {
3287       error = clazz->select_size( face->size, (FT_ULong)strike_index );
3288 
3289       FT_TRACE5(( "FT_Select_Size (%s driver):\n",
3290                   face->driver->root.clazz->module_name ));
3291     }
3292     else
3293     {
3294       FT_Select_Metrics( face, (FT_ULong)strike_index );
3295 
3296       FT_TRACE5(( "FT_Select_Size:\n" ));
3297     }
3298 
3299 #ifdef FT_DEBUG_LEVEL_TRACE
3300     {
3301       FT_Size_Metrics*  metrics = &face->size->metrics;
3302 
3303 
3304       FT_TRACE5(( "  x scale: %ld (%f)\n",
3305                   metrics->x_scale, metrics->x_scale / 65536.0 ));
3306       FT_TRACE5(( "  y scale: %ld (%f)\n",
3307                   metrics->y_scale, metrics->y_scale / 65536.0 ));
3308       FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
3309       FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
3310       FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
3311       FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
3312       FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
3313       FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
3314     }
3315 #endif
3316 
3317     return error;
3318   }
3319 
3320 
3321   /* documentation is in freetype.h */
3322 
3323   FT_EXPORT_DEF( FT_Error )
FT_Request_Size(FT_Face face,FT_Size_Request req)3324   FT_Request_Size( FT_Face          face,
3325                    FT_Size_Request  req )
3326   {
3327     FT_Error         error;
3328     FT_Driver_Class  clazz;
3329     FT_ULong         strike_index;
3330 
3331 
3332     if ( !face )
3333       return FT_THROW( Invalid_Face_Handle );
3334 
3335     if ( !face->size )
3336       return FT_THROW( Invalid_Size_Handle );
3337 
3338     if ( !req || req->width < 0 || req->height < 0 ||
3339          req->type >= FT_SIZE_REQUEST_TYPE_MAX )
3340       return FT_THROW( Invalid_Argument );
3341 
3342     /* signal the auto-hinter to recompute its size metrics */
3343     /* (if requested)                                       */
3344     face->size->internal->autohint_metrics.x_scale = 0;
3345 
3346     clazz = face->driver->clazz;
3347 
3348     if ( clazz->request_size )
3349     {
3350       error = clazz->request_size( face->size, req );
3351 
3352       FT_TRACE5(( "FT_Request_Size (%s driver):\n",
3353                   face->driver->root.clazz->module_name ));
3354     }
3355     else if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
3356     {
3357       /*
3358        * The reason that a driver doesn't have `request_size' defined is
3359        * either that the scaling here suffices or that the supported formats
3360        * are bitmap-only and size matching is not implemented.
3361        *
3362        * In the latter case, a simple size matching is done.
3363        */
3364       error = FT_Match_Size( face, req, 0, &strike_index );
3365       if ( error )
3366         goto Exit;
3367 
3368       return FT_Select_Size( face, (FT_Int)strike_index );
3369     }
3370     else
3371     {
3372       error = FT_Request_Metrics( face, req );
3373       if ( error )
3374         goto Exit;
3375 
3376       FT_TRACE5(( "FT_Request_Size:\n" ));
3377     }
3378 
3379 #ifdef FT_DEBUG_LEVEL_TRACE
3380     {
3381       FT_Size_Metrics*  metrics = &face->size->metrics;
3382 
3383 
3384       FT_TRACE5(( "  x scale: %ld (%f)\n",
3385                   metrics->x_scale, metrics->x_scale / 65536.0 ));
3386       FT_TRACE5(( "  y scale: %ld (%f)\n",
3387                   metrics->y_scale, metrics->y_scale / 65536.0 ));
3388       FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
3389       FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
3390       FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
3391       FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
3392       FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
3393       FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
3394     }
3395 #endif
3396 
3397   Exit:
3398     return error;
3399   }
3400 
3401 
3402   /* documentation is in freetype.h */
3403 
3404   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)3405   FT_Set_Char_Size( FT_Face     face,
3406                     FT_F26Dot6  char_width,
3407                     FT_F26Dot6  char_height,
3408                     FT_UInt     horz_resolution,
3409                     FT_UInt     vert_resolution )
3410   {
3411     FT_Size_RequestRec  req;
3412 
3413 
3414     /* check of `face' delayed to `FT_Request_Size' */
3415 
3416     if ( !char_width )
3417       char_width = char_height;
3418     else if ( !char_height )
3419       char_height = char_width;
3420 
3421     if ( !horz_resolution )
3422       horz_resolution = vert_resolution;
3423     else if ( !vert_resolution )
3424       vert_resolution = horz_resolution;
3425 
3426     if ( char_width  < 1 * 64 )
3427       char_width  = 1 * 64;
3428     if ( char_height < 1 * 64 )
3429       char_height = 1 * 64;
3430 
3431     if ( !horz_resolution )
3432       horz_resolution = vert_resolution = 72;
3433 
3434     req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
3435     req.width          = char_width;
3436     req.height         = char_height;
3437     req.horiResolution = horz_resolution;
3438     req.vertResolution = vert_resolution;
3439 
3440     return FT_Request_Size( face, &req );
3441   }
3442 
3443 
3444   /* documentation is in freetype.h */
3445 
3446   FT_EXPORT_DEF( FT_Error )
FT_Set_Pixel_Sizes(FT_Face face,FT_UInt pixel_width,FT_UInt pixel_height)3447   FT_Set_Pixel_Sizes( FT_Face  face,
3448                       FT_UInt  pixel_width,
3449                       FT_UInt  pixel_height )
3450   {
3451     FT_Size_RequestRec  req;
3452 
3453 
3454     /* check of `face' delayed to `FT_Request_Size' */
3455 
3456     if ( pixel_width == 0 )
3457       pixel_width = pixel_height;
3458     else if ( pixel_height == 0 )
3459       pixel_height = pixel_width;
3460 
3461     if ( pixel_width  < 1 )
3462       pixel_width  = 1;
3463     if ( pixel_height < 1 )
3464       pixel_height = 1;
3465 
3466     /* use `>=' to avoid potential compiler warning on 16bit platforms */
3467     if ( pixel_width >= 0xFFFFU )
3468       pixel_width = 0xFFFFU;
3469     if ( pixel_height >= 0xFFFFU )
3470       pixel_height = 0xFFFFU;
3471 
3472     req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
3473     req.width          = (FT_Long)( pixel_width << 6 );
3474     req.height         = (FT_Long)( pixel_height << 6 );
3475     req.horiResolution = 0;
3476     req.vertResolution = 0;
3477 
3478     return FT_Request_Size( face, &req );
3479   }
3480 
3481 
3482   /* documentation is in freetype.h */
3483 
3484   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)3485   FT_Get_Kerning( FT_Face     face,
3486                   FT_UInt     left_glyph,
3487                   FT_UInt     right_glyph,
3488                   FT_UInt     kern_mode,
3489                   FT_Vector  *akerning )
3490   {
3491     FT_Error   error = FT_Err_Ok;
3492     FT_Driver  driver;
3493 
3494 
3495     if ( !face )
3496       return FT_THROW( Invalid_Face_Handle );
3497 
3498     if ( !akerning )
3499       return FT_THROW( Invalid_Argument );
3500 
3501     driver = face->driver;
3502 
3503     akerning->x = 0;
3504     akerning->y = 0;
3505 
3506     if ( driver->clazz->get_kerning )
3507     {
3508       error = driver->clazz->get_kerning( face,
3509                                           left_glyph,
3510                                           right_glyph,
3511                                           akerning );
3512       if ( !error )
3513       {
3514         if ( kern_mode != FT_KERNING_UNSCALED )
3515         {
3516           akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
3517           akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
3518 
3519           if ( kern_mode != FT_KERNING_UNFITTED )
3520           {
3521             FT_Pos  orig_x = akerning->x;
3522             FT_Pos  orig_y = akerning->y;
3523 
3524 
3525             /* we scale down kerning values for small ppem values */
3526             /* to avoid that rounding makes them too big.         */
3527             /* `25' has been determined heuristically.            */
3528             if ( face->size->metrics.x_ppem < 25 )
3529               akerning->x = FT_MulDiv( orig_x,
3530                                        face->size->metrics.x_ppem, 25 );
3531             if ( face->size->metrics.y_ppem < 25 )
3532               akerning->y = FT_MulDiv( orig_y,
3533                                        face->size->metrics.y_ppem, 25 );
3534 
3535             akerning->x = FT_PIX_ROUND( akerning->x );
3536             akerning->y = FT_PIX_ROUND( akerning->y );
3537 
3538 #ifdef FT_DEBUG_LEVEL_TRACE
3539             {
3540               FT_Pos  orig_x_rounded = FT_PIX_ROUND( orig_x );
3541               FT_Pos  orig_y_rounded = FT_PIX_ROUND( orig_y );
3542 
3543 
3544               if ( akerning->x != orig_x_rounded ||
3545                    akerning->y != orig_y_rounded )
3546                 FT_TRACE5(( "FT_Get_Kerning: horizontal kerning"
3547                             " (%ld, %ld) scaled down to (%ld, %ld) pixels\n",
3548                             orig_x_rounded / 64, orig_y_rounded / 64,
3549                             akerning->x / 64, akerning->y / 64 ));
3550             }
3551 #endif
3552           }
3553         }
3554       }
3555     }
3556 
3557     return error;
3558   }
3559 
3560 
3561   /* documentation is in freetype.h */
3562 
3563   FT_EXPORT_DEF( FT_Error )
FT_Get_Track_Kerning(FT_Face face,FT_Fixed point_size,FT_Int degree,FT_Fixed * akerning)3564   FT_Get_Track_Kerning( FT_Face    face,
3565                         FT_Fixed   point_size,
3566                         FT_Int     degree,
3567                         FT_Fixed*  akerning )
3568   {
3569     FT_Service_Kerning  service;
3570     FT_Error            error = FT_Err_Ok;
3571 
3572 
3573     if ( !face )
3574       return FT_THROW( Invalid_Face_Handle );
3575 
3576     if ( !akerning )
3577       return FT_THROW( Invalid_Argument );
3578 
3579     FT_FACE_FIND_SERVICE( face, service, KERNING );
3580     if ( !service )
3581       return FT_THROW( Unimplemented_Feature );
3582 
3583     error = service->get_track( face,
3584                                 point_size,
3585                                 degree,
3586                                 akerning );
3587 
3588     return error;
3589   }
3590 
3591 
3592   /* documentation is in freetype.h */
3593 
3594   FT_EXPORT_DEF( FT_Error )
FT_Select_Charmap(FT_Face face,FT_Encoding encoding)3595   FT_Select_Charmap( FT_Face      face,
3596                      FT_Encoding  encoding )
3597   {
3598     FT_CharMap*  cur;
3599     FT_CharMap*  limit;
3600 
3601 
3602     if ( !face )
3603       return FT_THROW( Invalid_Face_Handle );
3604 
3605     /* FT_ENCODING_NONE is a valid encoding for BDF, PCF, and Windows FNT */
3606     if ( encoding == FT_ENCODING_NONE && !face->num_charmaps )
3607       return FT_THROW( Invalid_Argument );
3608 
3609     /* FT_ENCODING_UNICODE is special.  We try to find the `best' Unicode */
3610     /* charmap available, i.e., one with UCS-4 characters, if possible.   */
3611     /*                                                                    */
3612     /* This is done by find_unicode_charmap() above, to share code.       */
3613     if ( encoding == FT_ENCODING_UNICODE )
3614       return find_unicode_charmap( face );
3615 
3616     cur = face->charmaps;
3617     if ( !cur )
3618       return FT_THROW( Invalid_CharMap_Handle );
3619 
3620     limit = cur + face->num_charmaps;
3621 
3622     for ( ; cur < limit; cur++ )
3623     {
3624       if ( cur[0]->encoding == encoding )
3625       {
3626         face->charmap = cur[0];
3627         return FT_Err_Ok;
3628       }
3629     }
3630 
3631     return FT_THROW( Invalid_Argument );
3632   }
3633 
3634 
3635   /* documentation is in freetype.h */
3636 
3637   FT_EXPORT_DEF( FT_Error )
FT_Set_Charmap(FT_Face face,FT_CharMap charmap)3638   FT_Set_Charmap( FT_Face     face,
3639                   FT_CharMap  charmap )
3640   {
3641     FT_CharMap*  cur;
3642     FT_CharMap*  limit;
3643 
3644 
3645     if ( !face )
3646       return FT_THROW( Invalid_Face_Handle );
3647 
3648     cur = face->charmaps;
3649     if ( !cur || !charmap )
3650       return FT_THROW( Invalid_CharMap_Handle );
3651 
3652     limit = cur + face->num_charmaps;
3653 
3654     for ( ; cur < limit; cur++ )
3655     {
3656       if ( cur[0] == charmap                    &&
3657            FT_Get_CMap_Format ( charmap ) != 14 )
3658       {
3659         face->charmap = cur[0];
3660         return FT_Err_Ok;
3661       }
3662     }
3663 
3664     return FT_THROW( Invalid_Argument );
3665   }
3666 
3667 
3668   /* documentation is in freetype.h */
3669 
3670   FT_EXPORT_DEF( FT_Int )
FT_Get_Charmap_Index(FT_CharMap charmap)3671   FT_Get_Charmap_Index( FT_CharMap  charmap )
3672   {
3673     FT_Int  i;
3674 
3675 
3676     if ( !charmap || !charmap->face )
3677       return -1;
3678 
3679     for ( i = 0; i < charmap->face->num_charmaps; i++ )
3680       if ( charmap->face->charmaps[i] == charmap )
3681         break;
3682 
3683     FT_ASSERT( i < charmap->face->num_charmaps );
3684 
3685     return i;
3686   }
3687 
3688 
3689   static void
ft_cmap_done_internal(FT_CMap cmap)3690   ft_cmap_done_internal( FT_CMap  cmap )
3691   {
3692     FT_CMap_Class  clazz  = cmap->clazz;
3693     FT_Face        face   = cmap->charmap.face;
3694     FT_Memory      memory = FT_FACE_MEMORY( face );
3695 
3696 
3697     if ( clazz->done )
3698       clazz->done( cmap );
3699 
3700     FT_FREE( cmap );
3701   }
3702 
3703 
3704   FT_BASE_DEF( void )
FT_CMap_Done(FT_CMap cmap)3705   FT_CMap_Done( FT_CMap  cmap )
3706   {
3707     if ( cmap )
3708     {
3709       FT_Face    face   = cmap->charmap.face;
3710       FT_Memory  memory = FT_FACE_MEMORY( face );
3711       FT_Error   error;
3712       FT_Int     i, j;
3713 
3714 
3715       for ( i = 0; i < face->num_charmaps; i++ )
3716       {
3717         if ( (FT_CMap)face->charmaps[i] == cmap )
3718         {
3719           FT_CharMap  last_charmap = face->charmaps[face->num_charmaps - 1];
3720 
3721 
3722           if ( FT_QRENEW_ARRAY( face->charmaps,
3723                                 face->num_charmaps,
3724                                 face->num_charmaps - 1 ) )
3725             return;
3726 
3727           /* remove it from our list of charmaps */
3728           for ( j = i + 1; j < face->num_charmaps; j++ )
3729           {
3730             if ( j == face->num_charmaps - 1 )
3731               face->charmaps[j - 1] = last_charmap;
3732             else
3733               face->charmaps[j - 1] = face->charmaps[j];
3734           }
3735 
3736           face->num_charmaps--;
3737 
3738           if ( (FT_CMap)face->charmap == cmap )
3739             face->charmap = NULL;
3740 
3741           ft_cmap_done_internal( cmap );
3742 
3743           break;
3744         }
3745       }
3746     }
3747   }
3748 
3749 
3750   FT_BASE_DEF( FT_Error )
FT_CMap_New(FT_CMap_Class clazz,FT_Pointer init_data,FT_CharMap charmap,FT_CMap * acmap)3751   FT_CMap_New( FT_CMap_Class  clazz,
3752                FT_Pointer     init_data,
3753                FT_CharMap     charmap,
3754                FT_CMap       *acmap )
3755   {
3756     FT_Error   error;
3757     FT_Face    face;
3758     FT_Memory  memory;
3759     FT_CMap    cmap = NULL;
3760 
3761 
3762     if ( !clazz || !charmap || !charmap->face )
3763       return FT_THROW( Invalid_Argument );
3764 
3765     face   = charmap->face;
3766     memory = FT_FACE_MEMORY( face );
3767 
3768     if ( !FT_ALLOC( cmap, clazz->size ) )
3769     {
3770       cmap->charmap = *charmap;
3771       cmap->clazz   = clazz;
3772 
3773       if ( clazz->init )
3774       {
3775         error = clazz->init( cmap, init_data );
3776         if ( error )
3777           goto Fail;
3778       }
3779 
3780       /* add it to our list of charmaps */
3781       if ( FT_QRENEW_ARRAY( face->charmaps,
3782                             face->num_charmaps,
3783                             face->num_charmaps + 1 ) )
3784         goto Fail;
3785 
3786       face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3787     }
3788 
3789   Exit:
3790     if ( acmap )
3791       *acmap = cmap;
3792 
3793     return error;
3794 
3795   Fail:
3796     ft_cmap_done_internal( cmap );
3797     cmap = NULL;
3798     goto Exit;
3799   }
3800 
3801 
3802   /* documentation is in freetype.h */
3803 
3804   FT_EXPORT_DEF( FT_UInt )
FT_Get_Char_Index(FT_Face face,FT_ULong charcode)3805   FT_Get_Char_Index( FT_Face   face,
3806                      FT_ULong  charcode )
3807   {
3808     FT_UInt  result = 0;
3809 
3810 
3811     if ( face && face->charmap )
3812     {
3813       FT_CMap  cmap = FT_CMAP( face->charmap );
3814 
3815 
3816       if ( charcode > 0xFFFFFFFFUL )
3817       {
3818         FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3819         FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
3820       }
3821 
3822       result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3823       if ( result >= (FT_UInt)face->num_glyphs )
3824         result = 0;
3825     }
3826 
3827     return result;
3828   }
3829 
3830 
3831   /* documentation is in freetype.h */
3832 
3833   FT_EXPORT_DEF( FT_ULong )
FT_Get_First_Char(FT_Face face,FT_UInt * agindex)3834   FT_Get_First_Char( FT_Face   face,
3835                      FT_UInt  *agindex )
3836   {
3837     FT_ULong  result = 0;
3838     FT_UInt   gindex = 0;
3839 
3840 
3841     /* only do something if we have a charmap, and we have glyphs at all */
3842     if ( face && face->charmap && face->num_glyphs )
3843     {
3844       gindex = FT_Get_Char_Index( face, 0 );
3845       if ( gindex == 0 )
3846         result = FT_Get_Next_Char( face, 0, &gindex );
3847     }
3848 
3849     if ( agindex )
3850       *agindex = gindex;
3851 
3852     return result;
3853   }
3854 
3855 
3856   /* documentation is in freetype.h */
3857 
3858   FT_EXPORT_DEF( FT_ULong )
FT_Get_Next_Char(FT_Face face,FT_ULong charcode,FT_UInt * agindex)3859   FT_Get_Next_Char( FT_Face   face,
3860                     FT_ULong  charcode,
3861                     FT_UInt  *agindex )
3862   {
3863     FT_ULong  result = 0;
3864     FT_UInt   gindex = 0;
3865 
3866 
3867     if ( face && face->charmap && face->num_glyphs )
3868     {
3869       FT_UInt32  code = (FT_UInt32)charcode;
3870       FT_CMap    cmap = FT_CMAP( face->charmap );
3871 
3872 
3873       do
3874       {
3875         gindex = cmap->clazz->char_next( cmap, &code );
3876 
3877       } while ( gindex >= (FT_UInt)face->num_glyphs );
3878 
3879       result = ( gindex == 0 ) ? 0 : code;
3880     }
3881 
3882     if ( agindex )
3883       *agindex = gindex;
3884 
3885     return result;
3886   }
3887 
3888 
3889   /* documentation is in freetype.h */
3890 
3891   FT_EXPORT_DEF( FT_Error )
FT_Face_Properties(FT_Face face,FT_UInt num_properties,FT_Parameter * properties)3892   FT_Face_Properties( FT_Face        face,
3893                       FT_UInt        num_properties,
3894                       FT_Parameter*  properties )
3895   {
3896     FT_Error  error = FT_Err_Ok;
3897 
3898 
3899     if ( num_properties > 0 && !properties )
3900     {
3901       error = FT_THROW( Invalid_Argument );
3902       goto Exit;
3903     }
3904 
3905     for ( ; num_properties > 0; num_properties-- )
3906     {
3907       if ( properties->tag == FT_PARAM_TAG_STEM_DARKENING )
3908       {
3909         if ( properties->data )
3910         {
3911           if ( *( (FT_Bool*)properties->data ) == TRUE )
3912             face->internal->no_stem_darkening = FALSE;
3913           else
3914             face->internal->no_stem_darkening = TRUE;
3915         }
3916         else
3917         {
3918           /* use module default */
3919           face->internal->no_stem_darkening = -1;
3920         }
3921       }
3922       else if ( properties->tag == FT_PARAM_TAG_LCD_FILTER_WEIGHTS )
3923       {
3924 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
3925         if ( properties->data )
3926         {
3927           ft_memcpy( face->internal->lcd_weights,
3928                      properties->data,
3929                      FT_LCD_FILTER_FIVE_TAPS );
3930           face->internal->lcd_filter_func = ft_lcd_filter_fir;
3931         }
3932 #else
3933         error = FT_THROW( Unimplemented_Feature );
3934         goto Exit;
3935 #endif
3936       }
3937       else if ( properties->tag == FT_PARAM_TAG_RANDOM_SEED )
3938       {
3939         if ( properties->data )
3940         {
3941           face->internal->random_seed = *( (FT_Int32*)properties->data );
3942           if ( face->internal->random_seed < 0 )
3943             face->internal->random_seed = 0;
3944         }
3945         else
3946         {
3947           /* use module default */
3948           face->internal->random_seed = -1;
3949         }
3950       }
3951       else
3952       {
3953         error = FT_THROW( Invalid_Argument );
3954         goto Exit;
3955       }
3956 
3957       if ( error )
3958         break;
3959 
3960       properties++;
3961     }
3962 
3963   Exit:
3964     return error;
3965   }
3966 
3967 
3968   /* documentation is in freetype.h */
3969 
3970   FT_EXPORT_DEF( FT_UInt )
FT_Face_GetCharVariantIndex(FT_Face face,FT_ULong charcode,FT_ULong variantSelector)3971   FT_Face_GetCharVariantIndex( FT_Face   face,
3972                                FT_ULong  charcode,
3973                                FT_ULong  variantSelector )
3974   {
3975     FT_UInt  result = 0;
3976 
3977 
3978     if ( face                                           &&
3979          face->charmap                                  &&
3980          face->charmap->encoding == FT_ENCODING_UNICODE )
3981     {
3982       FT_CharMap  charmap = find_variant_selector_charmap( face );
3983       FT_CMap     ucmap = FT_CMAP( face->charmap );
3984 
3985 
3986       if ( charmap )
3987       {
3988         FT_CMap  vcmap = FT_CMAP( charmap );
3989 
3990 
3991         if ( charcode > 0xFFFFFFFFUL )
3992         {
3993           FT_TRACE1(( "FT_Face_GetCharVariantIndex:"
3994                       " too large charcode" ));
3995           FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
3996         }
3997         if ( variantSelector > 0xFFFFFFFFUL )
3998         {
3999           FT_TRACE1(( "FT_Face_GetCharVariantIndex:"
4000                       " too large variantSelector" ));
4001           FT_TRACE1(( " 0x%lx is truncated\n", variantSelector ));
4002         }
4003 
4004         result = vcmap->clazz->char_var_index( vcmap, ucmap,
4005                                                (FT_UInt32)charcode,
4006                                                (FT_UInt32)variantSelector );
4007       }
4008     }
4009 
4010     return result;
4011   }
4012 
4013 
4014   /* documentation is in freetype.h */
4015 
4016   FT_EXPORT_DEF( FT_Int )
FT_Face_GetCharVariantIsDefault(FT_Face face,FT_ULong charcode,FT_ULong variantSelector)4017   FT_Face_GetCharVariantIsDefault( FT_Face   face,
4018                                    FT_ULong  charcode,
4019                                    FT_ULong  variantSelector )
4020   {
4021     FT_Int  result = -1;
4022 
4023 
4024     if ( face )
4025     {
4026       FT_CharMap  charmap = find_variant_selector_charmap( face );
4027 
4028 
4029       if ( charmap )
4030       {
4031         FT_CMap  vcmap = FT_CMAP( charmap );
4032 
4033 
4034         if ( charcode > 0xFFFFFFFFUL )
4035         {
4036           FT_TRACE1(( "FT_Face_GetCharVariantIsDefault:"
4037                       " too large charcode" ));
4038           FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
4039         }
4040         if ( variantSelector > 0xFFFFFFFFUL )
4041         {
4042           FT_TRACE1(( "FT_Face_GetCharVariantIsDefault:"
4043                       " too large variantSelector" ));
4044           FT_TRACE1(( " 0x%lx is truncated\n", variantSelector ));
4045         }
4046 
4047         result = vcmap->clazz->char_var_default( vcmap,
4048                                                  (FT_UInt32)charcode,
4049                                                  (FT_UInt32)variantSelector );
4050       }
4051     }
4052 
4053     return result;
4054   }
4055 
4056 
4057   /* documentation is in freetype.h */
4058 
4059   FT_EXPORT_DEF( FT_UInt32* )
FT_Face_GetVariantSelectors(FT_Face face)4060   FT_Face_GetVariantSelectors( FT_Face  face )
4061   {
4062     FT_UInt32  *result = NULL;
4063 
4064 
4065     if ( face )
4066     {
4067       FT_CharMap  charmap = find_variant_selector_charmap( face );
4068 
4069 
4070       if ( charmap )
4071       {
4072         FT_CMap    vcmap  = FT_CMAP( charmap );
4073         FT_Memory  memory = FT_FACE_MEMORY( face );
4074 
4075 
4076         result = vcmap->clazz->variant_list( vcmap, memory );
4077       }
4078     }
4079 
4080     return result;
4081   }
4082 
4083 
4084   /* documentation is in freetype.h */
4085 
4086   FT_EXPORT_DEF( FT_UInt32* )
FT_Face_GetVariantsOfChar(FT_Face face,FT_ULong charcode)4087   FT_Face_GetVariantsOfChar( FT_Face   face,
4088                              FT_ULong  charcode )
4089   {
4090     FT_UInt32  *result = NULL;
4091 
4092 
4093     if ( face )
4094     {
4095       FT_CharMap  charmap = find_variant_selector_charmap( face );
4096 
4097 
4098       if ( charmap )
4099       {
4100         FT_CMap    vcmap  = FT_CMAP( charmap );
4101         FT_Memory  memory = FT_FACE_MEMORY( face );
4102 
4103 
4104         if ( charcode > 0xFFFFFFFFUL )
4105         {
4106           FT_TRACE1(( "FT_Face_GetVariantsOfChar: too large charcode" ));
4107           FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
4108         }
4109 
4110         result = vcmap->clazz->charvariant_list( vcmap, memory,
4111                                                  (FT_UInt32)charcode );
4112       }
4113     }
4114     return result;
4115   }
4116 
4117 
4118   /* documentation is in freetype.h */
4119 
4120   FT_EXPORT_DEF( FT_UInt32* )
FT_Face_GetCharsOfVariant(FT_Face face,FT_ULong variantSelector)4121   FT_Face_GetCharsOfVariant( FT_Face   face,
4122                              FT_ULong  variantSelector )
4123   {
4124     FT_UInt32  *result = NULL;
4125 
4126 
4127     if ( face )
4128     {
4129       FT_CharMap  charmap = find_variant_selector_charmap( face );
4130 
4131 
4132       if ( charmap )
4133       {
4134         FT_CMap    vcmap  = FT_CMAP( charmap );
4135         FT_Memory  memory = FT_FACE_MEMORY( face );
4136 
4137 
4138         if ( variantSelector > 0xFFFFFFFFUL )
4139         {
4140           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
4141           FT_TRACE1(( " 0x%lx is truncated\n", variantSelector ));
4142         }
4143 
4144         result = vcmap->clazz->variantchar_list( vcmap, memory,
4145                                                  (FT_UInt32)variantSelector );
4146       }
4147     }
4148 
4149     return result;
4150   }
4151 
4152 
4153   /* documentation is in freetype.h */
4154 
4155   FT_EXPORT_DEF( FT_UInt )
FT_Get_Name_Index(FT_Face face,const FT_String * glyph_name)4156   FT_Get_Name_Index( FT_Face           face,
4157                      const FT_String*  glyph_name )
4158   {
4159     FT_UInt  result = 0;
4160 
4161 
4162     if ( face                       &&
4163          FT_HAS_GLYPH_NAMES( face ) &&
4164          glyph_name                 )
4165     {
4166       FT_Service_GlyphDict  service;
4167 
4168 
4169       FT_FACE_LOOKUP_SERVICE( face,
4170                               service,
4171                               GLYPH_DICT );
4172 
4173       if ( service && service->name_index )
4174         result = service->name_index( face, glyph_name );
4175     }
4176 
4177     return result;
4178   }
4179 
4180 
4181   /* documentation is in freetype.h */
4182 
4183   FT_EXPORT_DEF( FT_Error )
FT_Get_Glyph_Name(FT_Face face,FT_UInt glyph_index,FT_Pointer buffer,FT_UInt buffer_max)4184   FT_Get_Glyph_Name( FT_Face     face,
4185                      FT_UInt     glyph_index,
4186                      FT_Pointer  buffer,
4187                      FT_UInt     buffer_max )
4188   {
4189     FT_Error              error;
4190     FT_Service_GlyphDict  service;
4191 
4192 
4193     if ( !face )
4194       return FT_THROW( Invalid_Face_Handle );
4195 
4196     if ( !buffer || buffer_max == 0 )
4197       return FT_THROW( Invalid_Argument );
4198 
4199     /* clean up buffer */
4200     ((FT_Byte*)buffer)[0] = '\0';
4201 
4202     if ( (FT_Long)glyph_index >= face->num_glyphs )
4203       return FT_THROW( Invalid_Glyph_Index );
4204 
4205     if ( !FT_HAS_GLYPH_NAMES( face ) )
4206       return FT_THROW( Invalid_Argument );
4207 
4208     FT_FACE_LOOKUP_SERVICE( face, service, GLYPH_DICT );
4209     if ( service && service->get_name )
4210       error = service->get_name( face, glyph_index, buffer, buffer_max );
4211     else
4212       error = FT_THROW( Invalid_Argument );
4213 
4214     return error;
4215   }
4216 
4217 
4218   /* documentation is in freetype.h */
4219 
4220   FT_EXPORT_DEF( const char* )
FT_Get_Postscript_Name(FT_Face face)4221   FT_Get_Postscript_Name( FT_Face  face )
4222   {
4223     const char*  result = NULL;
4224 
4225 
4226     if ( !face )
4227       goto Exit;
4228 
4229     if ( !result )
4230     {
4231       FT_Service_PsFontName  service;
4232 
4233 
4234       FT_FACE_LOOKUP_SERVICE( face,
4235                               service,
4236                               POSTSCRIPT_FONT_NAME );
4237 
4238       if ( service && service->get_ps_font_name )
4239         result = service->get_ps_font_name( face );
4240     }
4241 
4242   Exit:
4243     return result;
4244   }
4245 
4246 
4247   /* documentation is in tttables.h */
4248 
4249   FT_EXPORT_DEF( void* )
FT_Get_Sfnt_Table(FT_Face face,FT_Sfnt_Tag tag)4250   FT_Get_Sfnt_Table( FT_Face      face,
4251                      FT_Sfnt_Tag  tag )
4252   {
4253     void*                  table = NULL;
4254     FT_Service_SFNT_Table  service;
4255 
4256 
4257     if ( face && FT_IS_SFNT( face ) )
4258     {
4259       FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
4260       if ( service )
4261         table = service->get_table( face, tag );
4262     }
4263 
4264     return table;
4265   }
4266 
4267 
4268   /* documentation is in tttables.h */
4269 
4270   FT_EXPORT_DEF( FT_Error )
FT_Load_Sfnt_Table(FT_Face face,FT_ULong tag,FT_Long offset,FT_Byte * buffer,FT_ULong * length)4271   FT_Load_Sfnt_Table( FT_Face    face,
4272                       FT_ULong   tag,
4273                       FT_Long    offset,
4274                       FT_Byte*   buffer,
4275                       FT_ULong*  length )
4276   {
4277     FT_Service_SFNT_Table  service;
4278 
4279 
4280     if ( !face || !FT_IS_SFNT( face ) )
4281       return FT_THROW( Invalid_Face_Handle );
4282 
4283     FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
4284     if ( !service )
4285       return FT_THROW( Unimplemented_Feature );
4286 
4287     return service->load_table( face, tag, offset, buffer, length );
4288   }
4289 
4290 
4291   /* documentation is in tttables.h */
4292 
4293   FT_EXPORT_DEF( FT_Error )
FT_Sfnt_Table_Info(FT_Face face,FT_UInt table_index,FT_ULong * tag,FT_ULong * length)4294   FT_Sfnt_Table_Info( FT_Face    face,
4295                       FT_UInt    table_index,
4296                       FT_ULong  *tag,
4297                       FT_ULong  *length )
4298   {
4299     FT_Service_SFNT_Table  service;
4300     FT_ULong               offset;
4301 
4302 
4303     /* test for valid `length' delayed to `service->table_info' */
4304 
4305     if ( !face || !FT_IS_SFNT( face ) )
4306       return FT_THROW( Invalid_Face_Handle );
4307 
4308     FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
4309     if ( !service )
4310       return FT_THROW( Unimplemented_Feature );
4311 
4312     return service->table_info( face, table_index, tag, &offset, length );
4313   }
4314 
4315 
4316   /* documentation is in tttables.h */
4317 
4318   FT_EXPORT_DEF( FT_ULong )
FT_Get_CMap_Language_ID(FT_CharMap charmap)4319   FT_Get_CMap_Language_ID( FT_CharMap  charmap )
4320   {
4321     FT_Service_TTCMaps  service;
4322     FT_Face             face;
4323     TT_CMapInfo         cmap_info;
4324 
4325 
4326     if ( !charmap || !charmap->face )
4327       return 0;
4328 
4329     face = charmap->face;
4330     FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
4331     if ( !service )
4332       return 0;
4333     if ( service->get_cmap_info( charmap, &cmap_info ))
4334       return 0;
4335 
4336     return cmap_info.language;
4337   }
4338 
4339 
4340   /* documentation is in tttables.h */
4341 
4342   FT_EXPORT_DEF( FT_Long )
FT_Get_CMap_Format(FT_CharMap charmap)4343   FT_Get_CMap_Format( FT_CharMap  charmap )
4344   {
4345     FT_Service_TTCMaps  service;
4346     FT_Face             face;
4347     TT_CMapInfo         cmap_info;
4348 
4349 
4350     if ( !charmap || !charmap->face )
4351       return -1;
4352 
4353     face = charmap->face;
4354     FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
4355     if ( !service )
4356       return -1;
4357     if ( service->get_cmap_info( charmap, &cmap_info ))
4358       return -1;
4359 
4360     return cmap_info.format;
4361   }
4362 
4363 
4364   /* documentation is in ftsizes.h */
4365 
4366   FT_EXPORT_DEF( FT_Error )
FT_Activate_Size(FT_Size size)4367   FT_Activate_Size( FT_Size  size )
4368   {
4369     FT_Face  face;
4370 
4371 
4372     if ( !size )
4373       return FT_THROW( Invalid_Size_Handle );
4374 
4375     face = size->face;
4376     if ( !face || !face->driver )
4377       return FT_THROW( Invalid_Face_Handle );
4378 
4379     /* we don't need anything more complex than that; all size objects */
4380     /* are already listed by the face                                  */
4381     face->size = size;
4382 
4383     return FT_Err_Ok;
4384   }
4385 
4386 
4387   /*************************************************************************/
4388   /*************************************************************************/
4389   /*************************************************************************/
4390   /****                                                                 ****/
4391   /****                                                                 ****/
4392   /****                        R E N D E R E R S                        ****/
4393   /****                                                                 ****/
4394   /****                                                                 ****/
4395   /*************************************************************************/
4396   /*************************************************************************/
4397   /*************************************************************************/
4398 
4399   /* lookup a renderer by glyph format in the library's list */
4400   FT_BASE_DEF( FT_Renderer )
FT_Lookup_Renderer(FT_Library library,FT_Glyph_Format format,FT_ListNode * node)4401   FT_Lookup_Renderer( FT_Library       library,
4402                       FT_Glyph_Format  format,
4403                       FT_ListNode*     node )
4404   {
4405     FT_ListNode  cur;
4406     FT_Renderer  result = NULL;
4407 
4408 
4409     if ( !library )
4410       goto Exit;
4411 
4412     cur = library->renderers.head;
4413 
4414     if ( node )
4415     {
4416       if ( *node )
4417         cur = (*node)->next;
4418       *node = NULL;
4419     }
4420 
4421     while ( cur )
4422     {
4423       FT_Renderer  renderer = FT_RENDERER( cur->data );
4424 
4425 
4426       if ( renderer->glyph_format == format )
4427       {
4428         if ( node )
4429           *node = cur;
4430 
4431         result = renderer;
4432         break;
4433       }
4434       cur = cur->next;
4435     }
4436 
4437   Exit:
4438     return result;
4439   }
4440 
4441 
4442   static FT_Renderer
ft_lookup_glyph_renderer(FT_GlyphSlot slot)4443   ft_lookup_glyph_renderer( FT_GlyphSlot  slot )
4444   {
4445     FT_Face      face    = slot->face;
4446     FT_Library   library = FT_FACE_LIBRARY( face );
4447     FT_Renderer  result  = library->cur_renderer;
4448 
4449 
4450     if ( !result || result->glyph_format != slot->format )
4451       result = FT_Lookup_Renderer( library, slot->format, 0 );
4452 
4453     return result;
4454   }
4455 
4456 
4457   static void
ft_set_current_renderer(FT_Library library)4458   ft_set_current_renderer( FT_Library  library )
4459   {
4460     FT_Renderer  renderer;
4461 
4462 
4463     renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
4464     library->cur_renderer = renderer;
4465   }
4466 
4467 
4468   static FT_Error
ft_add_renderer(FT_Module module)4469   ft_add_renderer( FT_Module  module )
4470   {
4471     FT_Library   library = module->library;
4472     FT_Memory    memory  = library->memory;
4473     FT_Error     error;
4474     FT_ListNode  node    = NULL;
4475 
4476 
4477     if ( FT_QNEW( node ) )
4478       goto Exit;
4479 
4480     {
4481       FT_Renderer         render = FT_RENDERER( module );
4482       FT_Renderer_Class*  clazz  = (FT_Renderer_Class*)module->clazz;
4483 
4484 
4485       render->clazz        = clazz;
4486       render->glyph_format = clazz->glyph_format;
4487 
4488       /* allocate raster object if needed */
4489       if ( clazz->raster_class->raster_new )
4490       {
4491         error = clazz->raster_class->raster_new( memory, &render->raster );
4492         if ( error )
4493           goto Fail;
4494 
4495         render->raster_render = clazz->raster_class->raster_render;
4496         render->render        = clazz->render_glyph;
4497       }
4498 
4499       /* add to list */
4500       node->data = module;
4501       FT_List_Add( &library->renderers, node );
4502 
4503       ft_set_current_renderer( library );
4504     }
4505 
4506   Fail:
4507     if ( error )
4508       FT_FREE( node );
4509 
4510   Exit:
4511     return error;
4512   }
4513 
4514 
4515   static void
ft_remove_renderer(FT_Module module)4516   ft_remove_renderer( FT_Module  module )
4517   {
4518     FT_Library   library;
4519     FT_Memory    memory;
4520     FT_ListNode  node;
4521 
4522 
4523     library = module->library;
4524     if ( !library )
4525       return;
4526 
4527     memory = library->memory;
4528 
4529     node = FT_List_Find( &library->renderers, module );
4530     if ( node )
4531     {
4532       FT_Renderer  render = FT_RENDERER( module );
4533 
4534 
4535       /* release raster object, if any */
4536       if ( render->raster )
4537         render->clazz->raster_class->raster_done( render->raster );
4538 
4539       /* remove from list */
4540       FT_List_Remove( &library->renderers, node );
4541       FT_FREE( node );
4542 
4543       ft_set_current_renderer( library );
4544     }
4545   }
4546 
4547 
4548   /* documentation is in ftrender.h */
4549 
4550   FT_EXPORT_DEF( FT_Renderer )
FT_Get_Renderer(FT_Library library,FT_Glyph_Format format)4551   FT_Get_Renderer( FT_Library       library,
4552                    FT_Glyph_Format  format )
4553   {
4554     /* test for valid `library' delayed to `FT_Lookup_Renderer' */
4555 
4556     return FT_Lookup_Renderer( library, format, 0 );
4557   }
4558 
4559 
4560   /* documentation is in ftrender.h */
4561 
4562   FT_EXPORT_DEF( FT_Error )
FT_Set_Renderer(FT_Library library,FT_Renderer renderer,FT_UInt num_params,FT_Parameter * parameters)4563   FT_Set_Renderer( FT_Library     library,
4564                    FT_Renderer    renderer,
4565                    FT_UInt        num_params,
4566                    FT_Parameter*  parameters )
4567   {
4568     FT_ListNode  node;
4569     FT_Error     error = FT_Err_Ok;
4570 
4571     FT_Renderer_SetModeFunc  set_mode;
4572 
4573 
4574     if ( !library )
4575     {
4576       error = FT_THROW( Invalid_Library_Handle );
4577       goto Exit;
4578     }
4579 
4580     if ( !renderer )
4581     {
4582       error = FT_THROW( Invalid_Argument );
4583       goto Exit;
4584     }
4585 
4586     if ( num_params > 0 && !parameters )
4587     {
4588       error = FT_THROW( Invalid_Argument );
4589       goto Exit;
4590     }
4591 
4592     node = FT_List_Find( &library->renderers, renderer );
4593     if ( !node )
4594     {
4595       error = FT_THROW( Invalid_Argument );
4596       goto Exit;
4597     }
4598 
4599     FT_List_Up( &library->renderers, node );
4600 
4601     if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
4602       library->cur_renderer = renderer;
4603 
4604     set_mode = renderer->clazz->set_mode;
4605 
4606     for ( ; num_params > 0; num_params-- )
4607     {
4608       error = set_mode( renderer, parameters->tag, parameters->data );
4609       if ( error )
4610         break;
4611       parameters++;
4612     }
4613 
4614   Exit:
4615     return error;
4616   }
4617 
4618 
4619   FT_BASE_DEF( FT_Error )
FT_Render_Glyph_Internal(FT_Library library,FT_GlyphSlot slot,FT_Render_Mode render_mode)4620   FT_Render_Glyph_Internal( FT_Library      library,
4621                             FT_GlyphSlot    slot,
4622                             FT_Render_Mode  render_mode )
4623   {
4624     FT_Error     error = FT_Err_Ok;
4625     FT_Face      face  = slot->face;
4626     FT_Renderer  renderer;
4627 
4628 
4629     switch ( slot->format )
4630     {
4631     default:
4632       if ( slot->internal->load_flags & FT_LOAD_COLOR )
4633       {
4634         FT_LayerIterator  iterator;
4635 
4636         FT_UInt  base_glyph = slot->glyph_index;
4637 
4638         FT_Bool  have_layers;
4639         FT_UInt  glyph_index;
4640         FT_UInt  color_index;
4641 
4642 
4643         /* check whether we have colored glyph layers */
4644         iterator.p  = NULL;
4645         have_layers = FT_Get_Color_Glyph_Layer( face,
4646                                                 base_glyph,
4647                                                 &glyph_index,
4648                                                 &color_index,
4649                                                 &iterator );
4650         if ( have_layers )
4651         {
4652           error = FT_New_GlyphSlot( face, NULL );
4653           if ( !error )
4654           {
4655             TT_Face       ttface = (TT_Face)face;
4656             SFNT_Service  sfnt   = (SFNT_Service)ttface->sfnt;
4657 
4658 
4659             do
4660             {
4661               FT_Int32  load_flags = slot->internal->load_flags;
4662 
4663 
4664               /* disable the `FT_LOAD_COLOR' flag to avoid recursion */
4665               /* right here in this function                         */
4666               load_flags &= ~FT_LOAD_COLOR;
4667 
4668               /* render into the new `face->glyph' glyph slot */
4669               load_flags |= FT_LOAD_RENDER;
4670 
4671               error = FT_Load_Glyph( face, glyph_index, load_flags );
4672               if ( error )
4673                 break;
4674 
4675               /* blend new `face->glyph' into old `slot'; */
4676               /* at the first call, `slot' is still empty */
4677               error = sfnt->colr_blend( ttface,
4678                                         color_index,
4679                                         slot,
4680                                         face->glyph );
4681               if ( error )
4682                 break;
4683 
4684             } while ( FT_Get_Color_Glyph_Layer( face,
4685                                                 base_glyph,
4686                                                 &glyph_index,
4687                                                 &color_index,
4688                                                 &iterator ) );
4689 
4690             if ( !error )
4691               slot->format = FT_GLYPH_FORMAT_BITMAP;
4692 
4693             /* this call also restores `slot' as the glyph slot */
4694             FT_Done_GlyphSlot( face->glyph );
4695           }
4696 
4697           if ( !error )
4698             return error;
4699 
4700           /* Failed to do the colored layer.  Draw outline instead. */
4701           slot->format = FT_GLYPH_FORMAT_OUTLINE;
4702         }
4703       }
4704 
4705       {
4706         FT_ListNode  node = NULL;
4707 
4708 
4709         /* small shortcut for the very common case */
4710         if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
4711         {
4712           renderer = library->cur_renderer;
4713           node     = library->renderers.head;
4714         }
4715         else
4716           renderer = FT_Lookup_Renderer( library, slot->format, &node );
4717 
4718         error = FT_ERR( Cannot_Render_Glyph );
4719         while ( renderer )
4720         {
4721           error = renderer->render( renderer, slot, render_mode, NULL );
4722           if ( !error                                   ||
4723                FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
4724             break;
4725 
4726           /* FT_Err_Cannot_Render_Glyph is returned if the render mode   */
4727           /* is unsupported by the current renderer for this glyph image */
4728           /* format.                                                     */
4729 
4730           /* now, look for another renderer that supports the same */
4731           /* format.                                               */
4732           renderer = FT_Lookup_Renderer( library, slot->format, &node );
4733         }
4734 
4735         /* it is not an error if we cannot render a bitmap glyph */
4736         if ( FT_ERR_EQ( error, Cannot_Render_Glyph ) &&
4737              slot->format == FT_GLYPH_FORMAT_BITMAP  )
4738           error = FT_Err_Ok;
4739       }
4740     }
4741 
4742 #ifdef FT_DEBUG_LEVEL_TRACE
4743 
4744 #undef  FT_COMPONENT
4745 #define FT_COMPONENT  checksum
4746 
4747     /*
4748      * Computing the MD5 checksum is expensive, unnecessarily distorting a
4749      * possible profiling of FreeType if compiled with tracing support.  For
4750      * this reason, we execute the following code only if explicitly
4751      * requested.
4752      */
4753 
4754     /* we use FT_TRACE3 in this block */
4755     if ( !error                               &&
4756          ft_trace_levels[trace_checksum] >= 3 &&
4757          slot->bitmap.buffer                  )
4758     {
4759       FT_Bitmap  bitmap;
4760       FT_Error   err;
4761 
4762 
4763       FT_Bitmap_Init( &bitmap );
4764 
4765       /* we convert to a single bitmap format for computing the checksum */
4766       /* this also converts the bitmap flow to `down' (i.e., pitch > 0)  */
4767       err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 );
4768       if ( !err )
4769       {
4770         MD5_CTX        ctx;
4771         unsigned char  md5[16];
4772         unsigned long  coverage = 0;
4773         int            i, j;
4774         int            rows  = (int)bitmap.rows;
4775         int            pitch = bitmap.pitch;
4776 
4777 
4778         FT_TRACE3(( "FT_Render_Glyph: bitmap %dx%d, %s (mode %d)\n",
4779                     pitch,
4780                     rows,
4781                     pixel_modes[slot->bitmap.pixel_mode],
4782                     slot->bitmap.pixel_mode ));
4783 
4784         for ( i = 0; i < rows; i++ )
4785           for ( j = 0; j < pitch; j++ )
4786             coverage += bitmap.buffer[i * pitch + j];
4787 
4788         FT_TRACE3(( "  Total coverage: %lu\n", coverage ));
4789 
4790         MD5_Init( &ctx );
4791         if ( bitmap.buffer )
4792           MD5_Update( &ctx, bitmap.buffer,
4793                       (unsigned long)rows * (unsigned long)pitch );
4794         MD5_Final( md5, &ctx );
4795 
4796         FT_TRACE3(( "  MD5 checksum: " ));
4797         for ( i = 0; i < 16; i++ )
4798           FT_TRACE3(( "%02X", md5[i] ));
4799         FT_TRACE3(( "\n" ));
4800       }
4801 
4802       FT_Bitmap_Done( library, &bitmap );
4803     }
4804 
4805     /*
4806      * Dump bitmap in Netpbm format (PBM or PGM).
4807      */
4808 
4809     /* we use FT_TRACE7 in this block */
4810     if ( !error                               &&
4811          ft_trace_levels[trace_checksum] >= 7 &&
4812          slot->bitmap.buffer                  )
4813     {
4814       if ( slot->bitmap.rows  < 128U &&
4815            slot->bitmap.width < 128U )
4816       {
4817         int  rows  = (int)slot->bitmap.rows;
4818         int  width = (int)slot->bitmap.width;
4819         int  pitch =      slot->bitmap.pitch;
4820         int  i, j, m;
4821 
4822         unsigned char*  topleft = slot->bitmap.buffer;
4823 
4824 
4825         if ( pitch < 0 )
4826           topleft -= pitch * ( rows - 1 );
4827 
4828         FT_TRACE7(( "Netpbm image: start\n" ));
4829         switch ( slot->bitmap.pixel_mode )
4830         {
4831         case FT_PIXEL_MODE_MONO:
4832           FT_TRACE7(( "P1 %d %d\n", width, rows ));
4833           for ( i = 0; i < rows; i++ )
4834           {
4835             for ( j = 0; j < width; )
4836               for ( m = 128; m > 0 && j < width; m >>= 1, j++ )
4837                 FT_TRACE7(( " %d",
4838                             ( topleft[i * pitch + j / 8] & m ) != 0 ));
4839             FT_TRACE7(( "\n" ));
4840           }
4841           break;
4842 
4843         default:
4844           FT_TRACE7(( "P2 %d %d 255\n", width, rows ));
4845           for ( i = 0; i < rows; i++ )
4846           {
4847             for ( j = 0; j < width; j += 1 )
4848               FT_TRACE7(( " %3u", topleft[i * pitch + j] ));
4849             FT_TRACE7(( "\n" ));
4850           }
4851         }
4852         FT_TRACE7(( "Netpbm image: end\n" ));
4853       }
4854       else
4855         FT_TRACE7(( "Netpbm image: too large, omitted\n" ));
4856     }
4857 
4858 #undef  FT_COMPONENT
4859 #define FT_COMPONENT  objs
4860 
4861 #endif /* FT_DEBUG_LEVEL_TRACE */
4862 
4863     return error;
4864   }
4865 
4866 
4867   /* documentation is in freetype.h */
4868 
4869   FT_EXPORT_DEF( FT_Error )
FT_Render_Glyph(FT_GlyphSlot slot,FT_Render_Mode render_mode)4870   FT_Render_Glyph( FT_GlyphSlot    slot,
4871                    FT_Render_Mode  render_mode )
4872   {
4873     FT_Library  library;
4874 
4875 
4876     if ( !slot || !slot->face )
4877       return FT_THROW( Invalid_Argument );
4878 
4879     library = FT_FACE_LIBRARY( slot->face );
4880 
4881     return FT_Render_Glyph_Internal( library, slot, render_mode );
4882   }
4883 
4884 
4885   /*************************************************************************/
4886   /*************************************************************************/
4887   /*************************************************************************/
4888   /****                                                                 ****/
4889   /****                                                                 ****/
4890   /****                         M O D U L E S                           ****/
4891   /****                                                                 ****/
4892   /****                                                                 ****/
4893   /*************************************************************************/
4894   /*************************************************************************/
4895   /*************************************************************************/
4896 
4897 
4898   /**************************************************************************
4899    *
4900    * @Function:
4901    *   Destroy_Module
4902    *
4903    * @Description:
4904    *   Destroys a given module object.  For drivers, this also destroys
4905    *   all child faces.
4906    *
4907    * @InOut:
4908    *   module ::
4909    *     A handle to the target driver object.
4910    *
4911    * @Note:
4912    *   The driver _must_ be LOCKED!
4913    */
4914   static void
Destroy_Module(FT_Module module)4915   Destroy_Module( FT_Module  module )
4916   {
4917     FT_Memory         memory  = module->memory;
4918     FT_Module_Class*  clazz   = module->clazz;
4919     FT_Library        library = module->library;
4920 
4921 
4922     if ( library && library->auto_hinter == module )
4923       library->auto_hinter = NULL;
4924 
4925     /* if the module is a renderer */
4926     if ( FT_MODULE_IS_RENDERER( module ) )
4927       ft_remove_renderer( module );
4928 
4929     /* if the module is a font driver, add some steps */
4930     if ( FT_MODULE_IS_DRIVER( module ) )
4931       Destroy_Driver( FT_DRIVER( module ) );
4932 
4933     /* finalize the module object */
4934     if ( clazz->module_done )
4935       clazz->module_done( module );
4936 
4937     /* discard it */
4938     FT_FREE( module );
4939   }
4940 
4941 
4942   /* documentation is in ftmodapi.h */
4943 
4944   FT_EXPORT_DEF( FT_Error )
FT_Add_Module(FT_Library library,const FT_Module_Class * clazz)4945   FT_Add_Module( FT_Library              library,
4946                  const FT_Module_Class*  clazz )
4947   {
4948     FT_Error   error;
4949     FT_Memory  memory;
4950     FT_Module  module = NULL;
4951     FT_UInt    nn;
4952 
4953 
4954 #define FREETYPE_VER_FIXED  ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
4955                                 FREETYPE_MINOR                  )
4956 
4957     if ( !library )
4958       return FT_THROW( Invalid_Library_Handle );
4959 
4960     if ( !clazz )
4961       return FT_THROW( Invalid_Argument );
4962 
4963     /* check FreeType version */
4964     if ( clazz->module_requires > FREETYPE_VER_FIXED )
4965       return FT_THROW( Invalid_Version );
4966 
4967     /* look for a module with the same name in the library's table */
4968     for ( nn = 0; nn < library->num_modules; nn++ )
4969     {
4970       module = library->modules[nn];
4971       if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
4972       {
4973         /* this installed module has the same name, compare their versions */
4974         if ( clazz->module_version <= module->clazz->module_version )
4975           return FT_THROW( Lower_Module_Version );
4976 
4977         /* remove the module from our list, then exit the loop to replace */
4978         /* it by our new version..                                        */
4979         FT_Remove_Module( library, module );
4980         break;
4981       }
4982     }
4983 
4984     memory = library->memory;
4985     error  = FT_Err_Ok;
4986 
4987     if ( library->num_modules >= FT_MAX_MODULES )
4988     {
4989       error = FT_THROW( Too_Many_Drivers );
4990       goto Exit;
4991     }
4992 
4993     /* allocate module object */
4994     if ( FT_ALLOC( module, clazz->module_size ) )
4995       goto Exit;
4996 
4997     /* base initialization */
4998     module->library = library;
4999     module->memory  = memory;
5000     module->clazz   = (FT_Module_Class*)clazz;
5001 
5002     /* check whether the module is a renderer - this must be performed */
5003     /* before the normal module initialization                         */
5004     if ( FT_MODULE_IS_RENDERER( module ) )
5005     {
5006       /* add to the renderers list */
5007       error = ft_add_renderer( module );
5008       if ( error )
5009         goto Fail;
5010     }
5011 
5012     /* is the module a auto-hinter? */
5013     if ( FT_MODULE_IS_HINTER( module ) )
5014       library->auto_hinter = module;
5015 
5016     /* if the module is a font driver */
5017     if ( FT_MODULE_IS_DRIVER( module ) )
5018     {
5019       FT_Driver  driver = FT_DRIVER( module );
5020 
5021 
5022       driver->clazz = (FT_Driver_Class)module->clazz;
5023     }
5024 
5025     if ( clazz->module_init )
5026     {
5027       error = clazz->module_init( module );
5028       if ( error )
5029         goto Fail;
5030     }
5031 
5032     /* add module to the library's table */
5033     library->modules[library->num_modules++] = module;
5034 
5035   Exit:
5036     return error;
5037 
5038   Fail:
5039     if ( FT_MODULE_IS_RENDERER( module ) )
5040     {
5041       FT_Renderer  renderer = FT_RENDERER( module );
5042 
5043 
5044       if ( renderer->clazz                                          &&
5045            renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
5046            renderer->raster                                         )
5047         renderer->clazz->raster_class->raster_done( renderer->raster );
5048     }
5049 
5050     FT_FREE( module );
5051     goto Exit;
5052   }
5053 
5054 
5055   /* documentation is in ftmodapi.h */
5056 
5057   FT_EXPORT_DEF( FT_Module )
FT_Get_Module(FT_Library library,const char * module_name)5058   FT_Get_Module( FT_Library   library,
5059                  const char*  module_name )
5060   {
5061     FT_Module   result = NULL;
5062     FT_Module*  cur;
5063     FT_Module*  limit;
5064 
5065 
5066     if ( !library || !module_name )
5067       return result;
5068 
5069     cur   = library->modules;
5070     limit = cur + library->num_modules;
5071 
5072     for ( ; cur < limit; cur++ )
5073       if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
5074       {
5075         result = cur[0];
5076         break;
5077       }
5078 
5079     return result;
5080   }
5081 
5082 
5083   /* documentation is in ftobjs.h */
5084 
5085   FT_BASE_DEF( const void* )
FT_Get_Module_Interface(FT_Library library,const char * mod_name)5086   FT_Get_Module_Interface( FT_Library   library,
5087                            const char*  mod_name )
5088   {
5089     FT_Module  module;
5090 
5091 
5092     /* test for valid `library' delayed to FT_Get_Module() */
5093 
5094     module = FT_Get_Module( library, mod_name );
5095 
5096     return module ? module->clazz->module_interface : 0;
5097   }
5098 
5099 
5100   FT_BASE_DEF( FT_Pointer )
ft_module_get_service(FT_Module module,const char * service_id,FT_Bool global)5101   ft_module_get_service( FT_Module    module,
5102                          const char*  service_id,
5103                          FT_Bool      global )
5104   {
5105     FT_Pointer  result = NULL;
5106 
5107 
5108     if ( module )
5109     {
5110       FT_ASSERT( module->clazz && module->clazz->get_interface );
5111 
5112       /* first, look for the service in the module */
5113       if ( module->clazz->get_interface )
5114         result = module->clazz->get_interface( module, service_id );
5115 
5116       if ( global && !result )
5117       {
5118         /* we didn't find it, look in all other modules then */
5119         FT_Library  library = module->library;
5120         FT_Module*  cur     = library->modules;
5121         FT_Module*  limit   = cur + library->num_modules;
5122 
5123 
5124         for ( ; cur < limit; cur++ )
5125         {
5126           if ( cur[0] != module )
5127           {
5128             FT_ASSERT( cur[0]->clazz );
5129 
5130             if ( cur[0]->clazz->get_interface )
5131             {
5132               result = cur[0]->clazz->get_interface( cur[0], service_id );
5133               if ( result )
5134                 break;
5135             }
5136           }
5137         }
5138       }
5139     }
5140 
5141     return result;
5142   }
5143 
5144 
5145   /* documentation is in ftmodapi.h */
5146 
5147   FT_EXPORT_DEF( FT_Error )
FT_Remove_Module(FT_Library library,FT_Module module)5148   FT_Remove_Module( FT_Library  library,
5149                     FT_Module   module )
5150   {
5151     /* try to find the module from the table, then remove it from there */
5152 
5153     if ( !library )
5154       return FT_THROW( Invalid_Library_Handle );
5155 
5156     if ( module )
5157     {
5158       FT_Module*  cur   = library->modules;
5159       FT_Module*  limit = cur + library->num_modules;
5160 
5161 
5162       for ( ; cur < limit; cur++ )
5163       {
5164         if ( cur[0] == module )
5165         {
5166           /* remove it from the table */
5167           library->num_modules--;
5168           limit--;
5169           while ( cur < limit )
5170           {
5171             cur[0] = cur[1];
5172             cur++;
5173           }
5174           limit[0] = NULL;
5175 
5176           /* destroy the module */
5177           Destroy_Module( module );
5178 
5179           return FT_Err_Ok;
5180         }
5181       }
5182     }
5183     return FT_THROW( Invalid_Driver_Handle );
5184   }
5185 
5186 
5187   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)5188   ft_property_do( FT_Library        library,
5189                   const FT_String*  module_name,
5190                   const FT_String*  property_name,
5191                   void*             value,
5192                   FT_Bool           set,
5193                   FT_Bool           value_is_string )
5194   {
5195     FT_Module*           cur;
5196     FT_Module*           limit;
5197     FT_Module_Interface  interface;
5198 
5199     FT_Service_Properties  service;
5200 
5201 #ifdef FT_DEBUG_LEVEL_ERROR
5202     const FT_String*  set_name  = "FT_Property_Set";
5203     const FT_String*  get_name  = "FT_Property_Get";
5204     const FT_String*  func_name = set ? set_name : get_name;
5205 #endif
5206 
5207     FT_Bool  missing_func;
5208 
5209 
5210     if ( !library )
5211       return FT_THROW( Invalid_Library_Handle );
5212 
5213     if ( !module_name || !property_name || !value )
5214       return FT_THROW( Invalid_Argument );
5215 
5216     cur   = library->modules;
5217     limit = cur + library->num_modules;
5218 
5219     /* search module */
5220     for ( ; cur < limit; cur++ )
5221       if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) )
5222         break;
5223 
5224     if ( cur == limit )
5225     {
5226       FT_TRACE2(( "%s: can't find module `%s'\n",
5227                   func_name, module_name ));
5228       return FT_THROW( Missing_Module );
5229     }
5230 
5231     /* check whether we have a service interface */
5232     if ( !cur[0]->clazz->get_interface )
5233     {
5234       FT_TRACE2(( "%s: module `%s' doesn't support properties\n",
5235                   func_name, module_name ));
5236       return FT_THROW( Unimplemented_Feature );
5237     }
5238 
5239     /* search property service */
5240     interface = cur[0]->clazz->get_interface( cur[0],
5241                                               FT_SERVICE_ID_PROPERTIES );
5242     if ( !interface )
5243     {
5244       FT_TRACE2(( "%s: module `%s' doesn't support properties\n",
5245                   func_name, module_name ));
5246       return FT_THROW( Unimplemented_Feature );
5247     }
5248 
5249     service = (FT_Service_Properties)interface;
5250 
5251     if ( set )
5252       missing_func = FT_BOOL( !service->set_property );
5253     else
5254       missing_func = FT_BOOL( !service->get_property );
5255 
5256     if ( missing_func )
5257     {
5258       FT_TRACE2(( "%s: property service of module `%s' is broken\n",
5259                   func_name, module_name ));
5260       return FT_THROW( Unimplemented_Feature );
5261     }
5262 
5263     return set ? service->set_property( cur[0],
5264                                         property_name,
5265                                         value,
5266                                         value_is_string )
5267                : service->get_property( cur[0],
5268                                         property_name,
5269                                         value );
5270   }
5271 
5272 
5273   /* documentation is in ftmodapi.h */
5274 
5275   FT_EXPORT_DEF( FT_Error )
FT_Property_Set(FT_Library library,const FT_String * module_name,const FT_String * property_name,const void * value)5276   FT_Property_Set( FT_Library        library,
5277                    const FT_String*  module_name,
5278                    const FT_String*  property_name,
5279                    const void*       value )
5280   {
5281     return ft_property_do( library,
5282                            module_name,
5283                            property_name,
5284                            (void*)value,
5285                            TRUE,
5286                            FALSE );
5287   }
5288 
5289 
5290   /* documentation is in ftmodapi.h */
5291 
5292   FT_EXPORT_DEF( FT_Error )
FT_Property_Get(FT_Library library,const FT_String * module_name,const FT_String * property_name,void * value)5293   FT_Property_Get( FT_Library        library,
5294                    const FT_String*  module_name,
5295                    const FT_String*  property_name,
5296                    void*             value )
5297   {
5298     return ft_property_do( library,
5299                            module_name,
5300                            property_name,
5301                            value,
5302                            FALSE,
5303                            FALSE );
5304   }
5305 
5306 
5307 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
5308 
5309   /* this variant is used for handling the FREETYPE_PROPERTIES */
5310   /* environment variable                                      */
5311 
5312   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)5313   ft_property_string_set( FT_Library        library,
5314                           const FT_String*  module_name,
5315                           const FT_String*  property_name,
5316                           FT_String*        value )
5317   {
5318     return ft_property_do( library,
5319                            module_name,
5320                            property_name,
5321                            (void*)value,
5322                            TRUE,
5323                            TRUE );
5324   }
5325 
5326 #endif
5327 
5328 
5329   /*************************************************************************/
5330   /*************************************************************************/
5331   /*************************************************************************/
5332   /****                                                                 ****/
5333   /****                                                                 ****/
5334   /****                         L I B R A R Y                           ****/
5335   /****                                                                 ****/
5336   /****                                                                 ****/
5337   /*************************************************************************/
5338   /*************************************************************************/
5339   /*************************************************************************/
5340 
5341 
5342   /* documentation is in ftmodapi.h */
5343 
5344   FT_EXPORT_DEF( FT_Error )
FT_Reference_Library(FT_Library library)5345   FT_Reference_Library( FT_Library  library )
5346   {
5347     if ( !library )
5348       return FT_THROW( Invalid_Library_Handle );
5349 
5350     library->refcount++;
5351 
5352     return FT_Err_Ok;
5353   }
5354 
5355 
5356   /* documentation is in ftmodapi.h */
5357 
5358   FT_EXPORT_DEF( FT_Error )
FT_New_Library(FT_Memory memory,FT_Library * alibrary)5359   FT_New_Library( FT_Memory    memory,
5360                   FT_Library  *alibrary )
5361   {
5362     FT_Library  library = NULL;
5363     FT_Error    error;
5364 
5365 
5366     if ( !memory || !alibrary )
5367       return FT_THROW( Invalid_Argument );
5368 
5369 #ifndef FT_DEBUG_LOGGING
5370 #ifdef FT_DEBUG_LEVEL_ERROR
5371     /* init debugging support */
5372     ft_debug_init();
5373 #endif /* FT_DEBUG_LEVEL_ERROR */
5374 #endif /* !FT_DEBUG_LOGGING */
5375 
5376     /* first of all, allocate the library object */
5377     if ( FT_NEW( library ) )
5378       return error;
5379 
5380     library->memory = memory;
5381 
5382     library->version_major = FREETYPE_MAJOR;
5383     library->version_minor = FREETYPE_MINOR;
5384     library->version_patch = FREETYPE_PATCH;
5385 
5386     library->refcount = 1;
5387 
5388     /* That's ok now */
5389     *alibrary = library;
5390 
5391     return FT_Err_Ok;
5392   }
5393 
5394 
5395   /* documentation is in freetype.h */
5396 
5397   FT_EXPORT_DEF( void )
FT_Library_Version(FT_Library library,FT_Int * amajor,FT_Int * aminor,FT_Int * apatch)5398   FT_Library_Version( FT_Library   library,
5399                       FT_Int      *amajor,
5400                       FT_Int      *aminor,
5401                       FT_Int      *apatch )
5402   {
5403     FT_Int  major = 0;
5404     FT_Int  minor = 0;
5405     FT_Int  patch = 0;
5406 
5407 
5408     if ( library )
5409     {
5410       major = library->version_major;
5411       minor = library->version_minor;
5412       patch = library->version_patch;
5413     }
5414 
5415     if ( amajor )
5416       *amajor = major;
5417 
5418     if ( aminor )
5419       *aminor = minor;
5420 
5421     if ( apatch )
5422       *apatch = patch;
5423   }
5424 
5425 
5426   /* documentation is in ftmodapi.h */
5427 
5428   FT_EXPORT_DEF( FT_Error )
FT_Done_Library(FT_Library library)5429   FT_Done_Library( FT_Library  library )
5430   {
5431     FT_Memory  memory;
5432 
5433 
5434     if ( !library )
5435       return FT_THROW( Invalid_Library_Handle );
5436 
5437     library->refcount--;
5438     if ( library->refcount > 0 )
5439       goto Exit;
5440 
5441     memory = library->memory;
5442 
5443     /*
5444      * Close all faces in the library.  If we don't do this, we can have
5445      * some subtle memory leaks.
5446      *
5447      * Example:
5448      *
5449      * - the cff font driver uses the pshinter module in cff_size_done
5450      * - if the pshinter module is destroyed before the cff font driver,
5451      *   opened FT_Face objects managed by the driver are not properly
5452      *   destroyed, resulting in a memory leak
5453      *
5454      * Some faces are dependent on other faces, like Type42 faces that
5455      * depend on TrueType faces synthesized internally.
5456      *
5457      * The order of drivers should be specified in driver_name[].
5458      */
5459     {
5460       FT_UInt      m, n;
5461       const char*  driver_name[] = { "type42", NULL };
5462 
5463 
5464       for ( m = 0;
5465             m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
5466             m++ )
5467       {
5468         for ( n = 0; n < library->num_modules; n++ )
5469         {
5470           FT_Module    module      = library->modules[n];
5471           const char*  module_name = module->clazz->module_name;
5472           FT_List      faces;
5473 
5474 
5475           if ( driver_name[m]                                &&
5476                ft_strcmp( module_name, driver_name[m] ) != 0 )
5477             continue;
5478 
5479           if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
5480             continue;
5481 
5482           FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
5483 
5484           faces = &FT_DRIVER( module )->faces_list;
5485           while ( faces->head )
5486           {
5487             FT_Done_Face( FT_FACE( faces->head->data ) );
5488             if ( faces->head )
5489               FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
5490           }
5491         }
5492       }
5493     }
5494 
5495     /* Close all other modules in the library */
5496 #if 1
5497     /* XXX Modules are removed in the reversed order so that  */
5498     /* type42 module is removed before truetype module.  This */
5499     /* avoids double free in some occasions.  It is a hack.   */
5500     while ( library->num_modules > 0 )
5501       FT_Remove_Module( library,
5502                         library->modules[library->num_modules - 1] );
5503 #else
5504     {
5505       FT_UInt  n;
5506 
5507 
5508       for ( n = 0; n < library->num_modules; n++ )
5509       {
5510         FT_Module  module = library->modules[n];
5511 
5512 
5513         if ( module )
5514         {
5515           Destroy_Module( module );
5516           library->modules[n] = NULL;
5517         }
5518       }
5519     }
5520 #endif
5521 
5522     FT_FREE( library );
5523 
5524   Exit:
5525     return FT_Err_Ok;
5526   }
5527 
5528 
5529   /* documentation is in ftmodapi.h */
5530 
5531   FT_EXPORT_DEF( void )
FT_Set_Debug_Hook(FT_Library library,FT_UInt hook_index,FT_DebugHook_Func debug_hook)5532   FT_Set_Debug_Hook( FT_Library         library,
5533                      FT_UInt            hook_index,
5534                      FT_DebugHook_Func  debug_hook )
5535   {
5536     if ( library && debug_hook &&
5537          hook_index <
5538            ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
5539       library->debug_hooks[hook_index] = debug_hook;
5540   }
5541 
5542 
5543   /* documentation is in ftmodapi.h */
5544 
5545   FT_EXPORT_DEF( FT_TrueTypeEngineType )
FT_Get_TrueType_Engine_Type(FT_Library library)5546   FT_Get_TrueType_Engine_Type( FT_Library  library )
5547   {
5548     FT_TrueTypeEngineType  result = FT_TRUETYPE_ENGINE_TYPE_NONE;
5549 
5550 
5551     if ( library )
5552     {
5553       FT_Module  module = FT_Get_Module( library, "truetype" );
5554 
5555 
5556       if ( module )
5557       {
5558         FT_Service_TrueTypeEngine  service;
5559 
5560 
5561         service = (FT_Service_TrueTypeEngine)
5562                     ft_module_get_service( module,
5563                                            FT_SERVICE_ID_TRUETYPE_ENGINE,
5564                                            0 );
5565         if ( service )
5566           result = service->engine_type;
5567       }
5568     }
5569 
5570     return result;
5571   }
5572 
5573 
5574   /* documentation is in freetype.h */
5575 
5576   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)5577   FT_Get_SubGlyph_Info( FT_GlyphSlot  glyph,
5578                         FT_UInt       sub_index,
5579                         FT_Int       *p_index,
5580                         FT_UInt      *p_flags,
5581                         FT_Int       *p_arg1,
5582                         FT_Int       *p_arg2,
5583                         FT_Matrix    *p_transform )
5584   {
5585     FT_Error  error = FT_ERR( Invalid_Argument );
5586 
5587 
5588     if ( glyph                                      &&
5589          glyph->subglyphs                           &&
5590          glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
5591          sub_index < glyph->num_subglyphs           )
5592     {
5593       FT_SubGlyph  subg = glyph->subglyphs + sub_index;
5594 
5595 
5596       *p_index     = subg->index;
5597       *p_flags     = subg->flags;
5598       *p_arg1      = subg->arg1;
5599       *p_arg2      = subg->arg2;
5600       *p_transform = subg->transform;
5601 
5602       error = FT_Err_Ok;
5603     }
5604 
5605     return error;
5606   }
5607 
5608 
5609   /* documentation is in freetype.h */
5610 
5611   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)5612   FT_Get_Color_Glyph_Layer( FT_Face            face,
5613                             FT_UInt            base_glyph,
5614                             FT_UInt           *aglyph_index,
5615                             FT_UInt           *acolor_index,
5616                             FT_LayerIterator*  iterator )
5617   {
5618     TT_Face       ttface;
5619     SFNT_Service  sfnt;
5620 
5621 
5622     if ( !face                                   ||
5623          !aglyph_index                           ||
5624          !acolor_index                           ||
5625          !iterator                               ||
5626          base_glyph >= (FT_UInt)face->num_glyphs )
5627       return 0;
5628 
5629     if ( !FT_IS_SFNT( face ) )
5630       return 0;
5631 
5632     ttface = (TT_Face)face;
5633     sfnt   = (SFNT_Service)ttface->sfnt;
5634 
5635     if ( sfnt->get_colr_layer )
5636       return sfnt->get_colr_layer( ttface,
5637                                    base_glyph,
5638                                    aglyph_index,
5639                                    acolor_index,
5640                                    iterator );
5641     else
5642       return 0;
5643   }
5644 
5645 
5646   /* documentation is in freetype.h */
5647 
5648   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)5649   FT_Get_Color_Glyph_Paint( FT_Face                  face,
5650                             FT_UInt                  base_glyph,
5651                             FT_Color_Root_Transform  root_transform,
5652                             FT_OpaquePaint*          paint )
5653   {
5654     TT_Face       ttface;
5655     SFNT_Service  sfnt;
5656 
5657 
5658     if ( !face || !paint )
5659       return 0;
5660 
5661     if ( !FT_IS_SFNT( face ) )
5662       return 0;
5663 
5664     ttface = (TT_Face)face;
5665     sfnt   = (SFNT_Service)ttface->sfnt;
5666 
5667     if ( sfnt->get_colr_layer )
5668       return sfnt->get_colr_glyph_paint( ttface,
5669                                          base_glyph,
5670                                          root_transform,
5671                                          paint );
5672     else
5673       return 0;
5674   }
5675 
5676 
5677   /* documentation is in ftcolor.h */
5678 
5679   FT_EXPORT_DEF( FT_Bool )
FT_Get_Color_Glyph_ClipBox(FT_Face face,FT_UInt base_glyph,FT_ClipBox * clip_box)5680   FT_Get_Color_Glyph_ClipBox( FT_Face      face,
5681                               FT_UInt      base_glyph,
5682                               FT_ClipBox*  clip_box )
5683   {
5684     TT_Face       ttface;
5685     SFNT_Service  sfnt;
5686 
5687 
5688     if ( !face || !clip_box )
5689       return 0;
5690 
5691     if ( !FT_IS_SFNT( face ) )
5692       return 0;
5693 
5694     ttface = (TT_Face)face;
5695     sfnt   = (SFNT_Service)ttface->sfnt;
5696 
5697     if ( sfnt->get_color_glyph_clipbox )
5698       return sfnt->get_color_glyph_clipbox( ttface,
5699                                             base_glyph,
5700                                             clip_box );
5701     else
5702       return 0;
5703   }
5704 
5705 
5706   /* documentation is in freetype.h */
5707 
5708   FT_EXPORT_DEF( FT_Bool )
FT_Get_Paint_Layers(FT_Face face,FT_LayerIterator * layer_iterator,FT_OpaquePaint * paint)5709   FT_Get_Paint_Layers( FT_Face            face,
5710                        FT_LayerIterator*  layer_iterator,
5711                        FT_OpaquePaint*    paint )
5712   {
5713     TT_Face       ttface;
5714     SFNT_Service  sfnt;
5715 
5716 
5717     if ( !face || !paint || !layer_iterator )
5718       return 0;
5719 
5720     if ( !FT_IS_SFNT( face ) )
5721       return 0;
5722 
5723     ttface = (TT_Face)face;
5724     sfnt   = (SFNT_Service)ttface->sfnt;
5725 
5726     if ( sfnt->get_paint_layers )
5727       return sfnt->get_paint_layers( ttface, layer_iterator, paint );
5728     else
5729       return 0;
5730   }
5731 
5732 
5733   /* documentation is in freetype.h */
5734 
5735   FT_EXPORT_DEF( FT_Bool )
FT_Get_Paint(FT_Face face,FT_OpaquePaint opaque_paint,FT_COLR_Paint * paint)5736   FT_Get_Paint( FT_Face face,
5737                 FT_OpaquePaint  opaque_paint,
5738                 FT_COLR_Paint*  paint )
5739   {
5740     TT_Face       ttface;
5741     SFNT_Service  sfnt;
5742 
5743 
5744     if ( !face || !paint || !paint )
5745       return 0;
5746 
5747     if ( !FT_IS_SFNT( face ) )
5748       return 0;
5749 
5750     ttface = (TT_Face)face;
5751     sfnt   = (SFNT_Service)ttface->sfnt;
5752 
5753     if ( sfnt->get_paint )
5754       return sfnt->get_paint( ttface, opaque_paint, paint );
5755     else
5756       return 0;
5757   }
5758 
5759 
5760   /* documentation is in freetype.h */
5761 
5762   FT_EXPORT_DEF( FT_Bool )
FT_Get_Colorline_Stops(FT_Face face,FT_ColorStop * color_stop,FT_ColorStopIterator * iterator)5763   FT_Get_Colorline_Stops ( FT_Face                face,
5764                            FT_ColorStop *         color_stop,
5765                            FT_ColorStopIterator  *iterator )
5766   {
5767     TT_Face       ttface;
5768     SFNT_Service  sfnt;
5769 
5770 
5771     if ( !face || !color_stop || !iterator )
5772       return 0;
5773 
5774     if ( !FT_IS_SFNT( face ) )
5775       return 0;
5776 
5777     ttface = (TT_Face)face;
5778     sfnt   = (SFNT_Service)ttface->sfnt;
5779 
5780     if ( sfnt->get_colorline_stops )
5781       return sfnt->get_colorline_stops ( ttface, color_stop, iterator );
5782     else
5783       return 0;
5784   }
5785 
5786 
5787 /* END */
5788