1 /***************************************************************************/ 2 /* */ 3 /* cidgload.c */ 4 /* */ 5 /* CID-keyed Type1 Glyph Loader (body). */ 6 /* */ 7 /* Copyright 1996-2016 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 <ft2build.h> 20 #include "cidload.h" 21 #include "cidgload.h" 22 #include FT_INTERNAL_DEBUG_H 23 #include FT_INTERNAL_STREAM_H 24 #include FT_OUTLINE_H 25 #include FT_INTERNAL_CALC_H 26 27 #include "ciderrs.h" 28 29 30 /*************************************************************************/ 31 /* */ 32 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 33 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 34 /* messages during execution. */ 35 /* */ 36 #undef FT_COMPONENT 37 #define FT_COMPONENT trace_cidgload 38 39 40 FT_CALLBACK_DEF( FT_Error ) cid_load_glyph(T1_Decoder decoder,FT_UInt glyph_index)41 cid_load_glyph( T1_Decoder decoder, 42 FT_UInt glyph_index ) 43 { 44 CID_Face face = (CID_Face)decoder->builder.face; 45 CID_FaceInfo cid = &face->cid; 46 FT_Byte* p; 47 FT_ULong fd_select; 48 FT_Stream stream = face->cid_stream; 49 FT_Error error = FT_Err_Ok; 50 FT_Byte* charstring = NULL; 51 FT_Memory memory = face->root.memory; 52 FT_ULong glyph_length = 0; 53 PSAux_Service psaux = (PSAux_Service)face->psaux; 54 55 #ifdef FT_CONFIG_OPTION_INCREMENTAL 56 FT_Incremental_InterfaceRec *inc = 57 face->root.internal->incremental_interface; 58 #endif 59 60 61 FT_TRACE1(( "cid_load_glyph: glyph index %d\n", glyph_index )); 62 63 #ifdef FT_CONFIG_OPTION_INCREMENTAL 64 65 /* For incremental fonts get the character data using */ 66 /* the callback function. */ 67 if ( inc ) 68 { 69 FT_Data glyph_data; 70 71 72 error = inc->funcs->get_glyph_data( inc->object, 73 glyph_index, &glyph_data ); 74 if ( error ) 75 goto Exit; 76 77 p = (FT_Byte*)glyph_data.pointer; 78 fd_select = cid_get_offset( &p, (FT_Byte)cid->fd_bytes ); 79 80 if ( glyph_data.length != 0 ) 81 { 82 glyph_length = (FT_ULong)( glyph_data.length - cid->fd_bytes ); 83 (void)FT_ALLOC( charstring, glyph_length ); 84 if ( !error ) 85 ft_memcpy( charstring, glyph_data.pointer + cid->fd_bytes, 86 glyph_length ); 87 } 88 89 inc->funcs->free_glyph_data( inc->object, &glyph_data ); 90 91 if ( error ) 92 goto Exit; 93 } 94 95 else 96 97 #endif /* FT_CONFIG_OPTION_INCREMENTAL */ 98 99 /* For ordinary fonts read the CID font dictionary index */ 100 /* and charstring offset from the CIDMap. */ 101 { 102 FT_UInt entry_len = (FT_UInt)( cid->fd_bytes + cid->gd_bytes ); 103 FT_ULong off1, off2; 104 105 106 if ( FT_STREAM_SEEK( cid->data_offset + cid->cidmap_offset + 107 glyph_index * entry_len ) || 108 FT_FRAME_ENTER( 2 * entry_len ) ) 109 goto Exit; 110 111 p = (FT_Byte*)stream->cursor; 112 fd_select = cid_get_offset( &p, (FT_Byte)cid->fd_bytes ); 113 off1 = cid_get_offset( &p, (FT_Byte)cid->gd_bytes ); 114 p += cid->fd_bytes; 115 off2 = cid_get_offset( &p, (FT_Byte)cid->gd_bytes ); 116 FT_FRAME_EXIT(); 117 118 if ( fd_select >= (FT_ULong)cid->num_dicts || 119 off2 > stream->size || 120 off1 > off2 ) 121 { 122 FT_TRACE0(( "cid_load_glyph: invalid glyph stream offsets\n" )); 123 error = FT_THROW( Invalid_Offset ); 124 goto Exit; 125 } 126 127 glyph_length = off2 - off1; 128 if ( glyph_length == 0 ) 129 goto Exit; 130 if ( FT_ALLOC( charstring, glyph_length ) ) 131 goto Exit; 132 if ( FT_STREAM_READ_AT( cid->data_offset + off1, 133 charstring, glyph_length ) ) 134 goto Exit; 135 } 136 137 /* Now set up the subrs array and parse the charstrings. */ 138 { 139 CID_FaceDict dict; 140 CID_Subrs cid_subrs = face->subrs + fd_select; 141 FT_UInt cs_offset; 142 143 144 /* Set up subrs */ 145 decoder->num_subrs = cid_subrs->num_subrs; 146 decoder->subrs = cid_subrs->code; 147 decoder->subrs_len = 0; 148 decoder->subrs_hash = NULL; 149 150 /* Set up font matrix */ 151 dict = cid->font_dicts + fd_select; 152 153 decoder->font_matrix = dict->font_matrix; 154 decoder->font_offset = dict->font_offset; 155 decoder->lenIV = dict->private_dict.lenIV; 156 157 /* Decode the charstring. */ 158 159 /* Adjustment for seed bytes. */ 160 cs_offset = decoder->lenIV >= 0 ? (FT_UInt)decoder->lenIV : 0; 161 if ( cs_offset > glyph_length ) 162 { 163 FT_TRACE0(( "cid_load_glyph: invalid glyph stream offsets\n" )); 164 error = FT_THROW( Invalid_Offset ); 165 goto Exit; 166 } 167 168 /* Decrypt only if lenIV >= 0. */ 169 if ( decoder->lenIV >= 0 ) 170 psaux->t1_decrypt( charstring, glyph_length, 4330 ); 171 172 error = decoder->funcs.parse_charstrings( 173 decoder, charstring + cs_offset, 174 glyph_length - cs_offset ); 175 } 176 177 #ifdef FT_CONFIG_OPTION_INCREMENTAL 178 179 /* Incremental fonts can optionally override the metrics. */ 180 if ( !error && inc && inc->funcs->get_glyph_metrics ) 181 { 182 FT_Incremental_MetricsRec metrics; 183 184 185 metrics.bearing_x = FIXED_TO_INT( decoder->builder.left_bearing.x ); 186 metrics.bearing_y = 0; 187 metrics.advance = FIXED_TO_INT( decoder->builder.advance.x ); 188 metrics.advance_v = FIXED_TO_INT( decoder->builder.advance.y ); 189 190 error = inc->funcs->get_glyph_metrics( inc->object, 191 glyph_index, FALSE, &metrics ); 192 193 decoder->builder.left_bearing.x = INT_TO_FIXED( metrics.bearing_x ); 194 decoder->builder.advance.x = INT_TO_FIXED( metrics.advance ); 195 decoder->builder.advance.y = INT_TO_FIXED( metrics.advance_v ); 196 } 197 198 #endif /* FT_CONFIG_OPTION_INCREMENTAL */ 199 200 Exit: 201 FT_FREE( charstring ); 202 203 return error; 204 } 205 206 207 #if 0 208 209 210 /*************************************************************************/ 211 /*************************************************************************/ 212 /*************************************************************************/ 213 /********** *********/ 214 /********** *********/ 215 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/ 216 /********** *********/ 217 /********** The following code is in charge of computing *********/ 218 /********** the maximum advance width of the font. It *********/ 219 /********** quickly processes each glyph charstring to *********/ 220 /********** extract the value from either a `sbw' or `seac' *********/ 221 /********** operator. *********/ 222 /********** *********/ 223 /*************************************************************************/ 224 /*************************************************************************/ 225 /*************************************************************************/ 226 227 228 FT_LOCAL_DEF( FT_Error ) 229 cid_face_compute_max_advance( CID_Face face, 230 FT_Int* max_advance ) 231 { 232 FT_Error error; 233 T1_DecoderRec decoder; 234 FT_Int glyph_index; 235 236 PSAux_Service psaux = (PSAux_Service)face->psaux; 237 238 239 *max_advance = 0; 240 241 /* Initialize load decoder */ 242 error = psaux->t1_decoder_funcs->init( &decoder, 243 (FT_Face)face, 244 0, /* size */ 245 0, /* glyph slot */ 246 0, /* glyph names! XXX */ 247 0, /* blend == 0 */ 248 0, /* hinting == 0 */ 249 cid_load_glyph ); 250 if ( error ) 251 return error; 252 253 /* TODO: initialize decoder.len_buildchar and decoder.buildchar */ 254 /* if we ever support CID-keyed multiple master fonts */ 255 256 decoder.builder.metrics_only = 1; 257 decoder.builder.load_points = 0; 258 259 /* for each glyph, parse the glyph charstring and extract */ 260 /* the advance width */ 261 for ( glyph_index = 0; glyph_index < face->root.num_glyphs; 262 glyph_index++ ) 263 { 264 /* now get load the unscaled outline */ 265 error = cid_load_glyph( &decoder, glyph_index ); 266 /* ignore the error if one occurred - skip to next glyph */ 267 } 268 269 *max_advance = FIXED_TO_INT( decoder.builder.advance.x ); 270 271 psaux->t1_decoder_funcs->done( &decoder ); 272 273 return FT_Err_Ok; 274 } 275 276 277 #endif /* 0 */ 278 279 280 FT_LOCAL_DEF( FT_Error ) cid_slot_load_glyph(FT_GlyphSlot cidglyph,FT_Size cidsize,FT_UInt glyph_index,FT_Int32 load_flags)281 cid_slot_load_glyph( FT_GlyphSlot cidglyph, /* CID_GlyphSlot */ 282 FT_Size cidsize, /* CID_Size */ 283 FT_UInt glyph_index, 284 FT_Int32 load_flags ) 285 { 286 CID_GlyphSlot glyph = (CID_GlyphSlot)cidglyph; 287 FT_Error error; 288 T1_DecoderRec decoder; 289 CID_Face face = (CID_Face)cidglyph->face; 290 FT_Bool hinting; 291 292 PSAux_Service psaux = (PSAux_Service)face->psaux; 293 FT_Matrix font_matrix; 294 FT_Vector font_offset; 295 296 297 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 298 { 299 error = FT_THROW( Invalid_Argument ); 300 goto Exit; 301 } 302 303 if ( load_flags & FT_LOAD_NO_RECURSE ) 304 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; 305 306 glyph->x_scale = cidsize->metrics.x_scale; 307 glyph->y_scale = cidsize->metrics.y_scale; 308 309 cidglyph->outline.n_points = 0; 310 cidglyph->outline.n_contours = 0; 311 312 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 && 313 ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); 314 315 cidglyph->format = FT_GLYPH_FORMAT_OUTLINE; 316 317 error = psaux->t1_decoder_funcs->init( &decoder, 318 cidglyph->face, 319 cidsize, 320 cidglyph, 321 0, /* glyph names -- XXX */ 322 0, /* blend == 0 */ 323 hinting, 324 FT_LOAD_TARGET_MODE( load_flags ), 325 cid_load_glyph ); 326 if ( error ) 327 goto Exit; 328 329 /* TODO: initialize decoder.len_buildchar and decoder.buildchar */ 330 /* if we ever support CID-keyed multiple master fonts */ 331 332 /* set up the decoder */ 333 decoder.builder.no_recurse = FT_BOOL( 334 ( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ) ); 335 336 error = cid_load_glyph( &decoder, glyph_index ); 337 if ( error ) 338 goto Exit; 339 340 font_matrix = decoder.font_matrix; 341 font_offset = decoder.font_offset; 342 343 /* save new glyph tables */ 344 psaux->t1_decoder_funcs->done( &decoder ); 345 346 /* now set the metrics -- this is rather simple, as */ 347 /* the left side bearing is the xMin, and the top side */ 348 /* bearing the yMax */ 349 cidglyph->outline.flags &= FT_OUTLINE_OWNER; 350 cidglyph->outline.flags |= FT_OUTLINE_REVERSE_FILL; 351 352 /* for composite glyphs, return only left side bearing and */ 353 /* advance width */ 354 if ( load_flags & FT_LOAD_NO_RECURSE ) 355 { 356 FT_Slot_Internal internal = cidglyph->internal; 357 358 359 cidglyph->metrics.horiBearingX = 360 FIXED_TO_INT( decoder.builder.left_bearing.x ); 361 cidglyph->metrics.horiAdvance = 362 FIXED_TO_INT( decoder.builder.advance.x ); 363 364 internal->glyph_matrix = font_matrix; 365 internal->glyph_delta = font_offset; 366 internal->glyph_transformed = 1; 367 } 368 else 369 { 370 FT_BBox cbox; 371 FT_Glyph_Metrics* metrics = &cidglyph->metrics; 372 373 374 /* copy the _unscaled_ advance width */ 375 metrics->horiAdvance = 376 FIXED_TO_INT( decoder.builder.advance.x ); 377 cidglyph->linearHoriAdvance = 378 FIXED_TO_INT( decoder.builder.advance.x ); 379 cidglyph->internal->glyph_transformed = 0; 380 381 /* make up vertical ones */ 382 metrics->vertAdvance = ( face->cid.font_bbox.yMax - 383 face->cid.font_bbox.yMin ) >> 16; 384 cidglyph->linearVertAdvance = metrics->vertAdvance; 385 386 cidglyph->format = FT_GLYPH_FORMAT_OUTLINE; 387 388 if ( cidsize->metrics.y_ppem < 24 ) 389 cidglyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 390 391 /* apply the font matrix, if any */ 392 if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L || 393 font_matrix.xy != 0 || font_matrix.yx != 0 ) 394 { 395 FT_Outline_Transform( &cidglyph->outline, &font_matrix ); 396 397 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, 398 font_matrix.xx ); 399 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, 400 font_matrix.yy ); 401 } 402 403 if ( font_offset.x || font_offset.y ) 404 { 405 FT_Outline_Translate( &cidglyph->outline, 406 font_offset.x, 407 font_offset.y ); 408 409 metrics->horiAdvance += font_offset.x; 410 metrics->vertAdvance += font_offset.y; 411 } 412 413 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ) 414 { 415 /* scale the outline and the metrics */ 416 FT_Int n; 417 FT_Outline* cur = decoder.builder.base; 418 FT_Vector* vec = cur->points; 419 FT_Fixed x_scale = glyph->x_scale; 420 FT_Fixed y_scale = glyph->y_scale; 421 422 423 /* First of all, scale the points */ 424 if ( !hinting || !decoder.builder.hints_funcs ) 425 for ( n = cur->n_points; n > 0; n--, vec++ ) 426 { 427 vec->x = FT_MulFix( vec->x, x_scale ); 428 vec->y = FT_MulFix( vec->y, y_scale ); 429 } 430 431 /* Then scale the metrics */ 432 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); 433 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); 434 } 435 436 /* compute the other metrics */ 437 FT_Outline_Get_CBox( &cidglyph->outline, &cbox ); 438 439 metrics->width = cbox.xMax - cbox.xMin; 440 metrics->height = cbox.yMax - cbox.yMin; 441 442 metrics->horiBearingX = cbox.xMin; 443 metrics->horiBearingY = cbox.yMax; 444 445 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 446 { 447 /* make up vertical ones */ 448 ft_synthesize_vertical_metrics( metrics, 449 metrics->vertAdvance ); 450 } 451 } 452 453 Exit: 454 return error; 455 } 456 457 458 /* END */ 459