1 /****************************************************************************
5 * Adobe's code for font instances (body).
7 * Copyright 2007-2014 Adobe Systems Incorporated.
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.
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.
40 #include FT_INTERNAL_CALC_H
50 /* Compute a stem darkening amount in character space. */
52 cf2_computeDarkening( CF2_Fixed emRatio,
55 CF2_Fixed* darkenAmount,
56 CF2_Fixed boldenAmount,
58 FT_Int* darkenParams )
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.
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,
72 * and piecewise linear in-between:
87 * | +-----------------
89 * +---------------------------------------------> stem
93 * This corresponds to the following values for the
94 * `darkening-parameters' property:
96 * (x1, y1) = (500, 400)
97 * (x2, y2) = (1000, 275)
98 * (x3, y3) = (1667, 275)
99 * (x4, y4) = (2333, 0)
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;
116 if ( boldenAmount == 0 && !stemDarkened )
119 /* protect against range problems and divide by zero */
120 if ( emRatio < cf2_doubleToFixed( .01 ) )
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];
135 /* convert from true character space to 1000 unit character space; */
136 /* add synthetic emboldening effect */
138 /* `stemWidthPer1000' will not overflow for a legitimate font */
140 stemWidthPer1000 = FT_MulFix( stemWidth + boldenAmount, emRatio );
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 */
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. */
156 logBase2 = FT_MSB( (FT_UInt32)stemWidthPer1000 ) +
157 FT_MSB( (FT_UInt32)ppem );
159 if ( logBase2 >= 46 )
160 /* possible overflow */
161 scaledStem = cf2_intToFixed( x4 );
163 scaledStem = FT_MulFix( stemWidthPer1000, ppem );
165 /* now apply the darkening parameters */
167 if ( scaledStem < cf2_intToFixed( x1 ) )
168 *darkenAmount = FT_DivFix( cf2_intToFixed( y1 ), ppem );
170 else if ( scaledStem < cf2_intToFixed( x2 ) )
172 FT_Int xdelta = x2 - x1;
173 FT_Int ydelta = y2 - y1;
174 FT_Int x = stemWidthPer1000 -
175 FT_DivFix( cf2_intToFixed( x1 ), ppem );
181 *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
182 FT_DivFix( cf2_intToFixed( y1 ), ppem );
185 else if ( scaledStem < cf2_intToFixed( x3 ) )
189 FT_Int xdelta = x3 - x2;
190 FT_Int ydelta = y3 - y2;
191 FT_Int x = stemWidthPer1000 -
192 FT_DivFix( cf2_intToFixed( x2 ), ppem );
198 *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
199 FT_DivFix( cf2_intToFixed( y2 ), ppem );
203 else if ( scaledStem < cf2_intToFixed( x4 ) )
207 FT_Int xdelta = x4 - x3;
208 FT_Int ydelta = y4 - y3;
209 FT_Int x = stemWidthPer1000 -
210 FT_DivFix( cf2_intToFixed( x3 ), ppem );
216 *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
217 FT_DivFix( cf2_intToFixed( y3 ), ppem );
224 *darkenAmount = FT_DivFix( cf2_intToFixed( y4 ), ppem );
227 /* use half the amount on each side and convert back to true */
228 /* character space */
229 *darkenAmount = FT_DivFix( *darkenAmount, 2 * emRatio );
232 /* add synthetic emboldening effect in character space */
233 *darkenAmount += boldenAmount / 2;
237 /* set up values for the current FontDict and matrix; */
238 /* called for each glyph to be rendered */
240 /* caller's transform is adjusted for subpixel positioning */
242 cf2_font_setup( CF2_Font font,
243 const CF2_Matrix* transform )
245 /* pointer to parsed font object */
246 PS_Decoder* decoder = font->decoder;
248 FT_Bool needExtraSetup = FALSE;
250 CFF_VStoreRec* vstore;
251 FT_Bool hasVariations = FALSE;
253 /* character space units */
254 CF2_Fixed boldenX = font->syntheticEmboldeningAmountX;
255 CF2_Fixed boldenY = font->syntheticEmboldeningAmountY;
260 CF2_UInt lenNormalizedV = 0;
261 FT_Fixed* normalizedV = NULL;
263 /* clear previous error */
264 font->error = FT_Err_Ok;
266 /* if a CID fontDict has changed, we need to recompute some cached */
268 subFont = cf2_getSubfont( decoder );
269 if ( font->lastSubfont != subFont )
271 font->lastSubfont = subFont;
272 needExtraSetup = TRUE;
277 /* check for variation vectors */
278 vstore = cf2_getVStore( decoder );
279 hasVariations = ( vstore->dataCount != 0 );
283 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
284 FT_Service_CFFLoad cffload = (FT_Service_CFFLoad)font->cffload;
287 /* check whether Private DICT in this subfont needs to be reparsed */
288 font->error = cf2_getNormalizedVector( decoder,
294 if ( cffload->blend_check_vector( &subFont->blend,
295 subFont->private_dict.vsindex,
299 /* blend has changed, reparse */
300 cffload->load_private_dict( decoder->cff,
304 needExtraSetup = TRUE;
308 /* copy from subfont */
309 font->blend.font = subFont->blend.font;
311 /* clear state of charstring blend */
312 font->blend.usedBV = FALSE;
314 /* initialize value for charstring */
315 font->vsindex = subFont->private_dict.vsindex;
317 /* store vector inputs for blends in charstring */
318 font->lenNDV = lenNormalizedV;
319 font->NDV = normalizedV;
323 /* if ppem has changed, we need to recompute some cached data */
324 /* note: because of CID font matrix concatenation, ppem and transform */
325 /* do not necessarily track. */
326 ppem = cf2_getPpemY( decoder );
327 if ( font->ppem != ppem )
330 needExtraSetup = TRUE;
333 /* copy hinted flag on each call */
334 font->hinted = FT_BOOL( font->renderingFlags & CF2_FlagsHinted );
336 /* determine if transform has changed; */
337 /* include Fontmatrix but ignore translation */
338 if ( ft_memcmp( transform,
339 &font->currentTransform,
340 4 * sizeof ( CF2_Fixed ) ) != 0 )
342 /* save `key' information for `cache of one' matrix data; */
343 /* save client transform, without the translation */
344 font->currentTransform = *transform;
345 font->currentTransform.tx =
346 font->currentTransform.ty = cf2_intToFixed( 0 );
348 /* TODO: FreeType transform is simple scalar; for now, use identity */
350 font->innerTransform = *transform;
351 font->outerTransform.a =
352 font->outerTransform.d = cf2_intToFixed( 1 );
353 font->outerTransform.b =
354 font->outerTransform.c = cf2_intToFixed( 0 );
356 needExtraSetup = TRUE;
360 * font->darkened is set to true if there is a stem darkening request or
361 * the font is synthetic emboldened.
362 * font->darkened controls whether to adjust blue zones, winding order,
366 if ( font->stemDarkened != ( font->renderingFlags & CF2_FlagsDarkened ) )
369 FT_BOOL( font->renderingFlags & CF2_FlagsDarkened );
371 /* blue zones depend on darkened flag */
372 needExtraSetup = TRUE;
375 /* recompute variables that are dependent on transform or FontDict or */
377 if ( needExtraSetup )
379 /* StdVW is found in the private dictionary; */
380 /* recompute darkening amounts whenever private dictionary or */
381 /* transform change */
382 /* Note: a rendering flag turns darkening on or off, so we want to */
383 /* store the `on' amounts; */
384 /* darkening amount is computed in character space */
385 /* TODO: testing size-dependent darkening here; */
386 /* what to do for rotations? */
390 CF2_Int unitsPerEm = font->unitsPerEm;
393 if ( unitsPerEm == 0 )
396 ppem = FT_MAX( cf2_intToFixed( 4 ),
397 font->ppem ); /* use minimum ppem of 4 */
400 /* since vstem is measured in the x-direction, we use the `a' member */
401 /* of the fontMatrix */
402 emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->a );
405 /* Freetype does not preserve the fontMatrix when parsing; use */
406 /* unitsPerEm instead. */
407 /* TODO: check precision of this */
408 emRatio = cf2_intToFixed( 1000 ) / unitsPerEm;
409 font->stdVW = cf2_getStdVW( decoder );
411 if ( font->stdVW <= 0 )
412 font->stdVW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
416 /* Ensure that boldenX is at least 1 pixel for synthetic bold font */
417 /* (similar to what Avalon does) */
418 boldenX = FT_MAX( boldenX,
419 FT_DivFix( cf2_intToFixed( unitsPerEm ), ppem ) );
421 /* Synthetic emboldening adds at least 1 pixel to darkenX, while */
422 /* stem darkening adds at most half pixel. Since the purpose of */
423 /* stem darkening (readability at small sizes) is met with */
424 /* synthetic emboldening, no need to add stem darkening for a */
425 /* synthetic bold font. */
426 cf2_computeDarkening( emRatio,
432 font->darkenParams );
435 cf2_computeDarkening( emRatio,
441 font->darkenParams );
444 /* since hstem is measured in the y-direction, we use the `d' member */
445 /* of the fontMatrix */
446 /* TODO: use the same units per em as above; check this */
447 emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->d );
450 /* set the default stem width, because it must be the same for all */
451 /* family members; */
452 /* choose a constant for StdHW that depends on font contrast */
453 stdHW = cf2_getStdHW( decoder );
455 if ( stdHW > 0 && font->stdVW > MUL_INT32( 2, stdHW ) )
456 font->stdHW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
459 /* low contrast font gets less hstem darkening */
460 font->stdHW = FT_DivFix( cf2_intToFixed( 110 ), emRatio );
463 cf2_computeDarkening( emRatio,
469 font->darkenParams );
471 if ( font->darkenX != 0 || font->darkenY != 0 )
472 font->darkened = TRUE;
474 font->darkened = FALSE;
476 font->reverseWinding = FALSE; /* initial expectation is CCW */
478 /* compute blue zones for this instance */
479 cf2_blues_init( &font->blues, font );
481 } /* needExtraSetup */
485 /* equivalent to AdobeGetOutline */
486 FT_LOCAL_DEF( FT_Error )
487 cf2_getGlyphOutline( CF2_Font font,
488 CF2_Buffer charstring,
489 const CF2_Matrix* transform,
490 CF2_F16Dot16* glyphWidth )
492 FT_Error lastError = FT_Err_Ok;
494 FT_Vector translation;
497 FT_Vector advancePoint;
500 CF2_Fixed advWidth = 0;
504 /* Note: use both integer and fraction for outlines. This allows bbox */
505 /* to come out directly. */
507 translation.x = transform->tx;
508 translation.y = transform->ty;
510 /* set up values based on transform */
511 cf2_font_setup( font, transform );
513 goto exit; /* setup encountered an error */
515 /* reset darken direction */
516 font->reverseWinding = FALSE;
518 /* winding order only affects darkening */
519 needWinding = font->darkened;
523 /* reset output buffer */
524 cf2_outline_reset( &font->outline );
526 /* build the outline, passing the full translation */
527 cf2_interpT2CharString( font,
529 (CF2_OutlineCallbacks)&font->outline,
542 /* check winding order */
543 if ( font->outline.root.windingMomentum >= 0 ) /* CFF is CCW */
546 /* invert darkening and render again */
547 /* TODO: this should be a parameter to getOutline-computeOffset */
548 font->reverseWinding = TRUE;
550 needWinding = FALSE; /* exit after next iteration */
553 /* finish storing client outline */
554 cf2_outline_close( &font->outline );
557 /* FreeType just wants the advance width; there is no translation */
558 *glyphWidth = advWidth;
560 /* free resources and collect errors from objects we've used */
561 cf2_setError( &font->error, lastError );