Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/font/NativeStrike.java
32287 views
/*1* Copyright (c) 2003, 2012, 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.getLogger()119.severe("Could not create native strike " +120new String(nameBytes));121}122return;123}124numGlyphs = nativeFont.getMapper().getNumGlyphs();125this.disposer = new NativeStrikeDisposer(nativeFont, desc,126pScalerContext);127}128129/* The asymmetry of the following methods is to help preserve130* performance with minimal textual changes to the calling code131* when moving initialisation of these arrays out of the constructor.132* This may be restructured later when there's more room for changes133*/134private boolean usingIntGlyphImages() {135if (intGlyphImages != null) {136return true;137} else if (longAddresses) {138return false;139} else {140/* We could obtain minGlyphIndex and index relative to that141* if we need to save space.142*/143int glyphLenArray = getMaxGlyph(pScalerContext);144145/* This shouldn't be necessary - its a precaution */146if (glyphLenArray < numGlyphs) {147glyphLenArray = numGlyphs;148}149intGlyphImages = new int[glyphLenArray];150this.disposer.intGlyphImages = intGlyphImages;151return true;152}153}154155private long[] getLongGlyphImages() {156if (longGlyphImages == null && longAddresses) {157158/* We could obtain minGlyphIndex and index relative to that159* if we need to save space.160*/161int glyphLenArray = getMaxGlyph(pScalerContext);162163/* This shouldn't be necessary - its a precaution */164if (glyphLenArray < numGlyphs) {165glyphLenArray = numGlyphs;166}167longGlyphImages = new long[glyphLenArray];168this.disposer.longGlyphImages = longGlyphImages;169}170return longGlyphImages;171}172173NativeStrike(NativeFont nativeFont, FontStrikeDesc desc,174boolean nocache) {175super(nativeFont, desc);176this.nativeFont = nativeFont;177178int ptSize = (int)desc.glyphTx.getScaleY();179double scale = desc.devTx.getScaleX(); // uniform scale180byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);181pScalerContext = createScalerContext(nameBytes, ptSize, scale);182183int numGlyphs = nativeFont.getMapper().getNumGlyphs();184}185186/* We want the native font to be responsible for reporting the187* font metrics, even if it often delegates to another font.188* The code here isn't yet implementing exactly that. If the glyph189* transform was something native couldn't handle, there's no native190* context from which to obtain metrics. Need to revise this to obtain191* the metrics and transform them. But currently in such a case it192* gets the metrics from a different font - its glyph delegate font.193*/194StrikeMetrics getFontMetrics() {195if (strikeMetrics == null) {196if (pScalerContext != 0) {197strikeMetrics = nativeFont.getFontMetrics(pScalerContext);198}199if (strikeMetrics != null && fontTx != null) {200strikeMetrics.convertToUserSpace(fontTx);201}202}203return strikeMetrics;204}205206private native long createScalerContext(byte[] nameBytes,207int ptSize, double scale);208209private native int getMaxGlyph(long pScalerContext);210211private native long createNullScalerContext();212213void getGlyphImagePtrs(int[] glyphCodes, long[] images,int len) {214for (int i=0; i<len; i++) {215images[i] = getGlyphImagePtr(glyphCodes[i]);216}217}218219long getGlyphImagePtr(int glyphCode) {220long glyphPtr;221222if (usingIntGlyphImages()) {223if ((glyphPtr = intGlyphImages[glyphCode] & INTMASK) != 0L) {224return glyphPtr;225} else {226glyphPtr = nativeFont.getGlyphImage(pScalerContext,glyphCode);227/* Synchronize in case some other thread has updated this228* cache entry already - unlikely but possible.229*/230synchronized (this) {231if (intGlyphImages[glyphCode] == 0) {232intGlyphImages[glyphCode] = (int)glyphPtr;233return glyphPtr;234} else {235StrikeCache.freeIntPointer((int)glyphPtr);236return intGlyphImages[glyphCode] & INTMASK;237}238}239}240}241/* must be using long (8 byte) addresses */242else if ((glyphPtr = getLongGlyphImages()[glyphCode]) != 0L) {243return glyphPtr;244} else {245glyphPtr = nativeFont.getGlyphImage(pScalerContext, glyphCode);246247synchronized (this) {248if (longGlyphImages[glyphCode] == 0L) {249longGlyphImages[glyphCode] = glyphPtr;250return glyphPtr;251} else {252StrikeCache.freeLongPointer(glyphPtr);253return longGlyphImages[glyphCode];254}255}256}257}258259/* This is used when a FileFont uses the native names to create a260* delegate NativeFont/Strike to get images from native. This is used261* because Solaris TrueType fonts have external PCF bitmaps rather than262* embedded bitmaps. This is really only important for CJK fonts as263* for most scripts the external X11 bitmaps aren't much better - if264* at all - than the results from hinting the outlines.265*/266long getGlyphImagePtrNoCache(int glyphCode) {267return nativeFont.getGlyphImageNoDefault(pScalerContext, glyphCode);268}269270void getGlyphImageBounds(int glyphcode, Point2D.Float pt,271Rectangle result) {272}273274Point2D.Float getGlyphMetrics(int glyphCode) {275Point2D.Float pt = new Point2D.Float(getGlyphAdvance(glyphCode), 0f);276return pt;277}278279float getGlyphAdvance(int glyphCode) {280return nativeFont.getGlyphAdvance(pScalerContext, glyphCode);281}282283Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {284return nativeFont.getGlyphOutlineBounds(pScalerContext, glyphCode);285}286287GeneralPath getGlyphOutline(int glyphCode, float x, float y) {288return new GeneralPath();289}290291GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {292return new GeneralPath();293}294295}296297298