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 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 
  70 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 
  80 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 
 126 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     return id;
 140 }
 141 
 142 LEGlyphID FontInstanceAdapter::mapCharToGlyph(LEUnicode32 ch) const
 143 {
 144     LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.f2dCharToGlyphMID, ch);
 145     return id;
 146 }
 147 
 148 void FontInstanceAdapter::mapCharsToWideGlyphs(const LEUnicode chars[],
 149     le_int32 offset, le_int32 count, le_bool reverse,
 150     const LECharMapper *mapper, le_uint32 glyphs[]) const
 151 {
 152     le_int32 i, out = 0, dir = 1;
 153 
 154     if (reverse) {
 155         out = count - 1;
 156         dir = -1;
 157     }
 158 
 159     for (i = offset; i < offset + count; i += 1, out += dir) {
 160                 LEUnicode16 high = chars[i];
 161                 LEUnicode32 code = high;
 162 
 163                 if (i < offset + count - 1 && high >= 0xD800 && high <= 0xDBFF) {
 164                         LEUnicode16 low = chars[i + 1];
 165 
 166                         if (low >= 0xDC00 && low <= 0xDFFF) {
 167                                 code = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
 168                         }
 169                 }
 170 
 171         glyphs[out] = mapCharToWideGlyph(code, mapper);
 172 
 173                 if (code >= 0x10000) {
 174                         i += 1;
 175                         glyphs[out += dir] = 0xFFFF;
 176                 }
 177     }
 178 }
 179 
 180 le_uint32 FontInstanceAdapter::mapCharToWideGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
 181 {
 182     LEUnicode32 mappedChar = mapper->mapChar(ch);
 183 
 184     if (mappedChar == 0xFFFF) {
 185         return 0xFFFF;
 186     }
 187 
 188     if (mappedChar == 0x200C || mappedChar == 0x200D) {
 189         return 1;
 190     }
 191 
 192     return (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.charToGlyphMID,
 193                                          mappedChar);
 194 }
 195 
 196 void FontInstanceAdapter::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
 197 {
 198     getWideGlyphAdvance((le_uint32)glyph, advance);
 199 }
 200 
 201 void FontInstanceAdapter::getKerningAdjustment(LEPoint &adjustment) const
 202 {
 203     float xx, xy, yx, yy;
 204     le_bool isIdentityMatrix;
 205 
 206     isIdentityMatrix = (txMat[0] == 1 && txMat[1] == 0 &&
 207                         txMat[2] == 0 && txMat[3] == 1);
 208 
 209     if (!isIdentityMatrix) {
 210       xx = adjustment.fX;
 211       xy = xx * txMat[1];
 212       xx = xx * txMat[0];
 213 
 214       yy = adjustment.fY;
 215       yx = yy * txMat[2];
 216       yy = yy * txMat[3];
 217 
 218       adjustment.fX = xx + yx;
 219       adjustment.fY = xy + yy;
 220     }
 221 
 222     jobject pt = env->NewObject(sunFontIDs.pt2DFloatClass,
 223                                 sunFontIDs.pt2DFloatCtr,
 224                                 adjustment.fX, adjustment.fY);
 225     env->CallObjectMethod(fontStrike, sunFontIDs.adjustPointMID, pt);
 226     adjustment.fX = env->GetFloatField(pt, sunFontIDs.xFID);
 227     adjustment.fY = env->GetFloatField(pt, sunFontIDs.yFID);
 228 }
 229 
 230 void 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 
 246 le_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 
 271 void 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 
 297 float 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 }