Path: blob/master/src/java.desktop/unix/classes/sun/font/NativeStrike.java
41153 views
/*1* Copyright (c) 2003, 2020, 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*/2425package sun.font;2627import java.awt.geom.AffineTransform;28import java.awt.geom.GeneralPath;29import java.awt.geom.Point2D;30import java.awt.Rectangle;31import java.awt.geom.Rectangle2D;32import java.awt.geom.NoninvertibleTransformException;3334class NativeStrike extends PhysicalStrike {3536NativeFont nativeFont;37int numGlyphs;38AffineTransform invertDevTx;39AffineTransform fontTx;4041/* The following method prepares data used in obtaining FontMetrics.42* This is the one case in which we allow anything other than a43* simple scale to be used with a native font. We do this because in44* order to ensure that clients get the overall metrics they expect45* for a font whatever coordinate system (combination of font and46* device transform) they use.47* X11 fonts can only have a scale applied (remind : non-uniform?)48* We strip out everything else and if necessary obtain an inverse49* tx which we use to return metrics for the font in the transformed50* coordinate system of the font. ie we pass X11 a simple scale, and51* then apply the non-scale part of the font TX to that result.52*/53private int getNativePointSize() {54/* Make a copy of the glyphTX in which we will store the55* font transform, inverting the devTx if necessary56*/57double[] mat = new double[4];58desc.glyphTx.getMatrix(mat);59fontTx = new AffineTransform(mat);6061/* Now work backwards to get the font transform */62if (!desc.devTx.isIdentity() &&63desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {64try {65invertDevTx = desc.devTx.createInverse();66fontTx.concatenate(invertDevTx);67} catch (NoninvertibleTransformException e) {68e.printStackTrace();69}70}7172/* At this point the fontTx may be a simple +ve scale, or it73* may be something more complex.74*/75Point2D.Float pt = new Point2D.Float(1f,1f);76fontTx.deltaTransform(pt, pt);77double ptSize = Math.abs(pt.y);78int ttype = fontTx.getType();79if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||80fontTx.getScaleY() <= 0) {81/* We need to create an inverse transform that doesn't82* include the point size (strictly the uniform scale)83*/84fontTx.scale(1/ptSize, 1/ptSize);85} else {86fontTx = null; // no need87}88return (int)ptSize;89}9091NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {92super(nativeFont, desc);93this.nativeFont = nativeFont;949596/* If this is a delegate for bitmaps, we expect to have97* been invoked only for a simple scale. If that's not98* true, just bail99*/100if (nativeFont.isBitmapDelegate) {101int ttype = desc.glyphTx.getType();102if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||103desc.glyphTx.getScaleX() <= 0) {104numGlyphs = 0;105return;106}107}108109int ptSize = getNativePointSize();110byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);111double scale = Math.abs(desc.devTx.getScaleX());112pScalerContext = createScalerContext(nameBytes, ptSize, scale);113if (pScalerContext == 0L) {114SunFontManager.getInstance().deRegisterBadFont(nativeFont);115pScalerContext = createNullScalerContext();116numGlyphs = 0;117if (FontUtilities.isLogging()) {118FontUtilities.logSevere("Could not create native strike " +119new String(nameBytes));120}121return;122}123numGlyphs = nativeFont.getMapper().getNumGlyphs();124this.disposer = new NativeStrikeDisposer(nativeFont, desc,125pScalerContext);126}127128/* The asymmetry of the following methods is to help preserve129* performance with minimal textual changes to the calling code130* when moving initialisation of these arrays out of the constructor.131* This may be restructured later when there's more room for changes132*/133private boolean usingIntGlyphImages() {134if (intGlyphImages != null) {135return true;136} else if (longAddresses) {137return false;138} else {139/* We could obtain minGlyphIndex and index relative to that140* if we need to save space.141*/142int glyphLenArray = getMaxGlyph(pScalerContext);143144/* This shouldn't be necessary - its a precaution */145if (glyphLenArray < numGlyphs) {146glyphLenArray = numGlyphs;147}148intGlyphImages = new int[glyphLenArray];149this.disposer.intGlyphImages = intGlyphImages;150return true;151}152}153154private long[] getLongGlyphImages() {155if (longGlyphImages == null && longAddresses) {156157/* We could obtain minGlyphIndex and index relative to that158* if we need to save space.159*/160int glyphLenArray = getMaxGlyph(pScalerContext);161162/* This shouldn't be necessary - its a precaution */163if (glyphLenArray < numGlyphs) {164glyphLenArray = numGlyphs;165}166longGlyphImages = new long[glyphLenArray];167this.disposer.longGlyphImages = longGlyphImages;168}169return longGlyphImages;170}171172NativeStrike(NativeFont nativeFont, FontStrikeDesc desc,173boolean nocache) {174super(nativeFont, desc);175this.nativeFont = nativeFont;176177int ptSize = (int)desc.glyphTx.getScaleY();178double scale = desc.devTx.getScaleX(); // uniform scale179byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);180pScalerContext = createScalerContext(nameBytes, ptSize, scale);181182int numGlyphs = nativeFont.getMapper().getNumGlyphs();183}184185/* We want the native font to be responsible for reporting the186* font metrics, even if it often delegates to another font.187* The code here isn't yet implementing exactly that. If the glyph188* transform was something native couldn't handle, there's no native189* context from which to obtain metrics. Need to revise this to obtain190* the metrics and transform them. But currently in such a case it191* gets the metrics from a different font - its glyph delegate font.192*/193StrikeMetrics getFontMetrics() {194if (strikeMetrics == null) {195if (pScalerContext != 0) {196strikeMetrics = nativeFont.getFontMetrics(pScalerContext);197}198if (strikeMetrics != null && fontTx != null) {199strikeMetrics.convertToUserSpace(fontTx);200}201}202return strikeMetrics;203}204205private native long createScalerContext(byte[] nameBytes,206int ptSize, double scale);207208private native int getMaxGlyph(long pScalerContext);209210private native long createNullScalerContext();211212void getGlyphImagePtrs(int[] glyphCodes, long[] images,int len) {213for (int i=0; i<len; i++) {214images[i] = getGlyphImagePtr(glyphCodes[i]);215}216}217218long getGlyphImagePtr(int glyphCode) {219long glyphPtr;220221if (usingIntGlyphImages()) {222if ((glyphPtr = intGlyphImages[glyphCode] & INTMASK) != 0L) {223return glyphPtr;224} else {225glyphPtr = nativeFont.getGlyphImage(pScalerContext,glyphCode);226/* Synchronize in case some other thread has updated this227* cache entry already - unlikely but possible.228*/229synchronized (this) {230if (intGlyphImages[glyphCode] == 0) {231intGlyphImages[glyphCode] = (int)glyphPtr;232return glyphPtr;233} else {234StrikeCache.freeIntPointer((int)glyphPtr);235return intGlyphImages[glyphCode] & INTMASK;236}237}238}239}240/* must be using long (8 byte) addresses */241else if ((glyphPtr = getLongGlyphImages()[glyphCode]) != 0L) {242return glyphPtr;243} else {244glyphPtr = nativeFont.getGlyphImage(pScalerContext, glyphCode);245246synchronized (this) {247if (longGlyphImages[glyphCode] == 0L) {248longGlyphImages[glyphCode] = glyphPtr;249return glyphPtr;250} else {251StrikeCache.freeLongPointer(glyphPtr);252return longGlyphImages[glyphCode];253}254}255}256}257258/* This is used when a FileFont uses the native names to create a259* delegate NativeFont/Strike to get images from native. This is used260* because Solaris TrueType fonts have external PCF bitmaps rather than261* embedded bitmaps. This is really only important for CJK fonts as262* for most scripts the external X11 bitmaps aren't much better - if263* at all - than the results from hinting the outlines.264*/265long getGlyphImagePtrNoCache(int glyphCode) {266return nativeFont.getGlyphImageNoDefault(pScalerContext, glyphCode);267}268269void getGlyphImageBounds(int glyphcode, Point2D.Float pt,270Rectangle result) {271}272273Point2D.Float getGlyphMetrics(int glyphCode) {274Point2D.Float pt = new Point2D.Float(getGlyphAdvance(glyphCode), 0f);275return pt;276}277278float getGlyphAdvance(int glyphCode) {279return nativeFont.getGlyphAdvance(pScalerContext, glyphCode);280}281282Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {283return nativeFont.getGlyphOutlineBounds(pScalerContext, glyphCode);284}285286GeneralPath getGlyphOutline(int glyphCode, float x, float y) {287return new GeneralPath();288}289290GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {291return new GeneralPath();292}293294}295296297