-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
sunFont.c
346 lines (300 loc) · 12.7 KB
/
sunFont.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "stdlib.h"
#include "string.h"
#include "gdefs.h"
#include "jlong.h"
#include "jni_util.h"
#include "sunfontids.h"
#include "fontscalerdefs.h"
#include "sun_font_SunFontManager.h"
#include "sun_font_NullFontScaler.h"
#include "sun_font_StrikeCache.h"
static void *theNullScalerContext = NULL;
extern void AccelGlyphCache_RemoveAllCellInfos(GlyphInfo *glyph);
/*
* Declare library specific JNI_Onload entry if static build
*/
DEF_STATIC_JNI_OnLoad
JNIEXPORT jlong JNICALL
Java_sun_font_NullFontScaler_getNullScalerContext
(JNIEnv *env, jclass scalerClass) {
if (theNullScalerContext == NULL) {
theNullScalerContext = malloc(1);
}
return ptr_to_jlong(theNullScalerContext);
}
int isNullScalerContext(void *context) {
return theNullScalerContext == context;
}
/* Eventually we may rework it to be a singleton.
* This will require additional checks in freeLongMemory/freeIntMemory
* and on other hand malformed fonts (main source of null glyph images)
* are supposed to be collected fast.
* But perhaps it is still right thing to do.
* Even better is to eliminate the need to have this native method
* but for this it is necessary to rework Strike and drawing logic
* to be able to live with NULL pointers without performance hit.
*/
JNIEXPORT jlong JNICALL Java_sun_font_NullFontScaler_getGlyphImage
(JNIEnv *env, jobject scaler, jlong pContext, jint glyphCode) {
void *nullscaler = calloc(sizeof(GlyphInfo), 1);
return ptr_to_jlong(nullscaler);
}
void initLCDGammaTables();
/* placeholder for extern variable */
static int initialisedFontIDs = 0;
FontManagerNativeIDs sunFontIDs;
static void initFontIDs(JNIEnv *env) {
jclass tmpClass;
if (initialisedFontIDs) {
return;
}
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/TrueTypeFont"));
CHECK_NULL(sunFontIDs.ttReadBlockMID =
(*env)->GetMethodID(env, tmpClass, "readBlock",
"(Ljava/nio/ByteBuffer;II)I"));
CHECK_NULL(sunFontIDs.ttReadBytesMID =
(*env)->GetMethodID(env, tmpClass, "readBytes", "(II)[B"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/Type1Font"));
CHECK_NULL(sunFontIDs.readFileMID =
(*env)->GetMethodID(env, tmpClass,
"readFile", "(Ljava/nio/ByteBuffer;)V"));
CHECK_NULL(tmpClass =
(*env)->FindClass(env, "java/awt/geom/Point2D$Float"));
sunFontIDs.pt2DFloatClass = (jclass)(*env)->NewGlobalRef(env, tmpClass);
CHECK_NULL(sunFontIDs.pt2DFloatCtr =
(*env)->GetMethodID(env, sunFontIDs.pt2DFloatClass, "<init>","(FF)V"));
CHECK_NULL(sunFontIDs.xFID =
(*env)->GetFieldID(env, sunFontIDs.pt2DFloatClass, "x", "F"));
CHECK_NULL(sunFontIDs.yFID =
(*env)->GetFieldID(env, sunFontIDs.pt2DFloatClass, "y", "F"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/StrikeMetrics"));
CHECK_NULL(sunFontIDs.strikeMetricsClass =
(jclass)(*env)->NewGlobalRef(env, tmpClass));
CHECK_NULL(sunFontIDs.strikeMetricsCtr =
(*env)->GetMethodID(env, sunFontIDs.strikeMetricsClass,
"<init>", "(FFFFFFFFFF)V"));
CHECK_NULL(tmpClass =
(*env)->FindClass(env, "java/awt/geom/Rectangle2D$Float"));
sunFontIDs.rect2DFloatClass = (jclass)(*env)->NewGlobalRef(env, tmpClass);
CHECK_NULL(sunFontIDs.rect2DFloatCtr =
(*env)->GetMethodID(env, sunFontIDs.rect2DFloatClass, "<init>", "()V"));
CHECK_NULL(sunFontIDs.rect2DFloatCtr4 =
(*env)->GetMethodID(env, sunFontIDs.rect2DFloatClass,
"<init>", "(FFFF)V"));
CHECK_NULL(sunFontIDs.rectF2DX =
(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "x", "F"));
CHECK_NULL(sunFontIDs.rectF2DY =
(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "y", "F"));
CHECK_NULL(sunFontIDs.rectF2DWidth =
(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "width", "F"));
CHECK_NULL(sunFontIDs.rectF2DHeight =
(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "height", "F"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "java/awt/geom/GeneralPath"));
sunFontIDs.gpClass = (jclass)(*env)->NewGlobalRef(env, tmpClass);
CHECK_NULL(sunFontIDs.gpCtr =
(*env)->GetMethodID(env, sunFontIDs.gpClass, "<init>", "(I[BI[FI)V"));
CHECK_NULL(sunFontIDs.gpCtrEmpty =
(*env)->GetMethodID(env, sunFontIDs.gpClass, "<init>", "()V"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/Font2D"));
CHECK_NULL(sunFontIDs.f2dCharToGlyphMID =
(*env)->GetMethodID(env, tmpClass, "charToGlyph", "(I)I"));
CHECK_NULL(sunFontIDs.f2dCharToVariationGlyphMID =
(*env)->GetMethodID(env, tmpClass, "charToVariationGlyph", "(II)I"));
CHECK_NULL(sunFontIDs.getMapperMID =
(*env)->GetMethodID(env, tmpClass, "getMapper",
"()Lsun/font/CharToGlyphMapper;"));
CHECK_NULL(sunFontIDs.getTableBytesMID =
(*env)->GetMethodID(env, tmpClass, "getTableBytes", "(I)[B"));
CHECK_NULL(sunFontIDs.canDisplayMID =
(*env)->GetMethodID(env, tmpClass, "canDisplay", "(C)Z"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/CharToGlyphMapper"));
CHECK_NULL(sunFontIDs.charToGlyphMID =
(*env)->GetMethodID(env, tmpClass, "charToGlyph", "(I)I"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/PhysicalStrike"));
CHECK_NULL(sunFontIDs.getGlyphMetricsMID =
(*env)->GetMethodID(env, tmpClass, "getGlyphMetrics",
"(I)Ljava/awt/geom/Point2D$Float;"));
CHECK_NULL(sunFontIDs.getGlyphPointMID =
(*env)->GetMethodID(env, tmpClass, "getGlyphPoint",
"(II)Ljava/awt/geom/Point2D$Float;"));
CHECK_NULL(sunFontIDs.adjustPointMID =
(*env)->GetMethodID(env, tmpClass, "adjustPoint",
"(Ljava/awt/geom/Point2D$Float;)V"));
CHECK_NULL(sunFontIDs.pScalerContextFID =
(*env)->GetFieldID(env, tmpClass, "pScalerContext", "J"));
CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/GlyphList"));
CHECK_NULL(sunFontIDs.glyphListX =
(*env)->GetFieldID(env, tmpClass, "gposx", "F"));
CHECK_NULL(sunFontIDs.glyphListY =
(*env)->GetFieldID(env, tmpClass, "gposy", "F"));
CHECK_NULL(sunFontIDs.glyphListLen =
(*env)->GetFieldID(env, tmpClass, "len", "I"));
CHECK_NULL(sunFontIDs.glyphImages =
(*env)->GetFieldID(env, tmpClass, "images", "[J"));
CHECK_NULL(sunFontIDs.glyphListUsePos =
(*env)->GetFieldID(env, tmpClass, "usePositions", "Z"));
CHECK_NULL(sunFontIDs.glyphListPos =
(*env)->GetFieldID(env, tmpClass, "positions", "[F"));
CHECK_NULL(sunFontIDs.lcdRGBOrder =
(*env)->GetFieldID(env, tmpClass, "lcdRGBOrder", "Z"));
CHECK_NULL(sunFontIDs.lcdSubPixPos =
(*env)->GetFieldID(env, tmpClass, "lcdSubPixPos", "Z"));
initLCDGammaTables();
initialisedFontIDs = 1;
}
JNIEXPORT void JNICALL
Java_sun_font_SunFontManager_initIDs
(JNIEnv *env, jclass cls) {
initFontIDs(env);
}
JNIEXPORT FontManagerNativeIDs getSunFontIDs(JNIEnv *env) {
initFontIDs(env);
return sunFontIDs;
}
/*
* Class: sun_font_StrikeCache
* Method: freeIntPointer
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeIntPointer
(JNIEnv *env, jclass cacheClass, jint ptr) {
/* Note this is used for freeing a glyph which was allocated
* but never placed into the glyph cache. The caller holds the
* only reference, therefore it is unnecessary to invalidate any
* accelerated glyph cache cells as we do in freeInt/LongMemory().
*/
if (ptr != 0) {
free((void*)((intptr_t)ptr));
}
}
/*
* Class: sun_font_StrikeCache
* Method: freeLongPointer
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeLongPointer
(JNIEnv *env, jclass cacheClass, jlong ptr) {
/* Note this is used for freeing a glyph which was allocated
* but never placed into the glyph cache. The caller holds the
* only reference, therefore it is unnecessary to invalidate any
* accelerated glyph cache cells as we do in freeInt/LongMemory().
*/
if (ptr != 0L) {
free(jlong_to_ptr(ptr));
}
}
/*
* Class: sun_font_StrikeCache
* Method: freeIntMemory
* Signature: ([I)V
*/
JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeIntMemory
(JNIEnv *env, jclass cacheClass, jintArray jmemArray, jlong pContext) {
int len = (*env)->GetArrayLength(env, jmemArray);
jint* ptrs =
(jint*)(*env)->GetPrimitiveArrayCritical(env, jmemArray, NULL);
int i;
if (ptrs) {
for (i=0; i< len; i++) {
if (ptrs[i] != 0) {
GlyphInfo *ginfo = (GlyphInfo *)((intptr_t)ptrs[i]);
if (ginfo->cellInfo != NULL &&
ginfo->managed == MANAGED_GLYPH) {
// invalidate this glyph's accelerated cache cell
AccelGlyphCache_RemoveAllCellInfos(ginfo);
}
free(ginfo);
}
}
(*env)->ReleasePrimitiveArrayCritical(env, jmemArray, ptrs, JNI_ABORT);
}
if (!isNullScalerContext(jlong_to_ptr(pContext))) {
free(jlong_to_ptr(pContext));
}
}
/*
* Class: sun_font_StrikeCache
* Method: freeLongMemory
* Signature: ([J)V
*/
JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeLongMemory
(JNIEnv *env, jclass cacheClass, jlongArray jmemArray, jlong pContext) {
int len = (*env)->GetArrayLength(env, jmemArray);
jlong* ptrs =
(jlong*)(*env)->GetPrimitiveArrayCritical(env, jmemArray, NULL);
int i;
if (ptrs) {
for (i=0; i< len; i++) {
if (ptrs[i] != 0L) {
GlyphInfo *ginfo = (GlyphInfo *) jlong_to_ptr(ptrs[i]);
if (ginfo->cellInfo != NULL &&
ginfo->managed == MANAGED_GLYPH) {
AccelGlyphCache_RemoveAllCellInfos(ginfo);
}
free((void*)ginfo);
}
}
(*env)->ReleasePrimitiveArrayCritical(env, jmemArray, ptrs, JNI_ABORT);
}
if (!isNullScalerContext(jlong_to_ptr(pContext))) {
free(jlong_to_ptr(pContext));
}
}
JNIEXPORT void JNICALL
Java_sun_font_StrikeCache_getGlyphCacheDescription
(JNIEnv *env, jclass cls, jlongArray results) {
jlong* nresults;
GlyphInfo *info;
size_t baseAddr;
if ((*env)->GetArrayLength(env, results) < 13) {
return;
}
nresults = (jlong*)(*env)->GetPrimitiveArrayCritical(env, results, NULL);
if (nresults == NULL) {
return;
}
info = (GlyphInfo*) calloc(1, sizeof(GlyphInfo));
if (info == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, results, nresults, 0);
return;
}
baseAddr = (size_t)info;
nresults[0] = sizeof(void*);
nresults[1] = sizeof(GlyphInfo);
nresults[2] = 0;
nresults[3] = (size_t)&(info->advanceY)-baseAddr;
nresults[4] = (size_t)&(info->width)-baseAddr;
nresults[5] = (size_t)&(info->height)-baseAddr;
nresults[6] = (size_t)&(info->rowBytes)-baseAddr;
nresults[7] = (size_t)&(info->topLeftX)-baseAddr;
nresults[8] = (size_t)&(info->topLeftY)-baseAddr;
nresults[9] = (size_t)&(info->image)-baseAddr;
nresults[10] = (jlong)(uintptr_t)info; /* invisible glyph */
nresults[11] = (size_t)&(info->cellInfo)-baseAddr;
nresults[12] = (size_t)&(info->managed)-baseAddr;
(*env)->ReleasePrimitiveArrayCritical(env, results, nresults, 0);
}