1 /***************************************************************************/
5 /* OpenType font driver implementation (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 /***************************************************************************/
20 #include FT_FREETYPE_H
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_INTERNAL_SFNT_H
24 #include FT_SERVICE_CID_H
25 #include FT_SERVICE_POSTSCRIPT_INFO_H
26 #include FT_SERVICE_POSTSCRIPT_NAME_H
27 #include FT_SERVICE_TT_CMAP_H
38 #include FT_SERVICE_XFREE86_NAME_H
39 #include FT_SERVICE_GLYPH_DICT_H
40 #include FT_SERVICE_PROPERTIES_H
41 #include FT_CFF_DRIVER_H
44 /*************************************************************************/
46 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
47 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
48 /* messages during execution. */
51 #define FT_COMPONENT trace_cffdriver
54 /*************************************************************************/
55 /*************************************************************************/
56 /*************************************************************************/
62 /*************************************************************************/
63 /*************************************************************************/
64 /*************************************************************************/
68 #define PAIR_TAG( left, right ) ( ( (FT_ULong)left << 16 ) | \
72 /*************************************************************************/
78 /* A driver method used to return the kerning vector between two */
79 /* glyphs of the same face. */
82 /* face :: A handle to the source face object. */
84 /* left_glyph :: The index of the left glyph in the kern pair. */
86 /* right_glyph :: The index of the right glyph in the kern pair. */
89 /* kerning :: The kerning vector. This is in font units for */
90 /* scalable formats, and in pixels for fixed-sizes */
94 /* FreeType error code. 0 means success. */
97 /* Only horizontal layouts (left-to-right & right-to-left) are */
98 /* supported by this function. Other layouts, or more sophisticated */
99 /* kernings, are out of scope of this method (the basic driver */
100 /* interface is meant to be simple). */
102 /* They can be implemented by format-specific interfaces. */
104 FT_CALLBACK_DEF( FT_Error )
105 cff_get_kerning( FT_Face ttface, /* TT_Face */
110 TT_Face face = (TT_Face)ttface;
111 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
118 kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
127 /*************************************************************************/
133 /* A driver method used to load a glyph within a given glyph slot. */
136 /* slot :: A handle to the target slot object where the glyph */
137 /* will be loaded. */
139 /* size :: A handle to the source face size at which the glyph */
140 /* must be scaled, loaded, etc. */
142 /* glyph_index :: The index of the glyph in the font file. */
144 /* load_flags :: A flag indicating what to load for this glyph. The */
145 /* FT_LOAD_??? constants can be used to control the */
146 /* glyph loading process (e.g., whether the outline */
147 /* should be scaled, whether to load bitmaps or not, */
148 /* whether to hint the outline, etc). */
151 /* FreeType error code. 0 means success. */
153 FT_CALLBACK_DEF( FT_Error )
154 cff_glyph_load( FT_GlyphSlot cffslot, /* CFF_GlyphSlot */
155 FT_Size cffsize, /* CFF_Size */
157 FT_Int32 load_flags )
160 CFF_GlyphSlot slot = (CFF_GlyphSlot)cffslot;
161 CFF_Size size = (CFF_Size)cffsize;
165 return FT_THROW( Invalid_Slot_Handle );
167 FT_TRACE1(( "cff_glyph_load: glyph index %d\n", glyph_index ));
169 /* check whether we want a scaled outline or bitmap */
171 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
173 /* reset the size object if necessary */
174 if ( load_flags & FT_LOAD_NO_SCALE )
179 /* these two objects must have the same parent */
180 if ( cffsize->face != cffslot->face )
181 return FT_THROW( Invalid_Face_Handle );
184 /* now load the glyph outline if necessary */
185 error = cff_slot_load( slot, size, glyph_index, load_flags );
187 /* force drop-out mode to 2 - irrelevant now */
188 /* slot->outline.dropout_mode = 2; */
194 FT_CALLBACK_DEF( FT_Error )
195 cff_get_advances( FT_Face face,
202 FT_Error error = FT_Err_Ok;
203 FT_GlyphSlot slot = face->glyph;
206 flags |= (FT_UInt32)FT_LOAD_ADVANCE_ONLY;
208 for ( nn = 0; nn < count; nn++ )
210 error = cff_glyph_load( slot, face->size, start + nn, flags );
214 advances[nn] = ( flags & FT_LOAD_VERTICAL_LAYOUT )
215 ? slot->linearVertAdvance
216 : slot->linearHoriAdvance;
229 cff_get_glyph_name( CFF_Face face,
234 CFF_Font font = (CFF_Font)face->extra.data;
240 if ( !font->psnames )
242 FT_ERROR(( "cff_get_glyph_name:"
243 " cannot get glyph name from CFF & CEF fonts\n"
245 " without the `PSNames' module\n" ));
246 error = FT_THROW( Missing_Module );
250 /* first, locate the sid in the charset table */
251 sid = font->charset.sids[glyph_index];
253 /* now, lookup the name itself */
254 gname = cff_index_get_sid_string( font, sid );
257 FT_STRCPYN( buffer, gname, buffer_max );
267 cff_get_name_index( CFF_Face face,
268 FT_String* glyph_name )
272 FT_Service_PsCMaps psnames;
278 cff = (CFF_FontRec *)face->extra.data;
279 charset = &cff->charset;
281 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
285 for ( i = 0; i < cff->num_glyphs; i++ )
287 sid = charset->sids[i];
290 name = cff_index_get_string( cff, sid - 391 );
292 name = (FT_String *)psnames->adobe_std_strings( sid );
297 if ( !ft_strcmp( glyph_name, name ) )
305 FT_DEFINE_SERVICE_GLYPHDICTREC(
306 cff_service_glyph_dict,
307 (FT_GlyphDict_GetNameFunc) cff_get_glyph_name,
308 (FT_GlyphDict_NameIndexFunc)cff_get_name_index
313 * POSTSCRIPT INFO SERVICE
318 cff_ps_has_glyph_names( FT_Face face )
320 return ( face->face_flags & FT_FACE_FLAG_GLYPH_NAMES ) > 0;
325 cff_ps_get_font_info( CFF_Face face,
326 PS_FontInfoRec* afont_info )
328 CFF_Font cff = (CFF_Font)face->extra.data;
329 FT_Error error = FT_Err_Ok;
332 if ( cff && cff->font_info == NULL )
334 CFF_FontRecDict dict = &cff->top_font.font_dict;
335 PS_FontInfoRec *font_info = NULL;
336 FT_Memory memory = face->root.memory;
339 if ( FT_ALLOC( font_info, sizeof ( *font_info ) ) )
342 font_info->version = cff_index_get_sid_string( cff,
344 font_info->notice = cff_index_get_sid_string( cff,
346 font_info->full_name = cff_index_get_sid_string( cff,
348 font_info->family_name = cff_index_get_sid_string( cff,
350 font_info->weight = cff_index_get_sid_string( cff,
352 font_info->italic_angle = dict->italic_angle;
353 font_info->is_fixed_pitch = dict->is_fixed_pitch;
354 font_info->underline_position = (FT_Short)dict->underline_position;
355 font_info->underline_thickness = (FT_Short)dict->underline_thickness;
357 cff->font_info = font_info;
361 *afont_info = *cff->font_info;
368 FT_DEFINE_SERVICE_PSINFOREC(
370 (PS_GetFontInfoFunc) cff_ps_get_font_info,
371 (PS_GetFontExtraFunc) NULL,
372 (PS_HasGlyphNamesFunc) cff_ps_has_glyph_names,
373 (PS_GetFontPrivateFunc)NULL, /* unsupported with CFF fonts */
374 (PS_GetFontValueFunc) NULL /* not implemented */
379 * POSTSCRIPT NAME SERVICE
384 cff_get_ps_name( CFF_Face face )
386 CFF_Font cff = (CFF_Font)face->extra.data;
389 return (const char*)cff->font_name;
393 FT_DEFINE_SERVICE_PSFONTNAMEREC(
395 (FT_PsName_GetFunc)cff_get_ps_name
402 * If the charmap is a synthetic Unicode encoding cmap or
403 * a Type 1 standard (or expert) encoding cmap, hide TT CMAP INFO
404 * service defined in SFNT module.
406 * Otherwise call the service function in the sfnt module.
410 cff_get_cmap_info( FT_CharMap charmap,
411 TT_CMapInfo *cmap_info )
413 FT_CMap cmap = FT_CMAP( charmap );
414 FT_Error error = FT_Err_Ok;
416 FT_Face face = FT_CMAP_FACE( cmap );
417 FT_Library library = FT_FACE_LIBRARY( face );
420 cmap_info->language = 0;
421 cmap_info->format = 0;
423 if ( cmap->clazz != &CFF_CMAP_ENCODING_CLASS_REC_GET &&
424 cmap->clazz != &CFF_CMAP_UNICODE_CLASS_REC_GET )
426 FT_Module sfnt = FT_Get_Module( library, "sfnt" );
427 FT_Service_TTCMaps service =
428 (FT_Service_TTCMaps)ft_module_get_service( sfnt,
429 FT_SERVICE_ID_TT_CMAP );
432 if ( service && service->get_cmap_info )
433 error = service->get_cmap_info( charmap, cmap_info );
440 FT_DEFINE_SERVICE_TTCMAPSREC(
441 cff_service_get_cmap_info,
442 (TT_CMap_Info_GetFunc)cff_get_cmap_info
451 cff_get_ros( CFF_Face face,
452 const char* *registry,
453 const char* *ordering,
456 FT_Error error = FT_Err_Ok;
457 CFF_Font cff = (CFF_Font)face->extra.data;
462 CFF_FontRecDict dict = &cff->top_font.font_dict;
465 if ( dict->cid_registry == 0xFFFFU )
467 error = FT_THROW( Invalid_Argument );
473 if ( cff->registry == NULL )
474 cff->registry = cff_index_get_sid_string( cff,
475 dict->cid_registry );
476 *registry = cff->registry;
481 if ( cff->ordering == NULL )
482 cff->ordering = cff_index_get_sid_string( cff,
483 dict->cid_ordering );
484 *ordering = cff->ordering;
488 * XXX: According to Adobe TechNote #5176, the supplement in CFF
489 * can be a real number. We truncate it to fit public API
490 * since freetype-2.3.6.
494 if ( dict->cid_supplement < FT_INT_MIN ||
495 dict->cid_supplement > FT_INT_MAX )
496 FT_TRACE1(( "cff_get_ros: too large supplement %d is truncated\n",
497 dict->cid_supplement ));
498 *supplement = (FT_Int)dict->cid_supplement;
508 cff_get_is_cid( CFF_Face face,
511 FT_Error error = FT_Err_Ok;
512 CFF_Font cff = (CFF_Font)face->extra.data;
519 CFF_FontRecDict dict = &cff->top_font.font_dict;
522 if ( dict->cid_registry != 0xFFFFU )
531 cff_get_cid_from_glyph_index( CFF_Face face,
535 FT_Error error = FT_Err_Ok;
539 cff = (CFF_Font)face->extra.data;
544 CFF_FontRecDict dict = &cff->top_font.font_dict;
547 if ( dict->cid_registry == 0xFFFFU )
549 error = FT_THROW( Invalid_Argument );
553 if ( glyph_index > cff->num_glyphs )
555 error = FT_THROW( Invalid_Argument );
559 c = cff->charset.sids[glyph_index];
570 FT_DEFINE_SERVICE_CIDREC(
571 cff_service_cid_info,
572 (FT_CID_GetRegistryOrderingSupplementFunc)cff_get_ros,
573 (FT_CID_GetIsInternallyCIDKeyedFunc) cff_get_is_cid,
574 (FT_CID_GetCIDFromGlyphIndexFunc) cff_get_cid_from_glyph_index
583 cff_property_set( FT_Module module, /* CFF_Driver */
584 const char* property_name,
587 FT_Error error = FT_Err_Ok;
588 CFF_Driver driver = (CFF_Driver)module;
591 if ( !ft_strcmp( property_name, "darkening-parameters" ) )
593 FT_Int* darken_params = (FT_Int*)value;
595 FT_Int x1 = darken_params[0];
596 FT_Int y1 = darken_params[1];
597 FT_Int x2 = darken_params[2];
598 FT_Int y2 = darken_params[3];
599 FT_Int x3 = darken_params[4];
600 FT_Int y3 = darken_params[5];
601 FT_Int x4 = darken_params[6];
602 FT_Int y4 = darken_params[7];
605 if ( x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0 ||
606 y1 < 0 || y2 < 0 || y3 < 0 || y4 < 0 ||
607 x1 > x2 || x2 > x3 || x3 > x4 ||
608 y1 > 500 || y2 > 500 || y3 > 500 || y4 > 500 )
609 return FT_THROW( Invalid_Argument );
611 driver->darken_params[0] = x1;
612 driver->darken_params[1] = y1;
613 driver->darken_params[2] = x2;
614 driver->darken_params[3] = y2;
615 driver->darken_params[4] = x3;
616 driver->darken_params[5] = y3;
617 driver->darken_params[6] = x4;
618 driver->darken_params[7] = y4;
622 else if ( !ft_strcmp( property_name, "hinting-engine" ) )
624 FT_UInt* hinting_engine = (FT_UInt*)value;
627 #ifndef CFF_CONFIG_OPTION_OLD_ENGINE
628 if ( *hinting_engine != FT_CFF_HINTING_ADOBE )
629 error = FT_ERR( Unimplemented_Feature );
632 driver->hinting_engine = *hinting_engine;
636 else if ( !ft_strcmp( property_name, "no-stem-darkening" ) )
638 FT_Bool* no_stem_darkening = (FT_Bool*)value;
641 driver->no_stem_darkening = *no_stem_darkening;
646 FT_TRACE0(( "cff_property_set: missing property `%s'\n",
648 return FT_THROW( Missing_Property );
653 cff_property_get( FT_Module module, /* CFF_Driver */
654 const char* property_name,
657 FT_Error error = FT_Err_Ok;
658 CFF_Driver driver = (CFF_Driver)module;
661 if ( !ft_strcmp( property_name, "darkening-parameters" ) )
663 FT_Int* darken_params = driver->darken_params;
664 FT_Int* val = (FT_Int*)value;
667 val[0] = darken_params[0];
668 val[1] = darken_params[1];
669 val[2] = darken_params[2];
670 val[3] = darken_params[3];
671 val[4] = darken_params[4];
672 val[5] = darken_params[5];
673 val[6] = darken_params[6];
674 val[7] = darken_params[7];
678 else if ( !ft_strcmp( property_name, "hinting-engine" ) )
680 FT_UInt hinting_engine = driver->hinting_engine;
681 FT_UInt* val = (FT_UInt*)value;
684 *val = hinting_engine;
688 else if ( !ft_strcmp( property_name, "no-stem-darkening" ) )
690 FT_Bool no_stem_darkening = driver->no_stem_darkening;
691 FT_Bool* val = (FT_Bool*)value;
694 *val = no_stem_darkening;
699 FT_TRACE0(( "cff_property_get: missing property `%s'\n",
701 return FT_THROW( Missing_Property );
705 FT_DEFINE_SERVICE_PROPERTIESREC(
706 cff_service_properties,
707 (FT_Properties_SetFunc)cff_property_set,
708 (FT_Properties_GetFunc)cff_property_get )
711 /*************************************************************************/
712 /*************************************************************************/
713 /*************************************************************************/
716 /**** D R I V E R I N T E R F A C E ****/
719 /*************************************************************************/
720 /*************************************************************************/
721 /*************************************************************************/
723 #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
724 FT_DEFINE_SERVICEDESCREC7(
726 FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_CFF,
727 FT_SERVICE_ID_POSTSCRIPT_INFO, &CFF_SERVICE_PS_INFO_GET,
728 FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &CFF_SERVICE_PS_NAME_GET,
729 FT_SERVICE_ID_GLYPH_DICT, &CFF_SERVICE_GLYPH_DICT_GET,
730 FT_SERVICE_ID_TT_CMAP, &CFF_SERVICE_GET_CMAP_INFO_GET,
731 FT_SERVICE_ID_CID, &CFF_SERVICE_CID_INFO_GET,
732 FT_SERVICE_ID_PROPERTIES, &CFF_SERVICE_PROPERTIES_GET
735 FT_DEFINE_SERVICEDESCREC6(
737 FT_SERVICE_ID_XF86_NAME, FT_XF86_FORMAT_CFF,
738 FT_SERVICE_ID_POSTSCRIPT_INFO, &CFF_SERVICE_PS_INFO_GET,
739 FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &CFF_SERVICE_PS_NAME_GET,
740 FT_SERVICE_ID_TT_CMAP, &CFF_SERVICE_GET_CMAP_INFO_GET,
741 FT_SERVICE_ID_CID, &CFF_SERVICE_CID_INFO_GET,
742 FT_SERVICE_ID_PROPERTIES, &CFF_SERVICE_PROPERTIES_GET
747 FT_CALLBACK_DEF( FT_Module_Interface )
748 cff_get_interface( FT_Module driver, /* CFF_Driver */
749 const char* module_interface )
753 FT_Module_Interface result;
756 /* CFF_SERVICES_GET dereferences `library' in PIC mode */
757 #ifdef FT_CONFIG_OPTION_PIC
760 library = driver->library;
765 result = ft_service_list_lookup( CFF_SERVICES_GET, module_interface );
766 if ( result != NULL )
769 /* `driver' is not yet evaluated in non-PIC mode */
770 #ifndef FT_CONFIG_OPTION_PIC
773 library = driver->library;
778 /* we pass our request to the `sfnt' module */
779 sfnt = FT_Get_Module( library, "sfnt" );
781 return sfnt ? sfnt->clazz->get_interface( sfnt, module_interface ) : 0;
785 /* The FT_DriverInterface structure is defined in ftdriver.h. */
787 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
788 #define CFF_SIZE_SELECT cff_size_select
790 #define CFF_SIZE_SELECT 0
796 FT_MODULE_FONT_DRIVER |
797 FT_MODULE_DRIVER_SCALABLE |
798 FT_MODULE_DRIVER_HAS_HINTER,
800 sizeof ( CFF_DriverRec ),
805 0, /* module-specific interface */
811 /* now the specific driver fields */
812 sizeof ( TT_FaceRec ),
813 sizeof ( CFF_SizeRec ),
814 sizeof ( CFF_GlyphSlotRec ),
826 0, /* FT_Face_AttachFunc */