Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
240 lines (214 loc) · 7.82 KB

File metadata and controls

240 lines (214 loc) · 7.82 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
* (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
* (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
*
* The original version of this source code and documentation is
* copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
* of IBM. These materials are provided under terms of a License
* Agreement between Taligent and Sun. This technology is protected
* by multiple US and International patents.
*
* This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.awt.font;
/*
* one info for each side of each glyph
* separate infos for grow and shrink case
* !!! this doesn't really need to be a separate class. If we keep it
* separate, probably the newJustify code from TextLayout belongs here as well.
*/
class TextJustifier {
private GlyphJustificationInfo[] info;
private int start;
private int limit;
static boolean DEBUG = false;
/**
* Initialize the justifier with an array of infos corresponding to each
* glyph. Start and limit indicate the range of the array to examine.
*/
TextJustifier(GlyphJustificationInfo[] info, int start, int limit) {
this.info = info;
this.start = start;
this.limit = limit;
if (DEBUG) {
System.out.println("start: " + start + ", limit: " + limit);
for (int i = start; i < limit; i++) {
GlyphJustificationInfo gji = info[i];
System.out.println("w: " + gji.weight + ", gp: " +
gji.growPriority + ", gll: " +
gji.growLeftLimit + ", grl: " +
gji.growRightLimit);
}
}
}
public static final int MAX_PRIORITY = 3;
/**
* Return an array of deltas twice as long as the original info array,
* indicating the amount by which each side of each glyph should grow
* or shrink.
*
* Delta should be positive to expand the line, and negative to compress it.
*/
public float[] justify(float delta) {
float[] deltas = new float[info.length * 2];
boolean grow = delta > 0;
if (DEBUG)
System.out.println("delta: " + delta);
// make separate passes through glyphs in order of decreasing priority
// until justifyDelta is zero or we run out of priorities.
int fallbackPriority = -1;
for (int p = 0; delta != 0; p++) {
/*
* special case 'fallback' iteration, set flag and recheck
* highest priority
*/
boolean lastPass = p > MAX_PRIORITY;
if (lastPass)
p = fallbackPriority;
// pass through glyphs, first collecting weights and limits
float weight = 0;
float gslimit = 0;
float absorbweight = 0;
for (int i = start; i < limit; i++) {
GlyphJustificationInfo gi = info[i];
if ((grow ? gi.growPriority : gi.shrinkPriority) == p) {
if (fallbackPriority == -1) {
fallbackPriority = p;
}
if (i != start) { // ignore left of first character
weight += gi.weight;
if (grow) {
gslimit += gi.growLeftLimit;
if (gi.growAbsorb) {
absorbweight += gi.weight;
}
} else {
gslimit += gi.shrinkLeftLimit;
if (gi.shrinkAbsorb) {
absorbweight += gi.weight;
}
}
}
if (i + 1 != limit) { // ignore right of last character
weight += gi.weight;
if (grow) {
gslimit += gi.growRightLimit;
if (gi.growAbsorb) {
absorbweight += gi.weight;
}
} else {
gslimit += gi.shrinkRightLimit;
if (gi.shrinkAbsorb) {
absorbweight += gi.weight;
}
}
}
}
}
// did we hit the limit?
if (!grow) {
gslimit = -gslimit; // negative for negative deltas
}
boolean hitLimit = (weight == 0) || (!lastPass && ((delta < 0) == (delta < gslimit)));
boolean absorbing = hitLimit && absorbweight > 0;
// predivide delta by weight
float weightedDelta = delta / weight; // not used if weight == 0
float weightedAbsorb = 0;
if (hitLimit && absorbweight > 0) {
weightedAbsorb = (delta - gslimit) / absorbweight;
}
if (DEBUG) {
System.out.println("pass: " + p +
", d: " + delta +
", l: " + gslimit +
", w: " + weight +
", aw: " + absorbweight +
", wd: " + weightedDelta +
", wa: " + weightedAbsorb +
", hit: " + (hitLimit ? "y" : "n"));
}
// now allocate this based on ratio of weight to total weight
int n = start * 2;
for (int i = start; i < limit; i++) {
GlyphJustificationInfo gi = info[i];
if ((grow ? gi.growPriority : gi.shrinkPriority) == p) {
if (i != start) { // ignore left
float d;
if (hitLimit) {
// factor in sign
d = grow ? gi.growLeftLimit : -gi.shrinkLeftLimit;
if (absorbing) {
// sign factored in already
d += gi.weight * weightedAbsorb;
}
} else {
// sign factored in already
d = gi.weight * weightedDelta;
}
deltas[n] += d;
}
n++;
if (i + 1 != limit) { // ignore right
float d;
if (hitLimit) {
d = grow ? gi.growRightLimit : -gi.shrinkRightLimit;
if (absorbing) {
d += gi.weight * weightedAbsorb;
}
} else {
d = gi.weight * weightedDelta;
}
deltas[n] += d;
}
n++;
} else {
n += 2;
}
}
if (!lastPass && hitLimit && !absorbing) {
delta -= gslimit;
} else {
delta = 0; // stop iteration
}
}
if (DEBUG) {
float total = 0;
for (int i = 0; i < deltas.length; i++) {
total += deltas[i];
System.out.print(deltas[i] + ", ");
if (i % 20 == 9) {
System.out.println();
}
}
System.out.println("\ntotal: " + total);
System.out.println();
}
return deltas;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.