Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/font/ExtendedTextSourceLabel.java
38829 views
1
/*
2
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
/*
26
*
27
* (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
28
*/
29
30
package sun.font;
31
32
import java.awt.Font;
33
import java.awt.Graphics2D;
34
import java.awt.Rectangle;
35
import java.awt.Shape;
36
37
import java.awt.font.FontRenderContext;
38
import java.awt.font.GlyphJustificationInfo;
39
import java.awt.font.GlyphMetrics;
40
import java.awt.font.LineMetrics;
41
import java.awt.font.TextAttribute;
42
43
import java.awt.geom.AffineTransform;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
47
import java.util.Map;
48
49
/**
50
* Default implementation of ExtendedTextLabel.
51
*/
52
53
// {jbr} I made this class package-private to keep the
54
// Decoration.Label API package-private.
55
56
/* public */
57
class ExtendedTextSourceLabel extends ExtendedTextLabel implements Decoration.Label {
58
59
TextSource source;
60
private Decoration decorator;
61
62
// caches
63
private Font font;
64
private AffineTransform baseTX;
65
private CoreMetrics cm;
66
67
Rectangle2D lb;
68
Rectangle2D ab;
69
Rectangle2D vb;
70
Rectangle2D ib;
71
StandardGlyphVector gv;
72
float[] charinfo;
73
74
/**
75
* Create from a TextSource.
76
*/
77
public ExtendedTextSourceLabel(TextSource source, Decoration decorator) {
78
this.source = source;
79
this.decorator = decorator;
80
finishInit();
81
}
82
83
/**
84
* Create from a TextSource, optionally using cached data from oldLabel starting at the offset.
85
* If present oldLabel must have been created from a run of text that includes the text used in
86
* the new label. Start in source corresponds to logical character offset in oldLabel.
87
*/
88
public ExtendedTextSourceLabel(TextSource source, ExtendedTextSourceLabel oldLabel, int offset) {
89
// currently no optimization.
90
this.source = source;
91
this.decorator = oldLabel.decorator;
92
finishInit();
93
}
94
95
private void finishInit() {
96
font = source.getFont();
97
98
Map<TextAttribute, ?> atts = font.getAttributes();
99
baseTX = AttributeValues.getBaselineTransform(atts);
100
if (baseTX == null){
101
cm = source.getCoreMetrics();
102
} else {
103
AffineTransform charTX = AttributeValues.getCharTransform(atts);
104
if (charTX == null) {
105
charTX = new AffineTransform();
106
}
107
font = font.deriveFont(charTX);
108
109
LineMetrics lm = font.getLineMetrics(source.getChars(), source.getStart(),
110
source.getStart() + source.getLength(), source.getFRC());
111
cm = CoreMetrics.get(lm);
112
}
113
}
114
115
116
// TextLabel API
117
118
public Rectangle2D getLogicalBounds() {
119
return getLogicalBounds(0, 0);
120
}
121
122
public Rectangle2D getLogicalBounds(float x, float y) {
123
if (lb == null) {
124
lb = createLogicalBounds();
125
}
126
return new Rectangle2D.Float((float)(lb.getX() + x),
127
(float)(lb.getY() + y),
128
(float)lb.getWidth(),
129
(float)lb.getHeight());
130
}
131
132
public float getAdvance() {
133
if (lb == null) {
134
lb = createLogicalBounds();
135
}
136
return (float)lb.getWidth();
137
}
138
139
public Rectangle2D getVisualBounds(float x, float y) {
140
if (vb == null) {
141
vb = decorator.getVisualBounds(this);
142
}
143
return new Rectangle2D.Float((float)(vb.getX() + x),
144
(float)(vb.getY() + y),
145
(float)vb.getWidth(),
146
(float)vb.getHeight());
147
}
148
149
public Rectangle2D getAlignBounds(float x, float y) {
150
if (ab == null) {
151
ab = createAlignBounds();
152
}
153
return new Rectangle2D.Float((float)(ab.getX() + x),
154
(float)(ab.getY() + y),
155
(float)ab.getWidth(),
156
(float)ab.getHeight());
157
158
}
159
160
public Rectangle2D getItalicBounds(float x, float y) {
161
if (ib == null) {
162
ib = createItalicBounds();
163
}
164
return new Rectangle2D.Float((float)(ib.getX() + x),
165
(float)(ib.getY() + y),
166
(float)ib.getWidth(),
167
(float)ib.getHeight());
168
169
}
170
171
public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {
172
return getGV().getPixelBounds(frc, x, y);
173
}
174
175
public boolean isSimple() {
176
return decorator == Decoration.getPlainDecoration() &&
177
baseTX == null;
178
}
179
180
public AffineTransform getBaselineTransform() {
181
return baseTX; // passing internal object, caller must not modify!
182
}
183
184
public Shape handleGetOutline(float x, float y) {
185
return getGV().getOutline(x, y);
186
}
187
188
public Shape getOutline(float x, float y) {
189
return decorator.getOutline(this, x, y);
190
}
191
192
public void handleDraw(Graphics2D g, float x, float y) {
193
g.drawGlyphVector(getGV(), x, y);
194
}
195
196
public void draw(Graphics2D g, float x, float y) {
197
decorator.drawTextAndDecorations(this, g, x, y);
198
}
199
200
/**
201
* The logical bounds extends from the origin of the glyphvector to the
202
* position at which a following glyphvector's origin should be placed.
203
* We always assume glyph vectors are rendered from left to right, so
204
* the origin is always to the left.
205
* <p> On a left-to-right run, combining marks and 'ligatured away'
206
* characters are to the right of their base characters. The charinfo
207
* array will record the character positions for these 'missing' characters
208
* as being at the origin+advance of the base glyph, with zero advance.
209
* (This is not necessarily the same as the glyph position, for example,
210
* an umlaut glyph may have a position to the left of this point, it depends
211
* on whether the font was designed so that such glyphs overhang to the left
212
* of their origin, or whether it presumes some kind of kerning to position
213
* the glyphs). Anyway, the left of the bounds is the origin of the first
214
* logical (leftmost) character, and the right is the origin + advance of the
215
* last logical (rightmost) character.
216
* <p> On a right-to-left run, these special characters are to the left
217
* of their base characters. Again, since 'glyph position' has been abstracted
218
* away, we can use the origin of the leftmost character, and the origin +
219
* advance of the rightmost character.
220
* <p> On a mixed run (hindi) we can't rely on the first logical character
221
* being the leftmost character. However we can again rely on the leftmost
222
* character origin and the rightmost character + advance.
223
*/
224
protected Rectangle2D createLogicalBounds() {
225
return getGV().getLogicalBounds();
226
}
227
228
public Rectangle2D handleGetVisualBounds() {
229
return getGV().getVisualBounds();
230
}
231
232
/**
233
* Like createLogicalBounds except ignore leading and logically trailing white space.
234
* this assumes logically trailing whitespace is also visually trailing.
235
* Whitespace is anything that has a zero visual width, regardless of its advance.
236
* <p> We make the same simplifying assumptions as in createLogicalBounds, namely
237
* that we can rely on the charinfo to shield us from any glyph positioning oddities
238
* in the font that place the glyph for a character at other than the pos + advance
239
* of the character to its left. So we no longer need to skip chars with zero
240
* advance, as their bounds (right and left) are already correct.
241
*/
242
protected Rectangle2D createAlignBounds() {
243
float[] info = getCharinfo();
244
245
float al = 0f;
246
float at = -cm.ascent;
247
float aw = 0f;
248
float ah = cm.ascent + cm.descent;
249
250
if (charinfo == null || charinfo.length == 0) {
251
return new Rectangle2D.Float(al, at, aw, ah);
252
}
253
254
boolean lineIsLTR = (source.getLayoutFlags() & 0x8) == 0;
255
int rn = info.length - numvals;
256
if (lineIsLTR) {
257
while (rn > 0 && info[rn+visw] == 0) {
258
rn -= numvals;
259
}
260
}
261
262
if (rn >= 0) {
263
int ln = 0;
264
while (ln < rn && ((info[ln+advx] == 0) || (!lineIsLTR && info[ln+visw] == 0))) {
265
ln += numvals;
266
}
267
268
al = Math.max(0f, info[ln+posx]);
269
aw = info[rn+posx] + info[rn+advx] - al;
270
}
271
272
/*
273
boolean lineIsLTR = source.lineIsLTR();
274
int rn = info.length - numvals;
275
while (rn > 0 && ((info[rn+advx] == 0) || (lineIsLTR && info[rn+visw] == 0))) {
276
rn -= numvals;
277
}
278
279
if (rn >= 0) {
280
int ln = 0;
281
while (ln < rn && ((info[ln+advx] == 0) || (!lineIsLTR && info[ln+visw] == 0))) {
282
ln += numvals;
283
}
284
285
al = Math.max(0f, info[ln+posx]);
286
aw = info[rn+posx] + info[rn+advx] - al;
287
}
288
*/
289
290
return new Rectangle2D.Float(al, at, aw, ah);
291
}
292
293
public Rectangle2D createItalicBounds() {
294
float ia = cm.italicAngle;
295
296
Rectangle2D lb = getLogicalBounds();
297
float l = (float)lb.getMinX();
298
float t = -cm.ascent;
299
float r = (float)lb.getMaxX();
300
float b = cm.descent;
301
if (ia != 0) {
302
if (ia > 0) {
303
l -= ia * (b - cm.ssOffset);
304
r -= ia * (t - cm.ssOffset);
305
} else {
306
l -= ia * (t - cm.ssOffset);
307
r -= ia * (b - cm.ssOffset);
308
}
309
}
310
return new Rectangle2D.Float(l, t, r - l, b - t);
311
}
312
313
private final StandardGlyphVector getGV() {
314
if (gv == null) {
315
gv = createGV();
316
}
317
318
return gv;
319
}
320
321
protected StandardGlyphVector createGV() {
322
FontRenderContext frc = source.getFRC();
323
int flags = source.getLayoutFlags();
324
char[] context = source.getChars();
325
int start = source.getStart();
326
int length = source.getLength();
327
328
GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines
329
gv = gl.layout(font, frc, context, start, length, flags, null); // ??? use textsource
330
GlyphLayout.done(gl);
331
332
return gv;
333
}
334
335
// ExtendedTextLabel API
336
337
private static final int posx = 0,
338
posy = 1,
339
advx = 2,
340
advy = 3,
341
visx = 4,
342
visy = 5,
343
visw = 6,
344
vish = 7;
345
private static final int numvals = 8;
346
347
public int getNumCharacters() {
348
return source.getLength();
349
}
350
351
public CoreMetrics getCoreMetrics() {
352
return cm;
353
}
354
355
public float getCharX(int index) {
356
validate(index);
357
float[] charinfo = getCharinfo();
358
int idx = l2v(index) * numvals + posx;
359
if (charinfo == null || idx >= charinfo.length) {
360
return 0f;
361
} else {
362
return charinfo[idx];
363
}
364
}
365
366
public float getCharY(int index) {
367
validate(index);
368
float[] charinfo = getCharinfo();
369
int idx = l2v(index) * numvals + posy;
370
if (charinfo == null || idx >= charinfo.length) {
371
return 0f;
372
} else {
373
return charinfo[idx];
374
}
375
}
376
377
public float getCharAdvance(int index) {
378
validate(index);
379
float[] charinfo = getCharinfo();
380
int idx = l2v(index) * numvals + advx;
381
if (charinfo == null || idx >= charinfo.length) {
382
return 0f;
383
} else {
384
return charinfo[idx];
385
}
386
}
387
388
public Rectangle2D handleGetCharVisualBounds(int index) {
389
validate(index);
390
float[] charinfo = getCharinfo();
391
index = l2v(index) * numvals;
392
if (charinfo == null || (index+vish) >= charinfo.length) {
393
return new Rectangle2D.Float();
394
}
395
return new Rectangle2D.Float(
396
charinfo[index + visx],
397
charinfo[index + visy],
398
charinfo[index + visw],
399
charinfo[index + vish]);
400
}
401
402
public Rectangle2D getCharVisualBounds(int index, float x, float y) {
403
404
Rectangle2D bounds = decorator.getCharVisualBounds(this, index);
405
if (x != 0 || y != 0) {
406
bounds.setRect(bounds.getX()+x,
407
bounds.getY()+y,
408
bounds.getWidth(),
409
bounds.getHeight());
410
}
411
return bounds;
412
}
413
414
private void validate(int index) {
415
if (index < 0) {
416
throw new IllegalArgumentException("index " + index + " < 0");
417
} else if (index >= source.getLength()) {
418
throw new IllegalArgumentException("index " + index + " < " + source.getLength());
419
}
420
}
421
422
/*
423
public int hitTestChar(float x, float y) {
424
// !!! return index of char hit, for swing
425
// result is negative for trailing-edge hits
426
// no italics so no problem at margins.
427
// for now, ignore y since we assume horizontal text
428
429
// find non-combining char origin to right of x
430
float[] charinfo = getCharinfo();
431
432
int n = 0;
433
int e = source.getLength();
434
while (n < e && charinfo[n + advx] != 0 && charinfo[n + posx] > x) {
435
n += numvals;
436
}
437
float rightx = n < e ? charinfo[n+posx] : charinfo[e - numvals + posx] + charinfo[e - numvals + advx];
438
439
// find non-combining char to left of that char
440
n -= numvals;
441
while (n >= 0 && charinfo[n+advx] == 0) {
442
n -= numvals;
443
}
444
float leftx = n >= 0 ? charinfo[n+posx] : 0;
445
float lefta = n >= 0 ? charinfo[n+advx] : 0;
446
447
n /= numvals;
448
449
boolean left = true;
450
if (x < leftx + lefta / 2f) {
451
// left of prev char
452
} else if (x < (leftx + lefta + rightx) / 2f) {
453
// right of prev char
454
left = false;
455
} else {
456
// left of follow char
457
n += 1;
458
}
459
460
if ((source.getLayoutFlags() & 0x1) != 0) {
461
n = getNumCharacters() - 1 - n;
462
left = !left;
463
}
464
465
return left ? n : -n;
466
}
467
*/
468
469
public int logicalToVisual(int logicalIndex) {
470
validate(logicalIndex);
471
return l2v(logicalIndex);
472
}
473
474
public int visualToLogical(int visualIndex) {
475
validate(visualIndex);
476
return v2l(visualIndex);
477
}
478
479
public int getLineBreakIndex(int start, float width) {
480
float[] charinfo = getCharinfo();
481
int length = source.getLength();
482
--start;
483
while (width >= 0 && ++start < length) {
484
int cidx = l2v(start) * numvals + advx;
485
if (cidx >= charinfo.length) {
486
break; // layout bailed for some reason
487
}
488
float adv = charinfo[cidx];
489
width -= adv;
490
}
491
492
return start;
493
}
494
495
public float getAdvanceBetween(int start, int limit) {
496
float a = 0f;
497
498
float[] charinfo = getCharinfo();
499
--start;
500
while (++start < limit) {
501
int cidx = l2v(start) * numvals + advx;
502
if (cidx >= charinfo.length) {
503
break; // layout bailed for some reason
504
}
505
a += charinfo[cidx];
506
}
507
508
return a;
509
}
510
511
public boolean caretAtOffsetIsValid(int offset) {
512
// REMIND: improve this implementation
513
514
// Ligature formation can either be done in logical order,
515
// with the ligature glyph logically preceding the null
516
// chars; or in visual order, with the ligature glyph to
517
// the left of the null chars. This method's implementation
518
// must reflect which strategy is used.
519
520
if (offset == 0 || offset == source.getLength()) {
521
return true;
522
}
523
char c = source.getChars()[source.getStart() + offset];
524
if (c == '\t' || c == '\n' || c == '\r') { // hack
525
return true;
526
}
527
int v = l2v(offset);
528
529
// If ligatures are always to the left, do this stuff:
530
//if (!(source.getLayoutFlags() & 0x1) == 0) {
531
// v += 1;
532
// if (v == source.getLength()) {
533
// return true;
534
// }
535
//}
536
537
int idx = v * numvals + advx;
538
float[] charinfo = getCharinfo();
539
if (charinfo == null || idx >= charinfo.length) {
540
return false;
541
} else {
542
return charinfo[idx] != 0;
543
}
544
}
545
546
private final float[] getCharinfo() {
547
if (charinfo == null) {
548
charinfo = createCharinfo();
549
}
550
return charinfo;
551
}
552
553
/*
554
* This takes the glyph info record obtained from the glyph vector and converts it into a similar record
555
* adjusted to represent character data instead. For economy we don't use glyph info records in this processing.
556
*
557
* Here are some constraints:
558
* - there can be more glyphs than characters (glyph insertion, perhaps based on normalization, has taken place)
559
* - there can not be fewer glyphs than characters (0xffff glyphs are inserted for characters ligaturized away)
560
* - each glyph maps to a single character, when multiple glyphs exist for a character they all map to it, but
561
* no two characters map to the same glyph
562
* - multiple glyphs mapping to the same character need not be in sequence (thai, tamil have split characters)
563
* - glyphs may be arbitrarily reordered (Indic reorders glyphs)
564
* - all glyphs share the same bidi level
565
* - all glyphs share the same horizontal (or vertical) baseline
566
* - combining marks visually follow their base character in the glyph array-- i.e. in an rtl gv they are
567
* to the left of their base character-- and have zero advance.
568
*
569
* The output maps this to character positions, and therefore caret positions, via the following assumptions:
570
* - zero-advance glyphs do not contribute to the advance of their character (i.e. position is ignored), conversely
571
* if a glyph is to contribute to the advance of its character it must have a non-zero (float) advance
572
* - no carets can appear between a zero width character and its preceding character, where 'preceding' is
573
* defined logically.
574
* - no carets can appear within a split character
575
* - no carets can appear within a local reordering (i.e. Indic reordering, or non-adjacent split characters)
576
* - all characters lie on the same baseline, and it is either horizontal or vertical
577
* - the charinfo is in uniform ltr or rtl order (visual order), since local reorderings and split characters are removed
578
*
579
* The algorithm works in the following way:
580
* 1) we scan the glyphs ltr or rtl based on the bidi run direction
581
* 2) we can work in place, since we always consume a glyph for each char we write
582
* a) if the line is ltr, we start writing at position 0 until we finish, there may be leftver space
583
* b) if the line is rtl and 1-1, we start writing at position numChars/glyphs - 1 until we finish at 0
584
* c) otherwise if we don't finish at 0, we have to copy the data down
585
* 3) we consume clusters in the following way:
586
* a) the first element is always consumed
587
* b) subsequent elements are consumed if:
588
* i) their advance is zero
589
* ii) their character index <= the character index of any character seen in this cluster
590
* iii) the minimum character index seen in this cluster isn't adjacent to the previous cluster
591
* c) character data is written as follows for horizontal lines (x/y and w/h are exchanged on vertical lines)
592
* i) the x position is the position of the leftmost glyph whose advance is not zero
593
* ii)the y position is the baseline
594
* iii) the x advance is the distance to the maximum x + adv of all glyphs whose advance is not zero
595
* iv) the y advance is the baseline
596
* v) vis x,y,w,h tightly encloses the vis x,y,w,h of all the glyphs with nonzero w and h
597
* 4) we can make some simple optimizations if we know some things:
598
* a) if the mapping is 1-1, unidirectional, and there are no zero-adv glyphs, we just return the glyphinfo
599
* b) if the mapping is 1-1, unidirectional, we just adjust the remaining glyphs to originate at right/left of the base
600
* c) if the mapping is 1-1, we compute the base position and advance as we go, then go back to adjust the remaining glyphs
601
* d) otherwise we keep separate track of the write position as we do (c) since no glyph in the cluster may be in the
602
* position we are writing.
603
* e) most clusters are simply the single base glyph in the same position as its character, so we try to avoid
604
* copying its data unnecessarily.
605
* 5) the glyph vector ought to provide access to these 'global' attributes to enable these optimizations. A single
606
* int with flags set is probably ok, we could also provide accessors for each attribute. This doesn't map to
607
* the GlyphMetrics flags very well, so I won't attempt to keep them similar. It might be useful to add those
608
* in addition to these.
609
* int FLAG_HAS_ZERO_ADVANCE_GLYPHS = 1; // set if there are zero-advance glyphs
610
* int FLAG_HAS_NONUNIFORM_ORDER = 2; // set if some glyphs are rearranged out of character visual order
611
* int FLAG_HAS_SPLIT_CHARACTERS = 4; // set if multiple glyphs per character
612
* int getDescriptionFlags(); // return an int containing the above flags
613
* boolean hasZeroAdvanceGlyphs();
614
* boolean hasNonuniformOrder();
615
* boolean hasSplitCharacters();
616
* The optimized cases in (4) correspond to values 0, 1, 3, and 7 returned by getDescriptionFlags().
617
*/
618
protected float[] createCharinfo() {
619
StandardGlyphVector gv = getGV();
620
float[] glyphinfo = null;
621
try {
622
glyphinfo = gv.getGlyphInfo();
623
}
624
catch (Exception e) {
625
System.out.println(source);
626
}
627
628
/*
629
if ((gv.getDescriptionFlags() & 0x7) == 0) {
630
return glyphinfo;
631
}
632
*/
633
634
int numGlyphs = gv.getNumGlyphs();
635
if (numGlyphs == 0) {
636
return glyphinfo;
637
}
638
int[] indices = gv.getGlyphCharIndices(0, numGlyphs, null);
639
640
boolean DEBUG = false;
641
if (DEBUG) {
642
System.err.println("number of glyphs: " + numGlyphs);
643
for (int i = 0; i < numGlyphs; ++i) {
644
System.err.println("g: " + i +
645
", x: " + glyphinfo[i*numvals+posx] +
646
", a: " + glyphinfo[i*numvals+advx] +
647
", n: " + indices[i]);
648
}
649
}
650
651
int minIndex = indices[0]; // smallest index seen this cluster
652
int maxIndex = minIndex; // largest index seen this cluster
653
int nextMin = 0; // expected smallest index for this cluster
654
int cp = 0; // character position
655
int cx = 0; // character index (logical)
656
int gp = 0; // glyph position
657
int gx = 0; // glyph index (visual)
658
int gxlimit = numGlyphs; // limit of gx, when we reach this we're done
659
int pdelta = numvals; // delta for incrementing positions
660
int xdelta = 1; // delta for incrementing indices
661
662
boolean ltr = (source.getLayoutFlags() & 0x1) == 0;
663
if (!ltr) {
664
minIndex = indices[numGlyphs - 1];
665
maxIndex = minIndex;
666
nextMin = 0; // still logical
667
cp = glyphinfo.length - numvals;
668
cx = 0; // still logical
669
gp = glyphinfo.length - numvals;
670
gx = numGlyphs - 1;
671
gxlimit = -1;
672
pdelta = -numvals;
673
xdelta = -1;
674
}
675
676
/*
677
// to support vertical, use 'ixxxx' indices and swap horiz and vertical components
678
if (source.isVertical()) {
679
iposx = posy;
680
iposy = posx;
681
iadvx = advy;
682
iadvy = advx;
683
ivisx = visy;
684
ivisy = visx;
685
ivish = visw;
686
ivisw = vish;
687
} else {
688
// use standard values
689
}
690
*/
691
692
// use intermediates to reduce array access when we need to
693
float cposl = 0, cposr = 0, cvisl = 0, cvist = 0, cvisr = 0, cvisb = 0;
694
float baseline = 0;
695
696
// record if we have to copy data even when no cluster
697
boolean mustCopy = false;
698
699
while (gx != gxlimit) {
700
// start of new cluster
701
boolean haveCopy = false;
702
int clusterExtraGlyphs = 0;
703
704
minIndex = indices[gx];
705
maxIndex = minIndex;
706
707
// advance to next glyph
708
gx += xdelta;
709
gp += pdelta;
710
711
/*
712
while (gx != gxlimit && (glyphinfo[gp + advx] == 0 ||
713
minIndex != nextMin || indices[gx] <= maxIndex)) {
714
*/
715
while (gx != gxlimit &&
716
((glyphinfo[gp + advx] == 0) ||
717
(minIndex != nextMin) ||
718
(indices[gx] <= maxIndex) ||
719
(maxIndex - minIndex > clusterExtraGlyphs))) {
720
// initialize base data first time through, using base glyph
721
if (!haveCopy) {
722
int gps = gp - pdelta;
723
724
cposl = glyphinfo[gps + posx];
725
cposr = cposl + glyphinfo[gps + advx];
726
cvisl = glyphinfo[gps + visx];
727
cvist = glyphinfo[gps + visy];
728
cvisr = cvisl + glyphinfo[gps + visw];
729
cvisb = cvist + glyphinfo[gps + vish];
730
731
haveCopy = true;
732
}
733
734
// have an extra glyph in this cluster
735
++clusterExtraGlyphs;
736
737
// adjust advance only if new glyph has non-zero advance
738
float radvx = glyphinfo[gp + advx];
739
if (radvx != 0) {
740
float rposx = glyphinfo[gp + posx];
741
cposl = Math.min(cposl, rposx);
742
cposr = Math.max(cposr, rposx + radvx);
743
}
744
745
// adjust visible bounds only if new glyph has non-empty bounds
746
float rvisw = glyphinfo[gp + visw];
747
if (rvisw != 0) {
748
float rvisx = glyphinfo[gp + visx];
749
float rvisy = glyphinfo[gp + visy];
750
cvisl = Math.min(cvisl, rvisx);
751
cvist = Math.min(cvist, rvisy);
752
cvisr = Math.max(cvisr, rvisx + rvisw);
753
cvisb = Math.max(cvisb, rvisy + glyphinfo[gp + vish]);
754
}
755
756
// adjust min, max index
757
minIndex = Math.min(minIndex, indices[gx]);
758
maxIndex = Math.max(maxIndex, indices[gx]);
759
760
// get ready to examine next glyph
761
gx += xdelta;
762
gp += pdelta;
763
}
764
// done with cluster, gx and gp are set for next glyph
765
766
if (DEBUG) {
767
System.out.println("minIndex = " + minIndex + ", maxIndex = " + maxIndex);
768
}
769
770
nextMin = maxIndex + 1;
771
772
// do common character adjustments
773
glyphinfo[cp + posy] = baseline;
774
glyphinfo[cp + advy] = 0;
775
776
if (haveCopy) {
777
// save adjustments to the base character
778
glyphinfo[cp + posx] = cposl;
779
glyphinfo[cp + advx] = cposr - cposl;
780
glyphinfo[cp + visx] = cvisl;
781
glyphinfo[cp + visy] = cvist;
782
glyphinfo[cp + visw] = cvisr - cvisl;
783
glyphinfo[cp + vish] = cvisb - cvist;
784
785
// compare number of chars read with number of glyphs read.
786
// if more glyphs than chars, set mustCopy to true, as we'll always have
787
// to copy the data from here on out.
788
if (maxIndex - minIndex < clusterExtraGlyphs) {
789
mustCopy = true;
790
}
791
792
// Fix the characters that follow the base character.
793
// New values are all the same. Note we fix the number of characters
794
// we saw, not the number of glyphs we saw.
795
if (minIndex < maxIndex) {
796
if (!ltr) {
797
// if rtl, characters to left of base, else to right. reuse cposr.
798
cposr = cposl;
799
}
800
cvisr -= cvisl; // reuse, convert to deltas.
801
cvisb -= cvist;
802
803
int iMinIndex = minIndex, icp = cp / 8;
804
805
while (minIndex < maxIndex) {
806
++minIndex;
807
cx += xdelta;
808
cp += pdelta;
809
810
if (cp < 0 || cp >= glyphinfo.length) {
811
if (DEBUG) System.out.println("minIndex = " + iMinIndex + ", maxIndex = " + maxIndex + ", cp = " + icp);
812
}
813
814
glyphinfo[cp + posx] = cposr;
815
glyphinfo[cp + posy] = baseline;
816
glyphinfo[cp + advx] = 0;
817
glyphinfo[cp + advy] = 0;
818
glyphinfo[cp + visx] = cvisl;
819
glyphinfo[cp + visy] = cvist;
820
glyphinfo[cp + visw] = cvisr;
821
glyphinfo[cp + vish] = cvisb;
822
}
823
}
824
825
// no longer using this copy
826
haveCopy = false;
827
} else if (mustCopy) {
828
// out of synch, so we have to copy all the time now
829
int gpr = gp - pdelta;
830
831
glyphinfo[cp + posx] = glyphinfo[gpr + posx];
832
glyphinfo[cp + advx] = glyphinfo[gpr + advx];
833
glyphinfo[cp + visx] = glyphinfo[gpr + visx];
834
glyphinfo[cp + visy] = glyphinfo[gpr + visy];
835
glyphinfo[cp + visw] = glyphinfo[gpr + visw];
836
glyphinfo[cp + vish] = glyphinfo[gpr + vish];
837
}
838
// else glyphinfo is already at the correct character position, and is unchanged, so just leave it
839
840
// reset for new cluster
841
cp += pdelta;
842
cx += xdelta;
843
}
844
845
if (mustCopy && !ltr) {
846
// data written to wrong end of array, need to shift down
847
848
cp -= pdelta; // undo last increment, get start of valid character data in array
849
System.arraycopy(glyphinfo, cp, glyphinfo, 0, glyphinfo.length - cp);
850
}
851
852
if (DEBUG) {
853
char[] chars = source.getChars();
854
int start = source.getStart();
855
int length = source.getLength();
856
System.out.println("char info for " + length + " characters");
857
for(int i = 0; i < length * numvals;) {
858
System.out.println(" ch: " + Integer.toHexString(chars[start + v2l(i / numvals)]) +
859
" x: " + glyphinfo[i++] +
860
" y: " + glyphinfo[i++] +
861
" xa: " + glyphinfo[i++] +
862
" ya: " + glyphinfo[i++] +
863
" l: " + glyphinfo[i++] +
864
" t: " + glyphinfo[i++] +
865
" w: " + glyphinfo[i++] +
866
" h: " + glyphinfo[i++]);
867
}
868
}
869
870
return glyphinfo;
871
}
872
873
/**
874
* Map logical character index to visual character index.
875
* <p>
876
* This ignores hindi reordering. @see createCharinfo
877
*/
878
protected int l2v(int index) {
879
return (source.getLayoutFlags() & 0x1) == 0 ? index : source.getLength() - 1 - index;
880
}
881
882
/**
883
* Map visual character index to logical character index.
884
* <p>
885
* This ignores hindi reordering. @see createCharinfo
886
*/
887
protected int v2l(int index) {
888
return (source.getLayoutFlags() & 0x1) == 0 ? index : source.getLength() - 1 - index;
889
}
890
891
public TextLineComponent getSubset(int start, int limit, int dir) {
892
return new ExtendedTextSourceLabel(source.getSubSource(start, limit-start, dir), decorator);
893
}
894
895
public String toString() {
896
if (true) {
897
return source.toString(source.WITHOUT_CONTEXT);
898
}
899
StringBuffer buf = new StringBuffer();
900
buf.append(super.toString());
901
buf.append("[source:");
902
buf.append(source.toString(source.WITHOUT_CONTEXT));
903
buf.append(", lb:");
904
buf.append(lb);
905
buf.append(", ab:");
906
buf.append(ab);
907
buf.append(", vb:");
908
buf.append(vb);
909
buf.append(", gv:");
910
buf.append(gv);
911
buf.append(", ci: ");
912
if (charinfo == null) {
913
buf.append("null");
914
} else {
915
buf.append(charinfo[0]);
916
for (int i = 1; i < charinfo.length;) {
917
buf.append(i % numvals == 0 ? "; " : ", ");
918
buf.append(charinfo[i]);
919
}
920
}
921
buf.append("]");
922
923
return buf.toString();
924
}
925
926
//public static ExtendedTextLabel create(TextSource source) {
927
// return new ExtendedTextSourceLabel(source);
928
//}
929
930
public int getNumJustificationInfos() {
931
return getGV().getNumGlyphs();
932
}
933
934
935
public void getJustificationInfos(GlyphJustificationInfo[] infos, int infoStart, int charStart, int charLimit) {
936
// This simple implementation only uses spaces for justification.
937
// Since regular characters aren't justified, we don't need to deal with
938
// special infos for combining marks or ligature substitution glyphs.
939
// added character justification for kanjii only 2/22/98
940
941
StandardGlyphVector gv = getGV();
942
943
float[] charinfo = getCharinfo();
944
945
float size = gv.getFont().getSize2D();
946
947
GlyphJustificationInfo nullInfo =
948
new GlyphJustificationInfo(0,
949
false, GlyphJustificationInfo.PRIORITY_NONE, 0, 0,
950
false, GlyphJustificationInfo.PRIORITY_NONE, 0, 0);
951
952
GlyphJustificationInfo spaceInfo =
953
new GlyphJustificationInfo(size,
954
true, GlyphJustificationInfo.PRIORITY_WHITESPACE, 0, size,
955
true, GlyphJustificationInfo.PRIORITY_WHITESPACE, 0, size / 4f);
956
957
GlyphJustificationInfo kanjiInfo =
958
new GlyphJustificationInfo(size,
959
true, GlyphJustificationInfo.PRIORITY_INTERCHAR, size, size,
960
false, GlyphJustificationInfo.PRIORITY_NONE, 0, 0);
961
962
char[] chars = source.getChars();
963
int offset = source.getStart();
964
965
// assume data is 1-1 and either all rtl or all ltr, for now
966
967
int numGlyphs = gv.getNumGlyphs();
968
int minGlyph = 0;
969
int maxGlyph = numGlyphs;
970
boolean ltr = (source.getLayoutFlags() & 0x1) == 0;
971
if (charStart != 0 || charLimit != source.getLength()) {
972
if (ltr) {
973
minGlyph = charStart;
974
maxGlyph = charLimit;
975
} else {
976
minGlyph = numGlyphs - charLimit;
977
maxGlyph = numGlyphs - charStart;
978
}
979
}
980
981
for (int i = 0; i < numGlyphs; ++i) {
982
GlyphJustificationInfo info = null;
983
if (i >= minGlyph && i < maxGlyph) {
984
if (charinfo[i * numvals + advx] == 0) { // combining marks don't justify
985
info = nullInfo;
986
} else {
987
int ci = v2l(i); // 1-1 assumption again
988
char c = chars[offset + ci];
989
if (Character.isWhitespace(c)) {
990
info = spaceInfo;
991
// CJK, Hangul, CJK Compatibility areas
992
} else if (c >= 0x4e00 &&
993
(c < 0xa000) ||
994
(c >= 0xac00 && c < 0xd7b0) ||
995
(c >= 0xf900 && c < 0xfb00)) {
996
info = kanjiInfo;
997
} else {
998
info = nullInfo;
999
}
1000
}
1001
}
1002
infos[infoStart + i] = info;
1003
}
1004
}
1005
1006
public TextLineComponent applyJustificationDeltas(float[] deltas, int deltaStart, boolean[] flags) {
1007
1008
// when we justify, we need to adjust the charinfo since spaces
1009
// change their advances. preserve the existing charinfo.
1010
1011
float[] newCharinfo = (float[])getCharinfo().clone();
1012
1013
// we only push spaces, so never need to rejustify
1014
flags[0] = false;
1015
1016
// preserve the existing gv.
1017
1018
StandardGlyphVector newgv = (StandardGlyphVector)getGV().clone();
1019
float[] newPositions = newgv.getGlyphPositions(null);
1020
int numGlyphs = newgv.getNumGlyphs();
1021
1022
/*
1023
System.out.println("oldgv: " + getGV() + ", newgv: " + newgv);
1024
System.out.println("newpositions: " + newPositions);
1025
for (int i = 0; i < newPositions.length; i += 2) {
1026
System.out.println("[" + (i/2) + "] " + newPositions[i] + ", " + newPositions[i+1]);
1027
}
1028
1029
System.out.println("deltas: " + deltas + " start: " + deltaStart);
1030
for (int i = deltaStart; i < deltaStart + numGlyphs; i += 2) {
1031
System.out.println("[" + (i/2) + "] " + deltas[i] + ", " + deltas[i+1]);
1032
}
1033
*/
1034
1035
char[] chars = source.getChars();
1036
int offset = source.getStart();
1037
1038
// accumulate the deltas to adjust positions and advances.
1039
// handle whitespace by modifying advance,
1040
// handle everything else by modifying position before and after
1041
1042
float deltaPos = 0;
1043
for (int i = 0; i < numGlyphs; ++i) {
1044
if (Character.isWhitespace(chars[offset + v2l(i)])) {
1045
newPositions[i*2] += deltaPos;
1046
1047
float deltaAdv = deltas[deltaStart + i*2] + deltas[deltaStart + i*2 + 1];
1048
1049
newCharinfo[i * numvals + posx] += deltaPos;
1050
newCharinfo[i * numvals + visx] += deltaPos;
1051
newCharinfo[i * numvals + advx] += deltaAdv;
1052
1053
deltaPos += deltaAdv;
1054
} else {
1055
deltaPos += deltas[deltaStart + i*2];
1056
1057
newPositions[i*2] += deltaPos;
1058
newCharinfo[i * numvals + posx] += deltaPos;
1059
newCharinfo[i * numvals + visx] += deltaPos;
1060
1061
deltaPos += deltas[deltaStart + i*2 + 1];
1062
}
1063
}
1064
newPositions[numGlyphs * 2] += deltaPos;
1065
1066
newgv.setGlyphPositions(newPositions);
1067
1068
/*
1069
newPositions = newgv.getGlyphPositions(null);
1070
System.out.println(">> newpositions: " + newPositions);
1071
for (int i = 0; i < newPositions.length; i += 2) {
1072
System.out.println("[" + (i/2) + "] " + newPositions[i] + ", " + newPositions[i+1]);
1073
}
1074
*/
1075
1076
ExtendedTextSourceLabel result = new ExtendedTextSourceLabel(source, decorator);
1077
result.gv = newgv;
1078
result.charinfo = newCharinfo;
1079
1080
return result;
1081
}
1082
}
1083
1084