Skip to content
0.2.0-dev Download ZIP
643 lines ·21.0 KB Raw
1/****************************************************************************
2 *
3 * cidgload.c
4 *
5 * CID-keyed Type1 Glyph Loader (body).
6 *
7 * Copyright (C) 1996-2026 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
9 *
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
15 *
16 */
17
18
19#include "cidload.h"
20#include "cidgload.h"
21#include <freetype/internal/ftdebug.h>
22#include <freetype/internal/ftstream.h>
23#include <freetype/ftoutln.h>
24#include <freetype/internal/ftcalc.h>
25
26#include <freetype/internal/psaux.h>
27#include <freetype/internal/cfftypes.h>
28#include <freetype/ftdriver.h>
29
30#include "ciderrs.h"
31
32
33 /**************************************************************************
34 *
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.
38 */
39#undef FT_COMPONENT
40#define FT_COMPONENT cidgload
41
42
43 /*
44 * A helper function to compute FD number (`fd_select`), the offset to the
45 * head of the glyph data (`off1`), and the offset to the and of the glyph
46 * data (`off2`).
47 *
48 * The number how many times `cid_get_offset` is invoked can be controlled
49 * by the number of non-NULL arguments. If `fd_select` is non-NULL but
50 * `off1` and `off2` are NULL, `cid_get_offset` is invoked only for
51 * `fd_select`; `off1` and `off2` are not validated.
52 *
53 */
54 FT_LOCAL_DEF( FT_Error )
55 cid_compute_fd_and_offsets( CID_Face face,
56 FT_UInt glyph_index,
57 FT_ULong* fd_select_p,
58 FT_ULong* off1_p,
59 FT_ULong* off2_p )
60 {
61 FT_Error error = FT_Err_Ok;
62
63 CID_FaceInfo cid = &face->cid;
64 FT_Stream stream = face->cid_stream;
65 FT_UInt entry_len = cid->fd_bytes + cid->gd_bytes;
66
67 FT_Byte* p;
68 FT_Bool need_frame_exit = 0;
69 FT_ULong fd_select, off1, off2;
70
71
72 /* For ordinary fonts, read the CID font dictionary index */
73 /* and charstring offset from the CIDMap. */
74
75 if ( FT_STREAM_SEEK( cid->data_offset + cid->cidmap_offset +
76 glyph_index * entry_len ) ||
77 FT_FRAME_ENTER( 2 * entry_len ) )
78 goto Exit;
79
80 need_frame_exit = 1;
81
82 p = (FT_Byte*)stream->cursor;
83 fd_select = cid_get_offset( &p, cid->fd_bytes );
84 off1 = cid_get_offset( &p, cid->gd_bytes );
85
86 p += cid->fd_bytes;
87 off2 = cid_get_offset( &p, cid->gd_bytes );
88
89 if ( fd_select_p )
90 *fd_select_p = fd_select;
91 if ( off1_p )
92 *off1_p = off1;
93 if ( off2_p )
94 *off2_p = off2;
95
96 if ( fd_select >= cid->num_dicts )
97 {
98 /*
99 * fd_select == 0xFF is often used to indicate that the CID
100 * has no charstring to be rendered, similar to GID = 0xFFFF
101 * in TrueType fonts.
102 */
103 if ( ( cid->fd_bytes == 1 && fd_select == 0xFFU ) ||
104 ( cid->fd_bytes == 2 && fd_select == 0xFFFFU ) )
105 {
106 FT_TRACE1(( "cid_load_glyph: fail for glyph index %u:\n",
107 glyph_index ));
108 FT_TRACE1(( " FD number %lu is the maximum\n",
109 fd_select ));
110 FT_TRACE1(( " integer fitting into %u byte%s\n",
111 cid->fd_bytes, cid->fd_bytes == 1 ? "" : "s" ));
112 }
113 else
114 {
115 FT_TRACE0(( "cid_load_glyph: fail for glyph index %u:\n",
116 glyph_index ));
117 FT_TRACE0(( " FD number %lu is larger\n",
118 fd_select ));
119 FT_TRACE0(( " than number of dictionaries (%u)\n",
120 cid->num_dicts ));
121 }
122
123 error = FT_THROW( Invalid_Offset );
124 goto Exit;
125 }
126 else if ( off2 > stream->size )
127 {
128 FT_TRACE0(( "cid_load_glyph: fail for glyph index %u:\n",
129 glyph_index ));
130 FT_TRACE0(( " end of the glyph data\n" ));
131 FT_TRACE0(( " is beyond the data stream\n" ));
132
133 error = FT_THROW( Invalid_Offset );
134 goto Exit;
135 }
136 else if ( off1 > off2 )
137 {
138 FT_TRACE0(( "cid_load_glyph: fail for glyph index %u:\n",
139 glyph_index ));
140 FT_TRACE0(( " the end position of glyph data\n" ));
141 FT_TRACE0(( " is set before the start position\n" ));
142
143 error = FT_THROW( Invalid_Offset );
144 }
145
146 Exit:
147 if ( need_frame_exit )
148 FT_FRAME_EXIT();
149
150 return error;
151 }
152
153
154 FT_CALLBACK_DEF( FT_Error )
155 cid_load_glyph( T1_Decoder decoder,
156 FT_UInt glyph_index )
157 {
158 CID_Face face = (CID_Face)decoder->builder.face;
159 CID_FaceInfo cid = &face->cid;
160 FT_Byte* p;
161 FT_ULong fd_select;
162 FT_Stream stream = face->cid_stream;
163 FT_Error error = FT_Err_Ok;
164 FT_Byte* charstring = NULL;
165 FT_Memory memory = face->root.memory;
166 FT_ULong glyph_length = 0;
167 PSAux_Service psaux = (PSAux_Service)face->psaux;
168
169 FT_Bool force_scaling = FALSE;
170
171#ifdef FT_CONFIG_OPTION_INCREMENTAL
172 FT_Incremental_InterfaceRec *inc =
173 face->root.internal->incremental_interface;
174#endif
175
176
177 FT_TRACE1(( "cid_load_glyph: glyph index %u\n", glyph_index ));
178
179#ifdef FT_CONFIG_OPTION_INCREMENTAL
180
181 /* For incremental fonts get the character data using */
182 /* the callback function. */
183 if ( inc )
184 {
185 FT_Data glyph_data;
186
187
188 error = inc->funcs->get_glyph_data( inc->object,
189 glyph_index, &glyph_data );
190 if ( error || glyph_data.length < cid->fd_bytes )
191 goto Exit;
192
193 p = (FT_Byte*)glyph_data.pointer;
194 fd_select = cid_get_offset( &p, cid->fd_bytes );
195
196 if ( fd_select >= cid->num_dicts )
197 {
198 /*
199 * fd_select == 0xFF is often used to indicate that the CID
200 * has no charstring to be rendered, similar to GID = 0xFFFF
201 * in TrueType fonts.
202 */
203 if ( ( cid->fd_bytes == 1 && fd_select == 0xFFU ) ||
204 ( cid->fd_bytes == 2 && fd_select == 0xFFFFU ) )
205 {
206 FT_TRACE1(( "cid_load_glyph: fail for glyph index %u:\n",
207 glyph_index ));
208 FT_TRACE1(( " FD number %lu is the maximum\n",
209 fd_select ));
210 FT_TRACE1(( " integer fitting into %u byte%s\n",
211 cid->fd_bytes, cid->fd_bytes == 1 ? "" : "s" ));
212 }
213 else
214 {
215 FT_TRACE0(( "cid_load_glyph: fail for glyph index %u:\n",
216 glyph_index ));
217 FT_TRACE0(( " FD number %lu is larger\n",
218 fd_select ));
219 FT_TRACE0(( " than number of dictionaries (%u)\n",
220 cid->num_dicts ));
221 }
222
223 error = FT_THROW( Invalid_Offset );
224 goto Exit;
225 }
226
227 glyph_length = glyph_data.length - cid->fd_bytes;
228
229 if ( !FT_QALLOC( charstring, glyph_length ) )
230 FT_MEM_COPY( charstring, glyph_data.pointer + cid->fd_bytes,
231 glyph_length );
232
233 inc->funcs->free_glyph_data( inc->object, &glyph_data );
234
235 if ( error )
236 goto Exit;
237 }
238
239 else
240
241#endif /* FT_CONFIG_OPTION_INCREMENTAL */
242 {
243 FT_ULong off1, off2;
244
245
246 error = cid_compute_fd_and_offsets( face, glyph_index,
247 &fd_select, &off1, &off2 );
248 if ( error )
249 goto Exit;
250
251 glyph_length = off2 - off1;
252
253 if ( glyph_length == 0 ||
254 FT_QALLOC( charstring, glyph_length ) ||
255 FT_STREAM_READ_AT( cid->data_offset + off1,
256 charstring, glyph_length ) )
257 goto Exit;
258 }
259
260 /* Now set up the subrs array and parse the charstrings. */
261 {
262 CID_FaceDict dict;
263 CID_Subrs cid_subrs = face->subrs + fd_select;
264 FT_UInt cs_offset;
265
266
267 /* Set up subrs */
268 decoder->num_subrs = cid_subrs->num_subrs;
269 decoder->subrs = cid_subrs->code;
270 decoder->subrs_len = 0;
271 decoder->subrs_hash = NULL;
272
273 /* Set up font matrix */
274 dict = cid->font_dicts + fd_select;
275
276 decoder->font_matrix = dict->font_matrix;
277 decoder->font_offset = dict->font_offset;
278 decoder->lenIV = dict->private_dict.lenIV;
279
280 /* Decode the charstring. */
281
282 /* Adjustment for seed bytes. */
283 cs_offset = decoder->lenIV >= 0 ? (FT_UInt)decoder->lenIV : 0;
284 if ( cs_offset > glyph_length )
285 {
286 FT_TRACE0(( "cid_load_glyph: fail for glyph_index=%u,"
287 " offset to the charstring is beyond glyph length\n",
288 glyph_index ));
289 error = FT_THROW( Invalid_Offset );
290 goto Exit;
291 }
292
293 /* Decrypt only if lenIV >= 0. */
294 if ( decoder->lenIV >= 0 )
295 psaux->t1_decrypt( charstring, glyph_length, 4330 );
296
297 /* choose which renderer to use */
298#ifdef T1_CONFIG_OPTION_OLD_ENGINE
299 if ( ( (PS_Driver)FT_FACE_DRIVER( face ) )->hinting_engine ==
300 FT_HINTING_FREETYPE ||
301 decoder->builder.metrics_only )
302 error = psaux->t1_decoder_funcs->parse_charstrings_old(
303 decoder,
304 charstring + cs_offset,
305 glyph_length - cs_offset );
306#else
307 if ( decoder->builder.metrics_only )
308 error = psaux->t1_decoder_funcs->parse_metrics(
309 decoder,
310 charstring + cs_offset,
311 glyph_length - cs_offset );
312#endif
313 else
314 {
315 PS_Decoder psdecoder;
316 CFF_SubFontRec subfont;
317
318
319 psaux->ps_decoder_init( &psdecoder, decoder, TRUE );
320
321 psaux->t1_make_subfont( FT_FACE( face ),
322 &dict->private_dict,
323 &subfont );
324 psdecoder.current_subfont = &subfont;
325
326 error = psaux->t1_decoder_funcs->parse_charstrings(
327 &psdecoder,
328 charstring + cs_offset,
329 glyph_length - cs_offset );
330
331 /* Adobe's engine uses 16.16 numbers everywhere; */
332 /* as a consequence, glyphs larger than 2000ppem get rejected */
333 if ( FT_ERR_EQ( error, Glyph_Too_Big ) )
334 {
335 /* this time, we retry unhinted and scale up the glyph later on */
336 /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */
337 /* 0x400 for both `x_scale' and `y_scale' in this case) */
338 ((CID_GlyphSlot)decoder->builder.glyph)->hint = FALSE;
339
340 force_scaling = TRUE;
341
342 error = psaux->t1_decoder_funcs->parse_charstrings(
343 &psdecoder,
344 charstring + cs_offset,
345 glyph_length - cs_offset );
346 }
347 }
348 }
349
350#ifdef FT_CONFIG_OPTION_INCREMENTAL
351
352 /* Incremental fonts can optionally override the metrics. */
353 if ( !error && inc && inc->funcs->get_glyph_metrics )
354 {
355 FT_Incremental_MetricsRec metrics;
356
357
358 metrics.bearing_x = FIXED_TO_INT( decoder->builder.left_bearing.x );
359 metrics.bearing_y = 0;
360 metrics.advance = FIXED_TO_INT( decoder->builder.advance.x );
361 metrics.advance_v = FIXED_TO_INT( decoder->builder.advance.y );
362
363 error = inc->funcs->get_glyph_metrics( inc->object,
364 glyph_index, FALSE, &metrics );
365
366 decoder->builder.left_bearing.x = INT_TO_FIXED( metrics.bearing_x );
367 decoder->builder.advance.x = INT_TO_FIXED( metrics.advance );
368 decoder->builder.advance.y = INT_TO_FIXED( metrics.advance_v );
369 }
370
371#endif /* FT_CONFIG_OPTION_INCREMENTAL */
372
373 Exit:
374 FT_FREE( charstring );
375
376 ((CID_GlyphSlot)decoder->builder.glyph)->scaled = force_scaling;
377
378 return error;
379 }
380
381
382#if 0
383
384
385 /*************************************************************************/
386 /*************************************************************************/
387 /*************************************************************************/
388 /********** *********/
389 /********** *********/
390 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
391 /********** *********/
392 /********** The following code is in charge of computing *********/
393 /********** the maximum advance width of the font. It *********/
394 /********** quickly processes each glyph charstring to *********/
395 /********** extract the value from either a `sbw' or `seac' *********/
396 /********** operator. *********/
397 /********** *********/
398 /*************************************************************************/
399 /*************************************************************************/
400 /*************************************************************************/
401
402
403 FT_LOCAL_DEF( FT_Error )
404 cid_face_compute_max_advance( CID_Face face,
405 FT_Int* max_advance )
406 {
407 FT_Error error;
408 T1_DecoderRec decoder;
409 FT_Int glyph_index;
410
411 PSAux_Service psaux = (PSAux_Service)face->psaux;
412
413
414 *max_advance = 0;
415
416 /* Initialize load decoder */
417 error = psaux->t1_decoder_funcs->init( &decoder,
418 (FT_Face)face,
419 0, /* size */
420 0, /* glyph slot */
421 0, /* glyph names! XXX */
422 0, /* blend == 0 */
423 0, /* hinting == 0 */
424 cid_load_glyph );
425 if ( error )
426 return error;
427
428 /* TODO: initialize decoder.len_buildchar and decoder.buildchar */
429 /* if we ever support CID-keyed multiple master fonts */
430
431 decoder.builder.metrics_only = 1;
432 decoder.builder.load_points = 0;
433
434 /* for each glyph, parse the glyph charstring and extract */
435 /* the advance width */
436 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
437 glyph_index++ )
438 {
439 /* now get load the unscaled outline */
440 error = cid_load_glyph( &decoder, glyph_index );
441 /* ignore the error if one occurred - skip to next glyph */
442 }
443
444 *max_advance = FIXED_TO_INT( decoder.builder.advance.x );
445
446 psaux->t1_decoder_funcs->done( &decoder );
447
448 return FT_Err_Ok;
449 }
450
451
452#endif /* 0 */
453
454
455 FT_LOCAL_DEF( FT_Error )
456 cid_slot_load_glyph( FT_GlyphSlot cidglyph, /* CID_GlyphSlot */
457 FT_Size cidsize, /* CID_Size */
458 FT_UInt glyph_index,
459 FT_Int32 load_flags )
460 {
461 CID_GlyphSlot glyph = (CID_GlyphSlot)cidglyph;
462 FT_Error error;
463 T1_DecoderRec decoder;
464 CID_Face face = (CID_Face)cidglyph->face;
465 FT_Bool hinting;
466 FT_Bool scaled;
467
468 PSAux_Service psaux = (PSAux_Service)face->psaux;
469 FT_Matrix font_matrix;
470 FT_Vector font_offset;
471 FT_Bool must_finish_decoder = FALSE;
472
473
474 if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
475 {
476 error = FT_THROW( Invalid_Argument );
477 goto Exit;
478 }
479
480 if ( load_flags & FT_LOAD_NO_RECURSE )
481 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
482
483 glyph->x_scale = cidsize->metrics.x_scale;
484 glyph->y_scale = cidsize->metrics.y_scale;
485
486 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
487 ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
488 scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 );
489
490 glyph->hint = hinting;
491 glyph->scaled = scaled;
492
493 error = psaux->t1_decoder_funcs->init( &decoder,
494 cidglyph->face,
495 cidsize,
496 cidglyph,
497 0, /* glyph names -- XXX */
498 0, /* blend == 0 */
499 hinting,
500 FT_LOAD_TARGET_MODE( load_flags ),
501 cid_load_glyph );
502 if ( error )
503 goto Exit;
504
505 /* TODO: initialize decoder.len_buildchar and decoder.buildchar */
506 /* if we ever support CID-keyed multiple master fonts */
507
508 must_finish_decoder = TRUE;
509
510 /* set up the decoder */
511 decoder.builder.no_recurse = FT_BOOL( load_flags & FT_LOAD_NO_RECURSE );
512
513 error = cid_load_glyph( &decoder, glyph_index );
514 if ( error )
515 goto Exit;
516
517 /* copy flags back for forced scaling */
518 hinting = glyph->hint;
519 scaled = glyph->scaled;
520
521 font_matrix = decoder.font_matrix;
522 font_offset = decoder.font_offset;
523
524 /* save new glyph tables */
525 psaux->t1_decoder_funcs->done( &decoder );
526
527 must_finish_decoder = FALSE;
528
529 /* now set the metrics -- this is rather simple, as */
530 /* the left side bearing is the xMin, and the top side */
531 /* bearing the yMax; for composite glyphs, return only */
532 /* left side bearing and advance width */
533 if ( load_flags & FT_LOAD_NO_RECURSE )
534 {
535 FT_Slot_Internal internal = cidglyph->internal;
536
537
538 cidglyph->metrics.horiBearingX =
539 FIXED_TO_INT( decoder.builder.left_bearing.x );
540 cidglyph->metrics.horiAdvance =
541 FIXED_TO_INT( decoder.builder.advance.x );
542
543 internal->glyph_matrix = font_matrix;
544 internal->glyph_delta = font_offset;
545 internal->glyph_transformed = 1;
546 }
547 else
548 {
549 FT_BBox cbox;
550 FT_Glyph_Metrics* metrics = &cidglyph->metrics;
551
552
553 cidglyph->format = FT_GLYPH_FORMAT_OUTLINE;
554
555 cidglyph->outline.flags &= FT_OUTLINE_OWNER;
556 cidglyph->outline.flags |= FT_OUTLINE_REVERSE_FILL;
557 if ( cidsize->metrics.y_ppem < 24 )
558 cidglyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
559
560 /* copy the _unscaled_ advance width */
561 metrics->horiAdvance =
562 FIXED_TO_INT( decoder.builder.advance.x );
563 cidglyph->linearHoriAdvance =
564 FIXED_TO_INT( decoder.builder.advance.x );
565 cidglyph->internal->glyph_transformed = 0;
566
567 /* make up vertical ones */
568 metrics->vertAdvance = ( face->cid.font_bbox.yMax -
569 face->cid.font_bbox.yMin ) >> 16;
570 cidglyph->linearVertAdvance = metrics->vertAdvance;
571
572 /* apply the font matrix, if any */
573 if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L ||
574 font_matrix.xy != 0 || font_matrix.yx != 0 )
575 {
576 FT_Outline_Transform( &cidglyph->outline, &font_matrix );
577
578 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance,
579 font_matrix.xx );
580 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance,
581 font_matrix.yy );
582 }
583
584 if ( font_offset.x || font_offset.y )
585 {
586 FT_Outline_Translate( &cidglyph->outline,
587 font_offset.x,
588 font_offset.y );
589
590 metrics->horiAdvance += font_offset.x;
591 metrics->vertAdvance += font_offset.y;
592 }
593
594 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || scaled )
595 {
596 /* scale the outline and the metrics */
597 FT_Int n;
598 FT_Outline* cur = decoder.builder.base;
599 FT_Vector* vec = cur->points;
600 FT_Fixed x_scale = glyph->x_scale;
601 FT_Fixed y_scale = glyph->y_scale;
602
603
604 /* First of all, scale the points */
605 if ( !hinting || !decoder.builder.hints_funcs )
606 for ( n = cur->n_points; n > 0; n--, vec++ )
607 {
608 vec->x = FT_MulFix( vec->x, x_scale );
609 vec->y = FT_MulFix( vec->y, y_scale );
610 }
611
612 /* Then scale the metrics */
613 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
614 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
615 }
616
617 /* compute the other metrics */
618 FT_Outline_Get_CBox( &cidglyph->outline, &cbox );
619
620 metrics->width = cbox.xMax - cbox.xMin;
621 metrics->height = cbox.yMax - cbox.yMin;
622
623 metrics->horiBearingX = cbox.xMin;
624 metrics->horiBearingY = cbox.yMax;
625
626 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
627 {
628 /* make up vertical ones */
629 ft_synthesize_vertical_metrics( metrics,
630 metrics->vertAdvance );
631 }
632 }
633
634 Exit:
635
636 if ( must_finish_decoder )
637 psaux->t1_decoder_funcs->done( &decoder );
638
639 return error;
640 }
641
642
643/* END */