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/CompositeStrike.java
38829 views
1
/*
2
* Copyright (c) 2003, 2005, 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
package sun.font;
27
28
import java.awt.Font;
29
import java.awt.Rectangle;
30
import java.awt.geom.GeneralPath;
31
import java.awt.geom.Point2D;
32
import java.awt.geom.Rectangle2D;
33
34
/*
35
* performance:
36
* it seems expensive that when using a composite font for
37
* every char you have to find which "slot" can display it.
38
* Just the fact that you need to check at all ..
39
* A composite glyph code ducks this by encoding the slot into the
40
* glyph code, but you still need to get from char to glyph code.
41
*/
42
public final class CompositeStrike extends FontStrike {
43
44
static final int SLOTMASK = 0xffffff;
45
46
private CompositeFont compFont;
47
private PhysicalStrike[] strikes;
48
int numGlyphs = 0;
49
50
CompositeStrike(CompositeFont font2D, FontStrikeDesc desc) {
51
this.compFont = font2D;
52
this.desc = desc;
53
this.disposer = new FontStrikeDisposer(compFont, desc);
54
if (desc.style != compFont.style) {
55
algoStyle = true;
56
if ((desc.style & Font.BOLD) == Font.BOLD &&
57
((compFont.style & Font.BOLD) == 0)) {
58
boldness = 1.33f;
59
}
60
if ((desc.style & Font.ITALIC) == Font.ITALIC &&
61
(compFont.style & Font.ITALIC) == 0) {
62
italic = 0.7f;
63
}
64
}
65
strikes = new PhysicalStrike[compFont.numSlots];
66
}
67
68
/* do I need this (see Strike::compositeStrikeForGlyph) */
69
PhysicalStrike getStrikeForGlyph(int glyphCode) {
70
return getStrikeForSlot(glyphCode >>> 24);
71
}
72
73
PhysicalStrike getStrikeForSlot(int slot) {
74
75
if (slot >= strikes.length) {
76
slot = 0;
77
}
78
79
PhysicalStrike strike = strikes[slot];
80
if (strike == null) {
81
strike =
82
(PhysicalStrike)(compFont.getSlotFont(slot).getStrike(desc));
83
84
strikes[slot] = strike;
85
}
86
return strike;
87
}
88
89
public int getNumGlyphs() {
90
return compFont.getNumGlyphs();
91
}
92
93
StrikeMetrics getFontMetrics() {
94
if (strikeMetrics == null) {
95
StrikeMetrics compMetrics = new StrikeMetrics();
96
for (int s=0; s<compFont.numMetricsSlots; s++) {
97
compMetrics.merge(getStrikeForSlot(s).getFontMetrics());
98
}
99
strikeMetrics = compMetrics;
100
}
101
return strikeMetrics;
102
}
103
104
105
/* Performance tweak: Slot 0 can often return all the glyphs
106
* Note slot zero doesn't need to be masked.
107
* Could go a step further and support getting a run of glyphs.
108
* This would help many locales a little.
109
*
110
* Note that if a client constructs an invalid a composite glyph that
111
* references an invalid slot, that the behaviour is currently
112
* that this slot index falls through to CompositeFont.getSlotFont(int)
113
* which will substitute a default font, from which to obtain the
114
* strike. If its an invalid glyph code for a valid slot, then the
115
* physical font for that slot will substitute the missing glyph.
116
*/
117
void getGlyphImagePtrs(int[] glyphCodes, long[] images, int len) {
118
PhysicalStrike strike = getStrikeForSlot(0);
119
int numptrs = strike.getSlot0GlyphImagePtrs(glyphCodes, images, len);
120
if (numptrs == len) {
121
return;
122
}
123
for (int i=numptrs; i< len; i++) {
124
strike = getStrikeForGlyph(glyphCodes[i]);
125
images[i] = strike.getGlyphImagePtr(glyphCodes[i] & SLOTMASK);
126
}
127
}
128
129
130
long getGlyphImagePtr(int glyphCode) {
131
PhysicalStrike strike = getStrikeForGlyph(glyphCode);
132
return strike.getGlyphImagePtr(glyphCode & SLOTMASK);
133
}
134
135
void getGlyphImageBounds(int glyphCode, Point2D.Float pt, Rectangle result) {
136
PhysicalStrike strike = getStrikeForGlyph(glyphCode);
137
strike.getGlyphImageBounds(glyphCode & SLOTMASK, pt, result);
138
}
139
140
Point2D.Float getGlyphMetrics(int glyphCode) {
141
PhysicalStrike strike = getStrikeForGlyph(glyphCode);
142
return strike.getGlyphMetrics(glyphCode & SLOTMASK);
143
}
144
145
Point2D.Float getCharMetrics(char ch) {
146
return getGlyphMetrics(compFont.getMapper().charToGlyph(ch));
147
}
148
149
float getGlyphAdvance(int glyphCode) {
150
PhysicalStrike strike = getStrikeForGlyph(glyphCode);
151
return strike.getGlyphAdvance(glyphCode & SLOTMASK);
152
}
153
154
/* REMIND where to cache?
155
* The glyph advance is already cached by physical strikes and that's a lot
156
* of the work.
157
* Also FontDesignMetrics maintains a latin char advance cache, so don't
158
* cache advances here as apps tend to hold onto metrics objects when
159
* performance is sensitive to it. Revisit this assumption later.
160
*/
161
float getCodePointAdvance(int cp) {
162
return getGlyphAdvance(compFont.getMapper().charToGlyph(cp));
163
}
164
165
Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {
166
PhysicalStrike strike = getStrikeForGlyph(glyphCode);
167
return strike.getGlyphOutlineBounds(glyphCode & SLOTMASK);
168
}
169
170
GeneralPath getGlyphOutline(int glyphCode, float x, float y) {
171
172
PhysicalStrike strike = getStrikeForGlyph(glyphCode);
173
GeneralPath path = strike.getGlyphOutline(glyphCode & SLOTMASK, x, y);
174
if (path == null) {
175
return new GeneralPath();
176
} else {
177
return path;
178
}
179
}
180
181
/* The physical font slot for each glyph is encoded in the glyph ID
182
* To be as efficient as possible we find a run of glyphs from the
183
* same slot and create a temporary array of these glyphs decoded
184
* to the slot. The slot font is then queried for the GeneralPath
185
* for that run of glyphs. GeneralPaths from each run are appended
186
* to create the shape for the whole glyph array.
187
*/
188
GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {
189
GeneralPath path = null;
190
GeneralPath gp;
191
int glyphIndex = 0;
192
int[] tmpGlyphs;
193
194
while (glyphIndex < glyphs.length) {
195
int start = glyphIndex;
196
int slot = glyphs[glyphIndex] >>> 24;
197
while (glyphIndex < glyphs.length &&
198
(glyphs[glyphIndex+1] >>> 24) == slot) {
199
glyphIndex++;
200
}
201
int tmpLen = glyphIndex-start+1;
202
tmpGlyphs = new int[tmpLen];
203
for (int i=0;i<tmpLen;i++) {
204
tmpGlyphs[i] = glyphs[i] & SLOTMASK;
205
}
206
gp = getStrikeForSlot(slot).getGlyphVectorOutline(tmpGlyphs, x, y);
207
if (path == null) {
208
path = gp;
209
} else if (gp != null) {
210
path.append(gp, false);
211
}
212
}
213
if (path == null) {
214
return new GeneralPath();
215
} else {
216
return path;
217
}
218
}
219
}
220
221