[EXPLORER] Fix the code that requests the taskbar to recalculate the size,
[reactos.git] / sdk / lib / 3rdparty / freetype / src / cff / cf2font.c
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
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_doubleToFixed( .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 /* called for each glyph to be rendered */
239
240 /* caller's transform is adjusted for subpixel positioning */
241 static void
242 cf2_font_setup( CF2_Font font,
243 const CF2_Matrix* transform )
244 {
245 /* pointer to parsed font object */
246 CFF_Decoder* decoder = font->decoder;
247
248 FT_Bool needExtraSetup = FALSE;
249
250 CFF_VStoreRec* vstore;
251 FT_Bool hasVariations = FALSE;
252
253 /* character space units */
254 CF2_Fixed boldenX = font->syntheticEmboldeningAmountX;
255 CF2_Fixed boldenY = font->syntheticEmboldeningAmountY;
256
257 CFF_SubFont subFont;
258 CF2_Fixed ppem;
259
260 CF2_UInt lenNormalizedV = 0;
261 FT_Fixed* normalizedV = NULL;
262
263
264 /* clear previous error */
265 font->error = FT_Err_Ok;
266
267 /* if a CID fontDict has changed, we need to recompute some cached */
268 /* data */
269 subFont = cf2_getSubfont( decoder );
270 if ( font->lastSubfont != subFont )
271 {
272 font->lastSubfont = subFont;
273 needExtraSetup = TRUE;
274 }
275
276 /* check for variation vectors */
277 vstore = cf2_getVStore( decoder );
278 hasVariations = ( vstore->dataCount != 0 );
279
280 if ( hasVariations )
281 {
282 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
283 /* check whether Private DICT in this subfont needs to be reparsed */
284 font->error = cf2_getNormalizedVector( decoder,
285 &lenNormalizedV,
286 &normalizedV );
287 if ( font->error )
288 return;
289
290 if ( cff_blend_check_vector( &subFont->blend,
291 subFont->private_dict.vsindex,
292 lenNormalizedV,
293 normalizedV ) )
294 {
295 /* blend has changed, reparse */
296 cff_load_private_dict( decoder->cff,
297 subFont,
298 lenNormalizedV,
299 normalizedV );
300 needExtraSetup = TRUE;
301 }
302 #endif
303
304 /* copy from subfont */
305 font->blend.font = subFont->blend.font;
306
307 /* clear state of charstring blend */
308 font->blend.usedBV = FALSE;
309
310 /* initialize value for charstring */
311 font->vsindex = subFont->private_dict.vsindex;
312
313 /* store vector inputs for blends in charstring */
314 font->lenNDV = lenNormalizedV;
315 font->NDV = normalizedV;
316 }
317
318 /* if ppem has changed, we need to recompute some cached data */
319 /* note: because of CID font matrix concatenation, ppem and transform */
320 /* do not necessarily track. */
321 ppem = cf2_getPpemY( decoder );
322 if ( font->ppem != ppem )
323 {
324 font->ppem = ppem;
325 needExtraSetup = TRUE;
326 }
327
328 /* copy hinted flag on each call */
329 font->hinted = (FT_Bool)( font->renderingFlags & CF2_FlagsHinted );
330
331 /* determine if transform has changed; */
332 /* include Fontmatrix but ignore translation */
333 if ( ft_memcmp( transform,
334 &font->currentTransform,
335 4 * sizeof ( CF2_Fixed ) ) != 0 )
336 {
337 /* save `key' information for `cache of one' matrix data; */
338 /* save client transform, without the translation */
339 font->currentTransform = *transform;
340 font->currentTransform.tx =
341 font->currentTransform.ty = cf2_intToFixed( 0 );
342
343 /* TODO: FreeType transform is simple scalar; for now, use identity */
344 /* for outer */
345 font->innerTransform = *transform;
346 font->outerTransform.a =
347 font->outerTransform.d = cf2_intToFixed( 1 );
348 font->outerTransform.b =
349 font->outerTransform.c = cf2_intToFixed( 0 );
350
351 needExtraSetup = TRUE;
352 }
353
354 /*
355 * font->darkened is set to true if there is a stem darkening request or
356 * the font is synthetic emboldened.
357 * font->darkened controls whether to adjust blue zones, winding order,
358 * and hinting.
359 *
360 */
361 if ( font->stemDarkened != ( font->renderingFlags & CF2_FlagsDarkened ) )
362 {
363 font->stemDarkened =
364 (FT_Bool)( font->renderingFlags & CF2_FlagsDarkened );
365
366 /* blue zones depend on darkened flag */
367 needExtraSetup = TRUE;
368 }
369
370 /* recompute variables that are dependent on transform or FontDict or */
371 /* darken flag */
372 if ( needExtraSetup )
373 {
374 /* StdVW is found in the private dictionary; */
375 /* recompute darkening amounts whenever private dictionary or */
376 /* transform change */
377 /* Note: a rendering flag turns darkening on or off, so we want to */
378 /* store the `on' amounts; */
379 /* darkening amount is computed in character space */
380 /* TODO: testing size-dependent darkening here; */
381 /* what to do for rotations? */
382
383 CF2_Fixed emRatio;
384 CF2_Fixed stdHW;
385 CF2_Int unitsPerEm = font->unitsPerEm;
386
387
388 if ( unitsPerEm == 0 )
389 unitsPerEm = 1000;
390
391 ppem = FT_MAX( cf2_intToFixed( 4 ),
392 font->ppem ); /* use minimum ppem of 4 */
393
394 #if 0
395 /* since vstem is measured in the x-direction, we use the `a' member */
396 /* of the fontMatrix */
397 emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->a );
398 #endif
399
400 /* Freetype does not preserve the fontMatrix when parsing; use */
401 /* unitsPerEm instead. */
402 /* TODO: check precision of this */
403 emRatio = cf2_intToFixed( 1000 ) / unitsPerEm;
404 font->stdVW = cf2_getStdVW( decoder );
405
406 if ( font->stdVW <= 0 )
407 font->stdVW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
408
409 if ( boldenX > 0 )
410 {
411 /* Ensure that boldenX is at least 1 pixel for synthetic bold font */
412 /* (similar to what Avalon does) */
413 boldenX = FT_MAX( boldenX,
414 FT_DivFix( cf2_intToFixed( unitsPerEm ), ppem ) );
415
416 /* Synthetic emboldening adds at least 1 pixel to darkenX, while */
417 /* stem darkening adds at most half pixel. Since the purpose of */
418 /* stem darkening (readability at small sizes) is met with */
419 /* synthetic emboldening, no need to add stem darkening for a */
420 /* synthetic bold font. */
421 cf2_computeDarkening( emRatio,
422 ppem,
423 font->stdVW,
424 &font->darkenX,
425 boldenX,
426 FALSE,
427 font->darkenParams );
428 }
429 else
430 cf2_computeDarkening( emRatio,
431 ppem,
432 font->stdVW,
433 &font->darkenX,
434 0,
435 font->stemDarkened,
436 font->darkenParams );
437
438 #if 0
439 /* since hstem is measured in the y-direction, we use the `d' member */
440 /* of the fontMatrix */
441 /* TODO: use the same units per em as above; check this */
442 emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->d );
443 #endif
444
445 /* set the default stem width, because it must be the same for all */
446 /* family members; */
447 /* choose a constant for StdHW that depends on font contrast */
448 stdHW = cf2_getStdHW( decoder );
449
450 if ( stdHW > 0 && font->stdVW > MUL_INT32( 2, stdHW ) )
451 font->stdHW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
452 else
453 {
454 /* low contrast font gets less hstem darkening */
455 font->stdHW = FT_DivFix( cf2_intToFixed( 110 ), emRatio );
456 }
457
458 cf2_computeDarkening( emRatio,
459 ppem,
460 font->stdHW,
461 &font->darkenY,
462 boldenY,
463 font->stemDarkened,
464 font->darkenParams );
465
466 if ( font->darkenX != 0 || font->darkenY != 0 )
467 font->darkened = TRUE;
468 else
469 font->darkened = FALSE;
470
471 font->reverseWinding = FALSE; /* initial expectation is CCW */
472
473 /* compute blue zones for this instance */
474 cf2_blues_init( &font->blues, font );
475
476 } /* needExtraSetup */
477 }
478
479
480 /* equivalent to AdobeGetOutline */
481 FT_LOCAL_DEF( FT_Error )
482 cf2_getGlyphOutline( CF2_Font font,
483 CF2_Buffer charstring,
484 const CF2_Matrix* transform,
485 CF2_F16Dot16* glyphWidth )
486 {
487 FT_Error lastError = FT_Err_Ok;
488
489 FT_Vector translation;
490
491 #if 0
492 FT_Vector advancePoint;
493 #endif
494
495 CF2_Fixed advWidth = 0;
496 FT_Bool needWinding;
497
498
499 /* Note: use both integer and fraction for outlines. This allows bbox */
500 /* to come out directly. */
501
502 translation.x = transform->tx;
503 translation.y = transform->ty;
504
505 /* set up values based on transform */
506 cf2_font_setup( font, transform );
507 if ( font->error )
508 goto exit; /* setup encountered an error */
509
510 /* reset darken direction */
511 font->reverseWinding = FALSE;
512
513 /* winding order only affects darkening */
514 needWinding = font->darkened;
515
516 while ( 1 )
517 {
518 /* reset output buffer */
519 cf2_outline_reset( &font->outline );
520
521 /* build the outline, passing the full translation */
522 cf2_interpT2CharString( font,
523 charstring,
524 (CF2_OutlineCallbacks)&font->outline,
525 &translation,
526 FALSE,
527 0,
528 0,
529 &advWidth );
530
531 if ( font->error )
532 goto exit;
533
534 if ( !needWinding )
535 break;
536
537 /* check winding order */
538 if ( font->outline.root.windingMomentum >= 0 ) /* CFF is CCW */
539 break;
540
541 /* invert darkening and render again */
542 /* TODO: this should be a parameter to getOutline-computeOffset */
543 font->reverseWinding = TRUE;
544
545 needWinding = FALSE; /* exit after next iteration */
546 }
547
548 /* finish storing client outline */
549 cf2_outline_close( &font->outline );
550
551 exit:
552 /* FreeType just wants the advance width; there is no translation */
553 *glyphWidth = advWidth;
554
555 /* free resources and collect errors from objects we've used */
556 cf2_setError( &font->error, lastError );
557
558 return font->error;
559 }
560
561
562 /* END */