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