Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/font/FontInstanceAdapter.cpp
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright IBM Corp. 1998-2001 - All Rights Reserved27*28* The original version of this source code and documentation is29* copyrighted and owned by IBM. These materials are provided30* under terms of a License Agreement between IBM and Sun.31* This technology is protected by multiple US and International32* patents. This notice and attribution to IBM may not be removed.33*/3435#include "FontInstanceAdapter.h"3637U_NAMESPACE_BEGIN3839FontInstanceAdapter::FontInstanceAdapter(JNIEnv *theEnv,40jobject theFont2D,41jobject theFontStrike,42float *matrix,43le_int32 xRes, le_int32 yRes,44le_int32 theUPEM,45TTLayoutTableCache *ltables)46: env(theEnv), font2D(theFont2D), fontStrike(theFontStrike),47xppem(0), yppem(0),48xScaleUnitsToPoints(0), yScaleUnitsToPoints(0),49xScalePixelsToUnits(0), yScalePixelsToUnits(0),50upem(theUPEM), layoutTables(ltables)51{52xPointSize = euclidianDistance(matrix[0], matrix[1]);53yPointSize = euclidianDistance(matrix[2], matrix[3]);5455txMat[0] = matrix[0]/xPointSize;56txMat[1] = matrix[1]/xPointSize;57txMat[2] = matrix[2]/yPointSize;58txMat[3] = matrix[3]/yPointSize;5960xppem = ((float) xRes / 72) * xPointSize;61yppem = ((float) yRes / 72) * yPointSize;6263xScaleUnitsToPoints = xPointSize / upem;64yScaleUnitsToPoints = yPointSize / upem;6566xScalePixelsToUnits = upem / xppem;67yScalePixelsToUnits = upem / yppem;68};697071const void *FontInstanceAdapter::getFontTable(LETag tableTag) const72{73size_t ignored = 0;74return getFontTable(tableTag, ignored);75}7677static const LETag cacheMap[LAYOUTCACHE_ENTRIES] = {78GPOS_TAG, GDEF_TAG, GSUB_TAG, MORT_TAG, MORX_TAG, KERN_TAG79};8081const void *FontInstanceAdapter::getFontTable(LETag tableTag, size_t &length) const82{83length = 0;8485if (!layoutTables) { // t1 font86return 0;87}8889// cache in font's pscaler object90// font disposer will handle for us9192int cacheIdx;93for (cacheIdx=0;cacheIdx<LAYOUTCACHE_ENTRIES;cacheIdx++) {94if (tableTag==cacheMap[cacheIdx]) break;95}9697if (cacheIdx<LAYOUTCACHE_ENTRIES) { // if found98if (layoutTables->entries[cacheIdx].len != -1) {99length = layoutTables->entries[cacheIdx].len;100return layoutTables->entries[cacheIdx].ptr;101}102} else {103//fprintf(stderr, "unexpected table request from font instance adapter: %x\n", tableTag);104// (don't load any other tables)105return 0;106}107108jbyte* result = 0;109jsize len = 0;110jbyteArray tableBytes = (jbyteArray)111env->CallObjectMethod(font2D, sunFontIDs.getTableBytesMID, tableTag);112if (!IS_NULL(tableBytes)) {113len = env->GetArrayLength(tableBytes);114result = new jbyte[len];115env->GetByteArrayRegion(tableBytes, 0, len, result);116}117118if (cacheIdx<LAYOUTCACHE_ENTRIES) { // if cacheable table119layoutTables->entries[cacheIdx].len = len;120layoutTables->entries[cacheIdx].ptr = (const void*)result;121}122123length = len;124return (const void*)result;125};126127LEGlyphID FontInstanceAdapter::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const128{129LEUnicode32 mappedChar = mapper->mapChar(ch);130131if (mappedChar == 0xFFFF || mappedChar == 0xFFFE) {132return 0xFFFF;133}134135if (mappedChar == 0x200C || mappedChar == 0x200D) {136return 1;137}138139LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.f2dCharToGlyphMID, (jint)mappedChar);140if ((int)id < 0) {141id = 0;142}143return id;144}145146LEGlyphID FontInstanceAdapter::mapCharToGlyph(LEUnicode32 ch) const147{148LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.f2dCharToGlyphMID, ch);149if ((int)id < 0) {150id = 0;151}152return id;153}154155void FontInstanceAdapter::mapCharsToWideGlyphs(const LEUnicode chars[],156le_int32 offset, le_int32 count, le_bool reverse,157const LECharMapper *mapper, le_uint32 glyphs[]) const158{159le_int32 i, out = 0, dir = 1;160161if (reverse) {162out = count - 1;163dir = -1;164}165166for (i = offset; i < offset + count; i += 1, out += dir) {167LEUnicode16 high = chars[i];168LEUnicode32 code = high;169170if (i < offset + count - 1 && high >= 0xD800 && high <= 0xDBFF) {171LEUnicode16 low = chars[i + 1];172173if (low >= 0xDC00 && low <= 0xDFFF) {174code = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;175}176}177178glyphs[out] = mapCharToWideGlyph(code, mapper);179180if (code >= 0x10000) {181i += 1;182glyphs[out += dir] = 0xFFFF;183}184}185}186187le_uint32 FontInstanceAdapter::mapCharToWideGlyph(LEUnicode32 ch, const LECharMapper *mapper) const188{189LEUnicode32 mappedChar = mapper->mapChar(ch);190191if (mappedChar == 0xFFFF) {192return 0xFFFF;193}194195if (mappedChar == 0x200C || mappedChar == 0x200D) {196return 1;197}198199LEGlyphID id = (LEGlyphID)env->CallIntMethod(font2D, sunFontIDs.charToGlyphMID,200mappedChar);201if ((int)id < 0) {202id = 0;203}204return id;205}206207void FontInstanceAdapter::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const208{209getWideGlyphAdvance((le_uint32)glyph, advance);210}211212void FontInstanceAdapter::getKerningAdjustment(LEPoint &adjustment) const213{214float xx, xy, yx, yy;215le_bool isIdentityMatrix;216217isIdentityMatrix = (txMat[0] == 1 && txMat[1] == 0 &&218txMat[2] == 0 && txMat[3] == 1);219220if (!isIdentityMatrix) {221xx = adjustment.fX;222xy = xx * txMat[1];223xx = xx * txMat[0];224225yy = adjustment.fY;226yx = yy * txMat[2];227yy = yy * txMat[3];228229adjustment.fX = xx + yx;230adjustment.fY = xy + yy;231}232233jobject pt = env->NewObject(sunFontIDs.pt2DFloatClass,234sunFontIDs.pt2DFloatCtr,235adjustment.fX, adjustment.fY);236if (pt == NULL) {237env->ExceptionClear();238adjustment.fX = 0.0f;239adjustment.fY = 0.0f;240} else {241env->CallObjectMethod(fontStrike, sunFontIDs.adjustPointMID, pt);242adjustment.fX = env->GetFloatField(pt, sunFontIDs.xFID);243adjustment.fY = env->GetFloatField(pt, sunFontIDs.yFID);244}245}246247void FontInstanceAdapter::getWideGlyphAdvance(le_uint32 glyph, LEPoint &advance) const248{249if ((glyph & 0xfffe) == 0xfffe) {250advance.fX = 0;251advance.fY = 0;252return;253}254jobject pt = env->CallObjectMethod(fontStrike,255sunFontIDs.getGlyphMetricsMID, glyph);256if (pt != NULL) {257advance.fX = env->GetFloatField(pt, sunFontIDs.xFID);258advance.fY = env->GetFloatField(pt, sunFontIDs.yFID);259env->DeleteLocalRef(pt);260}261}262263le_bool FontInstanceAdapter::getGlyphPoint(LEGlyphID glyph,264le_int32 pointNumber,265LEPoint &point) const266{267/* This upcall is not ideal, since it will make another down call.268* The intention is to move up some of this code into Java. But269* a HashMap has been added to the Java PhysicalStrike object to cache270* these points so that they don't need to be repeatedly recalculated271* which is expensive as it needs the font scaler to re-generate the272* hinted glyph outline. This turns out to be a huge win over 1.4.x273*/274jobject pt = env->CallObjectMethod(fontStrike,275sunFontIDs.getGlyphPointMID,276glyph, pointNumber);277if (pt != NULL) {278/* point is a java.awt.geom.Point2D.Float */279point.fX = env->GetFloatField(pt, sunFontIDs.xFID);280/* convert from java coordinate system to internal '+y up' coordinate system */281point.fY = -env->GetFloatField(pt, sunFontIDs.yFID);282return true;283} else {284return false;285}286}287288void FontInstanceAdapter::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const289{290float xx, xy, yx, yy;291le_bool isIdentityMatrix;292293isIdentityMatrix = (txMat[0] == 1 && txMat[1] == 0 &&294txMat[2] == 0 && txMat[3] == 1);295296xx = xFunits * xScaleUnitsToPoints;297xy = 0;298if (!isIdentityMatrix) {299xy = xx * txMat[1];300xx = xx * txMat[0];301};302303yx = 0;304yy = yFunits * yScaleUnitsToPoints;305if (!isIdentityMatrix) {306yx = yy * txMat[2];307yy = yy * txMat[3];308};309pixels.fX = xx + yx;310pixels.fY = xy + yy;311}312313314float FontInstanceAdapter::euclidianDistance(float a, float b)315{316if (a < 0) {317a = -a;318}319320if (b < 0) {321b = -b;322}323324if (a == 0) {325return b;326}327328if (b == 0) {329return a;330}331332float root = a > b ? a + (b / 2) : b + (a / 2); /* Do an initial approximation, in root */333334/* An unrolled Newton-Raphson iteration sequence */335root = (root + (a * (a / root)) + (b * (b / root)) + 1) / 2;336root = (root + (a * (a / root)) + (b * (b / root)) + 1) / 2;337root = (root + (a * (a / root)) + (b * (b / root)) + 1) / 2;338339return root;340}341342U_NAMESPACE_END343344345