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