1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/***************************************************************************/
/* */
/* cf2blues.c */
/* */
/* Adobe's code for handling Blue Zones (body). */
/* */
/* Copyright 2009-2014 Adobe Systems Incorporated. */
/* */
/* This software, and all works of authorship, whether in source or */
/* object code form as indicated by the copyright notice(s) included */
/* herein (collectively, the "Work") is made available, and may only be */
/* used, modified, and distributed under the FreeType Project License, */
/* LICENSE.TXT. Additionally, subject to the terms and conditions of the */
/* FreeType Project License, each contributor to the Work hereby grants */
/* to any individual or legal entity exercising permissions granted by */
/* the FreeType Project License and this section (hereafter, "You" or */
/* "Your") a perpetual, worldwide, non-exclusive, no-charge, */
/* royalty-free, irrevocable (except as stated in this section) patent */
/* license to make, have made, use, offer to sell, sell, import, and */
/* otherwise transfer the Work, where such license applies only to those */
/* patent claims licensable by such contributor that are necessarily */
/* infringed by their contribution(s) alone or by combination of their */
/* contribution(s) with the Work to which such contribution(s) was */
/* submitted. If You institute patent litigation against any entity */
/* (including a cross-claim or counterclaim in a lawsuit) alleging that */
/* the Work or a contribution incorporated within the Work constitutes */
/* direct or contributory patent infringement, then any patent licenses */
/* granted to You under this License for that Work shall terminate as of */
/* the date such litigation is filed. */
/* */
/* By using, modifying, or distributing the Work you indicate that you */
/* have read and understood the terms and conditions of the */
/* FreeType Project License as well as those provided in this section, */
/* and you accept them fully. */
/* */
/***************************************************************************/
#include "cf2ft.h"
#include FT_INTERNAL_DEBUG_H
#include "cf2blues.h"
#include "cf2hints.h"
#include "cf2font.h"
/*************************************************************************/
/* */
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
/* messages during execution. */
/* */
#undef FT_COMPONENT
#define FT_COMPONENT trace_cf2blues
/*
* For blue values, the FreeType parser produces an array of integers,
* while the Adobe CFF engine produces an array of fixed.
* Define a macro to convert FreeType to fixed.
*/
#define cf2_blueToFixed( x ) cf2_intToFixed( x )
FT_LOCAL_DEF( void )
cf2_blues_init( CF2_Blues blues,
CF2_Font font )
{
/* pointer to parsed font object */
CFF_Decoder* decoder = font->decoder;
CF2_Fixed zoneHeight;
CF2_Fixed maxZoneHeight = 0;
CF2_Fixed csUnitsPerPixel;
size_t numBlueValues;
size_t numOtherBlues;
size_t numFamilyBlues;
size_t numFamilyOtherBlues;
FT_Pos* blueValues;
FT_Pos* otherBlues;
FT_Pos* familyBlues;
FT_Pos* familyOtherBlues;
size_t i;
CF2_Fixed emBoxBottom, emBoxTop;
#if 0
CF2_Int unitsPerEm = font->unitsPerEm;
if ( unitsPerEm == 0 )
unitsPerEm = 1000;
#endif
FT_ZERO( blues );
blues->scale = font->innerTransform.d;
cf2_getBlueMetrics( decoder,
&blues->blueScale,
&blues->blueShift,
&blues->blueFuzz );
cf2_getBlueValues( decoder, &numBlueValues, &blueValues );
cf2_getOtherBlues( decoder, &numOtherBlues, &otherBlues );
cf2_getFamilyBlues( decoder, &numFamilyBlues, &familyBlues );
cf2_getFamilyOtherBlues( decoder, &numFamilyOtherBlues, &familyOtherBlues );
/*
* synthetic em box hint heuristic
*
* Apply this when ideographic dictionary (LanguageGroup 1) has no
* real alignment zones. Adobe tools generate dummy zones at -250 and
* 1100 for a 1000 unit em. Fonts with ICF-based alignment zones
* should not enable the heuristic. When the heuristic is enabled,
* the font's blue zones are ignored.
*
*/
/* get em box from OS/2 typoAscender/Descender */
/* TODO: FreeType does not parse these metrics. Skip them for now. */
#if 0
FCM_getHorizontalLineMetrics( &e,
font->font,
&ascender,
&descender,
&linegap );
if ( ascender - descender == unitsPerEm )
{
emBoxBottom = cf2_intToFixed( descender );
emBoxTop = cf2_intToFixed( ascender );
}
else
#endif
{
emBoxBottom = CF2_ICF_Bottom;
emBoxTop = CF2_ICF_Top;
}
if ( cf2_getLanguageGroup( decoder ) == 1 &&
( numBlueValues == 0 ||
( numBlueValues == 4 &&
cf2_blueToFixed( blueValues[0] ) < emBoxBottom &&
cf2_blueToFixed( blueValues[1] ) < emBoxBottom &&
cf2_blueToFixed( blueValues[2] ) > emBoxTop &&
cf2_blueToFixed( blueValues[3] ) > emBoxTop ) ) )
{
/*
* Construct hint edges suitable for synthetic ghost hints at top
* and bottom of em box. +-CF2_MIN_COUNTER allows for unhinted
* features above or below the last hinted edge. This also gives a
* net 1 pixel boost to the height of ideographic glyphs.
*
* Note: Adjust synthetic hints outward by epsilon (0x.0001) to
* avoid interference. E.g., some fonts have real hints at
* 880 and -120.
*/
blues->emBoxBottomEdge.csCoord = emBoxBottom - CF2_FIXED_EPSILON;
blues->emBoxBottomEdge.dsCoord = cf2_fixedRound(
FT_MulFix(
blues->emBoxBottomEdge.csCoord,
blues->scale ) ) -
CF2_MIN_COUNTER;
blues->emBoxBottomEdge.scale = blues->scale;
blues->emBoxBottomEdge.flags = CF2_GhostBottom |
CF2_Locked |
CF2_Synthetic;
blues->emBoxTopEdge.csCoord = emBoxTop + CF2_FIXED_EPSILON +
2 * font->darkenY;
blues->emBoxTopEdge.dsCoord = cf2_fixedRound(
FT_MulFix(
blues->emBoxTopEdge.csCoord,
blues->scale ) ) +
CF2_MIN_COUNTER;
blues->emBoxTopEdge.scale = blues->scale;
blues->emBoxTopEdge.flags = CF2_GhostTop |
CF2_Locked |
CF2_Synthetic;
blues->doEmBoxHints = TRUE; /* enable the heuristic */
return;
}
/* copy `BlueValues' and `OtherBlues' to a combined array of top and */
/* bottom zones */
for ( i = 0; i < numBlueValues; i += 2 )
{
blues->zone[blues->count].csBottomEdge =
cf2_blueToFixed( blueValues[i] );
blues->zone[blues->count].csTopEdge =
cf2_blueToFixed( blueValues[i + 1] );
zoneHeight = blues->zone[blues->count].csTopEdge -
blues->zone[blues->count].csBottomEdge;
if ( zoneHeight < 0 )
{
FT_TRACE4(( "cf2_blues_init: ignoring negative zone height\n" ));
continue; /* reject this zone */
}
if ( zoneHeight > maxZoneHeight )
{
/* take maximum before darkening adjustment */
/* so overshoot suppression point doesn't change */
maxZoneHeight = zoneHeight;
}
/* adjust both edges of top zone upward by twice darkening amount */
if ( i != 0 )
{
blues->zone[blues->count].csTopEdge += 2 * font->darkenY;
blues->zone[blues->count].csBottomEdge += 2 * font->darkenY;
}
/* first `BlueValue' is bottom zone; others are top */
if ( i == 0 )
{
blues->zone[blues->count].bottomZone =
TRUE;
blues->zone[blues->count].csFlatEdge =
blues->zone[blues->count].csTopEdge;
}
else
{
blues->zone[blues->count].bottomZone =
FALSE;
blues->zone[blues->count].csFlatEdge =
blues->zone[blues->count].csBottomEdge;
}
blues->count += 1;
}
for ( i = 0; i < numOtherBlues; i += 2 )
{
blues->zone[blues->count].csBottomEdge =
cf2_blueToFixed( otherBlues[i] );
blues->zone[blues->count].csTopEdge =
cf2_blueToFixed( otherBlues[i + 1] );
zoneHeight = blues->zone[blues->count].csTopEdge -
blues->zone[blues->count].csBottomEdge;
if ( zoneHeight < 0 )
{
FT_TRACE4(( "cf2_blues_init: ignoring negative zone height\n" ));
continue; /* reject this zone */
}
if ( zoneHeight > maxZoneHeight )
{
/* take maximum before darkening adjustment */
/* so overshoot suppression point doesn't change */
maxZoneHeight = zoneHeight;
}
/* Note: bottom zones are not adjusted for darkening amount */
/* all OtherBlues are bottom zone */
blues->zone[blues->count].bottomZone =
TRUE;
blues->zone[blues->count].csFlatEdge =
blues->zone[blues->count].csTopEdge;
blues->count += 1;
}
/* Adjust for FamilyBlues */
/* Search for the nearest flat edge in `FamilyBlues' or */
/* `FamilyOtherBlues'. According to the Black Book, any matching edge */
/* must be within one device pixel */
csUnitsPerPixel = FT_DivFix( cf2_intToFixed( 1 ), blues->scale );
/* loop on all zones in this font */
for ( i = 0; i < blues->count; i++ )
{
size_t j;
CF2_Fixed minDiff;
CF2_Fixed flatFamilyEdge, diff;
/* value for this font */
CF2_Fixed flatEdge = blues->zone[i].csFlatEdge;
if ( blues->zone[i].bottomZone )
{
/* In a bottom zone, the top edge is the flat edge. */
/* Search `FamilyOtherBlues' for bottom zones; look for closest */
/* Family edge that is within the one pixel threshold. */
minDiff = CF2_FIXED_MAX;
for ( j = 0; j < numFamilyOtherBlues; j += 2 )
{
/* top edge */
flatFamilyEdge = cf2_blueToFixed( familyOtherBlues[j + 1] );
diff = cf2_fixedAbs( flatEdge - flatFamilyEdge );
if ( diff < minDiff && diff < csUnitsPerPixel )
{
blues->zone[i].csFlatEdge = flatFamilyEdge;
minDiff = diff;
if ( diff == 0 )
break;
}
}
/* check the first member of FamilyBlues, which is a bottom zone */
if ( numFamilyBlues >= 2 )
{
/* top edge */
flatFamilyEdge = cf2_blueToFixed( familyBlues[1] );
diff = cf2_fixedAbs( flatEdge - flatFamilyEdge );
if ( diff < minDiff && diff < csUnitsPerPixel )
blues->zone[i].csFlatEdge = flatFamilyEdge;
}
}
else
{
/* In a top zone, the bottom edge is the flat edge. */
/* Search `FamilyBlues' for top zones; skip first zone, which is a */
/* bottom zone; look for closest Family edge that is within the */
/* one pixel threshold */
minDiff = CF2_FIXED_MAX;
for ( j = 2; j < numFamilyBlues; j += 2 )
{
/* bottom edge */
flatFamilyEdge = cf2_blueToFixed( familyBlues[j] );
/* adjust edges of top zone upward by twice darkening amount */
flatFamilyEdge += 2 * font->darkenY; /* bottom edge */
diff = cf2_fixedAbs( flatEdge - flatFamilyEdge );
if ( diff < minDiff && diff < csUnitsPerPixel )
{
blues->zone[i].csFlatEdge = flatFamilyEdge;
minDiff = diff;
if ( diff == 0 )
break;
}
}
}
}
/* TODO: enforce separation of zones, including BlueFuzz */
/* Adjust BlueScale; similar to AdjustBlueScale() in coretype */
/* `bcsetup.c'. */
if ( maxZoneHeight > 0 )
{
if ( blues->blueScale > FT_DivFix( cf2_intToFixed( 1 ),
maxZoneHeight ) )
{
/* clamp at maximum scale */
blues->blueScale = FT_DivFix( cf2_intToFixed( 1 ),
maxZoneHeight );
}
/*
* TODO: Revisit the bug fix for 613448. The minimum scale
* requirement catches a number of library fonts. For
* example, with default BlueScale (.039625) and 0.4 minimum,
* the test below catches any font with maxZoneHeight < 10.1.
* There are library fonts ranging from 2 to 10 that get
* caught, including e.g., Eurostile LT Std Medium with
* maxZoneHeight of 6.
*
*/
#if 0
if ( blueScale < .4 / maxZoneHeight )
{
tetraphilia_assert( 0 );
/* clamp at minimum scale, per bug 0613448 fix */
blueScale = .4 / maxZoneHeight;
}
#endif
}
/*
* Suppress overshoot and boost blue zones at small sizes. Boost
* amount varies linearly from 0.5 pixel near 0 to 0 pixel at
* blueScale cutoff.
* Note: This boost amount is different from the coretype heuristic.
*
*/
if ( blues->scale < blues->blueScale )
{
blues->suppressOvershoot = TRUE;
/* Change rounding threshold for `dsFlatEdge'. */
/* Note: constant changed from 0.5 to 0.6 to avoid a problem with */
/* 10ppem Arial */
blues->boost = cf2_floatToFixed( .6 ) -
FT_MulDiv( cf2_floatToFixed ( .6 ),
blues->scale,
blues->blueScale );
if ( blues->boost > 0x7FFF )
{
/* boost must remain less than 0.5, or baseline could go negative */
blues->boost = 0x7FFF;
}
}
/* boost and darkening have similar effects; don't do both */
if ( font->stemDarkened )
blues->boost = 0;
/* set device space alignment for each zone; */
/* apply boost amount before rounding flat edge */
for ( i = 0; i < blues->count; i++ )
{
if ( blues->zone[i].bottomZone )
blues->zone[i].dsFlatEdge = cf2_fixedRound(
FT_MulFix(
blues->zone[i].csFlatEdge,
blues->scale ) -
blues->boost );
else
blues->zone[i].dsFlatEdge = cf2_fixedRound(
FT_MulFix(
blues->zone[i].csFlatEdge,
blues->scale ) +
blues->boost );
}
}
/*
* Check whether `stemHint' is captured by one of the blue zones.
*
* Zero, one or both edges may be valid; only valid edges can be
* captured. For compatibility with CoolType, search top and bottom
* zones in the same pass (see `BlueLock'). If a hint is captured,
* return true and position the edge(s) in one of 3 ways:
*
* 1) If `BlueScale' suppresses overshoot, position the captured edge
* at the flat edge of the zone.
* 2) If overshoot is not suppressed and `BlueShift' requires
* overshoot, position the captured edge a minimum of 1 device pixel
* from the flat edge.
* 3) If overshoot is not suppressed or required, position the captured
* edge at the nearest device pixel.
*
*/
FT_LOCAL_DEF( FT_Bool )
cf2_blues_capture( const CF2_Blues blues,
CF2_Hint bottomHintEdge,
CF2_Hint topHintEdge )
{
/* TODO: validate? */
CF2_Fixed csFuzz = blues->blueFuzz;
/* new position of captured edge */
CF2_Fixed dsNew;
/* amount that hint is moved when positioned */
CF2_Fixed dsMove = 0;
FT_Bool captured = FALSE;
CF2_UInt i;
/* assert edge flags are consistent */
FT_ASSERT( !cf2_hint_isTop( bottomHintEdge ) &&
!cf2_hint_isBottom( topHintEdge ) );
/* TODO: search once without blue fuzz for compatibility with coretype? */
for ( i = 0; i < blues->count; i++ )
{
if ( blues->zone[i].bottomZone &&
cf2_hint_isBottom( bottomHintEdge ) )
{
if ( ( blues->zone[i].csBottomEdge - csFuzz ) <=
bottomHintEdge->csCoord &&
bottomHintEdge->csCoord <=
( blues->zone[i].csTopEdge + csFuzz ) )
{
/* bottom edge captured by bottom zone */
if ( blues->suppressOvershoot )
dsNew = blues->zone[i].dsFlatEdge;
else if ( ( blues->zone[i].csTopEdge - bottomHintEdge->csCoord ) >=
blues->blueShift )
{
/* guarantee minimum of 1 pixel overshoot */
dsNew = FT_MIN(
cf2_fixedRound( bottomHintEdge->dsCoord ),
blues->zone[i].dsFlatEdge - cf2_intToFixed( 1 ) );
}
else
{
/* simply round captured edge */
dsNew = cf2_fixedRound( bottomHintEdge->dsCoord );
}
dsMove = dsNew - bottomHintEdge->dsCoord;
captured = TRUE;
break;
}
}
if ( !blues->zone[i].bottomZone && cf2_hint_isTop( topHintEdge ) )
{
if ( ( blues->zone[i].csBottomEdge - csFuzz ) <=
topHintEdge->csCoord &&
topHintEdge->csCoord <=
( blues->zone[i].csTopEdge + csFuzz ) )
{
/* top edge captured by top zone */
if ( blues->suppressOvershoot )
dsNew = blues->zone[i].dsFlatEdge;
else if ( ( topHintEdge->csCoord - blues->zone[i].csBottomEdge ) >=
blues->blueShift )
{
/* guarantee minimum of 1 pixel overshoot */
dsNew = FT_MAX(
cf2_fixedRound( topHintEdge->dsCoord ),
blues->zone[i].dsFlatEdge + cf2_intToFixed( 1 ) );
}
else
{
/* simply round captured edge */
dsNew = cf2_fixedRound( topHintEdge->dsCoord );
}
dsMove = dsNew - topHintEdge->dsCoord;
captured = TRUE;
break;
}
}
}
if ( captured )
{
/* move both edges and flag them `locked' */
if ( cf2_hint_isValid( bottomHintEdge ) )
{
bottomHintEdge->dsCoord += dsMove;
cf2_hint_lock( bottomHintEdge );
}
if ( cf2_hint_isValid( topHintEdge ) )
{
topHintEdge->dsCoord += dsMove;
cf2_hint_lock( topHintEdge );
}
}
return captured;
}
/* END */
|