Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/font/CFont.java
41153 views
1
/*
2
* Copyright (c) 2011, 2017, 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.font.FontRenderContext;
30
import java.awt.geom.AffineTransform;
31
import java.awt.geom.GeneralPath;
32
import java.awt.geom.Point2D;
33
import java.awt.geom.Rectangle2D;
34
import java.util.ArrayList;
35
36
// Right now this class is final to avoid a problem with native code.
37
// For some reason the JNI IsInstanceOf was not working correctly
38
// so we are checking the class specifically. If we subclass this
39
// we need to modify the native code in CFontWrapper.m
40
public final class CFont extends PhysicalFont implements FontSubstitution {
41
42
/* CFontStrike doesn't call these methods so they are unimplemented.
43
* They are here to meet the requirements of PhysicalFont, needed
44
* because a CFont can sometimes be returned where a PhysicalFont
45
* is expected.
46
*/
47
StrikeMetrics getFontMetrics(long pScalerContext) {
48
throw new InternalError("Not implemented");
49
}
50
51
float getGlyphAdvance(long pScalerContext, int glyphCode) {
52
throw new InternalError("Not implemented");
53
}
54
55
void getGlyphMetrics(long pScalerContext, int glyphCode,
56
Point2D.Float metrics) {
57
throw new InternalError("Not implemented");
58
}
59
60
long getGlyphImage(long pScalerContext, int glyphCode) {
61
throw new InternalError("Not implemented");
62
}
63
64
Rectangle2D.Float getGlyphOutlineBounds(long pScalerContext,
65
int glyphCode) {
66
throw new InternalError("Not implemented");
67
}
68
69
GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,
70
float x, float y) {
71
throw new InternalError("Not implemented");
72
}
73
74
GeneralPath getGlyphVectorOutline(long pScalerContext,
75
int[] glyphs, int numGlyphs,
76
float x, float y) {
77
throw new InternalError("Not implemented");
78
}
79
80
@Override
81
protected byte[] getTableBytes(int tag) {
82
return getTableBytesNative(getNativeFontPtr(), tag);
83
}
84
85
private native byte[] getTableBytesNative(long nativeFontPtr, int tag);
86
87
private static native long createNativeFont(final String nativeFontName,
88
final int style);
89
private static native void disposeNativeFont(final long nativeFontPtr);
90
91
private boolean isFakeItalic;
92
private String nativeFontName;
93
private long nativeFontPtr;
94
95
private native float getWidthNative(final long nativeFontPtr);
96
private native float getWeightNative(final long nativeFontPtr);
97
98
private int fontWidth = -1;
99
private int fontWeight = -1;
100
101
@Override
102
public int getWidth() {
103
if (fontWidth == -1) {
104
// Apple use a range of -1 -> +1, where 0.0 is normal
105
// OpenType uses a % range from 50% -> 200% where 100% is normal
106
// and maps these onto the integer values 1->9.
107
// Since that is what Font2D.getWidth() expects, remap to that.
108
float fw = getWidthNative(getNativeFontPtr());
109
if (fw == 0.0) { // short cut the common case
110
fontWidth = Font2D.FWIDTH_NORMAL;
111
return fontWidth;
112
}
113
fw += 1.0; fw *= 100.0;
114
if (fw <= 50.0) {
115
fontWidth = 1;
116
} else if (fw <= 62.5) {
117
fontWidth = 2;
118
} else if (fw <= 75.0) {
119
fontWidth = 3;
120
} else if (fw <= 87.5) {
121
fontWidth = 4;
122
} else if (fw <= 100.0) {
123
fontWidth = 5;
124
} else if (fw <= 112.5) {
125
fontWidth = 6;
126
} else if (fw <= 125.0) {
127
fontWidth = 7;
128
} else if (fw <= 150.0) {
129
fontWidth = 8;
130
} else {
131
fontWidth = 9;
132
}
133
}
134
return fontWidth;
135
}
136
137
@Override
138
public int getWeight() {
139
if (fontWeight == -1) {
140
// Apple use a range of -1 -> +1, where 0 is medium/regular
141
// Map this on to the OpenType range of 100->900 where
142
// 500 is medium/regular.
143
// We'll actually map to 0->1000 but that's close enough.
144
float fw = getWeightNative(getNativeFontPtr());
145
if (fw == 0) {
146
return Font2D.FWEIGHT_NORMAL;
147
}
148
fw += 1.0; fw *= 500;
149
fontWeight = (int)fw;
150
}
151
return fontWeight;
152
}
153
154
// this constructor is called from CFontWrapper.m
155
public CFont(String name) {
156
this(name, name);
157
}
158
159
public CFont(String name, String inFamilyName) {
160
handle = new Font2DHandle(this);
161
fullName = name;
162
familyName = inFamilyName;
163
nativeFontName = fullName;
164
setStyle();
165
}
166
167
/* Called from CFontManager too */
168
public CFont(CFont other, String logicalFamilyName) {
169
handle = new Font2DHandle(this);
170
fullName = logicalFamilyName;
171
familyName = logicalFamilyName;
172
nativeFontName = other.nativeFontName;
173
style = other.style;
174
isFakeItalic = other.isFakeItalic;
175
}
176
177
public CFont createItalicVariant() {
178
CFont font = new CFont(this, familyName);
179
font.nativeFontName = fullName;
180
font.fullName =
181
fullName + (style == Font.BOLD ? "" : "-") + "Italic-Derived";
182
font.style |= Font.ITALIC;
183
font.isFakeItalic = true;
184
return font;
185
}
186
187
protected synchronized long getNativeFontPtr() {
188
if (nativeFontPtr == 0L) {
189
nativeFontPtr = createNativeFont(nativeFontName, style);
190
}
191
return nativeFontPtr;
192
}
193
194
private native long getCGFontPtrNative(long ptr);
195
196
// This digs the CGFont out of the AWTFont.
197
protected synchronized long getPlatformNativeFontPtr() {
198
return getCGFontPtrNative(getNativeFontPtr());
199
}
200
201
static native void getCascadeList(long nativeFontPtr, ArrayList<String> listOfString);
202
203
private CompositeFont createCompositeFont() {
204
ArrayList<String> listOfString = new ArrayList<String>();
205
getCascadeList(nativeFontPtr, listOfString);
206
207
// In some italic cases the standard Mac cascade list is missing Arabic.
208
listOfString.add("GeezaPro");
209
FontManager fm = FontManagerFactory.getInstance();
210
int numFonts = 1 + listOfString.size();
211
PhysicalFont[] fonts = new PhysicalFont[numFonts];
212
fonts[0] = this;
213
int idx = 1;
214
if (FontUtilities.isLogging()) {
215
FontUtilities.logInfo("Cascading list for " + this + " :");
216
}
217
for (String s : listOfString) {
218
if (FontUtilities.isLogging()) {
219
FontUtilities.logInfo("Fallback:" + s);
220
}
221
if (s.equals(".AppleSymbolsFB")) {
222
// Don't know why we get the weird name above .. replace.
223
s = "AppleSymbols";
224
}
225
Font2D f2d = fm.findFont2D(s, Font.PLAIN, FontManager.NO_FALLBACK);
226
if (f2d == null || f2d == this) {
227
continue;
228
}
229
fonts[idx++] = (PhysicalFont)f2d;
230
}
231
if (idx < fonts.length) {
232
PhysicalFont[] orig = fonts;
233
fonts = new PhysicalFont[idx];
234
System.arraycopy(orig, 0, fonts, 0, idx);
235
}
236
CompositeFont compFont = new CompositeFont(fonts);
237
compFont.mapper = new CCompositeGlyphMapper(compFont);
238
return compFont;
239
}
240
241
private CompositeFont compFont;
242
243
public CompositeFont getCompositeFont2D() {
244
if (compFont == null) {
245
compFont = createCompositeFont();
246
}
247
return compFont;
248
}
249
250
@SuppressWarnings("deprecation")
251
protected synchronized void finalize() {
252
if (nativeFontPtr != 0) {
253
disposeNativeFont(nativeFontPtr);
254
}
255
nativeFontPtr = 0;
256
}
257
258
protected CharToGlyphMapper getMapper() {
259
if (mapper == null) {
260
mapper = new CCharToGlyphMapper(this);
261
}
262
return mapper;
263
}
264
265
protected FontStrike createStrike(FontStrikeDesc desc) {
266
if (isFakeItalic) {
267
desc = new FontStrikeDesc(desc);
268
desc.glyphTx.concatenate(AffineTransform.getShearInstance(-0.2, 0));
269
}
270
return new CStrike(this, desc);
271
}
272
273
// <rdar://problem/5321707> sun.font.Font2D caches the last used strike,
274
// but does not check if the properties of the strike match the properties
275
// of the incoming java.awt.Font object (size, style, etc).
276
// Simple answer: don't cache.
277
private static FontRenderContext DEFAULT_FRC =
278
new FontRenderContext(null, false, false);
279
public FontStrike getStrike(final Font font) {
280
return getStrike(font, DEFAULT_FRC);
281
}
282
283
public boolean equals(Object o) {
284
if (!super.equals(o)) {
285
return false;
286
}
287
288
return ((Font2D)o).getStyle() == this.getStyle();
289
}
290
291
public int hashCode() {
292
return super.hashCode() ^ this.getStyle();
293
}
294
295
public String toString() {
296
return "CFont { fullName: " + fullName +
297
", familyName: " + familyName + ", style: " + style +
298
" } aka: " + super.toString();
299
}
300
}
301
302