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/native/sun/font/FontInstanceAdapter.cpp
38829 views
1
/*
2
* Copyright (c) 1999, 2013, 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
/*
27
* (C) Copyright IBM Corp. 1998-2001 - All Rights Reserved
28
*
29
* The original version of this source code and documentation is
30
* copyrighted and owned by IBM. These materials are provided
31
* under terms of a License Agreement between IBM and Sun.
32
* This technology is protected by multiple US and International
33
* patents. This notice and attribution to IBM may not be removed.
34
*/
35
36
#include "FontInstanceAdapter.h"
37
38
U_NAMESPACE_BEGIN
39
40
FontInstanceAdapter::FontInstanceAdapter(JNIEnv *theEnv,
41
jobject theFont2D,
42
jobject theFontStrike,
43
float *matrix,
44
le_int32 xRes, le_int32 yRes,
45
le_int32 theUPEM,
46
TTLayoutTableCache *ltables)
47
: env(theEnv), font2D(theFont2D), fontStrike(theFontStrike),
48
xppem(0), yppem(0),
49
xScaleUnitsToPoints(0), yScaleUnitsToPoints(0),
50
xScalePixelsToUnits(0), yScalePixelsToUnits(0),
51
upem(theUPEM), layoutTables(ltables)
52
{
53
xPointSize = euclidianDistance(matrix[0], matrix[1]);
54
yPointSize = euclidianDistance(matrix[2], matrix[3]);
55
56
txMat[0] = matrix[0]/xPointSize;
57
txMat[1] = matrix[1]/xPointSize;
58
txMat[2] = matrix[2]/yPointSize;
59
txMat[3] = matrix[3]/yPointSize;
60
61
xppem = ((float) xRes / 72) * xPointSize;
62
yppem = ((float) yRes / 72) * yPointSize;
63
64
xScaleUnitsToPoints = xPointSize / upem;
65
yScaleUnitsToPoints = yPointSize / upem;
66
67
xScalePixelsToUnits = upem / xppem;
68
yScalePixelsToUnits = upem / yppem;
69
};
70
71
72
const void *FontInstanceAdapter::getFontTable(LETag tableTag) const
73
{
74
size_t ignored = 0;
75
return getFontTable(tableTag, ignored);
76
}
77
78
static const LETag cacheMap[LAYOUTCACHE_ENTRIES] = {
79
GPOS_TAG, GDEF_TAG, GSUB_TAG, MORT_TAG, MORX_TAG, KERN_TAG
80
};
81
82
const void *FontInstanceAdapter::getFontTable(LETag tableTag, size_t &length) const
83
{
84
length = 0;
85
86
if (!layoutTables) { // t1 font
87
return 0;
88
}
89
90
// cache in font's pscaler object
91
// font disposer will handle for us
92
93
int cacheIdx;
94
for (cacheIdx=0;cacheIdx<LAYOUTCACHE_ENTRIES;cacheIdx++) {
95
if (tableTag==cacheMap[cacheIdx]) break;
96
}
97
98
if (cacheIdx<LAYOUTCACHE_ENTRIES) { // if found
99
if (layoutTables->entries[cacheIdx].len != -1) {
100
length = layoutTables->entries[cacheIdx].len;
101
return layoutTables->entries[cacheIdx].ptr;
102
}
103
} else {
104
//fprintf(stderr, "unexpected table request from font instance adapter: %x\n", tableTag);
105
// (don't load any other tables)
106
return 0;
107
}
108
109
jbyte* result = 0;
110
jsize len = 0;
111
jbyteArray tableBytes = (jbyteArray)
112
env->CallObjectMethod(font2D, sunFontIDs.getTableBytesMID, tableTag);
113
if (!IS_NULL(tableBytes)) {
114
len = env->GetArrayLength(tableBytes);
115
result = new jbyte[len];
116
env->GetByteArrayRegion(tableBytes, 0, len, result);
117
}
118
119
if (cacheIdx<LAYOUTCACHE_ENTRIES) { // if cacheable table
120
layoutTables->entries[cacheIdx].len = len;
121
layoutTables->entries[cacheIdx].ptr = (const void*)result;
122
}
123
124
length = len;
125
return (const void*)result;
126
};
127
128
LEGlyphID FontInstanceAdapter::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
129
{
130
LEUnicode32 mappedChar = mapper->mapChar(ch);
131
132
if (mappedChar == 0xFFFF || mappedChar == 0xFFFE) {
133
return 0xFFFF;
134
}
135
136
if (mappedChar == 0x200C || mappedChar == 0x200D) {
137
return 1;
138
}
139
140
LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.f2dCharToGlyphMID, (jint)mappedChar);
141
if ((int)id < 0) {
142
id = 0;
143
}
144
return id;
145
}
146
147
LEGlyphID FontInstanceAdapter::mapCharToGlyph(LEUnicode32 ch) const
148
{
149
LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.f2dCharToGlyphMID, ch);
150
if ((int)id < 0) {
151
id = 0;
152
}
153
return id;
154
}
155
156
void FontInstanceAdapter::mapCharsToWideGlyphs(const LEUnicode chars[],
157
le_int32 offset, le_int32 count, le_bool reverse,
158
const LECharMapper *mapper, le_uint32 glyphs[]) const
159
{
160
le_int32 i, out = 0, dir = 1;
161
162
if (reverse) {
163
out = count - 1;
164
dir = -1;
165
}
166
167
for (i = offset; i < offset + count; i += 1, out += dir) {
168
LEUnicode16 high = chars[i];
169
LEUnicode32 code = high;
170
171
if (i < offset + count - 1 && high >= 0xD800 && high <= 0xDBFF) {
172
LEUnicode16 low = chars[i + 1];
173
174
if (low >= 0xDC00 && low <= 0xDFFF) {
175
code = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
176
}
177
}
178
179
glyphs[out] = mapCharToWideGlyph(code, mapper);
180
181
if (code >= 0x10000) {
182
i += 1;
183
glyphs[out += dir] = 0xFFFF;
184
}
185
}
186
}
187
188
le_uint32 FontInstanceAdapter::mapCharToWideGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
189
{
190
LEUnicode32 mappedChar = mapper->mapChar(ch);
191
192
if (mappedChar == 0xFFFF) {
193
return 0xFFFF;
194
}
195
196
if (mappedChar == 0x200C || mappedChar == 0x200D) {
197
return 1;
198
}
199
200
LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.charToGlyphMID,
201
mappedChar);
202
if ((int)id < 0) {
203
id = 0;
204
}
205
return id;
206
}
207
208
void FontInstanceAdapter::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
209
{
210
getWideGlyphAdvance((le_uint32)glyph, advance);
211
}
212
213
void FontInstanceAdapter::getKerningAdjustment(LEPoint &adjustment) const
214
{
215
float xx, xy, yx, yy;
216
le_bool isIdentityMatrix;
217
218
isIdentityMatrix = (txMat[0] == 1 && txMat[1] == 0 &&
219
txMat[2] == 0 && txMat[3] == 1);
220
221
if (!isIdentityMatrix) {
222
xx = adjustment.fX;
223
xy = xx * txMat[1];
224
xx = xx * txMat[0];
225
226
yy = adjustment.fY;
227
yx = yy * txMat[2];
228
yy = yy * txMat[3];
229
230
adjustment.fX = xx + yx;
231
adjustment.fY = xy + yy;
232
}
233
234
jobject pt = env->NewObject(sunFontIDs.pt2DFloatClass,
235
sunFontIDs.pt2DFloatCtr,
236
adjustment.fX, adjustment.fY);
237
if (pt == NULL) {
238
env->ExceptionClear();
239
adjustment.fX = 0.0f;
240
adjustment.fY = 0.0f;
241
} else {
242
env->CallObjectMethod(fontStrike, sunFontIDs.adjustPointMID, pt);
243
adjustment.fX = env->GetFloatField(pt, sunFontIDs.xFID);
244
adjustment.fY = env->GetFloatField(pt, sunFontIDs.yFID);
245
}
246
}
247
248
void FontInstanceAdapter::getWideGlyphAdvance(le_uint32 glyph, LEPoint &advance) const
249
{
250
if ((glyph & 0xfffe) == 0xfffe) {
251
advance.fX = 0;
252
advance.fY = 0;
253
return;
254
}
255
jobject pt = env->CallObjectMethod(fontStrike,
256
sunFontIDs.getGlyphMetricsMID, glyph);
257
if (pt != NULL) {
258
advance.fX = env->GetFloatField(pt, sunFontIDs.xFID);
259
advance.fY = env->GetFloatField(pt, sunFontIDs.yFID);
260
env->DeleteLocalRef(pt);
261
}
262
}
263
264
le_bool FontInstanceAdapter::getGlyphPoint(LEGlyphID glyph,
265
le_int32 pointNumber,
266
LEPoint &point) const
267
{
268
/* This upcall is not ideal, since it will make another down call.
269
* The intention is to move up some of this code into Java. But
270
* a HashMap has been added to the Java PhysicalStrike object to cache
271
* these points so that they don't need to be repeatedly recalculated
272
* which is expensive as it needs the font scaler to re-generate the
273
* hinted glyph outline. This turns out to be a huge win over 1.4.x
274
*/
275
jobject pt = env->CallObjectMethod(fontStrike,
276
sunFontIDs.getGlyphPointMID,
277
glyph, pointNumber);
278
if (pt != NULL) {
279
/* point is a java.awt.geom.Point2D.Float */
280
point.fX = env->GetFloatField(pt, sunFontIDs.xFID);
281
/* convert from java coordinate system to internal '+y up' coordinate system */
282
point.fY = -env->GetFloatField(pt, sunFontIDs.yFID);
283
return true;
284
} else {
285
return false;
286
}
287
}
288
289
void FontInstanceAdapter::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const
290
{
291
float xx, xy, yx, yy;
292
le_bool isIdentityMatrix;
293
294
isIdentityMatrix = (txMat[0] == 1 && txMat[1] == 0 &&
295
txMat[2] == 0 && txMat[3] == 1);
296
297
xx = xFunits * xScaleUnitsToPoints;
298
xy = 0;
299
if (!isIdentityMatrix) {
300
xy = xx * txMat[1];
301
xx = xx * txMat[0];
302
};
303
304
yx = 0;
305
yy = yFunits * yScaleUnitsToPoints;
306
if (!isIdentityMatrix) {
307
yx = yy * txMat[2];
308
yy = yy * txMat[3];
309
};
310
pixels.fX = xx + yx;
311
pixels.fY = xy + yy;
312
}
313
314
315
float FontInstanceAdapter::euclidianDistance(float a, float b)
316
{
317
if (a < 0) {
318
a = -a;
319
}
320
321
if (b < 0) {
322
b = -b;
323
}
324
325
if (a == 0) {
326
return b;
327
}
328
329
if (b == 0) {
330
return a;
331
}
332
333
float root = a > b ? a + (b / 2) : b + (a / 2); /* Do an initial approximation, in root */
334
335
/* An unrolled Newton-Raphson iteration sequence */
336
root = (root + (a * (a / root)) + (b * (b / root)) + 1) / 2;
337
root = (root + (a * (a / root)) + (b * (b / root)) + 1) / 2;
338
root = (root + (a * (a / root)) + (b * (b / root)) + 1) / 2;
339
340
return root;
341
}
342
343
U_NAMESPACE_END
344
345