1
/***************************************************************************/
5
/* The FreeType private base classes (body). */
7
/* Copyright 1996-2014 by */
8
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16
/***************************************************************************/
22
#include FT_INTERNAL_VALIDATE_H
23
#include FT_INTERNAL_OBJECTS_H
24
#include FT_INTERNAL_DEBUG_H
25
#include FT_INTERNAL_RFORK_H
26
#include FT_INTERNAL_STREAM_H
27
#include FT_INTERNAL_SFNT_H /* for SFNT_Load_Table_Func */
28
#include FT_TRUETYPE_TABLES_H
29
#include FT_TRUETYPE_TAGS_H
30
#include FT_TRUETYPE_IDS_H
32
#include FT_SERVICE_PROPERTIES_H
33
#include FT_SERVICE_SFNT_H
34
#include FT_SERVICE_POSTSCRIPT_NAME_H
35
#include FT_SERVICE_GLYPH_DICT_H
36
#include FT_SERVICE_TT_CMAP_H
37
#include FT_SERVICE_KERNING_H
38
#include FT_SERVICE_TRUETYPE_ENGINE_H
40
#ifdef FT_CONFIG_OPTION_MAC_FONTS
45
#ifdef FT_DEBUG_LEVEL_TRACE
49
#if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
50
/* We disable the warning `conversion from XXX to YYY, */
51
/* possible loss of data' in order to compile cleanly with */
52
/* the maximum level of warnings: `md5.c' is non-FreeType */
53
/* code, and it gets used during development builds only. */
54
#pragma warning( push )
55
#pragma warning( disable : 4244 )
58
/* it's easiest to include `md5.c' directly */
61
#if defined( _MSC_VER )
62
#pragma warning( pop )
65
#endif /* FT_DEBUG_LEVEL_TRACE */
68
#define GRID_FIT_METRICS
71
FT_BASE_DEF( FT_Pointer )
72
ft_service_list_lookup( FT_ServiceDesc service_descriptors,
73
const char* service_id )
75
FT_Pointer result = NULL;
76
FT_ServiceDesc desc = service_descriptors;
79
if ( desc && service_id )
81
for ( ; desc->serv_id != NULL; desc++ )
83
if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
85
result = (FT_Pointer)desc->serv_data;
96
ft_validator_init( FT_Validator valid,
99
FT_ValidationLevel level )
102
valid->limit = limit;
103
valid->level = level;
104
valid->error = FT_Err_Ok;
108
FT_BASE_DEF( FT_Int )
109
ft_validator_run( FT_Validator valid )
111
/* This function doesn't work! None should call it. */
119
ft_validator_error( FT_Validator valid,
122
/* since the cast below also disables the compiler's */
123
/* type check, we introduce a dummy variable, which */
124
/* will be optimized away */
125
volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
128
valid->error = error;
130
/* throw away volatileness; use `jump_buffer' or the */
131
/* compiler may warn about an unused local variable */
132
ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
136
/*************************************************************************/
137
/*************************************************************************/
138
/*************************************************************************/
141
/**** S T R E A M ****/
144
/*************************************************************************/
145
/*************************************************************************/
146
/*************************************************************************/
149
/* create a new input stream from an FT_Open_Args structure */
151
FT_BASE_DEF( FT_Error )
152
FT_Stream_New( FT_Library library,
153
const FT_Open_Args* args,
158
FT_Stream stream = NULL;
164
return FT_THROW( Invalid_Library_Handle );
167
return FT_THROW( Invalid_Argument );
169
memory = library->memory;
171
if ( FT_NEW( stream ) )
174
stream->memory = memory;
176
if ( args->flags & FT_OPEN_MEMORY )
178
/* create a memory-based stream */
179
FT_Stream_OpenMemory( stream,
180
(const FT_Byte*)args->memory_base,
184
#ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
186
else if ( args->flags & FT_OPEN_PATHNAME )
188
/* create a normal system stream */
189
error = FT_Stream_Open( stream, args->pathname );
190
stream->pathname.pointer = args->pathname;
192
else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
194
/* use an existing, user-provided stream */
196
/* in this case, we do not need to allocate a new stream object */
197
/* since the caller is responsible for closing it himself */
199
stream = args->stream;
205
error = FT_THROW( Invalid_Argument );
210
stream->memory = memory; /* just to be certain */
220
FT_Stream_Free( FT_Stream stream,
225
FT_Memory memory = stream->memory;
228
FT_Stream_Close( stream );
236
/*************************************************************************/
238
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
239
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
240
/* messages during execution. */
243
#define FT_COMPONENT trace_objs
246
/*************************************************************************/
247
/*************************************************************************/
248
/*************************************************************************/
251
/**** FACE, SIZE & GLYPH SLOT OBJECTS ****/
254
/*************************************************************************/
255
/*************************************************************************/
256
/*************************************************************************/
260
ft_glyphslot_init( FT_GlyphSlot slot )
262
FT_Driver driver = slot->face->driver;
263
FT_Driver_Class clazz = driver->clazz;
264
FT_Memory memory = driver->root.memory;
265
FT_Error error = FT_Err_Ok;
266
FT_Slot_Internal internal = NULL;
269
slot->library = driver->root.library;
271
if ( FT_NEW( internal ) )
274
slot->internal = internal;
276
if ( FT_DRIVER_USES_OUTLINES( driver ) )
277
error = FT_GlyphLoader_New( memory, &internal->loader );
279
if ( !error && clazz->init_slot )
280
error = clazz->init_slot( slot );
288
ft_glyphslot_free_bitmap( FT_GlyphSlot slot )
290
if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
292
FT_Memory memory = FT_FACE_MEMORY( slot->face );
295
FT_FREE( slot->bitmap.buffer );
296
slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
300
/* assume that the bitmap buffer was stolen or not */
301
/* allocated from the heap */
302
slot->bitmap.buffer = NULL;
308
ft_glyphslot_set_bitmap( FT_GlyphSlot slot,
311
ft_glyphslot_free_bitmap( slot );
313
slot->bitmap.buffer = buffer;
315
FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
319
FT_BASE_DEF( FT_Error )
320
ft_glyphslot_alloc_bitmap( FT_GlyphSlot slot,
323
FT_Memory memory = FT_FACE_MEMORY( slot->face );
327
if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
328
FT_FREE( slot->bitmap.buffer );
330
slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
332
(void)FT_ALLOC( slot->bitmap.buffer, size );
338
ft_glyphslot_clear( FT_GlyphSlot slot )
340
/* free bitmap if needed */
341
ft_glyphslot_free_bitmap( slot );
343
/* clear all public fields in the glyph slot */
344
FT_ZERO( &slot->metrics );
345
FT_ZERO( &slot->outline );
347
slot->bitmap.width = 0;
348
slot->bitmap.rows = 0;
349
slot->bitmap.pitch = 0;
350
slot->bitmap.pixel_mode = 0;
351
/* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
353
slot->bitmap_left = 0;
354
slot->bitmap_top = 0;
355
slot->num_subglyphs = 0;
357
slot->control_data = 0;
358
slot->control_len = 0;
360
slot->format = FT_GLYPH_FORMAT_NONE;
362
slot->linearHoriAdvance = 0;
363
slot->linearVertAdvance = 0;
370
ft_glyphslot_done( FT_GlyphSlot slot )
372
FT_Driver driver = slot->face->driver;
373
FT_Driver_Class clazz = driver->clazz;
374
FT_Memory memory = driver->root.memory;
377
if ( clazz->done_slot )
378
clazz->done_slot( slot );
380
/* free bitmap buffer if needed */
381
ft_glyphslot_free_bitmap( slot );
383
/* slot->internal might be NULL in out-of-memory situations */
384
if ( slot->internal )
386
/* free glyph loader */
387
if ( FT_DRIVER_USES_OUTLINES( driver ) )
389
FT_GlyphLoader_Done( slot->internal->loader );
390
slot->internal->loader = 0;
393
FT_FREE( slot->internal );
398
/* documentation is in ftobjs.h */
400
FT_BASE_DEF( FT_Error )
401
FT_New_GlyphSlot( FT_Face face,
402
FT_GlyphSlot *aslot )
406
FT_Driver_Class clazz;
408
FT_GlyphSlot slot = NULL;
411
if ( !face || !face->driver )
412
return FT_THROW( Invalid_Argument );
414
driver = face->driver;
415
clazz = driver->clazz;
416
memory = driver->root.memory;
418
FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
419
if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
423
error = ft_glyphslot_init( slot );
426
ft_glyphslot_done( slot );
431
slot->next = face->glyph;
442
FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
447
/* documentation is in ftobjs.h */
450
FT_Done_GlyphSlot( FT_GlyphSlot slot )
454
FT_Driver driver = slot->face->driver;
455
FT_Memory memory = driver->root.memory;
460
/* Remove slot from its parent face's list */
462
cur = slot->face->glyph;
469
slot->face->glyph = cur->next;
471
prev->next = cur->next;
473
/* finalize client-specific data */
474
if ( slot->generic.finalizer )
475
slot->generic.finalizer( slot );
477
ft_glyphslot_done( slot );
488
/* documentation is in freetype.h */
490
FT_EXPORT_DEF( void )
491
FT_Set_Transform( FT_Face face,
495
FT_Face_Internal internal;
501
internal = face->internal;
503
internal->transform_flags = 0;
507
internal->transform_matrix.xx = 0x10000L;
508
internal->transform_matrix.xy = 0;
509
internal->transform_matrix.yx = 0;
510
internal->transform_matrix.yy = 0x10000L;
511
matrix = &internal->transform_matrix;
514
internal->transform_matrix = *matrix;
516
/* set transform_flags bit flag 0 if `matrix' isn't the identity */
517
if ( ( matrix->xy | matrix->yx ) ||
518
matrix->xx != 0x10000L ||
519
matrix->yy != 0x10000L )
520
internal->transform_flags |= 1;
524
internal->transform_delta.x = 0;
525
internal->transform_delta.y = 0;
526
delta = &internal->transform_delta;
529
internal->transform_delta = *delta;
531
/* set transform_flags bit flag 1 if `delta' isn't the null vector */
532
if ( delta->x | delta->y )
533
internal->transform_flags |= 2;
538
ft_lookup_glyph_renderer( FT_GlyphSlot slot );
541
#ifdef GRID_FIT_METRICS
543
ft_glyphslot_grid_fit_metrics( FT_GlyphSlot slot,
546
FT_Glyph_Metrics* metrics = &slot->metrics;
547
FT_Pos right, bottom;
552
metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
553
metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
555
right = FT_PIX_CEIL( metrics->vertBearingX + metrics->width );
556
bottom = FT_PIX_CEIL( metrics->vertBearingY + metrics->height );
558
metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
559
metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
561
metrics->width = right - metrics->vertBearingX;
562
metrics->height = bottom - metrics->vertBearingY;
566
metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
567
metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
569
right = FT_PIX_CEIL ( metrics->horiBearingX + metrics->width );
570
bottom = FT_PIX_FLOOR( metrics->horiBearingY - metrics->height );
572
metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
573
metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
575
metrics->width = right - metrics->horiBearingX;
576
metrics->height = metrics->horiBearingY - bottom;
579
metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
580
metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
582
#endif /* GRID_FIT_METRICS */
585
/* documentation is in freetype.h */
587
FT_EXPORT_DEF( FT_Error )
588
FT_Load_Glyph( FT_Face face,
590
FT_Int32 load_flags )
596
FT_Bool autohint = FALSE;
598
TT_Face ttface = (TT_Face)face;
601
if ( !face || !face->size || !face->glyph )
602
return FT_THROW( Invalid_Face_Handle );
604
/* The validity test for `glyph_index' is performed by the */
608
ft_glyphslot_clear( slot );
610
driver = face->driver;
611
library = driver->root.library;
612
hinter = library->auto_hinter;
614
/* resolve load flags dependencies */
616
if ( load_flags & FT_LOAD_NO_RECURSE )
617
load_flags |= FT_LOAD_NO_SCALE |
618
FT_LOAD_IGNORE_TRANSFORM;
620
if ( load_flags & FT_LOAD_NO_SCALE )
622
load_flags |= FT_LOAD_NO_HINTING |
625
load_flags &= ~FT_LOAD_RENDER;
629
* Determine whether we need to auto-hint or not.
630
* The general rules are:
632
* - Do only auto-hinting if we have a hinter module, a scalable font
633
* format dealing with outlines, and no transforms except simple
634
* slants and/or rotations by integer multiples of 90 degrees.
636
* - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
637
* have a native font hinter.
639
* - Otherwise, auto-hint for LIGHT hinting mode or if there isn't
640
* any hinting bytecode in the TrueType/OpenType font.
642
* - Exception: The font is `tricky' and requires the native hinter to
647
!( load_flags & FT_LOAD_NO_HINTING ) &&
648
!( load_flags & FT_LOAD_NO_AUTOHINT ) &&
649
FT_DRIVER_IS_SCALABLE( driver ) &&
650
FT_DRIVER_USES_OUTLINES( driver ) &&
651
!FT_IS_TRICKY( face ) &&
652
( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) ||
653
( face->internal->transform_matrix.yx == 0 &&
654
face->internal->transform_matrix.xx != 0 ) ||
655
( face->internal->transform_matrix.xx == 0 &&
656
face->internal->transform_matrix.yx != 0 ) ) )
658
if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
659
!FT_DRIVER_HAS_HINTER( driver ) )
663
FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
666
/* the check for `num_locations' assures that we actually */
667
/* test for instructions in a TTF and not in a CFF-based OTF */
669
/* since `maxSizeOfInstructions' might be unreliable, we */
670
/* check the size of the `fpgm' and `prep' tables, too -- */
671
/* the assumption is that there don't exist real TTFs where */
672
/* both `fpgm' and `prep' tables are missing */
673
if ( mode == FT_RENDER_MODE_LIGHT ||
674
face->internal->ignore_unpatented_hinter ||
675
( FT_IS_SFNT( face ) &&
676
ttface->num_locations &&
677
ttface->max_profile.maxSizeOfInstructions == 0 &&
678
ttface->font_program_size == 0 &&
679
ttface->cvt_program_size == 0 ) )
686
FT_AutoHinter_Interface hinting;
689
/* try to load embedded bitmaps first if available */
691
/* XXX: This is really a temporary hack that should disappear */
692
/* promptly with FreeType 2.1! */
694
if ( FT_HAS_FIXED_SIZES( face ) &&
695
( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
697
error = driver->clazz->load_glyph( slot, face->size,
699
load_flags | FT_LOAD_SBITS_ONLY );
701
if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
706
FT_Face_Internal internal = face->internal;
707
FT_Int transform_flags = internal->transform_flags;
710
/* since the auto-hinter calls FT_Load_Glyph by itself, */
711
/* make sure that glyphs aren't transformed */
712
internal->transform_flags = 0;
714
/* load auto-hinted outline */
715
hinting = (FT_AutoHinter_Interface)hinter->clazz->module_interface;
717
error = hinting->load_glyph( (FT_AutoHinter)hinter,
719
glyph_index, load_flags );
721
internal->transform_flags = transform_flags;
726
error = driver->clazz->load_glyph( slot,
733
if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
735
/* check that the loaded outline is correct */
736
error = FT_Outline_Check( &slot->outline );
740
#ifdef GRID_FIT_METRICS
741
if ( !( load_flags & FT_LOAD_NO_HINTING ) )
742
ft_glyphslot_grid_fit_metrics( slot,
743
FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
749
/* compute the advance */
750
if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
753
slot->advance.y = slot->metrics.vertAdvance;
757
slot->advance.x = slot->metrics.horiAdvance;
761
/* compute the linear advance in 16.16 pixels */
762
if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
763
( FT_IS_SCALABLE( face ) ) )
765
FT_Size_Metrics* metrics = &face->size->metrics;
769
slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
770
metrics->x_scale, 64 );
772
slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
773
metrics->y_scale, 64 );
776
if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
778
FT_Face_Internal internal = face->internal;
781
/* now, transform the glyph image if needed */
782
if ( internal->transform_flags )
785
FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
789
error = renderer->clazz->transform_glyph(
791
&internal->transform_matrix,
792
&internal->transform_delta );
793
else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
795
/* apply `standard' transformation if no renderer is available */
796
if ( internal->transform_flags & 1 )
797
FT_Outline_Transform( &slot->outline,
798
&internal->transform_matrix );
800
if ( internal->transform_flags & 2 )
801
FT_Outline_Translate( &slot->outline,
802
internal->transform_delta.x,
803
internal->transform_delta.y );
806
/* transform advance */
807
FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
811
FT_TRACE5(( " x advance: %d\n" , slot->advance.x ));
812
FT_TRACE5(( " y advance: %d\n" , slot->advance.y ));
814
FT_TRACE5(( " linear x advance: %d\n" , slot->linearHoriAdvance ));
815
FT_TRACE5(( " linear y advance: %d\n" , slot->linearVertAdvance ));
817
/* do we need to render the image now? */
819
slot->format != FT_GLYPH_FORMAT_BITMAP &&
820
slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
821
load_flags & FT_LOAD_RENDER )
823
FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
826
if ( mode == FT_RENDER_MODE_NORMAL &&
827
(load_flags & FT_LOAD_MONOCHROME ) )
828
mode = FT_RENDER_MODE_MONO;
830
error = FT_Render_Glyph( slot, mode );
838
/* documentation is in freetype.h */
840
FT_EXPORT_DEF( FT_Error )
841
FT_Load_Char( FT_Face face,
843
FT_Int32 load_flags )
849
return FT_THROW( Invalid_Face_Handle );
851
glyph_index = (FT_UInt)char_code;
853
glyph_index = FT_Get_Char_Index( face, char_code );
855
return FT_Load_Glyph( face, glyph_index, load_flags );
859
/* destructor for sizes list */
861
destroy_size( FT_Memory memory,
865
/* finalize client-specific data */
866
if ( size->generic.finalizer )
867
size->generic.finalizer( size );
869
/* finalize format-specific stuff */
870
if ( driver->clazz->done_size )
871
driver->clazz->done_size( size );
873
FT_FREE( size->internal );
879
ft_cmap_done_internal( FT_CMap cmap );
883
destroy_charmaps( FT_Face face,
892
for ( n = 0; n < face->num_charmaps; n++ )
894
FT_CMap cmap = FT_CMAP( face->charmaps[n] );
897
ft_cmap_done_internal( cmap );
899
face->charmaps[n] = NULL;
902
FT_FREE( face->charmaps );
903
face->num_charmaps = 0;
907
/* destructor for faces list */
909
destroy_face( FT_Memory memory,
913
FT_Driver_Class clazz = driver->clazz;
916
/* discard auto-hinting data */
917
if ( face->autohint.finalizer )
918
face->autohint.finalizer( face->autohint.data );
920
/* Discard glyph slots for this face. */
921
/* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
922
while ( face->glyph )
923
FT_Done_GlyphSlot( face->glyph );
925
/* discard all sizes for this face */
926
FT_List_Finalize( &face->sizes_list,
927
(FT_List_Destructor)destroy_size,
932
/* now discard client data */
933
if ( face->generic.finalizer )
934
face->generic.finalizer( face );
936
/* discard charmaps */
937
destroy_charmaps( face, memory );
939
/* finalize format-specific stuff */
940
if ( clazz->done_face )
941
clazz->done_face( face );
943
/* close the stream for this face if needed */
946
( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
951
if ( face->internal )
953
FT_FREE( face->internal );
960
Destroy_Driver( FT_Driver driver )
962
FT_List_Finalize( &driver->faces_list,
963
(FT_List_Destructor)destroy_face,
967
/* check whether we need to drop the driver's glyph loader */
968
if ( FT_DRIVER_USES_OUTLINES( driver ) )
969
FT_GlyphLoader_Done( driver->glyph_loader );
973
/*************************************************************************/
976
/* find_unicode_charmap */
979
/* This function finds a Unicode charmap, if there is one. */
980
/* And if there is more than one, it tries to favour the more */
981
/* extensive one, i.e., one that supports UCS-4 against those which */
982
/* are limited to the BMP (said UCS-2 encoding.) */
984
/* This function is called from open_face() (just below), and also */
985
/* from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ). */
988
find_unicode_charmap( FT_Face face )
994
/* caller should have already checked that `face' is valid */
997
first = face->charmaps;
1000
return FT_THROW( Invalid_CharMap_Handle );
1003
* The original TrueType specification(s) only specified charmap
1004
* formats that are capable of mapping 8 or 16 bit character codes to
1007
* However, recent updates to the Apple and OpenType specifications
1008
* introduced new formats that are capable of mapping 32-bit character
1009
* codes as well. And these are already used on some fonts, mainly to
1010
* map non-BMP Asian ideographs as defined in Unicode.
1012
* For compatibility purposes, these fonts generally come with
1013
* *several* Unicode charmaps:
1015
* - One of them in the "old" 16-bit format, that cannot access
1016
* all glyphs in the font.
1018
* - Another one in the "new" 32-bit format, that can access all
1021
* This function has been written to always favor a 32-bit charmap
1022
* when found. Otherwise, a 16-bit one is returned when found.
1025
/* Since the `interesting' table, with IDs (3,10), is normally the */
1026
/* last one, we loop backwards. This loses with type1 fonts with */
1027
/* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */
1028
/* chars (.01% ?), and this is the same about 99.99% of the time! */
1030
cur = first + face->num_charmaps; /* points after the last one */
1032
for ( ; --cur >= first; )
1034
if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1036
/* XXX If some new encodings to represent UCS-4 are added, */
1037
/* they should be added here. */
1038
if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
1039
cur[0]->encoding_id == TT_MS_ID_UCS_4 ) ||
1040
( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1041
cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) )
1043
#ifdef FT_MAX_CHARMAP_CACHEABLE
1044
if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1046
FT_ERROR(( "find_unicode_charmap: UCS-4 cmap is found "
1047
"at too late position (%d)\n", cur - first ));
1051
face->charmap = cur[0];
1057
/* We do not have any UCS-4 charmap. */
1058
/* Do the loop again and search for UCS-2 charmaps. */
1059
cur = first + face->num_charmaps;
1061
for ( ; --cur >= first; )
1063
if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1065
#ifdef FT_MAX_CHARMAP_CACHEABLE
1066
if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1068
FT_ERROR(( "find_unicode_charmap: UCS-2 cmap is found "
1069
"at too late position (%d)\n", cur - first ));
1073
face->charmap = cur[0];
1078
return FT_THROW( Invalid_CharMap_Handle );
1082
/*************************************************************************/
1085
/* find_variant_selector_charmap */
1088
/* This function finds the variant selector charmap, if there is one. */
1089
/* There can only be one (platform=0, specific=5, format=14). */
1092
find_variant_selector_charmap( FT_Face face )
1099
/* caller should have already checked that `face' is valid */
1102
first = face->charmaps;
1107
end = first + face->num_charmaps; /* points after the last one */
1109
for ( cur = first; cur < end; ++cur )
1111
if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1112
cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
1113
FT_Get_CMap_Format( cur[0] ) == 14 )
1115
#ifdef FT_MAX_CHARMAP_CACHEABLE
1116
if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1118
FT_ERROR(( "find_unicode_charmap: UVS cmap is found "
1119
"at too late position (%d)\n", cur - first ));
1131
/*************************************************************************/
1137
/* This function does some work for FT_Open_Face(). */
1140
open_face( FT_Driver driver,
1142
FT_Bool external_stream,
1145
FT_Parameter* params,
1149
FT_Driver_Class clazz;
1150
FT_Face face = NULL;
1151
FT_Face_Internal internal = NULL;
1153
FT_Error error, error2;
1156
clazz = driver->clazz;
1157
memory = driver->root.memory;
1159
/* allocate the face object and perform basic initialization */
1160
if ( FT_ALLOC( face, clazz->face_object_size ) )
1163
face->driver = driver;
1164
face->memory = memory;
1165
face->stream = *astream;
1167
/* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
1168
if ( external_stream )
1169
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
1171
if ( FT_NEW( internal ) )
1174
face->internal = internal;
1176
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1181
face->internal->incremental_interface = 0;
1182
for ( i = 0; i < num_params && !face->internal->incremental_interface;
1184
if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
1185
face->internal->incremental_interface =
1186
(FT_Incremental_Interface)params[i].data;
1190
if ( clazz->init_face )
1191
error = clazz->init_face( *astream,
1196
*astream = face->stream; /* Stream may have been changed. */
1200
/* select Unicode charmap by default */
1201
error2 = find_unicode_charmap( face );
1203
/* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
1206
/* no error should happen, but we want to play safe */
1207
if ( error2 && FT_ERR_NEQ( error2, Invalid_CharMap_Handle ) )
1218
destroy_charmaps( face, memory );
1219
if ( clazz->done_face )
1220
clazz->done_face( face );
1221
FT_FREE( internal );
1230
/* there's a Mac-specific extended implementation of FT_New_Face() */
1231
/* in src/base/ftmac.c */
1233
#ifndef FT_MACINTOSH
1235
/* documentation is in freetype.h */
1237
FT_EXPORT_DEF( FT_Error )
1238
FT_New_Face( FT_Library library,
1239
const char* pathname,
1246
/* test for valid `library' and `aface' delayed to FT_Open_Face() */
1248
return FT_THROW( Invalid_Argument );
1250
args.flags = FT_OPEN_PATHNAME;
1251
args.pathname = (char*)pathname;
1254
return FT_Open_Face( library, &args, face_index, aface );
1260
/* documentation is in freetype.h */
1262
FT_EXPORT_DEF( FT_Error )
1263
FT_New_Memory_Face( FT_Library library,
1264
const FT_Byte* file_base,
1272
/* test for valid `library' and `face' delayed to FT_Open_Face() */
1274
return FT_THROW( Invalid_Argument );
1276
args.flags = FT_OPEN_MEMORY;
1277
args.memory_base = file_base;
1278
args.memory_size = file_size;
1281
return FT_Open_Face( library, &args, face_index, aface );
1285
#ifdef FT_CONFIG_OPTION_MAC_FONTS
1287
/* The behavior here is very similar to that in base/ftmac.c, but it */
1288
/* is designed to work on non-mac systems, so no mac specific calls. */
1290
/* We look at the file and determine if it is a mac dfont file or a mac */
1291
/* resource file, or a macbinary file containing a mac resource file. */
1293
/* Unlike ftmac I'm not going to look at a `FOND'. I don't really see */
1294
/* the point, especially since there may be multiple `FOND' resources. */
1295
/* Instead I'll just look for `sfnt' and `POST' resources, ordered as */
1296
/* they occur in the file. */
1298
/* Note that multiple `POST' resources do not mean multiple postscript */
1299
/* fonts; they all get jammed together to make what is essentially a */
1302
/* We aren't interested in `NFNT' or `FONT' bitmap resources. */
1304
/* As soon as we get an `sfnt' load it into memory and pass it off to */
1307
/* If we have a (set of) `POST' resources, massage them into a (memory) */
1308
/* pfb file and pass that to FT_Open_Face. (As with ftmac.c I'm not */
1309
/* going to try to save the kerning info. After all that lives in the */
1310
/* `FOND' which isn't in the file containing the `POST' resources so */
1311
/* we don't really have access to it. */
1314
/* Finalizer for a memory stream; gets called by FT_Done_Face(). */
1315
/* It frees the memory it uses. */
1318
memory_stream_close( FT_Stream stream )
1320
FT_Memory memory = stream->memory;
1323
FT_FREE( stream->base );
1331
/* Create a new memory stream from a buffer and a size. */
1334
new_memory_stream( FT_Library library,
1337
FT_Stream_CloseFunc close,
1338
FT_Stream *astream )
1342
FT_Stream stream = NULL;
1346
return FT_THROW( Invalid_Library_Handle );
1349
return FT_THROW( Invalid_Argument );
1352
memory = library->memory;
1353
if ( FT_NEW( stream ) )
1356
FT_Stream_OpenMemory( stream, base, size );
1358
stream->close = close;
1367
/* Create a new FT_Face given a buffer and a driver name. */
1369
FT_LOCAL_DEF( FT_Error )
1370
open_face_from_buffer( FT_Library library,
1374
const char* driver_name,
1379
FT_Stream stream = NULL;
1380
FT_Memory memory = library->memory;
1383
error = new_memory_stream( library,
1386
memory_stream_close,
1394
args.flags = FT_OPEN_STREAM;
1395
args.stream = stream;
1398
args.flags = args.flags | FT_OPEN_DRIVER;
1399
args.driver = FT_Get_Module( library, driver_name );
1403
/* At this point, face_index has served its purpose; */
1404
/* whoever calls this function has already used it to */
1405
/* locate the correct font data. We should not propagate */
1406
/* this index to FT_Open_Face() (unless it is negative). */
1408
if ( face_index > 0 )
1412
error = FT_Open_Face( library, &args, face_index, aface );
1414
if ( error == FT_Err_Ok )
1415
(*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
1418
FT_Stream_Free( stream, 0 );
1421
FT_Stream_Close( stream );
1430
/* Look up `TYP1' or `CID ' table from sfnt table directory. */
1431
/* `offset' and `length' must exclude the binary header in tables. */
1433
/* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
1434
/* format too. Here, since we can't expect that the TrueType font */
1435
/* driver is loaded unconditially, we must parse the font by */
1436
/* ourselves. We are only interested in the name of the table and */
1440
ft_lookup_PS_in_sfnt_stream( FT_Stream stream,
1444
FT_Bool* is_sfnt_cid )
1447
FT_UShort numTables;
1448
FT_Long pstable_index;
1455
*is_sfnt_cid = FALSE;
1457
/* TODO: support for sfnt-wrapped PS/CID in TTC format */
1459
/* version check for 'typ1' (should be ignored?) */
1460
if ( FT_READ_ULONG( tag ) )
1462
if ( tag != TTAG_typ1 )
1463
return FT_THROW( Unknown_File_Format );
1465
if ( FT_READ_USHORT( numTables ) )
1467
if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
1471
*is_sfnt_cid = FALSE;
1473
for ( i = 0; i < numTables; i++ )
1475
if ( FT_READ_ULONG( tag ) || FT_STREAM_SKIP( 4 ) ||
1476
FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
1479
if ( tag == TTAG_CID )
1484
*is_sfnt_cid = TRUE;
1485
if ( face_index < 0 )
1488
else if ( tag == TTAG_TYP1 )
1493
*is_sfnt_cid = FALSE;
1494
if ( face_index < 0 )
1497
if ( face_index >= 0 && pstable_index == face_index )
1500
return FT_THROW( Table_Missing );
1504
FT_LOCAL_DEF( FT_Error )
1505
open_face_PS_from_sfnt_stream( FT_Library library,
1509
FT_Parameter *params,
1513
FT_Memory memory = library->memory;
1514
FT_ULong offset, length;
1516
FT_Bool is_sfnt_cid;
1517
FT_Byte* sfnt_ps = NULL;
1519
FT_UNUSED( num_params );
1520
FT_UNUSED( params );
1523
pos = FT_Stream_Pos( stream );
1525
error = ft_lookup_PS_in_sfnt_stream( stream,
1533
if ( FT_Stream_Seek( stream, pos + offset ) )
1536
if ( FT_ALLOC( sfnt_ps, (FT_Long)length ) )
1539
error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
1543
error = open_face_from_buffer( library,
1546
FT_MIN( face_index, 0 ),
1547
is_sfnt_cid ? "cid" : "type1",
1554
if ( FT_ERR_EQ( error, Unknown_File_Format ) )
1556
error1 = FT_Stream_Seek( stream, pos );
1566
#ifndef FT_MACINTOSH
1568
/* The resource header says we've got resource_cnt `POST' (type1) */
1569
/* resources in this file. They all need to be coalesced into */
1570
/* one lump which gets passed on to the type1 driver. */
1571
/* Here can be only one PostScript font in a file so face_index */
1572
/* must be 0 (or -1). */
1575
Mac_Read_POST_Resource( FT_Library library,
1578
FT_Long resource_cnt,
1582
FT_Error error = FT_ERR( Cannot_Open_Resource );
1583
FT_Memory memory = library->memory;
1584
FT_Byte* pfb_data = NULL;
1587
FT_Long pfb_len, pfb_pos, pfb_lenpos;
1591
if ( face_index == -1 )
1593
if ( face_index != 0 )
1596
/* Find the length of all the POST resources, concatenated. Assume */
1597
/* worst case (each resource in its own section). */
1599
for ( i = 0; i < resource_cnt; ++i )
1601
error = FT_Stream_Seek( stream, offsets[i] );
1604
if ( FT_READ_LONG( temp ) )
1606
pfb_len += temp + 6;
1609
if ( FT_ALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
1613
pfb_data[1] = 1; /* Ascii section */
1614
pfb_data[2] = 0; /* 4-byte length, fill in later */
1623
for ( i = 0; i < resource_cnt; ++i )
1625
error = FT_Stream_Seek( stream, offsets[i] );
1628
if ( FT_READ_LONG( rlen ) )
1630
if ( FT_READ_USHORT( flags ) )
1632
FT_TRACE3(( "POST fragment[%d]: offsets=0x%08x, rlen=0x%08x, flags=0x%04x\n",
1633
i, offsets[i], rlen, flags ));
1635
/* postpone the check of rlen longer than buffer until FT_Stream_Read() */
1636
if ( ( flags >> 8 ) == 0 ) /* Comment, should not be loaded */
1639
/* the flags are part of the resource, so rlen >= 2. */
1640
/* but some fonts declare rlen = 0 for empty fragment */
1646
if ( ( flags >> 8 ) == type )
1650
if ( pfb_lenpos + 3 > pfb_len + 2 )
1652
pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1653
pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1654
pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1655
pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1657
if ( ( flags >> 8 ) == 5 ) /* End of font mark */
1660
if ( pfb_pos + 6 > pfb_len + 2 )
1662
pfb_data[pfb_pos++] = 0x80;
1667
pfb_data[pfb_pos++] = (FT_Byte)type;
1668
pfb_lenpos = pfb_pos;
1669
pfb_data[pfb_pos++] = 0; /* 4-byte length, fill in later */
1670
pfb_data[pfb_pos++] = 0;
1671
pfb_data[pfb_pos++] = 0;
1672
pfb_data[pfb_pos++] = 0;
1675
error = FT_ERR( Cannot_Open_Resource );
1676
if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
1679
error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
1685
if ( pfb_pos + 2 > pfb_len + 2 )
1687
pfb_data[pfb_pos++] = 0x80;
1688
pfb_data[pfb_pos++] = 3;
1690
if ( pfb_lenpos + 3 > pfb_len + 2 )
1692
pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1693
pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1694
pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1695
pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1697
return open_face_from_buffer( library,
1705
FT_FREE( pfb_data );
1712
/* The resource header says we've got resource_cnt `sfnt' */
1713
/* (TrueType/OpenType) resources in this file. Look through */
1714
/* them for the one indicated by face_index, load it into mem, */
1715
/* pass it on the the truetype driver and return it. */
1718
Mac_Read_sfnt_Resource( FT_Library library,
1721
FT_Long resource_cnt,
1725
FT_Memory memory = library->memory;
1726
FT_Byte* sfnt_data = NULL;
1728
FT_Long flag_offset;
1731
FT_Long face_index_in_resource = 0;
1734
if ( face_index == -1 )
1736
if ( face_index >= resource_cnt )
1737
return FT_THROW( Cannot_Open_Resource );
1739
flag_offset = offsets[face_index];
1740
error = FT_Stream_Seek( stream, flag_offset );
1744
if ( FT_READ_LONG( rlen ) )
1747
return FT_THROW( Cannot_Open_Resource );
1749
error = open_face_PS_from_sfnt_stream( library,
1757
/* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
1758
if ( FT_Stream_Seek( stream, flag_offset + 4 ) )
1761
if ( FT_ALLOC( sfnt_data, (FT_Long)rlen ) )
1763
error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, rlen );
1767
is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1768
error = open_face_from_buffer( library,
1771
face_index_in_resource,
1772
is_cff ? "cff" : "truetype",
1780
/* Check for a valid resource fork header, or a valid dfont */
1781
/* header. In a resource fork the first 16 bytes are repeated */
1782
/* at the location specified by bytes 4-7. In a dfont bytes */
1783
/* 4-7 point to 16 bytes of zeroes instead. */
1786
IsMacResource( FT_Library library,
1788
FT_Long resource_offset,
1792
FT_Memory memory = library->memory;
1794
FT_Long map_offset, rdara_pos;
1795
FT_Long *data_offsets;
1799
error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
1800
&map_offset, &rdara_pos );
1804
/* POST resources must be sorted to concatenate properly */
1805
error = FT_Raccess_Get_DataOffsets( library, stream,
1806
map_offset, rdara_pos,
1808
&data_offsets, &count );
1811
error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
1812
face_index, aface );
1813
FT_FREE( data_offsets );
1814
/* POST exists in an LWFN providing a single face */
1816
(*aface)->num_faces = 1;
1820
/* sfnt resources should not be sorted to preserve the face order by
1822
error = FT_Raccess_Get_DataOffsets( library, stream,
1823
map_offset, rdara_pos,
1825
&data_offsets, &count );
1828
FT_Long face_index_internal = face_index % count;
1831
error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
1832
face_index_internal, aface );
1833
FT_FREE( data_offsets );
1835
(*aface)->num_faces = count;
1842
/* Check for a valid macbinary header, and if we find one */
1843
/* check that the (flattened) resource fork in it is valid. */
1846
IsMacBinary( FT_Library library,
1851
unsigned char header[128];
1853
FT_Long dlen, offset;
1856
if ( NULL == stream )
1857
return FT_THROW( Invalid_Stream_Operation );
1859
error = FT_Stream_Seek( stream, 0 );
1863
error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
1867
if ( header[ 0] != 0 ||
1873
header[2 + header[1]] != 0 )
1874
return FT_THROW( Unknown_File_Format );
1876
dlen = ( header[0x53] << 24 ) |
1877
( header[0x54] << 16 ) |
1878
( header[0x55] << 8 ) |
1881
rlen = ( header[0x57] << 24 ) |
1882
( header[0x58] << 16 ) |
1883
( header[0x59] << 8 ) |
1886
offset = 128 + ( ( dlen + 127 ) & ~127 );
1888
return IsMacResource( library, stream, offset, face_index, aface );
1896
load_face_in_embedded_rfork( FT_Library library,
1900
const FT_Open_Args *args )
1904
#define FT_COMPONENT trace_raccess
1906
FT_Memory memory = library->memory;
1907
FT_Error error = FT_ERR( Unknown_File_Format );
1910
char * file_names[FT_RACCESS_N_RULES];
1911
FT_Long offsets[FT_RACCESS_N_RULES];
1912
FT_Error errors[FT_RACCESS_N_RULES];
1913
FT_Bool is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */
1916
FT_Stream stream2 = 0;
1919
FT_Raccess_Guess( library, stream,
1920
args->pathname, file_names, offsets, errors );
1922
for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
1924
is_darwin_vfs = ft_raccess_rule_by_darwin_vfs( library, i );
1925
if ( is_darwin_vfs && vfs_rfork_has_no_font )
1927
FT_TRACE3(( "Skip rule %d: darwin vfs resource fork"
1928
" is already checked and"
1929
" no font is found\n", i ));
1935
FT_TRACE3(( "Error[%d] has occurred in rule %d\n", errors[i], i ));
1939
args2.flags = FT_OPEN_PATHNAME;
1940
args2.pathname = file_names[i] ? file_names[i] : args->pathname;
1942
FT_TRACE3(( "Try rule %d: %s (offset=%d) ...",
1943
i, args2.pathname, offsets[i] ));
1945
error = FT_Stream_New( library, &args2, &stream2 );
1946
if ( is_darwin_vfs && FT_ERR_EQ( error, Cannot_Open_Stream ) )
1947
vfs_rfork_has_no_font = TRUE;
1951
FT_TRACE3(( "failed\n" ));
1955
error = IsMacResource( library, stream2, offsets[i],
1956
face_index, aface );
1957
FT_Stream_Free( stream2, 0 );
1959
FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
1963
else if ( is_darwin_vfs )
1964
vfs_rfork_has_no_font = TRUE;
1967
for (i = 0; i < FT_RACCESS_N_RULES; i++)
1969
if ( file_names[i] )
1970
FT_FREE( file_names[i] );
1973
/* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
1975
error = FT_ERR( Unknown_File_Format );
1980
#define FT_COMPONENT trace_objs
1985
/* Check for some macintosh formats without Carbon framework. */
1986
/* Is this a macbinary file? If so look at the resource fork. */
1987
/* Is this a mac dfont file? */
1988
/* Is this an old style resource fork? (in data) */
1989
/* Else call load_face_in_embedded_rfork to try extra rules */
1990
/* (defined in `ftrfork.c'). */
1993
load_mac_face( FT_Library library,
1997
const FT_Open_Args *args )
2003
error = IsMacBinary( library, stream, face_index, aface );
2004
if ( FT_ERR_EQ( error, Unknown_File_Format ) )
2008
#define FT_COMPONENT trace_raccess
2010
FT_TRACE3(( "Try as dfont: %s ...", args->pathname ));
2012
error = IsMacResource( library, stream, 0, face_index, aface );
2014
FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
2017
#define FT_COMPONENT trace_objs
2021
if ( ( FT_ERR_EQ( error, Unknown_File_Format ) ||
2022
FT_ERR_EQ( error, Invalid_Stream_Operation ) ) &&
2023
( args->flags & FT_OPEN_PATHNAME ) )
2024
error = load_face_in_embedded_rfork( library, stream,
2025
face_index, aface, args );
2030
#endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2033
/* documentation is in freetype.h */
2035
FT_EXPORT_DEF( FT_Error )
2036
FT_Open_Face( FT_Library library,
2037
const FT_Open_Args* args,
2042
FT_Driver driver = NULL;
2043
FT_Memory memory = NULL;
2044
FT_Stream stream = NULL;
2045
FT_Face face = NULL;
2046
FT_ListNode node = NULL;
2047
FT_Bool external_stream;
2052
/* test for valid `library' delayed to */
2053
/* FT_Stream_New() */
2055
if ( ( !aface && face_index >= 0 ) || !args )
2056
return FT_THROW( Invalid_Argument );
2058
external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
2061
/* create input stream */
2062
error = FT_Stream_New( library, args, &stream );
2066
memory = library->memory;
2068
/* If the font driver is specified in the `args' structure, use */
2069
/* it. Otherwise, we scan the list of registered drivers. */
2070
if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
2072
driver = FT_DRIVER( args->driver );
2074
/* not all modules are drivers, so check... */
2075
if ( FT_MODULE_IS_DRIVER( driver ) )
2077
FT_Int num_params = 0;
2078
FT_Parameter* params = 0;
2081
if ( args->flags & FT_OPEN_PARAMS )
2083
num_params = args->num_params;
2084
params = args->params;
2087
error = open_face( driver, &stream, external_stream, face_index,
2088
num_params, params, &face );
2093
error = FT_THROW( Invalid_Handle );
2095
FT_Stream_Free( stream, external_stream );
2100
error = FT_ERR( Missing_Module );
2102
/* check each font driver for an appropriate format */
2103
cur = library->modules;
2104
limit = cur + library->num_modules;
2106
for ( ; cur < limit; cur++ )
2108
/* not all modules are font drivers, so check... */
2109
if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2111
FT_Int num_params = 0;
2112
FT_Parameter* params = 0;
2115
driver = FT_DRIVER( cur[0] );
2117
if ( args->flags & FT_OPEN_PARAMS )
2119
num_params = args->num_params;
2120
params = args->params;
2123
error = open_face( driver, &stream, external_stream, face_index,
2124
num_params, params, &face );
2128
#ifdef FT_CONFIG_OPTION_MAC_FONTS
2129
if ( ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2130
FT_ERR_EQ( error, Table_Missing ) )
2132
/* TrueType but essential tables are missing */
2133
if ( FT_Stream_Seek( stream, 0 ) )
2136
error = open_face_PS_from_sfnt_stream( library,
2144
FT_Stream_Free( stream, external_stream );
2150
if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2156
/* If we are on the mac, and we get an */
2157
/* FT_Err_Invalid_Stream_Operation it may be because we have an */
2158
/* empty data fork, so we need to check the resource fork. */
2159
if ( FT_ERR_NEQ( error, Cannot_Open_Stream ) &&
2160
FT_ERR_NEQ( error, Unknown_File_Format ) &&
2161
FT_ERR_NEQ( error, Invalid_Stream_Operation ) )
2164
#if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2165
error = load_mac_face( library, stream, face_index, aface, args );
2168
/* We don't want to go to Success here. We've already done that. */
2169
/* On the other hand, if we succeeded we still need to close this */
2170
/* stream (we opened a different stream which extracted the */
2171
/* interesting information out of this stream here. That stream */
2172
/* will still be open and the face will point to it). */
2173
FT_Stream_Free( stream, external_stream );
2177
if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2179
#endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2181
/* no driver is able to handle this format */
2182
error = FT_THROW( Unknown_File_Format );
2185
FT_Stream_Free( stream, external_stream );
2190
FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2192
/* add the face object to its driver's list */
2193
if ( FT_NEW( node ) )
2197
/* don't assume driver is the same as face->driver, so use */
2198
/* face->driver instead. */
2199
FT_List_Add( &face->driver->faces_list, node );
2201
/* now allocate a glyph slot object for the face */
2202
FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2204
if ( face_index >= 0 )
2206
error = FT_New_GlyphSlot( face, NULL );
2210
/* finally, allocate a size object for the face */
2215
FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2217
error = FT_New_Size( face, &size );
2227
if ( FT_IS_SCALABLE( face ) )
2229
if ( face->height < 0 )
2230
face->height = (FT_Short)-face->height;
2232
if ( !FT_HAS_VERTICAL( face ) )
2233
face->max_advance_height = (FT_Short)face->height;
2236
if ( FT_HAS_FIXED_SIZES( face ) )
2241
for ( i = 0; i < face->num_fixed_sizes; i++ )
2243
FT_Bitmap_Size* bsize = face->available_sizes + i;
2246
if ( bsize->height < 0 )
2247
bsize->height = (FT_Short)-bsize->height;
2248
if ( bsize->x_ppem < 0 )
2249
bsize->x_ppem = (FT_Short)-bsize->x_ppem;
2250
if ( bsize->y_ppem < 0 )
2251
bsize->y_ppem = -bsize->y_ppem;
2255
/* initialize internal face data */
2257
FT_Face_Internal internal = face->internal;
2260
internal->transform_matrix.xx = 0x10000L;
2261
internal->transform_matrix.xy = 0;
2262
internal->transform_matrix.yx = 0;
2263
internal->transform_matrix.yy = 0x10000L;
2265
internal->transform_delta.x = 0;
2266
internal->transform_delta.y = 0;
2268
internal->refcount = 1;
2274
FT_Done_Face( face );
2280
FT_Done_Face( face ); /* face must be in the driver's list */
2282
destroy_face( memory, face, driver );
2285
FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
2291
/* documentation is in freetype.h */
2293
FT_EXPORT_DEF( FT_Error )
2294
FT_Attach_File( FT_Face face,
2295
const char* filepathname )
2300
/* test for valid `face' delayed to FT_Attach_Stream() */
2302
if ( !filepathname )
2303
return FT_THROW( Invalid_Argument );
2306
open.flags = FT_OPEN_PATHNAME;
2307
open.pathname = (char*)filepathname;
2309
return FT_Attach_Stream( face, &open );
2313
/* documentation is in freetype.h */
2315
FT_EXPORT_DEF( FT_Error )
2316
FT_Attach_Stream( FT_Face face,
2317
FT_Open_Args* parameters )
2323
FT_Driver_Class clazz;
2326
/* test for valid `parameters' delayed to FT_Stream_New() */
2329
return FT_THROW( Invalid_Face_Handle );
2331
driver = face->driver;
2333
return FT_THROW( Invalid_Driver_Handle );
2335
error = FT_Stream_New( driver->root.library, parameters, &stream );
2339
/* we implement FT_Attach_Stream in each driver through the */
2340
/* `attach_file' interface */
2342
error = FT_ERR( Unimplemented_Feature );
2343
clazz = driver->clazz;
2344
if ( clazz->attach_file )
2345
error = clazz->attach_file( face, stream );
2347
/* close the attached stream */
2348
FT_Stream_Free( stream,
2349
(FT_Bool)( parameters->stream &&
2350
( parameters->flags & FT_OPEN_STREAM ) ) );
2357
/* documentation is in freetype.h */
2359
FT_EXPORT_DEF( FT_Error )
2360
FT_Reference_Face( FT_Face face )
2362
face->internal->refcount++;
2368
/* documentation is in freetype.h */
2370
FT_EXPORT_DEF( FT_Error )
2371
FT_Done_Face( FT_Face face )
2379
error = FT_ERR( Invalid_Face_Handle );
2380
if ( face && face->driver )
2382
face->internal->refcount--;
2383
if ( face->internal->refcount > 0 )
2387
driver = face->driver;
2388
memory = driver->root.memory;
2390
/* find face in driver's list */
2391
node = FT_List_Find( &driver->faces_list, face );
2394
/* remove face object from the driver's list */
2395
FT_List_Remove( &driver->faces_list, node );
2398
/* now destroy the object proper */
2399
destroy_face( memory, face, driver );
2409
/* documentation is in ftobjs.h */
2411
FT_EXPORT_DEF( FT_Error )
2412
FT_New_Size( FT_Face face,
2418
FT_Driver_Class clazz;
2421
FT_ListNode node = 0;
2425
return FT_THROW( Invalid_Face_Handle );
2428
return FT_THROW( Invalid_Size_Handle );
2430
if ( !face->driver )
2431
return FT_THROW( Invalid_Driver_Handle );
2435
driver = face->driver;
2436
clazz = driver->clazz;
2437
memory = face->memory;
2439
/* Allocate new size object and perform basic initialisation */
2440
if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
2445
/* for now, do not use any internal fields in size objects */
2448
if ( clazz->init_size )
2449
error = clazz->init_size( size );
2451
/* in case of success, add to the face's list */
2456
FT_List_Add( &face->sizes_list, node );
2470
/* documentation is in ftobjs.h */
2472
FT_EXPORT_DEF( FT_Error )
2473
FT_Done_Size( FT_Size size )
2483
return FT_THROW( Invalid_Size_Handle );
2487
return FT_THROW( Invalid_Face_Handle );
2489
driver = face->driver;
2491
return FT_THROW( Invalid_Driver_Handle );
2493
memory = driver->root.memory;
2496
node = FT_List_Find( &face->sizes_list, size );
2499
FT_List_Remove( &face->sizes_list, node );
2502
if ( face->size == size )
2505
if ( face->sizes_list.head )
2506
face->size = (FT_Size)(face->sizes_list.head->data);
2509
destroy_size( memory, size, driver );
2512
error = FT_THROW( Invalid_Size_Handle );
2518
/* documentation is in ftobjs.h */
2520
FT_BASE_DEF( FT_Error )
2521
FT_Match_Size( FT_Face face,
2522
FT_Size_Request req,
2523
FT_Bool ignore_width,
2524
FT_ULong* size_index )
2530
if ( !FT_HAS_FIXED_SIZES( face ) )
2531
return FT_THROW( Invalid_Face_Handle );
2533
/* FT_Bitmap_Size doesn't provide enough info... */
2534
if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2535
return FT_THROW( Unimplemented_Feature );
2537
w = FT_REQUEST_WIDTH ( req );
2538
h = FT_REQUEST_HEIGHT( req );
2540
if ( req->width && !req->height )
2542
else if ( !req->width && req->height )
2545
w = FT_PIX_ROUND( w );
2546
h = FT_PIX_ROUND( h );
2548
for ( i = 0; i < face->num_fixed_sizes; i++ )
2550
FT_Bitmap_Size* bsize = face->available_sizes + i;
2553
if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
2556
if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
2558
FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i ));
2561
*size_index = (FT_ULong)i;
2567
return FT_THROW( Invalid_Pixel_Size );
2571
/* documentation is in ftobjs.h */
2574
ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics,
2577
FT_Pos height = metrics->height;
2580
/* compensate for glyph with bbox above/below the baseline */
2581
if ( metrics->horiBearingY < 0 )
2583
if ( height < metrics->horiBearingY )
2584
height = metrics->horiBearingY;
2586
else if ( metrics->horiBearingY > 0 )
2587
height -= metrics->horiBearingY;
2589
/* the factor 1.2 is a heuristical value */
2591
advance = height * 12 / 10;
2593
metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
2594
metrics->vertBearingY = ( advance - height ) / 2;
2595
metrics->vertAdvance = advance;
2600
ft_recompute_scaled_metrics( FT_Face face,
2601
FT_Size_Metrics* metrics )
2603
/* Compute root ascender, descender, test height, and max_advance */
2605
#ifdef GRID_FIT_METRICS
2606
metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender,
2607
metrics->y_scale ) );
2609
metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender,
2610
metrics->y_scale ) );
2612
metrics->height = FT_PIX_ROUND( FT_MulFix( face->height,
2613
metrics->y_scale ) );
2615
metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
2616
metrics->x_scale ) );
2617
#else /* !GRID_FIT_METRICS */
2618
metrics->ascender = FT_MulFix( face->ascender,
2621
metrics->descender = FT_MulFix( face->descender,
2624
metrics->height = FT_MulFix( face->height,
2627
metrics->max_advance = FT_MulFix( face->max_advance_width,
2629
#endif /* !GRID_FIT_METRICS */
2634
FT_Select_Metrics( FT_Face face,
2635
FT_ULong strike_index )
2637
FT_Size_Metrics* metrics;
2638
FT_Bitmap_Size* bsize;
2641
metrics = &face->size->metrics;
2642
bsize = face->available_sizes + strike_index;
2644
metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
2645
metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
2647
if ( FT_IS_SCALABLE( face ) )
2649
metrics->x_scale = FT_DivFix( bsize->x_ppem,
2650
face->units_per_EM );
2651
metrics->y_scale = FT_DivFix( bsize->y_ppem,
2652
face->units_per_EM );
2654
ft_recompute_scaled_metrics( face, metrics );
2658
metrics->x_scale = 1L << 16;
2659
metrics->y_scale = 1L << 16;
2660
metrics->ascender = bsize->y_ppem;
2661
metrics->descender = 0;
2662
metrics->height = bsize->height << 6;
2663
metrics->max_advance = bsize->x_ppem;
2666
FT_TRACE5(( "FT_Select_Metrics:\n" ));
2667
FT_TRACE5(( " x scale: %d (%f)\n",
2668
metrics->x_scale, metrics->x_scale / 65536.0 ));
2669
FT_TRACE5(( " y scale: %d (%f)\n",
2670
metrics->y_scale, metrics->y_scale / 65536.0 ));
2671
FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2672
FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2673
FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2674
FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2675
FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2676
FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2681
FT_Request_Metrics( FT_Face face,
2682
FT_Size_Request req )
2684
FT_Size_Metrics* metrics;
2687
metrics = &face->size->metrics;
2689
if ( FT_IS_SCALABLE( face ) )
2691
FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0;
2694
switch ( req->type )
2696
case FT_SIZE_REQUEST_TYPE_NOMINAL:
2697
w = h = face->units_per_EM;
2700
case FT_SIZE_REQUEST_TYPE_REAL_DIM:
2701
w = h = face->ascender - face->descender;
2704
case FT_SIZE_REQUEST_TYPE_BBOX:
2705
w = face->bbox.xMax - face->bbox.xMin;
2706
h = face->bbox.yMax - face->bbox.yMin;
2709
case FT_SIZE_REQUEST_TYPE_CELL:
2710
w = face->max_advance_width;
2711
h = face->ascender - face->descender;
2714
case FT_SIZE_REQUEST_TYPE_SCALES:
2715
metrics->x_scale = (FT_Fixed)req->width;
2716
metrics->y_scale = (FT_Fixed)req->height;
2717
if ( !metrics->x_scale )
2718
metrics->x_scale = metrics->y_scale;
2719
else if ( !metrics->y_scale )
2720
metrics->y_scale = metrics->x_scale;
2721
goto Calculate_Ppem;
2723
case FT_SIZE_REQUEST_TYPE_MAX:
2727
/* to be on the safe side */
2734
scaled_w = FT_REQUEST_WIDTH ( req );
2735
scaled_h = FT_REQUEST_HEIGHT( req );
2737
/* determine scales */
2740
metrics->x_scale = FT_DivFix( scaled_w, w );
2744
metrics->y_scale = FT_DivFix( scaled_h, h );
2746
if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
2748
if ( metrics->y_scale > metrics->x_scale )
2749
metrics->y_scale = metrics->x_scale;
2751
metrics->x_scale = metrics->y_scale;
2756
metrics->y_scale = metrics->x_scale;
2757
scaled_h = FT_MulDiv( scaled_w, h, w );
2762
metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
2763
scaled_w = FT_MulDiv( scaled_h, w, h );
2767
/* calculate the ppems */
2768
if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2770
scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
2771
scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
2774
metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 );
2775
metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 );
2777
ft_recompute_scaled_metrics( face, metrics );
2782
metrics->x_scale = 1L << 16;
2783
metrics->y_scale = 1L << 16;
2786
FT_TRACE5(( "FT_Request_Metrics:\n" ));
2787
FT_TRACE5(( " x scale: %d (%f)\n",
2788
metrics->x_scale, metrics->x_scale / 65536.0 ));
2789
FT_TRACE5(( " y scale: %d (%f)\n",
2790
metrics->y_scale, metrics->y_scale / 65536.0 ));
2791
FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2792
FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2793
FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2794
FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2795
FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2796
FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2800
/* documentation is in freetype.h */
2802
FT_EXPORT_DEF( FT_Error )
2803
FT_Select_Size( FT_Face face,
2804
FT_Int strike_index )
2806
FT_Driver_Class clazz;
2809
if ( !face || !FT_HAS_FIXED_SIZES( face ) )
2810
return FT_THROW( Invalid_Face_Handle );
2812
if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
2813
return FT_THROW( Invalid_Argument );
2815
clazz = face->driver->clazz;
2817
if ( clazz->select_size )
2822
error = clazz->select_size( face->size, (FT_ULong)strike_index );
2824
#ifdef FT_DEBUG_LEVEL_TRACE
2826
FT_Size_Metrics* metrics = &face->size->metrics;
2829
FT_TRACE5(( "FT_Select_Size (font driver's `select_size'):\n" ));
2830
FT_TRACE5(( " x scale: %d (%f)\n",
2831
metrics->x_scale, metrics->x_scale / 65536.0 ));
2832
FT_TRACE5(( " y scale: %d (%f)\n",
2833
metrics->y_scale, metrics->y_scale / 65536.0 ));
2834
FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2835
FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2836
FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2837
FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2838
FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2839
FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2846
FT_Select_Metrics( face, (FT_ULong)strike_index );
2852
/* documentation is in freetype.h */
2854
FT_EXPORT_DEF( FT_Error )
2855
FT_Request_Size( FT_Face face,
2856
FT_Size_Request req )
2858
FT_Driver_Class clazz;
2859
FT_ULong strike_index;
2863
return FT_THROW( Invalid_Face_Handle );
2865
if ( !req || req->width < 0 || req->height < 0 ||
2866
req->type >= FT_SIZE_REQUEST_TYPE_MAX )
2867
return FT_THROW( Invalid_Argument );
2869
clazz = face->driver->clazz;
2871
if ( clazz->request_size )
2876
error = clazz->request_size( face->size, req );
2878
#ifdef FT_DEBUG_LEVEL_TRACE
2880
FT_Size_Metrics* metrics = &face->size->metrics;
2883
FT_TRACE5(( "FT_Request_Size (font driver's `request_size'):\n" ));
2884
FT_TRACE5(( " x scale: %d (%f)\n",
2885
metrics->x_scale, metrics->x_scale / 65536.0 ));
2886
FT_TRACE5(( " y scale: %d (%f)\n",
2887
metrics->y_scale, metrics->y_scale / 65536.0 ));
2888
FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2889
FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2890
FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2891
FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2892
FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2893
FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2901
* The reason that a driver doesn't have `request_size' defined is
2902
* either that the scaling here suffices or that the supported formats
2903
* are bitmap-only and size matching is not implemented.
2905
* In the latter case, a simple size matching is done.
2907
if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
2912
error = FT_Match_Size( face, req, 0, &strike_index );
2916
return FT_Select_Size( face, (FT_Int)strike_index );
2919
FT_Request_Metrics( face, req );
2925
/* documentation is in freetype.h */
2927
FT_EXPORT_DEF( FT_Error )
2928
FT_Set_Char_Size( FT_Face face,
2929
FT_F26Dot6 char_width,
2930
FT_F26Dot6 char_height,
2931
FT_UInt horz_resolution,
2932
FT_UInt vert_resolution )
2934
FT_Size_RequestRec req;
2938
char_width = char_height;
2939
else if ( !char_height )
2940
char_height = char_width;
2942
if ( !horz_resolution )
2943
horz_resolution = vert_resolution;
2944
else if ( !vert_resolution )
2945
vert_resolution = horz_resolution;
2947
if ( char_width < 1 * 64 )
2948
char_width = 1 * 64;
2949
if ( char_height < 1 * 64 )
2950
char_height = 1 * 64;
2952
if ( !horz_resolution )
2953
horz_resolution = vert_resolution = 72;
2955
req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2956
req.width = char_width;
2957
req.height = char_height;
2958
req.horiResolution = horz_resolution;
2959
req.vertResolution = vert_resolution;
2961
return FT_Request_Size( face, &req );
2965
/* documentation is in freetype.h */
2967
FT_EXPORT_DEF( FT_Error )
2968
FT_Set_Pixel_Sizes( FT_Face face,
2969
FT_UInt pixel_width,
2970
FT_UInt pixel_height )
2972
FT_Size_RequestRec req;
2975
if ( pixel_width == 0 )
2976
pixel_width = pixel_height;
2977
else if ( pixel_height == 0 )
2978
pixel_height = pixel_width;
2980
if ( pixel_width < 1 )
2982
if ( pixel_height < 1 )
2985
/* use `>=' to avoid potential compiler warning on 16bit platforms */
2986
if ( pixel_width >= 0xFFFFU )
2987
pixel_width = 0xFFFFU;
2988
if ( pixel_height >= 0xFFFFU )
2989
pixel_height = 0xFFFFU;
2991
req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2992
req.width = pixel_width << 6;
2993
req.height = pixel_height << 6;
2994
req.horiResolution = 0;
2995
req.vertResolution = 0;
2997
return FT_Request_Size( face, &req );
3001
/* documentation is in freetype.h */
3003
FT_EXPORT_DEF( FT_Error )
3004
FT_Get_Kerning( FT_Face face,
3006
FT_UInt right_glyph,
3008
FT_Vector *akerning )
3010
FT_Error error = FT_Err_Ok;
3015
return FT_THROW( Invalid_Face_Handle );
3018
return FT_THROW( Invalid_Argument );
3020
driver = face->driver;
3025
if ( driver->clazz->get_kerning )
3027
error = driver->clazz->get_kerning( face,
3033
if ( kern_mode != FT_KERNING_UNSCALED )
3035
akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
3036
akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
3038
if ( kern_mode != FT_KERNING_UNFITTED )
3040
/* we scale down kerning values for small ppem values */
3041
/* to avoid that rounding makes them too big. */
3042
/* `25' has been determined heuristically. */
3043
if ( face->size->metrics.x_ppem < 25 )
3044
akerning->x = FT_MulDiv( akerning->x,
3045
face->size->metrics.x_ppem, 25 );
3046
if ( face->size->metrics.y_ppem < 25 )
3047
akerning->y = FT_MulDiv( akerning->y,
3048
face->size->metrics.y_ppem, 25 );
3050
akerning->x = FT_PIX_ROUND( akerning->x );
3051
akerning->y = FT_PIX_ROUND( akerning->y );
3061
/* documentation is in freetype.h */
3063
FT_EXPORT_DEF( FT_Error )
3064
FT_Get_Track_Kerning( FT_Face face,
3065
FT_Fixed point_size,
3067
FT_Fixed* akerning )
3069
FT_Service_Kerning service;
3070
FT_Error error = FT_Err_Ok;
3074
return FT_THROW( Invalid_Face_Handle );
3077
return FT_THROW( Invalid_Argument );
3079
FT_FACE_FIND_SERVICE( face, service, KERNING );
3081
return FT_THROW( Unimplemented_Feature );
3083
error = service->get_track( face,
3092
/* documentation is in freetype.h */
3094
FT_EXPORT_DEF( FT_Error )
3095
FT_Select_Charmap( FT_Face face,
3096
FT_Encoding encoding )
3103
return FT_THROW( Invalid_Face_Handle );
3105
if ( encoding == FT_ENCODING_NONE )
3106
return FT_THROW( Invalid_Argument );
3108
/* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */
3109
/* charmap available, i.e., one with UCS-4 characters, if possible. */
3111
/* This is done by find_unicode_charmap() above, to share code. */
3112
if ( encoding == FT_ENCODING_UNICODE )
3113
return find_unicode_charmap( face );
3115
cur = face->charmaps;
3117
return FT_THROW( Invalid_CharMap_Handle );
3119
limit = cur + face->num_charmaps;
3121
for ( ; cur < limit; cur++ )
3123
if ( cur[0]->encoding == encoding )
3125
#ifdef FT_MAX_CHARMAP_CACHEABLE
3126
if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3128
FT_ERROR(( "FT_Select_Charmap: requested charmap is found (%d), "
3129
"but in too late position to cache\n",
3130
cur - face->charmaps ));
3134
face->charmap = cur[0];
3139
return FT_THROW( Invalid_Argument );
3143
/* documentation is in freetype.h */
3145
FT_EXPORT_DEF( FT_Error )
3146
FT_Set_Charmap( FT_Face face,
3147
FT_CharMap charmap )
3154
return FT_THROW( Invalid_Face_Handle );
3156
cur = face->charmaps;
3158
return FT_THROW( Invalid_CharMap_Handle );
3159
if ( FT_Get_CMap_Format( charmap ) == 14 )
3160
return FT_THROW( Invalid_Argument );
3162
limit = cur + face->num_charmaps;
3164
for ( ; cur < limit; cur++ )
3166
if ( cur[0] == charmap )
3168
#ifdef FT_MAX_CHARMAP_CACHEABLE
3169
if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3171
FT_ERROR(( "FT_Set_Charmap: requested charmap is found (%d), "
3172
"but in too late position to cache\n",
3173
cur - face->charmaps ));
3177
face->charmap = cur[0];
3181
return FT_THROW( Invalid_Argument );
3185
/* documentation is in freetype.h */
3187
FT_EXPORT_DEF( FT_Int )
3188
FT_Get_Charmap_Index( FT_CharMap charmap )
3193
if ( !charmap || !charmap->face )
3196
for ( i = 0; i < charmap->face->num_charmaps; i++ )
3197
if ( charmap->face->charmaps[i] == charmap )
3200
FT_ASSERT( i < charmap->face->num_charmaps );
3202
#ifdef FT_MAX_CHARMAP_CACHEABLE
3203
if ( i > FT_MAX_CHARMAP_CACHEABLE )
3205
FT_ERROR(( "FT_Get_Charmap_Index: requested charmap is found (%d), "
3206
"but in too late position to cache\n",
3216
ft_cmap_done_internal( FT_CMap cmap )
3218
FT_CMap_Class clazz = cmap->clazz;
3219
FT_Face face = cmap->charmap.face;
3220
FT_Memory memory = FT_FACE_MEMORY( face );
3224
clazz->done( cmap );
3231
FT_CMap_Done( FT_CMap cmap )
3235
FT_Face face = cmap->charmap.face;
3236
FT_Memory memory = FT_FACE_MEMORY( face );
3241
for ( i = 0; i < face->num_charmaps; i++ )
3243
if ( (FT_CMap)face->charmaps[i] == cmap )
3245
FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1];
3248
if ( FT_RENEW_ARRAY( face->charmaps,
3250
face->num_charmaps - 1 ) )
3253
/* remove it from our list of charmaps */
3254
for ( j = i + 1; j < face->num_charmaps; j++ )
3256
if ( j == face->num_charmaps - 1 )
3257
face->charmaps[j - 1] = last_charmap;
3259
face->charmaps[j - 1] = face->charmaps[j];
3262
face->num_charmaps--;
3264
if ( (FT_CMap)face->charmap == cmap )
3265
face->charmap = NULL;
3267
ft_cmap_done_internal( cmap );
3276
FT_BASE_DEF( FT_Error )
3277
FT_CMap_New( FT_CMap_Class clazz,
3278
FT_Pointer init_data,
3282
FT_Error error = FT_Err_Ok;
3285
FT_CMap cmap = NULL;
3288
if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
3289
return FT_THROW( Invalid_Argument );
3291
face = charmap->face;
3292
memory = FT_FACE_MEMORY( face );
3294
if ( !FT_ALLOC( cmap, clazz->size ) )
3296
cmap->charmap = *charmap;
3297
cmap->clazz = clazz;
3301
error = clazz->init( cmap, init_data );
3306
/* add it to our list of charmaps */
3307
if ( FT_RENEW_ARRAY( face->charmaps,
3309
face->num_charmaps + 1 ) )
3312
face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3322
ft_cmap_done_internal( cmap );
3328
/* documentation is in freetype.h */
3330
FT_EXPORT_DEF( FT_UInt )
3331
FT_Get_Char_Index( FT_Face face,
3337
if ( face && face->charmap )
3339
FT_CMap cmap = FT_CMAP( face->charmap );
3342
if ( charcode > 0xFFFFFFFFUL )
3344
FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3345
FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3347
result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3353
/* documentation is in freetype.h */
3355
FT_EXPORT_DEF( FT_ULong )
3356
FT_Get_First_Char( FT_Face face,
3359
FT_ULong result = 0;
3363
/* only do something if we have a charmap, and we have glyphs at all */
3364
if ( face && face->charmap && face->num_glyphs )
3366
gindex = FT_Get_Char_Index( face, 0 );
3367
if ( gindex == 0 || gindex >= (FT_UInt)face->num_glyphs )
3368
result = FT_Get_Next_Char( face, 0, &gindex );
3378
/* documentation is in freetype.h */
3380
FT_EXPORT_DEF( FT_ULong )
3381
FT_Get_Next_Char( FT_Face face,
3385
FT_ULong result = 0;
3389
if ( face && face->charmap && face->num_glyphs )
3391
FT_UInt32 code = (FT_UInt32)charcode;
3392
FT_CMap cmap = FT_CMAP( face->charmap );
3397
gindex = cmap->clazz->char_next( cmap, &code );
3399
} while ( gindex >= (FT_UInt)face->num_glyphs );
3401
result = ( gindex == 0 ) ? 0 : code;
3411
/* documentation is in freetype.h */
3413
FT_EXPORT_DEF( FT_UInt )
3414
FT_Face_GetCharVariantIndex( FT_Face face,
3416
FT_ULong variantSelector )
3421
if ( face && face->charmap &&
3422
face->charmap->encoding == FT_ENCODING_UNICODE )
3424
FT_CharMap charmap = find_variant_selector_charmap( face );
3425
FT_CMap ucmap = FT_CMAP( face->charmap );
3428
if ( charmap != NULL )
3430
FT_CMap vcmap = FT_CMAP( charmap );
3433
if ( charcode > 0xFFFFFFFFUL )
3435
FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3436
FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3438
if ( variantSelector > 0xFFFFFFFFUL )
3440
FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3441
FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3444
result = vcmap->clazz->char_var_index( vcmap, ucmap,
3445
(FT_UInt32)charcode,
3446
(FT_UInt32)variantSelector );
3454
/* documentation is in freetype.h */
3456
FT_EXPORT_DEF( FT_Int )
3457
FT_Face_GetCharVariantIsDefault( FT_Face face,
3459
FT_ULong variantSelector )
3466
FT_CharMap charmap = find_variant_selector_charmap( face );
3469
if ( charmap != NULL )
3471
FT_CMap vcmap = FT_CMAP( charmap );
3474
if ( charcode > 0xFFFFFFFFUL )
3476
FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3477
FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3479
if ( variantSelector > 0xFFFFFFFFUL )
3481
FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3482
FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3485
result = vcmap->clazz->char_var_default( vcmap,
3486
(FT_UInt32)charcode,
3487
(FT_UInt32)variantSelector );
3495
/* documentation is in freetype.h */
3497
FT_EXPORT_DEF( FT_UInt32* )
3498
FT_Face_GetVariantSelectors( FT_Face face )
3500
FT_UInt32 *result = NULL;
3505
FT_CharMap charmap = find_variant_selector_charmap( face );
3508
if ( charmap != NULL )
3510
FT_CMap vcmap = FT_CMAP( charmap );
3511
FT_Memory memory = FT_FACE_MEMORY( face );
3514
result = vcmap->clazz->variant_list( vcmap, memory );
3522
/* documentation is in freetype.h */
3524
FT_EXPORT_DEF( FT_UInt32* )
3525
FT_Face_GetVariantsOfChar( FT_Face face,
3528
FT_UInt32 *result = NULL;
3533
FT_CharMap charmap = find_variant_selector_charmap( face );
3536
if ( charmap != NULL )
3538
FT_CMap vcmap = FT_CMAP( charmap );
3539
FT_Memory memory = FT_FACE_MEMORY( face );
3542
if ( charcode > 0xFFFFFFFFUL )
3544
FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3545
FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3548
result = vcmap->clazz->charvariant_list( vcmap, memory,
3549
(FT_UInt32)charcode );
3556
/* documentation is in freetype.h */
3558
FT_EXPORT_DEF( FT_UInt32* )
3559
FT_Face_GetCharsOfVariant( FT_Face face,
3560
FT_ULong variantSelector )
3562
FT_UInt32 *result = NULL;
3567
FT_CharMap charmap = find_variant_selector_charmap( face );
3570
if ( charmap != NULL )
3572
FT_CMap vcmap = FT_CMAP( charmap );
3573
FT_Memory memory = FT_FACE_MEMORY( face );
3576
if ( variantSelector > 0xFFFFFFFFUL )
3578
FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3579
FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3582
result = vcmap->clazz->variantchar_list( vcmap, memory,
3583
(FT_UInt32)variantSelector );
3591
/* documentation is in freetype.h */
3593
FT_EXPORT_DEF( FT_UInt )
3594
FT_Get_Name_Index( FT_Face face,
3595
FT_String* glyph_name )
3600
if ( face && FT_HAS_GLYPH_NAMES( face ) )
3602
FT_Service_GlyphDict service;
3605
FT_FACE_LOOKUP_SERVICE( face,
3609
if ( service && service->name_index )
3610
result = service->name_index( face, glyph_name );
3617
/* documentation is in freetype.h */
3619
FT_EXPORT_DEF( FT_Error )
3620
FT_Get_Glyph_Name( FT_Face face,
3621
FT_UInt glyph_index,
3623
FT_UInt buffer_max )
3625
FT_Error error = FT_ERR( Invalid_Argument );
3628
/* clean up buffer */
3629
if ( buffer && buffer_max > 0 )
3630
((FT_Byte*)buffer)[0] = 0;
3633
(FT_Long)glyph_index <= face->num_glyphs &&
3634
FT_HAS_GLYPH_NAMES( face ) )
3636
FT_Service_GlyphDict service;
3639
FT_FACE_LOOKUP_SERVICE( face,
3643
if ( service && service->get_name )
3644
error = service->get_name( face, glyph_index, buffer, buffer_max );
3651
/* documentation is in freetype.h */
3653
FT_EXPORT_DEF( const char* )
3654
FT_Get_Postscript_Name( FT_Face face )
3656
const char* result = NULL;
3664
FT_Service_PsFontName service;
3667
FT_FACE_LOOKUP_SERVICE( face,
3669
POSTSCRIPT_FONT_NAME );
3671
if ( service && service->get_ps_font_name )
3672
result = service->get_ps_font_name( face );
3680
/* documentation is in tttables.h */
3682
FT_EXPORT_DEF( void* )
3683
FT_Get_Sfnt_Table( FT_Face face,
3687
FT_Service_SFNT_Table service;
3690
if ( face && FT_IS_SFNT( face ) )
3692
FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3693
if ( service != NULL )
3694
table = service->get_table( face, tag );
3701
/* documentation is in tttables.h */
3703
FT_EXPORT_DEF( FT_Error )
3704
FT_Load_Sfnt_Table( FT_Face face,
3710
FT_Service_SFNT_Table service;
3713
if ( !face || !FT_IS_SFNT( face ) )
3714
return FT_THROW( Invalid_Face_Handle );
3716
FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3717
if ( service == NULL )
3718
return FT_THROW( Unimplemented_Feature );
3720
return service->load_table( face, tag, offset, buffer, length );
3724
/* documentation is in tttables.h */
3726
FT_EXPORT_DEF( FT_Error )
3727
FT_Sfnt_Table_Info( FT_Face face,
3728
FT_UInt table_index,
3732
FT_Service_SFNT_Table service;
3736
if ( !face || !FT_IS_SFNT( face ) )
3737
return FT_THROW( Invalid_Face_Handle );
3739
FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3740
if ( service == NULL )
3741
return FT_THROW( Unimplemented_Feature );
3743
return service->table_info( face, table_index, tag, &offset, length );
3747
/* documentation is in tttables.h */
3749
FT_EXPORT_DEF( FT_ULong )
3750
FT_Get_CMap_Language_ID( FT_CharMap charmap )
3752
FT_Service_TTCMaps service;
3754
TT_CMapInfo cmap_info;
3757
if ( !charmap || !charmap->face )
3760
face = charmap->face;
3761
FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3762
if ( service == NULL )
3764
if ( service->get_cmap_info( charmap, &cmap_info ))
3767
return cmap_info.language;
3771
/* documentation is in tttables.h */
3773
FT_EXPORT_DEF( FT_Long )
3774
FT_Get_CMap_Format( FT_CharMap charmap )
3776
FT_Service_TTCMaps service;
3778
TT_CMapInfo cmap_info;
3781
if ( !charmap || !charmap->face )
3784
face = charmap->face;
3785
FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3786
if ( service == NULL )
3788
if ( service->get_cmap_info( charmap, &cmap_info ))
3791
return cmap_info.format;
3795
/* documentation is in ftsizes.h */
3797
FT_EXPORT_DEF( FT_Error )
3798
FT_Activate_Size( FT_Size size )
3804
return FT_THROW( Invalid_Argument );
3807
if ( face == NULL || face->driver == NULL )
3808
return FT_THROW( Invalid_Argument );
3810
/* we don't need anything more complex than that; all size objects */
3811
/* are already listed by the face */
3818
/*************************************************************************/
3819
/*************************************************************************/
3820
/*************************************************************************/
3823
/**** R E N D E R E R S ****/
3826
/*************************************************************************/
3827
/*************************************************************************/
3828
/*************************************************************************/
3830
/* lookup a renderer by glyph format in the library's list */
3831
FT_BASE_DEF( FT_Renderer )
3832
FT_Lookup_Renderer( FT_Library library,
3833
FT_Glyph_Format format,
3837
FT_Renderer result = 0;
3843
cur = library->renderers.head;
3848
cur = (*node)->next;
3854
FT_Renderer renderer = FT_RENDERER( cur->data );
3857
if ( renderer->glyph_format == format )
3874
ft_lookup_glyph_renderer( FT_GlyphSlot slot )
3876
FT_Face face = slot->face;
3877
FT_Library library = FT_FACE_LIBRARY( face );
3878
FT_Renderer result = library->cur_renderer;
3881
if ( !result || result->glyph_format != slot->format )
3882
result = FT_Lookup_Renderer( library, slot->format, 0 );
3889
ft_set_current_renderer( FT_Library library )
3891
FT_Renderer renderer;
3894
renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
3895
library->cur_renderer = renderer;
3900
ft_add_renderer( FT_Module module )
3902
FT_Library library = module->library;
3903
FT_Memory memory = library->memory;
3905
FT_ListNode node = NULL;
3908
if ( FT_NEW( node ) )
3912
FT_Renderer render = FT_RENDERER( module );
3913
FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz;
3916
render->clazz = clazz;
3917
render->glyph_format = clazz->glyph_format;
3919
/* allocate raster object if needed */
3920
if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3921
clazz->raster_class->raster_new )
3923
error = clazz->raster_class->raster_new( memory, &render->raster );
3927
render->raster_render = clazz->raster_class->raster_render;
3928
render->render = clazz->render_glyph;
3932
node->data = module;
3933
FT_List_Add( &library->renderers, node );
3935
ft_set_current_renderer( library );
3948
ft_remove_renderer( FT_Module module )
3955
library = module->library;
3959
memory = library->memory;
3961
node = FT_List_Find( &library->renderers, module );
3964
FT_Renderer render = FT_RENDERER( module );
3967
/* release raster object, if any */
3968
if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3970
render->clazz->raster_class->raster_done( render->raster );
3972
/* remove from list */
3973
FT_List_Remove( &library->renderers, node );
3976
ft_set_current_renderer( library );
3981
/* documentation is in ftrender.h */
3983
FT_EXPORT_DEF( FT_Renderer )
3984
FT_Get_Renderer( FT_Library library,
3985
FT_Glyph_Format format )
3987
/* test for valid `library' delayed to FT_Lookup_Renderer() */
3989
return FT_Lookup_Renderer( library, format, 0 );
3993
/* documentation is in ftrender.h */
3995
FT_EXPORT_DEF( FT_Error )
3996
FT_Set_Renderer( FT_Library library,
3997
FT_Renderer renderer,
3999
FT_Parameter* parameters )
4002
FT_Error error = FT_Err_Ok;
4006
return FT_THROW( Invalid_Library_Handle );
4009
return FT_THROW( Invalid_Argument );
4011
node = FT_List_Find( &library->renderers, renderer );
4014
error = FT_THROW( Invalid_Argument );
4018
FT_List_Up( &library->renderers, node );
4020
if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
4021
library->cur_renderer = renderer;
4023
if ( num_params > 0 )
4025
FT_Renderer_SetModeFunc set_mode = renderer->clazz->set_mode;
4028
for ( ; num_params > 0; num_params-- )
4030
error = set_mode( renderer, parameters->tag, parameters->data );
4042
FT_BASE_DEF( FT_Error )
4043
FT_Render_Glyph_Internal( FT_Library library,
4045
FT_Render_Mode render_mode )
4047
FT_Error error = FT_Err_Ok;
4048
FT_Renderer renderer;
4051
/* if it is already a bitmap, no need to do anything */
4052
switch ( slot->format )
4054
case FT_GLYPH_FORMAT_BITMAP: /* already a bitmap, don't do anything */
4059
FT_ListNode node = 0;
4063
/* small shortcut for the very common case */
4064
if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
4066
renderer = library->cur_renderer;
4067
node = library->renderers.head;
4070
renderer = FT_Lookup_Renderer( library, slot->format, &node );
4072
error = FT_ERR( Unimplemented_Feature );
4075
error = renderer->render( renderer, slot, render_mode, NULL );
4077
FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
4080
/* FT_Err_Cannot_Render_Glyph is returned if the render mode */
4081
/* is unsupported by the current renderer for this glyph image */
4084
/* now, look for another renderer that supports the same */
4086
renderer = FT_Lookup_Renderer( library, slot->format, &node );
4090
/* if we changed the current renderer for the glyph image format */
4091
/* we need to select it as the next current one */
4092
if ( !error && update && renderer )
4093
FT_Set_Renderer( library, renderer, 0, 0 );
4097
#ifdef FT_DEBUG_LEVEL_TRACE
4100
#define FT_COMPONENT trace_bitmap
4102
/* we convert to a single bitmap format for computing the checksum */
4108
FT_Bitmap_New( &bitmap );
4110
err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 );
4114
unsigned char md5[16];
4119
MD5_Update( &ctx, bitmap.buffer, bitmap.rows * bitmap.pitch );
4120
MD5_Final( md5, &ctx );
4122
FT_TRACE3(( "MD5 checksum for %dx%d bitmap:\n"
4124
bitmap.rows, bitmap.pitch ));
4125
for ( i = 0; i < 16; i++ )
4126
FT_TRACE3(( "%02X", md5[i] ));
4127
FT_TRACE3(( "\n" ));
4130
FT_Bitmap_Done( library, &bitmap );
4134
#define FT_COMPONENT trace_objs
4136
#endif /* FT_DEBUG_LEVEL_TRACE */
4142
/* documentation is in freetype.h */
4144
FT_EXPORT_DEF( FT_Error )
4145
FT_Render_Glyph( FT_GlyphSlot slot,
4146
FT_Render_Mode render_mode )
4151
if ( !slot || !slot->face )
4152
return FT_THROW( Invalid_Argument );
4154
library = FT_FACE_LIBRARY( slot->face );
4156
return FT_Render_Glyph_Internal( library, slot, render_mode );
4160
/*************************************************************************/
4161
/*************************************************************************/
4162
/*************************************************************************/
4165
/**** M O D U L E S ****/
4168
/*************************************************************************/
4169
/*************************************************************************/
4170
/*************************************************************************/
4173
/*************************************************************************/
4176
/* Destroy_Module */
4179
/* Destroys a given module object. For drivers, this also destroys */
4180
/* all child faces. */
4183
/* module :: A handle to the target driver object. */
4186
/* The driver _must_ be LOCKED! */
4189
Destroy_Module( FT_Module module )
4191
FT_Memory memory = module->memory;
4192
FT_Module_Class* clazz = module->clazz;
4193
FT_Library library = module->library;
4196
if ( library && library->auto_hinter == module )
4197
library->auto_hinter = 0;
4199
/* if the module is a renderer */
4200
if ( FT_MODULE_IS_RENDERER( module ) )
4201
ft_remove_renderer( module );
4203
/* if the module is a font driver, add some steps */
4204
if ( FT_MODULE_IS_DRIVER( module ) )
4205
Destroy_Driver( FT_DRIVER( module ) );
4207
/* finalize the module object */
4208
if ( clazz->module_done )
4209
clazz->module_done( module );
4216
/* documentation is in ftmodapi.h */
4218
FT_EXPORT_DEF( FT_Error )
4219
FT_Add_Module( FT_Library library,
4220
const FT_Module_Class* clazz )
4228
#define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
4232
return FT_THROW( Invalid_Library_Handle );
4235
return FT_THROW( Invalid_Argument );
4237
/* check freetype version */
4238
if ( clazz->module_requires > FREETYPE_VER_FIXED )
4239
return FT_THROW( Invalid_Version );
4241
/* look for a module with the same name in the library's table */
4242
for ( nn = 0; nn < library->num_modules; nn++ )
4244
module = library->modules[nn];
4245
if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
4247
/* this installed module has the same name, compare their versions */
4248
if ( clazz->module_version <= module->clazz->module_version )
4249
return FT_THROW( Lower_Module_Version );
4251
/* remove the module from our list, then exit the loop to replace */
4252
/* it by our new version.. */
4253
FT_Remove_Module( library, module );
4258
memory = library->memory;
4261
if ( library->num_modules >= FT_MAX_MODULES )
4263
error = FT_THROW( Too_Many_Drivers );
4267
/* allocate module object */
4268
if ( FT_ALLOC( module, clazz->module_size ) )
4271
/* base initialization */
4272
module->library = library;
4273
module->memory = memory;
4274
module->clazz = (FT_Module_Class*)clazz;
4276
/* check whether the module is a renderer - this must be performed */
4277
/* before the normal module initialization */
4278
if ( FT_MODULE_IS_RENDERER( module ) )
4280
/* add to the renderers list */
4281
error = ft_add_renderer( module );
4286
/* is the module a auto-hinter? */
4287
if ( FT_MODULE_IS_HINTER( module ) )
4288
library->auto_hinter = module;
4290
/* if the module is a font driver */
4291
if ( FT_MODULE_IS_DRIVER( module ) )
4293
/* allocate glyph loader if needed */
4294
FT_Driver driver = FT_DRIVER( module );
4297
driver->clazz = (FT_Driver_Class)module->clazz;
4298
if ( FT_DRIVER_USES_OUTLINES( driver ) )
4300
error = FT_GlyphLoader_New( memory, &driver->glyph_loader );
4306
if ( clazz->module_init )
4308
error = clazz->module_init( module );
4313
/* add module to the library's table */
4314
library->modules[library->num_modules++] = module;
4320
if ( FT_MODULE_IS_DRIVER( module ) )
4322
FT_Driver driver = FT_DRIVER( module );
4325
if ( FT_DRIVER_USES_OUTLINES( driver ) )
4326
FT_GlyphLoader_Done( driver->glyph_loader );
4329
if ( FT_MODULE_IS_RENDERER( module ) )
4331
FT_Renderer renderer = FT_RENDERER( module );
4334
if ( renderer->clazz &&
4335
renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
4337
renderer->clazz->raster_class->raster_done( renderer->raster );
4345
/* documentation is in ftmodapi.h */
4347
FT_EXPORT_DEF( FT_Module )
4348
FT_Get_Module( FT_Library library,
4349
const char* module_name )
4351
FT_Module result = 0;
4356
if ( !library || !module_name )
4359
cur = library->modules;
4360
limit = cur + library->num_modules;
4362
for ( ; cur < limit; cur++ )
4363
if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
4373
/* documentation is in ftobjs.h */
4375
FT_BASE_DEF( const void* )
4376
FT_Get_Module_Interface( FT_Library library,
4377
const char* mod_name )
4382
/* test for valid `library' delayed to FT_Get_Module() */
4384
module = FT_Get_Module( library, mod_name );
4386
return module ? module->clazz->module_interface : 0;
4390
FT_BASE_DEF( FT_Pointer )
4391
ft_module_get_service( FT_Module module,
4392
const char* service_id )
4394
FT_Pointer result = NULL;
4399
FT_ASSERT( module->clazz && module->clazz->get_interface );
4401
/* first, look for the service in the module */
4402
if ( module->clazz->get_interface )
4403
result = module->clazz->get_interface( module, service_id );
4405
if ( result == NULL )
4407
/* we didn't find it, look in all other modules then */
4408
FT_Library library = module->library;
4409
FT_Module* cur = library->modules;
4410
FT_Module* limit = cur + library->num_modules;
4413
for ( ; cur < limit; cur++ )
4415
if ( cur[0] != module )
4417
FT_ASSERT( cur[0]->clazz );
4419
if ( cur[0]->clazz->get_interface )
4421
result = cur[0]->clazz->get_interface( cur[0], service_id );
4422
if ( result != NULL )
4434
/* documentation is in ftmodapi.h */
4436
FT_EXPORT_DEF( FT_Error )
4437
FT_Remove_Module( FT_Library library,
4440
/* try to find the module from the table, then remove it from there */
4443
return FT_THROW( Invalid_Library_Handle );
4447
FT_Module* cur = library->modules;
4448
FT_Module* limit = cur + library->num_modules;
4451
for ( ; cur < limit; cur++ )
4453
if ( cur[0] == module )
4455
/* remove it from the table */
4456
library->num_modules--;
4458
while ( cur < limit )
4465
/* destroy the module */
4466
Destroy_Module( module );
4472
return FT_THROW( Invalid_Driver_Handle );
4477
ft_property_do( FT_Library library,
4478
const FT_String* module_name,
4479
const FT_String* property_name,
4485
FT_Module_Interface interface;
4487
FT_Service_Properties service;
4489
#ifdef FT_DEBUG_LEVEL_ERROR
4490
const FT_String* set_name = "FT_Property_Set";
4491
const FT_String* get_name = "FT_Property_Get";
4492
const FT_String* func_name = set ? set_name : get_name;
4495
FT_Bool missing_func;
4499
return FT_THROW( Invalid_Library_Handle );
4501
if ( !module_name || !property_name || !value )
4502
return FT_THROW( Invalid_Argument );
4504
cur = library->modules;
4505
limit = cur + library->num_modules;
4508
for ( ; cur < limit; cur++ )
4509
if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) )
4514
FT_ERROR(( "%s: can't find module `%s'\n",
4515
func_name, module_name ));
4516
return FT_THROW( Missing_Module );
4519
/* check whether we have a service interface */
4520
if ( !cur[0]->clazz->get_interface )
4522
FT_ERROR(( "%s: module `%s' doesn't support properties\n",
4523
func_name, module_name ));
4524
return FT_THROW( Unimplemented_Feature );
4527
/* search property service */
4528
interface = cur[0]->clazz->get_interface( cur[0],
4529
FT_SERVICE_ID_PROPERTIES );
4532
FT_ERROR(( "%s: module `%s' doesn't support properties\n",
4533
func_name, module_name ));
4534
return FT_THROW( Unimplemented_Feature );
4537
service = (FT_Service_Properties)interface;
4540
missing_func = (FT_Bool)( !service->set_property );
4542
missing_func = (FT_Bool)( !service->get_property );
4546
FT_ERROR(( "%s: property service of module `%s' is broken\n",
4547
func_name, module_name ));
4548
return FT_THROW( Unimplemented_Feature );
4551
return set ? service->set_property( cur[0], property_name, value )
4552
: service->get_property( cur[0], property_name, value );
4556
/* documentation is in ftmodapi.h */
4558
FT_EXPORT_DEF( FT_Error )
4559
FT_Property_Set( FT_Library library,
4560
const FT_String* module_name,
4561
const FT_String* property_name,
4564
return ft_property_do( library,
4572
/* documentation is in ftmodapi.h */
4574
FT_EXPORT_DEF( FT_Error )
4575
FT_Property_Get( FT_Library library,
4576
const FT_String* module_name,
4577
const FT_String* property_name,
4580
return ft_property_do( library,
4588
/*************************************************************************/
4589
/*************************************************************************/
4590
/*************************************************************************/
4593
/**** L I B R A R Y ****/
4596
/*************************************************************************/
4597
/*************************************************************************/
4598
/*************************************************************************/
4601
/* documentation is in ftmodapi.h */
4603
FT_EXPORT_DEF( FT_Error )
4604
FT_Reference_Library( FT_Library library )
4606
library->refcount++;
4612
/* documentation is in ftmodapi.h */
4614
FT_EXPORT_DEF( FT_Error )
4615
FT_New_Library( FT_Memory memory,
4616
FT_Library *alibrary )
4618
FT_Library library = NULL;
4623
return FT_THROW( Invalid_Argument );
4625
#ifdef FT_DEBUG_LEVEL_ERROR
4626
/* init debugging support */
4630
/* first of all, allocate the library object */
4631
if ( FT_NEW( library ) )
4634
library->memory = memory;
4636
#ifdef FT_CONFIG_OPTION_PIC
4637
/* initialize position independent code containers */
4638
error = ft_pic_container_init( library );
4643
/* allocate the render pool */
4644
library->raster_pool_size = FT_RENDER_POOL_SIZE;
4645
#if FT_RENDER_POOL_SIZE > 0
4646
if ( FT_ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) )
4650
library->version_major = FREETYPE_MAJOR;
4651
library->version_minor = FREETYPE_MINOR;
4652
library->version_patch = FREETYPE_PATCH;
4654
library->refcount = 1;
4657
*alibrary = library;
4662
#ifdef FT_CONFIG_OPTION_PIC
4663
ft_pic_container_destroy( library );
4670
/* documentation is in freetype.h */
4672
FT_EXPORT_DEF( void )
4673
FT_Library_Version( FT_Library library,
4685
major = library->version_major;
4686
minor = library->version_minor;
4687
patch = library->version_patch;
4701
/* documentation is in ftmodapi.h */
4703
FT_EXPORT_DEF( FT_Error )
4704
FT_Done_Library( FT_Library library )
4710
return FT_THROW( Invalid_Library_Handle );
4712
library->refcount--;
4713
if ( library->refcount > 0 )
4716
memory = library->memory;
4719
* Close all faces in the library. If we don't do this, we can have
4720
* some subtle memory leaks.
4724
* - the cff font driver uses the pshinter module in cff_size_done
4725
* - if the pshinter module is destroyed before the cff font driver,
4726
* opened FT_Face objects managed by the driver are not properly
4727
* destroyed, resulting in a memory leak
4729
* Some faces are dependent on other faces, like Type42 faces that
4730
* depend on TrueType faces synthesized internally.
4732
* The order of drivers should be specified in driver_name[].
4736
const char* driver_name[] = { "type42", NULL };
4740
m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
4743
for ( n = 0; n < library->num_modules; n++ )
4745
FT_Module module = library->modules[n];
4746
const char* module_name = module->clazz->module_name;
4750
if ( driver_name[m] &&
4751
ft_strcmp( module_name, driver_name[m] ) != 0 )
4754
if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
4757
FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
4759
faces = &FT_DRIVER( module )->faces_list;
4760
while ( faces->head )
4762
FT_Done_Face( FT_FACE( faces->head->data ) );
4764
FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
4770
/* Close all other modules in the library */
4772
/* XXX Modules are removed in the reversed order so that */
4773
/* type42 module is removed before truetype module. This */
4774
/* avoids double free in some occasions. It is a hack. */
4775
while ( library->num_modules > 0 )
4776
FT_Remove_Module( library,
4777
library->modules[library->num_modules - 1] );
4783
for ( n = 0; n < library->num_modules; n++ )
4785
FT_Module module = library->modules[n];
4790
Destroy_Module( module );
4791
library->modules[n] = 0;
4797
/* Destroy raster objects */
4798
FT_FREE( library->raster_pool );
4799
library->raster_pool_size = 0;
4801
#ifdef FT_CONFIG_OPTION_PIC
4802
/* Destroy pic container contents */
4803
ft_pic_container_destroy( library );
4813
/* documentation is in ftmodapi.h */
4815
FT_EXPORT_DEF( void )
4816
FT_Set_Debug_Hook( FT_Library library,
4818
FT_DebugHook_Func debug_hook )
4820
if ( library && debug_hook &&
4822
( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
4823
library->debug_hooks[hook_index] = debug_hook;
4827
/* documentation is in ftmodapi.h */
4829
FT_EXPORT_DEF( FT_TrueTypeEngineType )
4830
FT_Get_TrueType_Engine_Type( FT_Library library )
4832
FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE;
4837
FT_Module module = FT_Get_Module( library, "truetype" );
4842
FT_Service_TrueTypeEngine service;
4845
service = (FT_Service_TrueTypeEngine)
4846
ft_module_get_service( module,
4847
FT_SERVICE_ID_TRUETYPE_ENGINE );
4849
result = service->engine_type;
4857
/* documentation is in freetype.h */
4859
FT_EXPORT_DEF( FT_Error )
4860
FT_Get_SubGlyph_Info( FT_GlyphSlot glyph,
4866
FT_Matrix *p_transform )
4868
FT_Error error = FT_ERR( Invalid_Argument );
4873
glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
4874
sub_index < glyph->num_subglyphs )
4876
FT_SubGlyph subg = glyph->subglyphs + sub_index;
4879
*p_index = subg->index;
4880
*p_flags = subg->flags;
4881
*p_arg1 = subg->arg1;
4882
*p_arg2 = subg->arg2;
4883
*p_transform = subg->transform;