1
/***************************************************************************/
5
/* OpenType objects manager (body). */
7
/* Copyright 1996-2001, 2002, 2003, 2004 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
/***************************************************************************/
20
#include FT_INTERNAL_DEBUG_H
21
#include FT_INTERNAL_CALC_H
22
#include FT_INTERNAL_STREAM_H
24
#include FT_TRUETYPE_IDS_H
25
#include FT_TRUETYPE_TAGS_H
26
#include FT_INTERNAL_SFNT_H
27
#include FT_SERVICE_POSTSCRIPT_CMAPS_H
28
#include FT_INTERNAL_POSTSCRIPT_HINTS_H
35
/*************************************************************************/
37
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
38
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
39
/* messages during execution. */
42
#define FT_COMPONENT trace_cffobjs
45
/*************************************************************************/
49
/* Note that we store the global hints in the size's `internal' root */
52
/*************************************************************************/
55
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
58
sbit_size_reset( CFF_Size size )
61
FT_Error error = CFF_Err_Ok;
63
FT_ULong strike_index;
64
FT_Size_Metrics* metrics;
65
FT_Size_Metrics* sbit_metrics;
69
metrics = &size->root.metrics;
71
face = (CFF_Face)size->root.face;
72
sfnt = (SFNT_Service)face->sfnt;
74
sbit_metrics = &size->strike_metrics;
76
error = sfnt->set_sbit_strike( face,
77
metrics->x_ppem, metrics->y_ppem,
82
TT_SBit_Strike strike = face->sbit_strikes + strike_index;
85
sbit_metrics->x_ppem = metrics->x_ppem;
86
sbit_metrics->y_ppem = metrics->y_ppem;
88
sbit_metrics->ascender = strike->hori.ascender << 6;
89
sbit_metrics->descender = strike->hori.descender << 6;
91
/* XXX: Is this correct? */
92
sbit_metrics->height = sbit_metrics->ascender -
93
sbit_metrics->descender;
95
/* XXX: Is this correct? */
96
sbit_metrics->max_advance = ( strike->hori.min_origin_SB +
97
strike->hori.max_width +
98
strike->hori.min_advance_SB ) << 6;
100
size->strike_index = (FT_UInt)strike_index;
104
size->strike_index = 0xFFFFU;
106
sbit_metrics->x_ppem = 0;
107
sbit_metrics->y_ppem = 0;
108
sbit_metrics->ascender = 0;
109
sbit_metrics->descender = 0;
110
sbit_metrics->height = 0;
111
sbit_metrics->max_advance = 0;
117
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
120
static PSH_Globals_Funcs
121
cff_size_get_globals_funcs( CFF_Size size )
123
CFF_Face face = (CFF_Face)size->root.face;
124
CFF_Font font = (CFF_FontRec *)face->extra.data;
125
PSHinter_Service pshinter = (PSHinter_Service)font->pshinter;
129
module = FT_Get_Module( size->root.face->driver->root.library,
131
return ( module && pshinter && pshinter->get_globals_funcs )
132
? pshinter->get_globals_funcs( module )
138
cff_size_done( FT_Size cffsize ) /* CFF_Size */
140
CFF_Size size = (CFF_Size)cffsize;
143
if ( cffsize->internal )
145
PSH_Globals_Funcs funcs;
148
funcs = cff_size_get_globals_funcs( size );
150
funcs->destroy( (PSH_Globals)cffsize->internal );
152
cffsize->internal = 0;
157
FT_LOCAL_DEF( FT_Error )
158
cff_size_init( FT_Size cffsize ) /* CFF_Size */
160
CFF_Size size = (CFF_Size)cffsize;
161
FT_Error error = CFF_Err_Ok;
162
PSH_Globals_Funcs funcs = cff_size_get_globals_funcs( size );
168
CFF_Face face = (CFF_Face)cffsize->face;
169
CFF_Font font = (CFF_FontRec *)face->extra.data;
170
CFF_SubFont subfont = &font->top_font;
172
CFF_Private cpriv = &subfont->private_dict;
176
/* IMPORTANT: The CFF and Type1 private dictionaries have */
177
/* slightly different structures; we need to */
178
/* synthetize a type1 dictionary on the fly here. */
184
FT_MEM_ZERO( &priv, sizeof ( priv ) );
186
count = priv.num_blue_values = cpriv->num_blue_values;
187
for ( n = 0; n < count; n++ )
188
priv.blue_values[n] = (FT_Short)cpriv->blue_values[n];
190
count = priv.num_other_blues = cpriv->num_other_blues;
191
for ( n = 0; n < count; n++ )
192
priv.other_blues[n] = (FT_Short)cpriv->other_blues[n];
194
count = priv.num_family_blues = cpriv->num_family_blues;
195
for ( n = 0; n < count; n++ )
196
priv.family_blues[n] = (FT_Short)cpriv->family_blues[n];
198
count = priv.num_family_other_blues = cpriv->num_family_other_blues;
199
for ( n = 0; n < count; n++ )
200
priv.family_other_blues[n] = (FT_Short)cpriv->family_other_blues[n];
202
priv.blue_scale = cpriv->blue_scale;
203
priv.blue_shift = (FT_Int)cpriv->blue_shift;
204
priv.blue_fuzz = (FT_Int)cpriv->blue_fuzz;
206
priv.standard_width[0] = (FT_UShort)cpriv->standard_width;
207
priv.standard_height[0] = (FT_UShort)cpriv->standard_height;
209
count = priv.num_snap_widths = cpriv->num_snap_widths;
210
for ( n = 0; n < count; n++ )
211
priv.snap_widths[n] = (FT_Short)cpriv->snap_widths[n];
213
count = priv.num_snap_heights = cpriv->num_snap_heights;
214
for ( n = 0; n < count; n++ )
215
priv.snap_heights[n] = (FT_Short)cpriv->snap_heights[n];
217
priv.force_bold = cpriv->force_bold;
218
priv.language_group = cpriv->language_group;
219
priv.lenIV = cpriv->lenIV;
222
error = funcs->create( cffsize->face->memory, &priv, &globals );
224
cffsize->internal = (FT_Size_Internal)(void*)globals;
231
FT_LOCAL_DEF( FT_Error )
232
cff_size_reset( FT_Size cffsize, /* CFF_Size */
234
FT_UInt char_height )
236
CFF_Size size = (CFF_Size)cffsize;
237
PSH_Globals_Funcs funcs = cff_size_get_globals_funcs( size );
238
FT_Error error = CFF_Err_Ok;
239
FT_Face face = cffsize->face;
241
FT_UNUSED( char_width );
242
FT_UNUSED( char_height );
246
error = funcs->set_scale( (PSH_Globals)cffsize->internal,
247
cffsize->metrics.x_scale,
248
cffsize->metrics.y_scale,
251
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
253
if ( face->face_flags & FT_FACE_FLAG_FIXED_SIZES )
255
error = sbit_size_reset( size );
257
if ( !error && !( face->face_flags & FT_FACE_FLAG_SCALABLE ) )
258
cffsize->metrics = size->strike_metrics;
263
if ( face->face_flags & FT_FACE_FLAG_SCALABLE )
270
FT_LOCAL_DEF( FT_Error )
271
cff_point_size_reset( FT_Size cffsize,
272
FT_F26Dot6 char_width,
273
FT_F26Dot6 char_height,
274
FT_UInt horz_resolution,
275
FT_UInt vert_resolution )
277
FT_UNUSED( char_width );
278
FT_UNUSED( char_height );
279
FT_UNUSED( horz_resolution );
280
FT_UNUSED( vert_resolution );
282
return cff_size_reset( cffsize, 0, 0 );
286
/*************************************************************************/
290
/*************************************************************************/
293
cff_slot_done( FT_GlyphSlot slot )
295
slot->internal->glyph_hints = 0;
299
FT_LOCAL_DEF( FT_Error )
300
cff_slot_init( FT_GlyphSlot slot )
302
CFF_Face face = (CFF_Face)slot->face;
303
CFF_Font font = (CFF_FontRec *)face->extra.data;
304
PSHinter_Service pshinter = (PSHinter_Service)font->pshinter;
312
module = FT_Get_Module( slot->face->driver->root.library,
316
T2_Hints_Funcs funcs;
319
funcs = pshinter->get_t2_funcs( module );
320
slot->internal->glyph_hints = (void*)funcs;
328
/*************************************************************************/
332
/*************************************************************************/
335
cff_strcpy( FT_Memory memory,
336
const FT_String* source )
339
FT_String* result = 0;
340
FT_Int len = (FT_Int)ft_strlen( source );
343
if ( !FT_ALLOC( result, len + 1 ) )
345
FT_MEM_COPY( result, source, len );
355
FT_LOCAL_DEF( FT_Error )
356
cff_face_init( FT_Stream stream,
357
FT_Face cffface, /* CFF_Face */
360
FT_Parameter* params )
362
CFF_Face face = (CFF_Face)cffface;
365
FT_Service_PsCMaps psnames;
366
PSHinter_Service pshinter;
367
FT_Bool pure_cff = 1;
368
FT_Bool sfnt_format = 0;
371
FT_FACE_FIND_GLOBAL_SERVICE( face, sfnt, SFNT );
372
FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_NAMES );
373
FT_FACE_FIND_GLOBAL_SERVICE( face, pshinter, POSTSCRIPT_HINTER );
378
sfnt = (SFNT_Service)FT_Get_Module_Interface(
379
cffface->driver->root.library, "sfnt" );
383
FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
385
pshinter = (PSHinter_Service)FT_Get_Module_Interface(
386
cffface->driver->root.library, "pshinter" );
389
/* create input stream from resource */
390
if ( FT_STREAM_SEEK( 0 ) )
393
/* check whether we have a valid OpenType file */
394
error = sfnt->init_face( stream, face, face_index, num_params, params );
397
if ( face->format_tag != 0x4F54544FL ) /* `OTTO'; OpenType/CFF font */
399
FT_TRACE2(( "[not a valid OpenType/CFF font]\n" ));
403
/* if we are performing a simple font format check, exit immediately */
404
if ( face_index < 0 )
409
/* now, the font can be either an OpenType/CFF font, or an SVG CEF */
410
/* font; in the latter case it doesn't have a `head' table */
411
error = face->goto_table( face, TTAG_head, stream, 0 );
416
/* load font directory */
417
error = sfnt->load_face( stream, face,
418
face_index, num_params, params );
424
/* load the `cmap' table explicitly */
425
error = sfnt->load_charmaps( face, stream );
429
/* XXX: we don't load the GPOS table, as OpenType Layout */
430
/* support will be added later to a layout library on top of */
434
/* now load the CFF part of the file */
435
error = face->goto_table( face, TTAG_CFF, stream, 0 );
441
/* rewind to start of file; we are going to load a pure-CFF font */
442
if ( FT_STREAM_SEEK( 0 ) )
447
/* now load and parse the CFF table in the file */
450
CFF_FontRecDict dict;
451
FT_Memory memory = cffface->memory;
458
face->extra.data = cff;
459
error = cff_font_load( stream, face_index, cff );
463
cff->pshinter = pshinter;
464
cff->psnames = (void*)psnames;
466
/* Complement the root flags with some interesting information. */
467
/* Note that this is only necessary for pure CFF and CEF fonts. */
469
cffface->num_glyphs = cff->num_glyphs;
471
dict = &cff->top_font.font_dict;
473
/* we need the `PSNames' module for CFF and CEF formats */
474
/* which aren't CID-keyed */
475
if ( dict->cid_registry == 0xFFFFU && !psnames )
477
FT_ERROR(( "cff_face_init:" ));
478
FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
480
FT_ERROR(( " without the `PSNames' module\n" ));
486
char* style_name = NULL;
489
/* Set up num_faces. */
490
cffface->num_faces = cff->num_faces;
492
/* compute number of glyphs */
493
if ( dict->cid_registry != 0xFFFFU )
494
cffface->num_glyphs = dict->cid_count;
496
cffface->num_glyphs = cff->charstrings_index.count;
498
/* set global bbox, as well as EM size */
499
cffface->bbox.xMin = dict->font_bbox.xMin >> 16;
500
cffface->bbox.yMin = dict->font_bbox.yMin >> 16;
501
cffface->bbox.xMax = ( dict->font_bbox.xMax + 0xFFFFU ) >> 16;
502
cffface->bbox.yMax = ( dict->font_bbox.yMax + 0xFFFFU ) >> 16;
504
cffface->ascender = (FT_Short)( cffface->bbox.yMax );
505
cffface->descender = (FT_Short)( cffface->bbox.yMin );
506
cffface->height = (FT_Short)(
507
( ( cffface->ascender - cffface->descender ) * 12 ) / 10 );
509
if ( dict->units_per_em )
510
cffface->units_per_EM = dict->units_per_em;
512
cffface->units_per_EM = 1000;
514
cffface->underline_position =
515
(FT_Short)( dict->underline_position >> 16 );
516
cffface->underline_thickness =
517
(FT_Short)( dict->underline_thickness >> 16 );
519
/* retrieve font family & style name */
520
cffface->family_name = cff_index_get_name( &cff->name_index,
523
if ( cffface->family_name )
525
char* full = cff_index_get_sid_string( &cff->string_index,
529
char* family = cffface->family_name;
531
/* We try to extract the style name from the full name. */
532
/* We need to ignore spaces and dashes during the search. */
537
/* skip common characters at the start of both strings */
538
if ( *fullp == *family )
545
/* ignore spaces and dashes in full name during comparison */
546
if ( *fullp == ' ' || *fullp == '-' )
552
/* ignore spaces and dashes in family name during comparison */
553
if ( *family == ' ' || *family == '-' )
559
if ( !*family && *fullp )
561
/* Rhe full name begins with the same characters as the */
562
/* family name, with spaces and dashes removed. In this */
563
/* case, the remaining string in `fullp' will be used as */
564
/* the style name. */
565
style_name = cff_strcpy( memory, fullp );
574
char *cid_font_name =
575
cff_index_get_sid_string( &cff->string_index,
580
/* do we have a `/FontName' for a CID-keyed font? */
582
cffface->family_name = cid_font_name;
586
cffface->style_name = style_name;
588
/* assume "Regular" style if we don't know better */
589
cffface->style_name = cff_strcpy( memory, (char *)"Regular" );
591
/*******************************************************************/
593
/* Compute face flags. */
595
flags = FT_FACE_FLAG_SCALABLE | /* scalable outlines */
596
FT_FACE_FLAG_HORIZONTAL; /* horizontal data */
599
flags |= FT_FACE_FLAG_SFNT;
601
/* fixed width font? */
602
if ( dict->is_fixed_pitch )
603
flags |= FT_FACE_FLAG_FIXED_WIDTH;
605
/* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
607
/* kerning available? */
608
if ( face->kern_pairs )
609
flags |= FT_FACE_FLAG_KERNING;
612
cffface->face_flags = flags;
614
/*******************************************************************/
616
/* Compute style flags. */
620
if ( dict->italic_angle )
621
flags |= FT_STYLE_FLAG_ITALIC;
624
char *weight = cff_index_get_sid_string( &cff->string_index,
630
if ( !ft_strcmp( weight, "Bold" ) ||
631
!ft_strcmp( weight, "Black" ) )
632
flags |= FT_STYLE_FLAG_BOLD;
636
cffface->style_flags = flags;
639
#ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
640
/* CID-keyed CFF fonts don't have glyph names -- the SFNT loader */
641
/* has unset this flag because of the 3.0 `post' table */
642
if ( dict->cid_registry == 0xFFFFU )
643
cffface->face_flags |= FT_FACE_FLAG_GLYPH_NAMES;
646
/*******************************************************************/
648
/* Compute char maps. */
651
/* Try to synthetize a Unicode charmap if there is none available */
652
/* already. If an OpenType font contains a Unicode "cmap", we */
653
/* will use it, whatever be in the CFF part of the file. */
655
FT_CharMapRec cmaprec;
658
CFF_Encoding encoding = &cff->encoding;
661
for ( nn = 0; nn < (FT_UInt)cffface->num_charmaps; nn++ )
663
cmap = cffface->charmaps[nn];
665
/* Windows Unicode (3,1)? */
666
if ( cmap->platform_id == 3 && cmap->encoding_id == 1 )
669
/* Deprecated Unicode platform id? */
670
if ( cmap->platform_id == 0 )
671
goto Skip_Unicode; /* Standard Unicode (deprecated) */
674
/* since CID-keyed fonts don't contain glyph names, we can't */
675
/* construct a cmap */
676
if ( pure_cff && cff->top_font.font_dict.cid_registry != 0xFFFFU )
679
/* we didn't find a Unicode charmap -- synthetize one */
680
cmaprec.face = cffface;
681
cmaprec.platform_id = 3;
682
cmaprec.encoding_id = 1;
683
cmaprec.encoding = FT_ENCODING_UNICODE;
685
nn = (FT_UInt)cffface->num_charmaps;
687
FT_CMap_New( &cff_cmap_unicode_class_rec, NULL, &cmaprec, NULL );
689
/* if no Unicode charmap was previously selected, select this one */
690
if ( cffface->charmap == NULL && nn != (FT_UInt)cffface->num_charmaps )
691
cffface->charmap = cffface->charmaps[nn];
694
if ( encoding->count > 0 )
699
cmaprec.face = cffface;
700
cmaprec.platform_id = 7; /* Adobe platform id */
702
if ( encoding->offset == 0 )
704
cmaprec.encoding_id = TT_ADOBE_ID_STANDARD;
705
cmaprec.encoding = FT_ENCODING_ADOBE_STANDARD;
706
clazz = &cff_cmap_encoding_class_rec;
708
else if ( encoding->offset == 1 )
710
cmaprec.encoding_id = TT_ADOBE_ID_EXPERT;
711
cmaprec.encoding = FT_ENCODING_ADOBE_EXPERT;
712
clazz = &cff_cmap_encoding_class_rec;
716
cmaprec.encoding_id = TT_ADOBE_ID_CUSTOM;
717
cmaprec.encoding = FT_ENCODING_ADOBE_CUSTOM;
718
clazz = &cff_cmap_encoding_class_rec;
721
FT_CMap_New( clazz, NULL, &cmaprec, NULL );
730
error = CFF_Err_Unknown_File_Format;
736
cff_face_done( FT_Face cffface ) /* CFF_Face */
738
CFF_Face face = (CFF_Face)cffface;
739
FT_Memory memory = cffface->memory;
740
SFNT_Service sfnt = (SFNT_Service)face->sfnt;
744
sfnt->done_face( face );
747
CFF_Font cff = (CFF_Font)face->extra.data;
752
cff_font_done( cff );
753
FT_FREE( face->extra.data );
759
FT_LOCAL_DEF( FT_Error )
760
cff_driver_init( FT_Module module )
769
cff_driver_done( FT_Module module )