1 /***************************************************************************/ 2 /* */ 3 /* cf2font.c */ 4 /* */ 5 /* Adobe's code for font instances (body). */ 6 /* */ 7 /* Copyright 2007-2014 Adobe Systems Incorporated. */ 8 /* */ 9 /* This software, and all works of authorship, whether in source or */ 10 /* object code form as indicated by the copyright notice(s) included */ 11 /* herein (collectively, the "Work") is made available, and may only be */ 12 /* used, modified, and distributed under the FreeType Project License, */ 13 /* LICENSE.TXT. Additionally, subject to the terms and conditions of the */ 14 /* FreeType Project License, each contributor to the Work hereby grants */ 15 /* to any individual or legal entity exercising permissions granted by */ 16 /* the FreeType Project License and this section (hereafter, "You" or */ 17 /* "Your") a perpetual, worldwide, non-exclusive, no-charge, */ 18 /* royalty-free, irrevocable (except as stated in this section) patent */ 19 /* license to make, have made, use, offer to sell, sell, import, and */ 20 /* otherwise transfer the Work, where such license applies only to those */ 21 /* patent claims licensable by such contributor that are necessarily */ 22 /* infringed by their contribution(s) alone or by combination of their */ 23 /* contribution(s) with the Work to which such contribution(s) was */ 24 /* submitted. If You institute patent litigation against any entity */ 25 /* (including a cross-claim or counterclaim in a lawsuit) alleging that */ 26 /* the Work or a contribution incorporated within the Work constitutes */ 27 /* direct or contributory patent infringement, then any patent licenses */ 28 /* granted to You under this License for that Work shall terminate as of */ 29 /* the date such litigation is filed. */ 30 /* */ 31 /* By using, modifying, or distributing the Work you indicate that you */ 32 /* have read and understood the terms and conditions of the */ 33 /* FreeType Project License as well as those provided in this section, */ 34 /* and you accept them fully. */ 35 /* */ 36 /***************************************************************************/ 37 38 39 #include <ft2build.h> 40 #include FT_INTERNAL_CALC_H 41 42 #include "cf2ft.h" 43 44 #include "cf2glue.h" 45 #include "cf2font.h" 46 #include "cf2error.h" 47 #include "cf2intrp.h" 48 49 50 /* Compute a stem darkening amount in character space. */ 51 static void cf2_computeDarkening(CF2_Fixed emRatio,CF2_Fixed ppem,CF2_Fixed stemWidth,CF2_Fixed * darkenAmount,CF2_Fixed boldenAmount,FT_Bool stemDarkened,FT_Int * darkenParams)52 cf2_computeDarkening( CF2_Fixed emRatio, 53 CF2_Fixed ppem, 54 CF2_Fixed stemWidth, 55 CF2_Fixed* darkenAmount, 56 CF2_Fixed boldenAmount, 57 FT_Bool stemDarkened, 58 FT_Int* darkenParams ) 59 { 60 /* 61 * Total darkening amount is computed in 1000 unit character space 62 * using the modified 5 part curve as Adobe's Avalon rasterizer. 63 * The darkening amount is smaller for thicker stems. 64 * It becomes zero when the stem is thicker than 2.333 pixels. 65 * 66 * By default, we use 67 * 68 * darkenAmount = 0.4 pixels if scaledStem <= 0.5 pixels, 69 * darkenAmount = 0.275 pixels if 1 <= scaledStem <= 1.667 pixels, 70 * darkenAmount = 0 pixel if scaledStem >= 2.333 pixels, 71 * 72 * and piecewise linear in-between: 73 * 74 * 75 * darkening 76 * ^ 77 * | 78 * | (x1,y1) 79 * |--------+ 80 * | \ 81 * | \ 82 * | \ (x3,y3) 83 * | +----------+ 84 * | (x2,y2) \ 85 * | \ 86 * | \ 87 * | +----------------- 88 * | (x4,y4) 89 * +---------------------------------------------> stem 90 * thickness 91 * 92 * 93 * This corresponds to the following values for the 94 * `darkening-parameters' property: 95 * 96 * (x1, y1) = (500, 400) 97 * (x2, y2) = (1000, 275) 98 * (x3, y3) = (1667, 275) 99 * (x4, y4) = (2333, 0) 100 * 101 */ 102 103 /* Internal calculations are done in units per thousand for */ 104 /* convenience. The x axis is scaled stem width in */ 105 /* thousandths of a pixel. That is, 1000 is 1 pixel. */ 106 /* The y axis is darkening amount in thousandths of a pixel.*/ 107 /* In the code, below, dividing by ppem and */ 108 /* adjusting for emRatio converts darkenAmount to character */ 109 /* space (font units). */ 110 CF2_Fixed stemWidthPer1000, scaledStem; 111 FT_Int logBase2; 112 113 114 *darkenAmount = 0; 115 116 if ( boldenAmount == 0 && !stemDarkened ) 117 return; 118 119 /* protect against range problems and divide by zero */ 120 if ( emRatio < cf2_floatToFixed( .01 ) ) 121 return; 122 123 if ( stemDarkened ) 124 { 125 FT_Int x1 = darkenParams[0]; 126 FT_Int y1 = darkenParams[1]; 127 FT_Int x2 = darkenParams[2]; 128 FT_Int y2 = darkenParams[3]; 129 FT_Int x3 = darkenParams[4]; 130 FT_Int y3 = darkenParams[5]; 131 FT_Int x4 = darkenParams[6]; 132 FT_Int y4 = darkenParams[7]; 133 134 135 /* convert from true character space to 1000 unit character space; */ 136 /* add synthetic emboldening effect */ 137 138 /* `stemWidthPer1000' will not overflow for a legitimate font */ 139 140 stemWidthPer1000 = FT_MulFix( stemWidth + boldenAmount, emRatio ); 141 142 /* `scaledStem' can easily overflow, so we must clamp its maximum */ 143 /* value; the test doesn't need to be precise, but must be */ 144 /* conservative. The clamp value (default 2333) where */ 145 /* `darkenAmount' is zero is well below the overflow value of */ 146 /* 32767. */ 147 /* */ 148 /* FT_MSB computes the integer part of the base 2 logarithm. The */ 149 /* number of bits for the product is 1 or 2 more than the sum of */ 150 /* logarithms; remembering that the 16 lowest bits of the fraction */ 151 /* are dropped this is correct to within a factor of almost 4. */ 152 /* For example, 0x80.0000 * 0x80.0000 = 0x4000.0000 is 23+23 and */ 153 /* is flagged as possible overflow because 0xFF.FFFF * 0xFF.FFFF = */ 154 /* 0xFFFF.FE00 is also 23+23. */ 155 156 logBase2 = FT_MSB( (FT_UInt32)stemWidthPer1000 ) + 157 FT_MSB( (FT_UInt32)ppem ); 158 159 if ( logBase2 >= 46 ) 160 /* possible overflow */ 161 scaledStem = cf2_intToFixed( x4 ); 162 else 163 scaledStem = FT_MulFix( stemWidthPer1000, ppem ); 164 165 /* now apply the darkening parameters */ 166 167 if ( scaledStem < cf2_intToFixed( x1 ) ) 168 *darkenAmount = FT_DivFix( cf2_intToFixed( y1 ), ppem ); 169 170 else if ( scaledStem < cf2_intToFixed( x2 ) ) 171 { 172 FT_Int xdelta = x2 - x1; 173 FT_Int ydelta = y2 - y1; 174 FT_Int x = stemWidthPer1000 - 175 FT_DivFix( cf2_intToFixed( x1 ), ppem ); 176 177 178 if ( !xdelta ) 179 goto Try_x3; 180 181 *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) + 182 FT_DivFix( cf2_intToFixed( y1 ), ppem ); 183 } 184 185 else if ( scaledStem < cf2_intToFixed( x3 ) ) 186 { 187 Try_x3: 188 { 189 FT_Int xdelta = x3 - x2; 190 FT_Int ydelta = y3 - y2; 191 FT_Int x = stemWidthPer1000 - 192 FT_DivFix( cf2_intToFixed( x2 ), ppem ); 193 194 195 if ( !xdelta ) 196 goto Try_x4; 197 198 *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) + 199 FT_DivFix( cf2_intToFixed( y2 ), ppem ); 200 } 201 } 202 203 else if ( scaledStem < cf2_intToFixed( x4 ) ) 204 { 205 Try_x4: 206 { 207 FT_Int xdelta = x4 - x3; 208 FT_Int ydelta = y4 - y3; 209 FT_Int x = stemWidthPer1000 - 210 FT_DivFix( cf2_intToFixed( x3 ), ppem ); 211 212 213 if ( !xdelta ) 214 goto Use_y4; 215 216 *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) + 217 FT_DivFix( cf2_intToFixed( y3 ), ppem ); 218 } 219 } 220 221 else 222 { 223 Use_y4: 224 *darkenAmount = FT_DivFix( cf2_intToFixed( y4 ), ppem ); 225 } 226 227 /* use half the amount on each side and convert back to true */ 228 /* character space */ 229 *darkenAmount = FT_DivFix( *darkenAmount, 2 * emRatio ); 230 } 231 232 /* add synthetic emboldening effect in character space */ 233 *darkenAmount += boldenAmount / 2; 234 } 235 236 237 /* set up values for the current FontDict and matrix */ 238 239 /* caller's transform is adjusted for subpixel positioning */ 240 static void cf2_font_setup(CF2_Font font,const CF2_Matrix * transform)241 cf2_font_setup( CF2_Font font, 242 const CF2_Matrix* transform ) 243 { 244 /* pointer to parsed font object */ 245 CFF_Decoder* decoder = font->decoder; 246 247 FT_Bool needExtraSetup = FALSE; 248 249 /* character space units */ 250 CF2_Fixed boldenX = font->syntheticEmboldeningAmountX; 251 CF2_Fixed boldenY = font->syntheticEmboldeningAmountY; 252 253 CFF_SubFont subFont; 254 CF2_Fixed ppem; 255 256 257 /* clear previous error */ 258 font->error = FT_Err_Ok; 259 260 /* if a CID fontDict has changed, we need to recompute some cached */ 261 /* data */ 262 subFont = cf2_getSubfont( decoder ); 263 if ( font->lastSubfont != subFont ) 264 { 265 font->lastSubfont = subFont; 266 needExtraSetup = TRUE; 267 } 268 269 /* if ppem has changed, we need to recompute some cached data */ 270 /* note: because of CID font matrix concatenation, ppem and transform */ 271 /* do not necessarily track. */ 272 ppem = cf2_getPpemY( decoder ); 273 if ( font->ppem != ppem ) 274 { 275 font->ppem = ppem; 276 needExtraSetup = TRUE; 277 } 278 279 /* copy hinted flag on each call */ 280 font->hinted = (FT_Bool)( font->renderingFlags & CF2_FlagsHinted ); 281 282 /* determine if transform has changed; */ 283 /* include Fontmatrix but ignore translation */ 284 if ( ft_memcmp( transform, 285 &font->currentTransform, 286 4 * sizeof ( CF2_Fixed ) ) != 0 ) 287 { 288 /* save `key' information for `cache of one' matrix data; */ 289 /* save client transform, without the translation */ 290 font->currentTransform = *transform; 291 font->currentTransform.tx = 292 font->currentTransform.ty = cf2_intToFixed( 0 ); 293 294 /* TODO: FreeType transform is simple scalar; for now, use identity */ 295 /* for outer */ 296 font->innerTransform = *transform; 297 font->outerTransform.a = 298 font->outerTransform.d = cf2_intToFixed( 1 ); 299 font->outerTransform.b = 300 font->outerTransform.c = cf2_intToFixed( 0 ); 301 302 needExtraSetup = TRUE; 303 } 304 305 /* 306 * font->darkened is set to true if there is a stem darkening request or 307 * the font is synthetic emboldened. 308 * font->darkened controls whether to adjust blue zones, winding order, 309 * and hinting. 310 * 311 */ 312 if ( font->stemDarkened != ( font->renderingFlags & CF2_FlagsDarkened ) ) 313 { 314 font->stemDarkened = 315 (FT_Bool)( font->renderingFlags & CF2_FlagsDarkened ); 316 317 /* blue zones depend on darkened flag */ 318 needExtraSetup = TRUE; 319 } 320 321 /* recompute variables that are dependent on transform or FontDict or */ 322 /* darken flag */ 323 if ( needExtraSetup ) 324 { 325 /* StdVW is found in the private dictionary; */ 326 /* recompute darkening amounts whenever private dictionary or */ 327 /* transform change */ 328 /* Note: a rendering flag turns darkening on or off, so we want to */ 329 /* store the `on' amounts; */ 330 /* darkening amount is computed in character space */ 331 /* TODO: testing size-dependent darkening here; */ 332 /* what to do for rotations? */ 333 334 CF2_Fixed emRatio; 335 CF2_Fixed stdHW; 336 CF2_Int unitsPerEm = font->unitsPerEm; 337 338 339 if ( unitsPerEm == 0 ) 340 unitsPerEm = 1000; 341 342 ppem = FT_MAX( cf2_intToFixed( 4 ), 343 font->ppem ); /* use minimum ppem of 4 */ 344 345 #if 0 346 /* since vstem is measured in the x-direction, we use the `a' member */ 347 /* of the fontMatrix */ 348 emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->a ); 349 #endif 350 351 /* Freetype does not preserve the fontMatrix when parsing; use */ 352 /* unitsPerEm instead. */ 353 /* TODO: check precision of this */ 354 emRatio = cf2_intToFixed( 1000 ) / unitsPerEm; 355 font->stdVW = cf2_getStdVW( decoder ); 356 357 if ( font->stdVW <= 0 ) 358 font->stdVW = FT_DivFix( cf2_intToFixed( 75 ), emRatio ); 359 360 if ( boldenX > 0 ) 361 { 362 /* Ensure that boldenX is at least 1 pixel for synthetic bold font */ 363 /* (similar to what Avalon does) */ 364 boldenX = FT_MAX( boldenX, 365 FT_DivFix( cf2_intToFixed( unitsPerEm ), ppem ) ); 366 367 /* Synthetic emboldening adds at least 1 pixel to darkenX, while */ 368 /* stem darkening adds at most half pixel. Since the purpose of */ 369 /* stem darkening (readability at small sizes) is met with */ 370 /* synthetic emboldening, no need to add stem darkening for a */ 371 /* synthetic bold font. */ 372 cf2_computeDarkening( emRatio, 373 ppem, 374 font->stdVW, 375 &font->darkenX, 376 boldenX, 377 FALSE, 378 font->darkenParams ); 379 } 380 else 381 cf2_computeDarkening( emRatio, 382 ppem, 383 font->stdVW, 384 &font->darkenX, 385 0, 386 font->stemDarkened, 387 font->darkenParams ); 388 389 #if 0 390 /* since hstem is measured in the y-direction, we use the `d' member */ 391 /* of the fontMatrix */ 392 /* TODO: use the same units per em as above; check this */ 393 emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->d ); 394 #endif 395 396 /* set the default stem width, because it must be the same for all */ 397 /* family members; */ 398 /* choose a constant for StdHW that depends on font contrast */ 399 stdHW = cf2_getStdHW( decoder ); 400 401 if ( stdHW > 0 && font->stdVW > 2 * stdHW ) 402 font->stdHW = FT_DivFix( cf2_intToFixed( 75 ), emRatio ); 403 else 404 { 405 /* low contrast font gets less hstem darkening */ 406 font->stdHW = FT_DivFix( cf2_intToFixed( 110 ), emRatio ); 407 } 408 409 cf2_computeDarkening( emRatio, 410 ppem, 411 font->stdHW, 412 &font->darkenY, 413 boldenY, 414 font->stemDarkened, 415 font->darkenParams ); 416 417 if ( font->darkenX != 0 || font->darkenY != 0 ) 418 font->darkened = TRUE; 419 else 420 font->darkened = FALSE; 421 422 font->reverseWinding = FALSE; /* initial expectation is CCW */ 423 424 /* compute blue zones for this instance */ 425 cf2_blues_init( &font->blues, font ); 426 } 427 } 428 429 430 /* equivalent to AdobeGetOutline */ 431 FT_LOCAL_DEF( FT_Error ) cf2_getGlyphOutline(CF2_Font font,CF2_Buffer charstring,const CF2_Matrix * transform,CF2_F16Dot16 * glyphWidth)432 cf2_getGlyphOutline( CF2_Font font, 433 CF2_Buffer charstring, 434 const CF2_Matrix* transform, 435 CF2_F16Dot16* glyphWidth ) 436 { 437 FT_Error lastError = FT_Err_Ok; 438 439 FT_Vector translation; 440 441 #if 0 442 FT_Vector advancePoint; 443 #endif 444 445 CF2_Fixed advWidth = 0; 446 FT_Bool needWinding; 447 448 449 /* Note: use both integer and fraction for outlines. This allows bbox */ 450 /* to come out directly. */ 451 452 translation.x = transform->tx; 453 translation.y = transform->ty; 454 455 /* set up values based on transform */ 456 cf2_font_setup( font, transform ); 457 if ( font->error ) 458 goto exit; /* setup encountered an error */ 459 460 /* reset darken direction */ 461 font->reverseWinding = FALSE; 462 463 /* winding order only affects darkening */ 464 needWinding = font->darkened; 465 466 while ( 1 ) 467 { 468 /* reset output buffer */ 469 cf2_outline_reset( &font->outline ); 470 471 /* build the outline, passing the full translation */ 472 cf2_interpT2CharString( font, 473 charstring, 474 (CF2_OutlineCallbacks)&font->outline, 475 &translation, 476 FALSE, 477 0, 478 0, 479 &advWidth ); 480 481 if ( font->error ) 482 goto exit; 483 484 if ( !needWinding ) 485 break; 486 487 /* check winding order */ 488 if ( font->outline.root.windingMomentum >= 0 ) /* CFF is CCW */ 489 break; 490 491 /* invert darkening and render again */ 492 /* TODO: this should be a parameter to getOutline-computeOffset */ 493 font->reverseWinding = TRUE; 494 495 needWinding = FALSE; /* exit after next iteration */ 496 } 497 498 /* finish storing client outline */ 499 cf2_outline_close( &font->outline ); 500 501 exit: 502 /* FreeType just wants the advance width; there is no translation */ 503 *glyphWidth = advWidth; 504 505 /* free resources and collect errors from objects we've used */ 506 cf2_setError( &font->error, lastError ); 507 508 return font->error; 509 } 510 511 512 /* END */ 513