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