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/FontScaler.java
38829 views
1
/*
2
* Copyright (c) 2007, 2011, 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.geom.GeneralPath;
29
import java.awt.geom.Point2D;
30
import java.awt.geom.Rectangle2D;
31
import java.lang.ref.WeakReference;
32
import java.lang.reflect.Constructor;
33
34
import sun.java2d.Disposer;
35
import sun.java2d.DisposerRecord;
36
37
/* FontScaler is "internal interface" to font rasterizer library.
38
*
39
* Access to native rasterizers without going through this interface is
40
* strongly discouraged. In particular, this is important because native
41
* data could be disposed due to runtime font processing error at any time.
42
*
43
* FontScaler represents combination of particular rasterizer implementation
44
* and particular font. It does not include rasterization attributes such as
45
* transform. These attributes are part of native scalerContext object.
46
* This approach allows to share same scaler for different requests related
47
* to the same font file.
48
*
49
* Note that scaler may throw FontScalerException on any operation.
50
* Generally this means that runtime error had happened and scaler is not
51
* usable. Subsequent calls to this scaler should not cause crash but will
52
* likely cause exceptions to be thrown again.
53
*
54
* It is recommended that callee should replace its reference to the scaler
55
* with something else. For instance it could be FontManager.getNullScaler().
56
* Note that NullScaler is trivial and will not actually rasterize anything.
57
*
58
* Alternatively, callee can use more sophisticated error recovery strategies
59
* and for instance try to substitute failed scaler with new scaler instance
60
* using another font.
61
*
62
* Note that in case of error there is no need to call dispose(). Moreover,
63
* dispose() generally is called by Disposer thread and explicit calls to
64
* dispose might have unexpected sideeffects because scaler can be shared.
65
*
66
* Current disposing logic is the following:
67
* - scaler is registered in the Disposer by the FontManager (on creation)
68
* - scalers are disposed when associated Font2D object (e.g. TruetypeFont)
69
* is garbage collected. That's why this object implements DisposerRecord
70
* interface directly (as it is not used as indicator when it is safe
71
* to release native state) and that's why we have to use WeakReference
72
* to Font internally.
73
* - Majority of Font2D objects are linked from various mapping arrays
74
* (e.g. FontManager.localeFullNamesToFont). So, they are not collected.
75
* This logic only works for fonts created with Font.createFont()
76
*
77
* Notes:
78
* - Eventually we may consider releasing some of the scaler resources if
79
* it was not used for a while but we do not want to be too aggressive on
80
* this (and this is probably more important for Type1 fonts).
81
*/
82
public abstract class FontScaler implements DisposerRecord {
83
84
private static FontScaler nullScaler = null;
85
private static Constructor<FontScaler> scalerConstructor = null;
86
87
//Find preferred font scaler
88
//
89
//NB: we can allow property based preferences
90
// (theoretically logic can be font type specific)
91
static {
92
Class scalerClass = null;
93
Class arglst[] = new Class[] {Font2D.class, int.class,
94
boolean.class, int.class};
95
96
try {
97
if (FontUtilities.isOpenJDK) {
98
scalerClass = Class.forName("sun.font.FreetypeFontScaler");
99
} else {
100
scalerClass = Class.forName("sun.font.T2KFontScaler");
101
}
102
} catch (ClassNotFoundException e) {
103
scalerClass = NullFontScaler.class;
104
}
105
106
//NB: rewrite using factory? constructor is ugly way
107
try {
108
scalerConstructor = scalerClass.getConstructor(arglst);
109
} catch (NoSuchMethodException e) {
110
//should not happen
111
}
112
}
113
114
/* This is the only place to instantiate new FontScaler.
115
* Therefore this is very convinient place to register
116
* scaler with Disposer as well as trigger deregistring bad font
117
* in case when scaler reports this.
118
*/
119
public static FontScaler getScaler(Font2D font,
120
int indexInCollection,
121
boolean supportsCJK,
122
int filesize) {
123
FontScaler scaler = null;
124
125
try {
126
Object args[] = new Object[] {font, indexInCollection,
127
supportsCJK, filesize};
128
scaler = scalerConstructor.newInstance(args);
129
Disposer.addObjectRecord(font, scaler);
130
} catch (Throwable e) {
131
scaler = nullScaler;
132
133
//if we can not instantiate scaler assume bad font
134
//NB: technically it could be also because of internal scaler
135
// error but here we are assuming scaler is ok.
136
FontManager fm = FontManagerFactory.getInstance();
137
fm.deRegisterBadFont(font);
138
}
139
return scaler;
140
}
141
142
/*
143
* At the moment it is harmless to create 2 null scalers so, technically,
144
* syncronized keyword is not needed.
145
*
146
* But it is safer to keep it to avoid subtle problems if we will be adding
147
* checks like whether scaler is null scaler.
148
*/
149
public static synchronized FontScaler getNullScaler() {
150
if (nullScaler == null) {
151
nullScaler = new NullFontScaler();
152
}
153
return nullScaler;
154
}
155
156
protected WeakReference<Font2D> font = null;
157
protected long nativeScaler = 0; //used by decendants
158
//that have native state
159
protected boolean disposed = false;
160
161
abstract StrikeMetrics getFontMetrics(long pScalerContext)
162
throws FontScalerException;
163
164
abstract float getGlyphAdvance(long pScalerContext, int glyphCode)
165
throws FontScalerException;
166
167
abstract void getGlyphMetrics(long pScalerContext, int glyphCode,
168
Point2D.Float metrics)
169
throws FontScalerException;
170
171
/*
172
* Returns pointer to native GlyphInfo object.
173
* Callee is responsible for freeing this memory.
174
*
175
* Note:
176
* currently this method has to return not 0L but pointer to valid
177
* GlyphInfo object. Because Strike and drawing releated logic does
178
* expect that.
179
* In the future we may want to rework this to allow 0L here.
180
*/
181
abstract long getGlyphImage(long pScalerContext, int glyphCode)
182
throws FontScalerException;
183
184
abstract Rectangle2D.Float getGlyphOutlineBounds(long pContext,
185
int glyphCode)
186
throws FontScalerException;
187
188
abstract GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,
189
float x, float y)
190
throws FontScalerException;
191
192
abstract GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs,
193
int numGlyphs, float x, float y)
194
throws FontScalerException;
195
196
/* Used by Java2D disposer to ensure native resources are released.
197
Note: this method does not release any of created
198
scaler context objects! */
199
public void dispose() {}
200
201
/**
202
* Used when the native resources held by the scaler need
203
* to be released before the 2D disposer runs.
204
*/
205
public void disposeScaler() {}
206
207
/* At the moment these 3 methods are needed for Type1 fonts only.
208
* For Truetype fonts we extract required info outside of scaler
209
* on java layer.
210
*/
211
abstract int getNumGlyphs() throws FontScalerException;
212
abstract int getMissingGlyphCode() throws FontScalerException;
213
abstract int getGlyphCode(char charCode) throws FontScalerException;
214
215
/* This method returns table cache used by native layout engine.
216
* This cache is essentially just small collection of
217
* pointers to various truetype tables. See definition of TTLayoutTableCache
218
* in the fontscalerdefs.h for more details.
219
*
220
* Note that tables themselves have same format as defined in the truetype
221
* specification, i.e. font scaler do not need to perform any preprocessing.
222
*
223
* Probably it is better to have API to request pointers to each table
224
* separately instead of requesting pointer to some native structure.
225
* (then there is not need to share its definition by different
226
* implementations of scaler).
227
* However, this means multiple JNI calls and potential impact on performance.
228
*
229
* Note: return value 0 is legal.
230
* This means tables are not available (e.g. type1 font).
231
*/
232
abstract long getLayoutTableCache() throws FontScalerException;
233
234
/* Used by the OpenType engine for mark positioning. */
235
abstract Point2D.Float getGlyphPoint(long pScalerContext,
236
int glyphCode, int ptNumber)
237
throws FontScalerException;
238
239
abstract long getUnitsPerEm();
240
241
/* Returns pointer to native structure describing rasterization attributes.
242
Format of this structure is scaler-specific.
243
244
Callee is responsible for freeing scaler context (using free()).
245
246
Note:
247
Context is tightly associated with strike and it is actually
248
freed when corresponding strike is being released.
249
*/
250
abstract long createScalerContext(double[] matrix,
251
int aa, int fm,
252
float boldness, float italic,
253
boolean disableHinting);
254
255
/* Marks context as invalid because native scaler is invalid.
256
Notes:
257
- pointer itself is still valid and has to be released
258
- if pointer to native scaler was cached it
259
should not be neither disposed nor used.
260
it is very likely it is already disposed by this moment. */
261
abstract void invalidateScalerContext(long ppScalerContext);
262
}
263
264