1 /****************************************************************************
5 * OpenType Glyph Loader (body).
7 * Copyright (C) 1996-2020 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.
19 #include <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftstream.h>
21 #include <freetype/internal/sfnt.h>
22 #include <freetype/internal/ftcalc.h>
23 #include <freetype/internal/psaux.h>
24 #include <freetype/ftoutln.h>
25 #include <freetype/ftdriver.h>
33 /**************************************************************************
35 * The macro FT_COMPONENT is used in trace mode. It is an implicit
36 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
37 * messages during execution.
40 #define FT_COMPONENT cffgload
43 FT_LOCAL_DEF( FT_Error )
44 cff_get_glyph_data( TT_Face face,
49 #ifdef FT_CONFIG_OPTION_INCREMENTAL
50 /* For incremental fonts get the character data using the */
51 /* callback function. */
52 if ( face->root.internal->incremental_interface )
56 face->root.internal->incremental_interface->funcs->get_glyph_data(
57 face->root.internal->incremental_interface->object,
61 *pointer = (FT_Byte*)data.pointer;
62 *length = (FT_ULong)data.length;
67 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
70 CFF_Font cff = (CFF_Font)(face->extra.data);
73 return cff_index_access_element( &cff->charstrings_index, glyph_index,
80 cff_free_glyph_data( TT_Face face,
84 #ifndef FT_CONFIG_OPTION_INCREMENTAL
88 #ifdef FT_CONFIG_OPTION_INCREMENTAL
89 /* For incremental fonts get the character data using the */
90 /* callback function. */
91 if ( face->root.internal->incremental_interface )
96 data.pointer = *pointer;
97 data.length = (FT_Int)length;
99 face->root.internal->incremental_interface->funcs->free_glyph_data(
100 face->root.internal->incremental_interface->object, &data );
103 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
106 CFF_Font cff = (CFF_Font)(face->extra.data);
109 cff_index_forget_element( &cff->charstrings_index, pointer );
114 /*************************************************************************/
115 /*************************************************************************/
116 /*************************************************************************/
117 /********** *********/
118 /********** *********/
119 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
120 /********** *********/
121 /********** The following code is in charge of computing *********/
122 /********** the maximum advance width of the font. It *********/
123 /********** quickly processes each glyph charstring to *********/
124 /********** extract the value from either a `sbw' or `seac' *********/
125 /********** operator. *********/
126 /********** *********/
127 /*************************************************************************/
128 /*************************************************************************/
129 /*************************************************************************/
132 #if 0 /* unused until we support pure CFF fonts */
135 FT_LOCAL_DEF( FT_Error )
136 cff_compute_max_advance( TT_Face face,
137 FT_Int* max_advance )
139 FT_Error error = FT_Err_Ok;
142 CFF_Font cff = (CFF_Font)face->other;
144 PSAux_Service psaux = (PSAux_Service)face->psaux;
145 const CFF_Decoder_Funcs decoder_funcs = psaux->cff_decoder_funcs;
150 /* Initialize load decoder */
151 decoder_funcs->init( &decoder, face, 0, 0, 0, 0, 0, 0 );
153 decoder.builder.metrics_only = 1;
154 decoder.builder.load_points = 0;
156 /* For each glyph, parse the glyph charstring and extract */
157 /* the advance width. */
158 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
162 FT_ULong charstring_len;
165 /* now get load the unscaled outline */
166 error = cff_get_glyph_data( face, glyph_index,
167 &charstring, &charstring_len );
170 error = decoder_funcs->prepare( &decoder, size, glyph_index );
172 error = decoder_funcs->parse_charstrings_old( &decoder,
177 cff_free_glyph_data( face, &charstring, &charstring_len );
180 /* ignore the error if one has occurred -- skip to next glyph */
184 *max_advance = decoder.builder.advance.x;
193 FT_LOCAL_DEF( FT_Error )
194 cff_slot_load( CFF_GlyphSlot glyph,
197 FT_Int32 load_flags )
201 PS_Decoder psdecoder;
202 TT_Face face = (TT_Face)glyph->root.face;
203 FT_Bool hinting, scaled, force_scaling;
204 CFF_Font cff = (CFF_Font)face->extra.data;
206 PSAux_Service psaux = (PSAux_Service)face->psaux;
207 const CFF_Decoder_Funcs decoder_funcs = psaux->cff_decoder_funcs;
209 FT_Matrix font_matrix;
210 FT_Vector font_offset;
213 force_scaling = FALSE;
215 /* in a CID-keyed font, consider `glyph_index' as a CID and map */
216 /* it immediately to the real glyph_index -- if it isn't a */
217 /* subsetted font, glyph_indices and CIDs are identical, though */
218 if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
221 /* don't handle CID 0 (.notdef) which is directly mapped to GID 0 */
222 if ( glyph_index != 0 )
224 glyph_index = cff_charset_cid_to_gindex( &cff->charset,
226 if ( glyph_index == 0 )
227 return FT_THROW( Invalid_Argument );
230 else if ( glyph_index >= cff->num_glyphs )
231 return FT_THROW( Invalid_Argument );
233 if ( load_flags & FT_LOAD_NO_RECURSE )
234 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
236 glyph->x_scale = 0x10000L;
237 glyph->y_scale = 0x10000L;
240 glyph->x_scale = size->root.metrics.x_scale;
241 glyph->y_scale = size->root.metrics.y_scale;
244 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
246 /* try to load embedded bitmap if any */
248 /* XXX: The convention should be emphasized in */
249 /* the documents because it can be confusing. */
252 CFF_Face cff_face = (CFF_Face)size->root.face;
253 SFNT_Service sfnt = (SFNT_Service)cff_face->sfnt;
254 FT_Stream stream = cff_face->root.stream;
257 if ( size->strike_index != 0xFFFFFFFFUL &&
259 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
261 TT_SBit_MetricsRec metrics;
264 error = sfnt->load_sbit_image( face,
274 FT_Bool has_vertical_info;
279 glyph->root.outline.n_points = 0;
280 glyph->root.outline.n_contours = 0;
282 glyph->root.metrics.width = (FT_Pos)metrics.width * 64;
283 glyph->root.metrics.height = (FT_Pos)metrics.height * 64;
285 glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX * 64;
286 glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY * 64;
287 glyph->root.metrics.horiAdvance = (FT_Pos)metrics.horiAdvance * 64;
289 glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX * 64;
290 glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY * 64;
291 glyph->root.metrics.vertAdvance = (FT_Pos)metrics.vertAdvance * 64;
293 glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
295 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
297 glyph->root.bitmap_left = metrics.vertBearingX;
298 glyph->root.bitmap_top = metrics.vertBearingY;
302 glyph->root.bitmap_left = metrics.horiBearingX;
303 glyph->root.bitmap_top = metrics.horiBearingY;
306 /* compute linear advance widths */
308 (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 0,
312 glyph->root.linearHoriAdvance = advance;
314 has_vertical_info = FT_BOOL(
315 face->vertical_info &&
316 face->vertical.number_Of_VMetrics > 0 );
318 /* get the vertical metrics from the vmtx table if we have one */
319 if ( has_vertical_info )
321 (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
325 glyph->root.linearVertAdvance = advance;
329 /* make up vertical ones */
330 if ( face->os2.version != 0xFFFFU )
331 glyph->root.linearVertAdvance = (FT_Pos)
332 ( face->os2.sTypoAscender - face->os2.sTypoDescender );
334 glyph->root.linearVertAdvance = (FT_Pos)
335 ( face->horizontal.Ascender - face->horizontal.Descender );
343 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
345 /* return immediately if we only want the embedded bitmaps */
346 if ( load_flags & FT_LOAD_SBITS_ONLY )
347 return FT_THROW( Invalid_Argument );
349 /* if we have a CID subfont, use its matrix (which has already */
350 /* been multiplied with the root matrix) */
352 /* this scaling is only relevant if the PS hinter isn't active */
353 if ( cff->num_subfonts )
355 FT_Long top_upm, sub_upm;
356 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select,
360 if ( fd_index >= cff->num_subfonts )
361 fd_index = (FT_Byte)( cff->num_subfonts - 1 );
363 top_upm = (FT_Long)cff->top_font.font_dict.units_per_em;
364 sub_upm = (FT_Long)cff->subfonts[fd_index]->font_dict.units_per_em;
366 font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix;
367 font_offset = cff->subfonts[fd_index]->font_dict.font_offset;
369 if ( top_upm != sub_upm )
371 glyph->x_scale = FT_MulDiv( glyph->x_scale, top_upm, sub_upm );
372 glyph->y_scale = FT_MulDiv( glyph->y_scale, top_upm, sub_upm );
374 force_scaling = TRUE;
379 font_matrix = cff->top_font.font_dict.font_matrix;
380 font_offset = cff->top_font.font_dict.font_offset;
383 glyph->root.outline.n_points = 0;
384 glyph->root.outline.n_contours = 0;
386 /* top-level code ensures that FT_LOAD_NO_HINTING is set */
387 /* if FT_LOAD_NO_SCALE is active */
388 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
389 scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 );
391 glyph->hint = hinting;
392 glyph->scaled = scaled;
393 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */
396 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
397 PS_Driver driver = (PS_Driver)FT_FACE_DRIVER( face );
401 FT_ULong charstring_len;
404 decoder_funcs->init( &decoder, face, size, glyph, hinting,
405 FT_LOAD_TARGET_MODE( load_flags ),
407 cff_free_glyph_data );
409 /* this is for pure CFFs */
410 if ( load_flags & FT_LOAD_ADVANCE_ONLY )
411 decoder.width_only = TRUE;
413 decoder.builder.no_recurse =
414 FT_BOOL( load_flags & FT_LOAD_NO_RECURSE );
416 /* now load the unscaled outline */
417 error = cff_get_glyph_data( face, glyph_index,
418 &charstring, &charstring_len );
420 goto Glyph_Build_Finished;
422 error = decoder_funcs->prepare( &decoder, size, glyph_index );
424 goto Glyph_Build_Finished;
426 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
427 /* choose which CFF renderer to use */
428 if ( driver->hinting_engine == FT_HINTING_FREETYPE )
429 error = decoder_funcs->parse_charstrings_old( &decoder,
436 psaux->ps_decoder_init( &psdecoder, &decoder, FALSE );
438 error = decoder_funcs->parse_charstrings( &psdecoder,
442 /* Adobe's engine uses 16.16 numbers everywhere; */
443 /* as a consequence, glyphs larger than 2000ppem get rejected */
444 if ( FT_ERR_EQ( error, Glyph_Too_Big ) )
446 /* this time, we retry unhinted and scale up the glyph later on */
447 /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */
448 /* 0x400 for both `x_scale' and `y_scale' in this case) */
450 force_scaling = TRUE;
451 glyph->hint = hinting;
453 error = decoder_funcs->parse_charstrings( &psdecoder,
459 cff_free_glyph_data( face, &charstring, charstring_len );
462 goto Glyph_Build_Finished;
464 #ifdef FT_CONFIG_OPTION_INCREMENTAL
465 /* Control data and length may not be available for incremental */
467 if ( face->root.internal->incremental_interface )
469 glyph->root.control_data = NULL;
470 glyph->root.control_len = 0;
473 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
475 /* We set control_data and control_len if charstrings is loaded. */
476 /* See how charstring loads at cff_index_access_element() in */
479 CFF_Index csindex = &cff->charstrings_index;
482 if ( csindex->offsets )
484 glyph->root.control_data = csindex->bytes +
485 csindex->offsets[glyph_index] - 1;
486 glyph->root.control_len = (FT_Long)charstring_len;
490 Glyph_Build_Finished:
491 /* save new glyph tables, if no error */
493 decoder.builder.funcs.done( &decoder.builder );
494 /* XXX: anything to do for broken glyph entry? */
497 #ifdef FT_CONFIG_OPTION_INCREMENTAL
499 /* Incremental fonts can optionally override the metrics. */
501 face->root.internal->incremental_interface &&
502 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
504 FT_Incremental_MetricsRec metrics;
507 metrics.bearing_x = decoder.builder.left_bearing.x;
508 metrics.bearing_y = 0;
509 metrics.advance = decoder.builder.advance.x;
510 metrics.advance_v = decoder.builder.advance.y;
512 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
513 face->root.internal->incremental_interface->object,
514 glyph_index, FALSE, &metrics );
516 decoder.builder.left_bearing.x = metrics.bearing_x;
517 decoder.builder.advance.x = metrics.advance;
518 decoder.builder.advance.y = metrics.advance_v;
521 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
525 /* Now, set the metrics -- this is rather simple, as */
526 /* the left side bearing is the xMin, and the top side */
527 /* bearing the yMax. */
529 /* For composite glyphs, return only left side bearing and */
531 if ( load_flags & FT_LOAD_NO_RECURSE )
533 FT_Slot_Internal internal = glyph->root.internal;
536 glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
537 glyph->root.metrics.horiAdvance = decoder.glyph_width;
538 internal->glyph_matrix = font_matrix;
539 internal->glyph_delta = font_offset;
540 internal->glyph_transformed = 1;
545 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
546 FT_Bool has_vertical_info;
549 if ( face->horizontal.number_Of_HMetrics )
551 FT_Short horiBearingX = 0;
552 FT_UShort horiAdvance = 0;
555 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0,
559 metrics->horiAdvance = horiAdvance;
560 metrics->horiBearingX = horiBearingX;
561 glyph->root.linearHoriAdvance = horiAdvance;
565 /* copy the _unscaled_ advance width */
566 metrics->horiAdvance = decoder.glyph_width;
567 glyph->root.linearHoriAdvance = decoder.glyph_width;
570 glyph->root.internal->glyph_transformed = 0;
572 has_vertical_info = FT_BOOL( face->vertical_info &&
573 face->vertical.number_Of_VMetrics > 0 );
575 /* get the vertical metrics from the vmtx table if we have one */
576 if ( has_vertical_info )
578 FT_Short vertBearingY = 0;
579 FT_UShort vertAdvance = 0;
582 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
586 metrics->vertBearingY = vertBearingY;
587 metrics->vertAdvance = vertAdvance;
591 /* make up vertical ones */
592 if ( face->os2.version != 0xFFFFU )
593 metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender -
594 face->os2.sTypoDescender );
596 metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender -
597 face->horizontal.Descender );
600 glyph->root.linearVertAdvance = metrics->vertAdvance;
602 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
604 glyph->root.outline.flags = 0;
605 if ( size && size->root.metrics.y_ppem < 24 )
606 glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
608 glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
610 /* apply the font matrix, if any */
611 if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L ||
612 font_matrix.xy != 0 || font_matrix.yx != 0 )
614 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
616 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance,
618 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance,
622 if ( font_offset.x || font_offset.y )
624 FT_Outline_Translate( &glyph->root.outline,
628 metrics->horiAdvance += font_offset.x;
629 metrics->vertAdvance += font_offset.y;
632 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling )
634 /* scale the outline and the metrics */
636 FT_Outline* cur = &glyph->root.outline;
637 FT_Vector* vec = cur->points;
638 FT_Fixed x_scale = glyph->x_scale;
639 FT_Fixed y_scale = glyph->y_scale;
642 /* First of all, scale the points */
643 if ( !hinting || !decoder.builder.hints_funcs )
644 for ( n = cur->n_points; n > 0; n--, vec++ )
646 vec->x = FT_MulFix( vec->x, x_scale );
647 vec->y = FT_MulFix( vec->y, y_scale );
650 /* Then scale the metrics */
651 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
652 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
655 /* compute the other metrics */
656 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
658 metrics->width = cbox.xMax - cbox.xMin;
659 metrics->height = cbox.yMax - cbox.yMin;
661 metrics->horiBearingX = cbox.xMin;
662 metrics->horiBearingY = cbox.yMax;
664 if ( has_vertical_info )
666 metrics->vertBearingX = metrics->horiBearingX -
667 metrics->horiAdvance / 2;
668 metrics->vertBearingY = FT_MulFix( metrics->vertBearingY,
673 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
674 ft_synthesize_vertical_metrics( metrics,
675 metrics->vertAdvance );