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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
/* GlyphHints.java -- Data and methods for actual hinting
Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.awt.font.autofit;
import gnu.java.awt.font.FontDelegate;
import gnu.java.awt.font.opentype.truetype.Fixed;
import gnu.java.awt.font.opentype.truetype.Point;
import gnu.java.awt.font.opentype.truetype.Zone;
/**
* The data and methods used for the actual hinting process.
*/
class GlyphHints
implements Constants
{
int xScale;
int xDelta;
int yScale;
int yDelta;
AxisHints[] axis;
Point[] points;
int numPoints;
int maxPoints;
Point[] contours;
int numContours;
int maxContours;
ScriptMetrics metrics;
int flags;
GlyphHints()
{
axis = new AxisHints[Constants.DIMENSION_MAX];
axis[Constants.DIMENSION_VERT] = new AxisHints();
axis[Constants.DIMENSION_HORZ] = new AxisHints();
xScale = Fixed.ONE;
yScale = Fixed.ONE;
}
void rescale(ScriptMetrics m)
{
metrics = m;
// TODO: Copy scalerFlags.
}
void reload(Zone outline)
{
numPoints = 0;
numContours = 0;
axis[0].numSegments = 0;
axis[0].numEdges = 0;
axis[1].numSegments = 0;
axis[1].numEdges = 0;
// Create/reallocate the contours array.
int newMax = outline.getNumContours();
if (newMax > maxContours || contours == null)
{
newMax = (newMax + 3) & ~3; // Taken from afhints.c .
Point[] newContours = new Point[newMax];
if (contours != null)
{
System.arraycopy(contours, 0, newContours, 0, maxContours);
}
contours = newContours;
maxContours = newMax;
}
// Create/reallocate the points array.
newMax = outline.getSize() + 2;
if (newMax > maxPoints || points == null)
{
newMax = (newMax + 2 + 7) & ~7; // Taken from afhints.c .
Point[] newPoints = new Point[newMax];
if (points != null)
{
System.arraycopy(points, 0, newPoints, 0, maxPoints);
}
points = newPoints;
maxPoints = newMax;
}
numPoints = outline.getSize() - 4; // 4 phantom points.
numContours = outline.getNumContours();
// Set major direction. We don't handle Type 1 fonts yet.
axis[DIMENSION_HORZ].majorDir = DIR_UP;
axis[DIMENSION_VERT].majorDir = DIR_LEFT;
// TODO: Freetype seems to scale and translate the glyph at that point.
// I suppose that this is not really needed.
// The scales are scaling from font units to 1/64 device pixels.
xScale = Fixed.valueOf16(outline.scaleX * 64);
yScale = Fixed.valueOf16(outline.scaleY * 64);
// FIXME: What is that xDelta and yDelta used for?
System.arraycopy(outline.getPoints(), 0, points, 0, numPoints);
// Setup prev and next and contours array.
// TODO: Probably cache this.
contours = new Point[numContours];
Point currentContour = points[0];
for (int i = 0, cIndex = 0; i < numPoints; i++)
{
// Start new contour when the last point has been a contour end.
if (outline.isContourEnd(i))
{
// Connect the contour end point to the start point.
points[i].setNext(currentContour);
currentContour.setPrev(points[i]);
contours[cIndex] = currentContour;
cIndex++;
currentContour = i < numPoints - 1 ? points[i + 1] : null;
}
else
{
// Connect the current and the previous point.
points[i].setNext(points[i + 1]);
points[i + 1].setPrev(points[i]);
}
}
// Compute directions of in and out vectors of all points as well
// as the weak point flag.
for (int i = 0; i < numPoints; i++)
{
// Compute in and out dir.
Point p = points[i];
Point prev = p.getPrev();
int inX = p.getOrigX() - prev.getOrigX();
int inY = p.getOrigY() - prev.getOrigY();
p.setInDir(Utils.computeDirection(inX, inY));
Point next = p.getNext();
int outX = next.getOrigX() - p.getOrigX();
int outY = next.getOrigY() - p.getOrigY();
p.setOutDir(Utils.computeDirection(outX, outY));
if (p.isControlPoint())
{
setWeakPoint(p);
}
else if (p.getOutDir() == p.getInDir())
{
if (p.getOutDir() != DIR_NONE)
setWeakPoint(p);
else
{
int angleIn = Utils.atan(inY, inX);
int angleOut = Utils.atan(outY, outX);
int delta = Utils.angleDiff(angleIn, angleOut);
if (delta < 2 && delta > -2)
setWeakPoint(p);
}
}
else if (p.getInDir() == - p.getOutDir())
{
setWeakPoint(p);
}
}
computeInflectionPoints();
}
private void setWeakPoint(Point p)
{
p.setFlags((byte) (p.getFlags() | Point.FLAG_WEAK_INTERPOLATION));
}
/**
* Computes the inflection points for a glyph.
*/
private void computeInflectionPoints()
{
// Do each contour separately.
contours : for (int c = 0; c < contours.length; c++)
{
Point point = contours[c];
Point first = point;
Point start = point;
Point end = point;
do
{
end = end.getNext();
if (end == first)
continue contours;
} while (end.getOrigX() == first.getOrigX()
&& end.getOrigY() == first.getOrigY());
// Extend segment start whenever possible.
Point before = start;
int angleIn;
int angleSeg = Utils.atan(end.getOrigX() - start.getOrigX(),
end.getOrigY() - start.getOrigY());
do
{
do
{
start = before;
before = before.getPrev();
if (before == first)
continue contours;
} while (before.getOrigX() == start.getOrigX()
&& before.getOrigY() == start.getOrigY());
angleIn = Utils.atan(start.getOrigX() - before.getOrigX(),
start.getOrigY() - before.getOrigY());
} while (angleIn == angleSeg);
first = start;
int diffIn = Utils.angleDiff(angleIn, angleSeg);
// Now, process all segments in the contour.
Point after;
boolean finished = false;
int angleOut, diffOut;
do
{
// First, extend the current segment's end whenever possible.
after = end;
do
{
do
{
end = after;
after = after.getNext();
if (after == first)
finished = true;
} while (end.getOrigX() == after.getOrigX()
&& end.getOrigY() == after.getOrigY());
angleOut = Utils.atan(after.getOrigX() - end.getOrigX(),
after.getOrigY() - end.getOrigY());
} while (angleOut == angleSeg);
diffOut = Utils.angleDiff(angleSeg, angleOut);
if ((diffIn ^ diffOut) < 0)
{
// diffIn and diffOut have different signs, we have
// inflection points here.
do
{
start.addFlags(Point.FLAG_INFLECTION);
start = start.getNext();
} while (start != end);
start.addFlags(Point.FLAG_INFLECTION);
}
start = end;
end = after;
angleSeg = angleOut;
diffIn = diffOut;
} while (! finished);
}
}
boolean doHorizontal()
{
return (flags & FontDelegate.FLAG_NO_HINT_HORIZONTAL) == 0;
}
boolean doVertical()
{
return (flags & FontDelegate.FLAG_NO_HINT_VERTICAL) == 0;
}
void alignWeakPoints(int dim)
{
short touchFlag;
Point point;
// PASS 1 : Move segments to edge positions.
if (dim == DIMENSION_HORZ)
{
touchFlag = Point.FLAG_DONE_X;
for (int p = 0; p < numPoints; p++)
{
point = points[p];
point.setU(point.getX());
point.setV(point.getScaledX());
}
}
else
{
touchFlag = Point.FLAG_DONE_Y;
for (int p = 0; p < numPoints; p++)
{
point = points[p];
point.setU(point.getY());
point.setV(point.getScaledY());
}
}
point = points[0];
for (int c = 0; c < numContours; c++)
{
point = contours[c];
int idx = getPointIndex(point);
Point endPoint = point.getPrev();
int endIdx = getPointIndex(endPoint);
int firstIdx = idx;
while (idx <= endIdx
&& (point.getFlags() & touchFlag) == 0)
{
idx++;
point = points[idx];
}
if (idx <= endIdx)
{
int firstTouched = idx;
int curTouched = idx;
idx++;
point = points[idx];
while (idx <= endIdx)
{
if ((point.getFlags() & touchFlag) != 0)
{
// We found two successive touch points. We interpolate
// all contour points between them.
iupInterp(curTouched + 1, idx - 1, curTouched, idx);
curTouched = idx;
}
idx++;
point = points[idx];
}
if (curTouched == firstTouched)
{
// This is a special case: Only one point was touched in the
// contour. We thus simply shift the whole contour.
iupShift(firstIdx, endIdx, curTouched);
}
else
{
// Now interpolate after the last touched point to the end
// of the contour.
iupInterp(curTouched + 1, endIdx, curTouched, firstTouched);
// If the first contour point isn't touched, interpolate
// from the contour start to the first touched point.
if (firstTouched > 0)
{
iupInterp(firstIdx, firstTouched - 1, curTouched,
firstTouched);
}
}
}
}
// Now store the values back.
if (dim == DIMENSION_HORZ)
{
for (int p = 0; p < numPoints; p++)
{
point = points[p];
point.setX(point.getU());
}
}
else
{
for (int p = 0; p < numPoints; p++)
{
point = points[p];
point.setY(point.getU());
}
}
}
private void iupShift(int p1, int p2, int ref)
{
int delta = points[ref].getU() - points[ref].getV();
for (int p = p1; p < ref; p++)
{
points[p].setU(points[p].getV() + delta);
}
for (int p = ref + 1; p <= p2; p++)
{
points[p].setU(points[p].getV() + delta);
}
}
private void iupInterp(int p1, int p2, int ref1, int ref2)
{
int v1 = points[ref1].getV();
int v2 = points[ref2].getV();
int d1 = points[ref1].getU() - v1;
int d2 = points[ref2].getU() - v2;
if (p1 > p2)
return;
if (v1 == v2)
{
for (int p = p1; p <= p2; p++)
{
int u = points[p].getV();
if (u <= v1)
u += d1;
else
u += d2;
points[p].setU(u);
}
}
else if (v1 < v2)
{
for (int p = p1; p <= p2; p++)
{
int u = points[p].getV();
if (u <= v1)
u += d1;
else if (u >= v2)
u += d2;
else
{
u = points[ref1].getU() + Utils.mulDiv(u - v1,
points[ref2].getU()
- points[ref1].getU(),
v2 - v1);
}
points[p].setU(u);
}
}
else
{
for (int p = p1; p <= p2; p++)
{
int u = points[p].getV();
if (u <= v2)
u += d2;
else if (u >= v1)
u += d1;
else
{
u = points[ref1].getU() + Utils.mulDiv(u - v1,
points[ref2].getU()
- points[ref1].getU(),
v2 - v1);
}
points[p].setU(u);
}
}
}
void alignStrongPoints(int dim)
{
AxisHints ax = axis[dim];
Edge[] edges = ax.edges;
int numEdges = ax.numEdges;
short touchFlag;
if (dim == DIMENSION_HORZ)
touchFlag = Point.FLAG_DONE_X;
else
touchFlag = Point.FLAG_DONE_Y;
if (numEdges > 0)
{
for (int p = 0; p < numPoints; p++)
{
Point point = points[p];
if ((point.getFlags() & touchFlag) != 0)
continue;
// If this point is a candidate for weak interpolation, we
// interpolate it after all strong points have been processed.
if ((point.getFlags() & Point.FLAG_WEAK_INTERPOLATION) != 0
&& (point.getFlags() & Point.FLAG_INFLECTION) == 0)
continue;
int u, ou, fu, delta;
if (dim == DIMENSION_VERT)
{
u = point.getOrigY();
ou = point.getScaledY();
}
else
{
u = point.getOrigX();
ou = point.getScaledX();
}
fu = u;
// Is the point before the first edge?
Edge edge = edges[0];
// Inversed vertical dimension.
delta = edge.fpos - u;
if (delta >= 0)
{
u = edge.pos - (edge.opos - ou);
storePoint(point, u, dim, touchFlag);
}
else
{
// Is the point after the last edge?
edge = edges[numEdges - 1];
delta = u - edge.fpos;
if (delta >= 0)
{
u = edge.pos + (ou - edge.opos);
storePoint(point, u, dim, touchFlag);
}
else
{
// Find enclosing edges.
int min = 0;
int max = numEdges;
int mid, fpos;
boolean found = false;
while (min < max)
{
mid = (max + min) / 2;
edge = edges[mid];
fpos = edge.fpos;
if (u < fpos)
max = mid;
else if (u > fpos)
min = mid + 1;
else
{
// Directly on the edge.
u = edge.pos;
storePoint(point, u, dim, touchFlag);
found = true;
break;
}
}
if (! found)
{
Edge before = edges[min - 1];
Edge after = edges[min];
if (before.scale == 0)
{
before.scale = Fixed.div16(after.pos - before.pos,
after.fpos - before.fpos);
}
u = before.pos + Fixed.mul16(fu - before.fpos,
before.scale);
}
storePoint(point, u, dim, touchFlag);
}
}
}
}
}
private void storePoint(Point p, int u, int dim, short touchFlag)
{
if (dim == DIMENSION_HORZ)
p.setX(u);
else
p.setY(u);
p.addFlags(touchFlag);
}
void alignEdgePoints(int dim)
{
AxisHints ax = axis[dim];
Edge[] edges = ax.edges;
int numEdges = ax.numEdges;
for (int e = 0; e < numEdges; e++)
{
Edge edge = edges[e];
Segment seg = edge.first;
do
{
Point point = seg.first;
while (true)
{
if (dim == DIMENSION_HORZ)
{
point.setX(edge.pos);
point.addFlags(Point.FLAG_DONE_X);
}
else
{
point.setY(edge.pos);
point.addFlags(Point.FLAG_DONE_Y);
}
if (point == seg.last)
break;
point = point.getNext();
}
seg = seg.edgeNext;
} while (seg != edge.first);
}
}
private int getPointIndex(Point p)
{
int idx = -1;
for (int i = 0; i < numPoints; i++)
{
if (p == points[i])
{
idx = i;
break;
}
}
return idx;
}
public boolean doAlignEdgePoints()
{
return (flags & FontDelegate.FLAG_NO_HINT_EDGE_POINTS) == 0;
}
public boolean doAlignStrongPoints()
{
return (flags & FontDelegate.FLAG_NO_HINT_STRONG_POINTS) == 0;
}
public boolean doAlignWeakPoints()
{
return (flags & FontDelegate.FLAG_NO_HINT_WEAK_POINTS) == 0;
}
}
|