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