Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/font/CFontManager.java
41153 views
1
/*
2
* Copyright (c) 2011, 2021, 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.*;
29
import java.io.File;
30
import java.security.AccessController;
31
import java.security.PrivilegedAction;
32
import java.util.ArrayList;
33
import java.util.HashMap;
34
import java.util.Hashtable;
35
import java.util.Locale;
36
import java.util.TreeMap;
37
import java.util.Vector;
38
39
import javax.swing.plaf.FontUIResource;
40
41
import sun.awt.FontConfiguration;
42
import sun.awt.HeadlessToolkit;
43
import sun.awt.util.ThreadGroupUtils;
44
import sun.lwawt.macosx.*;
45
46
public final class CFontManager extends SunFontManager {
47
private static Hashtable<String, Font2D> genericFonts = new Hashtable<String, Font2D>();
48
49
@Override
50
protected FontConfiguration createFontConfiguration() {
51
FontConfiguration fc = new CFontConfiguration(this);
52
fc.init();
53
return fc;
54
}
55
56
@Override
57
public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,
58
boolean preferPropFonts)
59
{
60
return new CFontConfiguration(this, preferLocaleFonts, preferPropFonts);
61
}
62
63
/*
64
* Returns an array of two strings. The first element is the
65
* name of the font. The second element is the file name.
66
*/
67
@Override
68
protected String[] getDefaultPlatformFont() {
69
return new String[]{"Lucida Grande",
70
"/System/Library/Fonts/LucidaGrande.ttc"};
71
}
72
73
// This is a way to register any kind of Font2D, not just files and composites.
74
public static Font2D[] getGenericFonts() {
75
return genericFonts.values().toArray(new Font2D[0]);
76
}
77
78
public Font2D registerGenericFont(Font2D f)
79
{
80
return registerGenericFont(f, false);
81
}
82
public Font2D registerGenericFont(Font2D f, boolean logicalFont)
83
{
84
int rank = 4;
85
86
String fontName = f.fullName;
87
String familyName = f.familyName;
88
89
if (fontName == null || fontName.isEmpty()) {
90
return null;
91
}
92
93
// logical fonts always need to be added to the family
94
// plus they never need to be added to the generic font list
95
// or the fullNameToFont table since they are covers for
96
// already existing fonts in this list
97
if (logicalFont || !genericFonts.containsKey(fontName)) {
98
if (FontUtilities.debugFonts()) {
99
FontUtilities.logInfo("Add to Family " + familyName +
100
", Font " + fontName + " rank=" + rank);
101
}
102
FontFamily family = FontFamily.getFamily(familyName);
103
if (family == null) {
104
family = new FontFamily(familyName, false, rank);
105
family.setFont(f, f.style);
106
} else if (family.getRank() >= rank) {
107
family.setFont(f, f.style);
108
}
109
if (!logicalFont)
110
{
111
genericFonts.put(fontName, f);
112
fullNameToFont.put(fontName.toLowerCase(Locale.ENGLISH), f);
113
}
114
return f;
115
} else {
116
return genericFonts.get(fontName);
117
}
118
}
119
120
@Override
121
public Font2D[] getRegisteredFonts() {
122
Font2D[] regFonts = super.getRegisteredFonts();
123
124
// Add in the Mac OS X native fonts
125
Font2D[] genericFonts = getGenericFonts();
126
Font2D[] allFonts = new Font2D[regFonts.length+genericFonts.length];
127
System.arraycopy(regFonts, 0, allFonts, 0, regFonts.length);
128
System.arraycopy(genericFonts, 0, allFonts, regFonts.length, genericFonts.length);
129
130
return allFonts;
131
}
132
133
@Override
134
protected void addNativeFontFamilyNames(TreeMap<String, String> familyNames, Locale requestedLocale) {
135
Font2D[] genericfonts = getGenericFonts();
136
for (int i=0; i < genericfonts.length; i++) {
137
if (!(genericfonts[i] instanceof NativeFont)) {
138
String name = genericfonts[i].getFamilyName(requestedLocale);
139
familyNames.put(name.toLowerCase(requestedLocale), name);
140
}
141
}
142
}
143
144
protected void registerFontsInDir(final String dirName, boolean useJavaRasterizer,
145
int fontRank, boolean defer, boolean resolveSymLinks) {
146
147
@SuppressWarnings("removal")
148
String[] files = AccessController.doPrivileged((PrivilegedAction<String[]>) () -> {
149
return new File(dirName).list(getTrueTypeFilter());
150
});
151
152
if (files == null) {
153
return;
154
} else {
155
for (String f : files) {
156
loadNativeDirFonts(dirName+File.separator+f);
157
}
158
}
159
super.registerFontsInDir(dirName, useJavaRasterizer, fontRank, defer, resolveSymLinks);
160
}
161
162
private native void loadNativeDirFonts(String fontPath);
163
private native void loadNativeFonts();
164
165
void registerFont(String fontName, String fontFamilyName) {
166
final CFont font = new CFont(fontName, fontFamilyName);
167
168
registerGenericFont(font);
169
}
170
171
void registerItalicDerived() {
172
FontFamily[] famArr = FontFamily.getAllFontFamilies();
173
for (int i=0; i<famArr.length; i++) {
174
FontFamily family = famArr[i];
175
176
Font2D f2dPlain = family.getFont(Font.PLAIN);
177
if (f2dPlain != null && !(f2dPlain instanceof CFont)) continue;
178
Font2D f2dBold = family.getFont(Font.BOLD);
179
if (f2dBold != null && !(f2dBold instanceof CFont)) continue;
180
Font2D f2dItalic = family.getFont(Font.ITALIC);
181
if (f2dItalic != null && !(f2dItalic instanceof CFont)) continue;
182
Font2D f2dBoldItalic = family.getFont(Font.BOLD|Font.ITALIC);
183
if (f2dBoldItalic != null && !(f2dBoldItalic instanceof CFont)) continue;
184
185
CFont plain = (CFont)f2dPlain;
186
CFont bold = (CFont)f2dBold;
187
CFont italic = (CFont)f2dItalic;
188
CFont boldItalic = (CFont)f2dBoldItalic;
189
190
if (bold == null) bold = plain;
191
if (plain == null && bold == null) continue;
192
if (italic != null && boldItalic != null) continue;
193
if (plain != null && italic == null) {
194
registerGenericFont(plain.createItalicVariant(), true);
195
}
196
if (bold != null && boldItalic == null) {
197
registerGenericFont(bold.createItalicVariant(), true);
198
}
199
}
200
}
201
202
Object waitForFontsToBeLoaded = new Object();
203
private boolean loadedAllFonts = false;
204
205
@SuppressWarnings("removal")
206
public void loadFonts()
207
{
208
synchronized(waitForFontsToBeLoaded)
209
{
210
super.loadFonts();
211
java.security.AccessController.doPrivileged(
212
new java.security.PrivilegedAction<Object>() {
213
public Object run() {
214
if (!loadedAllFonts) {
215
loadNativeFonts();
216
registerItalicDerived();
217
loadedAllFonts = true;
218
}
219
return null;
220
}
221
}
222
);
223
224
String defaultFont = "Lucida Grande";
225
String defaultFallback = "Lucida Grande";
226
227
setupLogicalFonts("Dialog", defaultFont, defaultFallback);
228
setupLogicalFonts("Serif", "Times", "Times");
229
setupLogicalFonts("SansSerif", defaultFont, defaultFallback);
230
setupLogicalFonts("Monospaced", "Menlo", "Courier");
231
setupLogicalFonts("DialogInput", defaultFont, defaultFallback);
232
}
233
}
234
235
protected void setupLogicalFonts(String logicalName, String realName, String fallbackName) {
236
FontFamily realFamily = getFontFamilyWithExtraTry(logicalName, realName, fallbackName);
237
238
cloneStyledFont(realFamily, logicalName, Font.PLAIN);
239
cloneStyledFont(realFamily, logicalName, Font.BOLD);
240
cloneStyledFont(realFamily, logicalName, Font.ITALIC);
241
cloneStyledFont(realFamily, logicalName, Font.BOLD | Font.ITALIC);
242
}
243
244
protected FontFamily getFontFamilyWithExtraTry(String logicalName, String realName, String fallbackName){
245
FontFamily family = getFontFamily(realName, fallbackName);
246
if (family != null) return family;
247
248
// at this point, we recognize that we probably needed a fallback font
249
super.loadFonts();
250
251
family = getFontFamily(realName, fallbackName);
252
if (family != null) return family;
253
254
System.err.println("Warning: the fonts \"" + realName + "\" and \"" + fallbackName + "\" are not available for the Java logical font \"" + logicalName + "\", which may have unexpected appearance or behavior. Re-enable the \""+ realName +"\" font to remove this warning.");
255
return null;
256
}
257
258
protected FontFamily getFontFamily(String realName, String fallbackName){
259
FontFamily family = FontFamily.getFamily(realName);
260
if (family != null) return family;
261
262
family = FontFamily.getFamily(fallbackName);
263
if (family != null){
264
System.err.println("Warning: the font \"" + realName + "\" is not available, so \"" + fallbackName + "\" has been substituted, but may have unexpected appearance or behavor. Re-enable the \""+ realName +"\" font to remove this warning.");
265
return family;
266
}
267
268
return null;
269
}
270
271
protected boolean cloneStyledFont(FontFamily realFamily, String logicalFamilyName, int style) {
272
if (realFamily == null) return false;
273
274
Font2D realFont = realFamily.getFontWithExactStyleMatch(style);
275
if (realFont == null || !(realFont instanceof CFont)) return false;
276
277
CFont newFont = new CFont((CFont)realFont, logicalFamilyName);
278
registerGenericFont(newFont, true);
279
280
return true;
281
}
282
283
@Override
284
public String getFontPath(boolean noType1Fonts) {
285
// In the case of the Cocoa toolkit, since we go through NSFont, we don't need to register /Library/Fonts
286
Toolkit tk = Toolkit.getDefaultToolkit();
287
if (tk instanceof HeadlessToolkit) {
288
tk = ((HeadlessToolkit)tk).getUnderlyingToolkit();
289
}
290
if (tk instanceof LWCToolkit) {
291
return "";
292
}
293
294
// X11 case
295
return "/Library/Fonts";
296
}
297
298
@Override
299
protected FontUIResource getFontConfigFUIR(
300
String family, int style, int size)
301
{
302
String mappedName = FontUtilities.mapFcName(family);
303
if (mappedName == null) {
304
mappedName = "sansserif";
305
}
306
return new FontUIResource(mappedName, style, size);
307
}
308
309
// Only implemented on Windows
310
@Override
311
protected void populateFontFileNameMap(HashMap<String, String> fontToFileMap, HashMap<String, String> fontToFamilyNameMap,
312
HashMap<String, ArrayList<String>> familyToFontListMap, Locale locale) {}
313
}
314
315