0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.font;
0N/A
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.GeneralPath;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.geom.NoninvertibleTransformException;
0N/A
0N/A class NativeStrike extends PhysicalStrike {
0N/A
0N/A NativeFont nativeFont;
0N/A int numGlyphs;
0N/A AffineTransform invertDevTx;
0N/A AffineTransform fontTx;
0N/A
0N/A /* The following method prepares data used in obtaining FontMetrics.
0N/A * This is the one case in which we allow anything other than a
0N/A * simple scale to be used with a native font. We do this because in
0N/A * order to ensure that clients get the overall metrics they expect
0N/A * for a font whatever coordinate system (combination of font and
0N/A * device transform) they use.
0N/A * X11 fonts can only have a scale applied (remind : non-uniform?)
0N/A * We strip out everything else and if necessary obtain an inverse
0N/A * tx which we use to return metrics for the font in the transformed
0N/A * coordinate system of the font. ie we pass X11 a simple scale, and
0N/A * then apply the non-scale part of the font TX to that result.
0N/A */
0N/A private int getNativePointSize() {
0N/A /* Make a copy of the glyphTX in which we will store the
0N/A * font transform, inverting the devTx if necessary
0N/A */
0N/A double[] mat = new double[4];
0N/A desc.glyphTx.getMatrix(mat);
0N/A fontTx = new AffineTransform(mat);
0N/A
0N/A /* Now work backwards to get the font transform */
0N/A if (!desc.devTx.isIdentity() &&
0N/A desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
0N/A try {
0N/A invertDevTx = desc.devTx.createInverse();
0N/A fontTx.concatenate(invertDevTx);
0N/A } catch (NoninvertibleTransformException e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A /* At this point the fontTx may be a simple +ve scale, or it
0N/A * may be something more complex.
0N/A */
0N/A Point2D.Float pt = new Point2D.Float(1f,1f);
0N/A fontTx.deltaTransform(pt, pt);
0N/A double ptSize = Math.abs(pt.y);
0N/A int ttype = fontTx.getType();
0N/A if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
0N/A fontTx.getScaleY() <= 0) {
0N/A /* We need to create an inverse transform that doesn't
0N/A * include the point size (strictly the uniform scale)
0N/A */
0N/A fontTx.scale(1/ptSize, 1/ptSize);
0N/A } else {
0N/A fontTx = null; // no need
0N/A }
0N/A return (int)ptSize;
0N/A }
0N/A
0N/A NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
0N/A super(nativeFont, desc);
0N/A this.nativeFont = nativeFont;
0N/A
0N/A
0N/A /* If this is a delegate for bitmaps, we expect to have
0N/A * been invoked only for a simple scale. If that's not
0N/A * true, just bail
0N/A */
0N/A if (nativeFont.isBitmapDelegate) {
0N/A int ttype = desc.glyphTx.getType();
0N/A if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
0N/A desc.glyphTx.getScaleX() <= 0) {
0N/A numGlyphs = 0;
0N/A return;
0N/A }
0N/A }
0N/A
0N/A int ptSize = getNativePointSize();
0N/A byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
0N/A double scale = Math.abs(desc.devTx.getScaleX());
0N/A pScalerContext = createScalerContext(nameBytes, ptSize, scale);
0N/A if (pScalerContext == 0L) {
1686N/A SunFontManager.getInstance().deRegisterBadFont(nativeFont);
0N/A pScalerContext = createNullScalerContext();
0N/A numGlyphs = 0;
1686N/A if (FontUtilities.isLogging()) {
1686N/A FontUtilities.getLogger()
1686N/A .severe("Could not create native strike " +
0N/A new String(nameBytes));
0N/A }
0N/A return;
0N/A }
0N/A numGlyphs = nativeFont.getMapper().getNumGlyphs();
0N/A this.disposer = new NativeStrikeDisposer(nativeFont, desc,
0N/A pScalerContext);
0N/A }
0N/A
0N/A /* The asymmetry of the following methods is to help preserve
0N/A * performance with minimal textual changes to the calling code
0N/A * when moving initialisation of these arrays out of the constructor.
0N/A * This may be restructured later when there's more room for changes
0N/A */
0N/A private boolean usingIntGlyphImages() {
0N/A if (intGlyphImages != null) {
0N/A return true;
1686N/A } else if (longAddresses) {
0N/A return false;
0N/A } else {
0N/A /* We could obtain minGlyphIndex and index relative to that
0N/A * if we need to save space.
0N/A */
0N/A int glyphLenArray = getMaxGlyph(pScalerContext);
0N/A
0N/A /* This shouldn't be necessary - its a precaution */
0N/A if (glyphLenArray < numGlyphs) {
0N/A glyphLenArray = numGlyphs;
0N/A }
0N/A intGlyphImages = new int[glyphLenArray];
0N/A this.disposer.intGlyphImages = intGlyphImages;
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A private long[] getLongGlyphImages() {
1686N/A if (longGlyphImages == null && longAddresses) {
0N/A
0N/A /* We could obtain minGlyphIndex and index relative to that
0N/A * if we need to save space.
0N/A */
0N/A int glyphLenArray = getMaxGlyph(pScalerContext);
0N/A
0N/A /* This shouldn't be necessary - its a precaution */
0N/A if (glyphLenArray < numGlyphs) {
0N/A glyphLenArray = numGlyphs;
0N/A }
0N/A longGlyphImages = new long[glyphLenArray];
0N/A this.disposer.longGlyphImages = longGlyphImages;
0N/A }
0N/A return longGlyphImages;
0N/A }
0N/A
0N/A NativeStrike(NativeFont nativeFont, FontStrikeDesc desc,
0N/A boolean nocache) {
0N/A super(nativeFont, desc);
0N/A this.nativeFont = nativeFont;
0N/A
0N/A int ptSize = (int)desc.glyphTx.getScaleY();
0N/A double scale = desc.devTx.getScaleX(); // uniform scale
0N/A byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
0N/A pScalerContext = createScalerContext(nameBytes, ptSize, scale);
0N/A
0N/A int numGlyphs = nativeFont.getMapper().getNumGlyphs();
0N/A }
0N/A
0N/A /* We want the native font to be responsible for reporting the
0N/A * font metrics, even if it often delegates to another font.
0N/A * The code here isn't yet implementing exactly that. If the glyph
0N/A * transform was something native couldn't handle, there's no native
0N/A * context from which to obtain metrics. Need to revise this to obtain
0N/A * the metrics and transform them. But currently in such a case it
0N/A * gets the metrics from a different font - its glyph delegate font.
0N/A */
0N/A StrikeMetrics getFontMetrics() {
0N/A if (strikeMetrics == null) {
0N/A if (pScalerContext != 0) {
0N/A strikeMetrics = nativeFont.getFontMetrics(pScalerContext);
0N/A }
0N/A if (strikeMetrics != null && fontTx != null) {
0N/A strikeMetrics.convertToUserSpace(fontTx);
0N/A }
0N/A }
0N/A return strikeMetrics;
0N/A }
0N/A
0N/A private native long createScalerContext(byte[] nameBytes,
0N/A int ptSize, double scale);
0N/A
0N/A private native int getMaxGlyph(long pScalerContext);
0N/A
0N/A private native long createNullScalerContext();
0N/A
0N/A void getGlyphImagePtrs(int[] glyphCodes, long[] images,int len) {
0N/A for (int i=0; i<len; i++) {
0N/A images[i] = getGlyphImagePtr(glyphCodes[i]);
0N/A }
0N/A }
0N/A
0N/A long getGlyphImagePtr(int glyphCode) {
0N/A long glyphPtr;
0N/A
0N/A if (usingIntGlyphImages()) {
0N/A if ((glyphPtr = intGlyphImages[glyphCode] & INTMASK) != 0L) {
0N/A return glyphPtr;
0N/A } else {
0N/A glyphPtr = nativeFont.getGlyphImage(pScalerContext,glyphCode);
0N/A /* Synchronize in case some other thread has updated this
0N/A * cache entry already - unlikely but possible.
0N/A */
0N/A synchronized (this) {
0N/A if (intGlyphImages[glyphCode] == 0) {
0N/A intGlyphImages[glyphCode] = (int)glyphPtr;
0N/A return glyphPtr;
0N/A } else {
0N/A StrikeCache.freeIntPointer((int)glyphPtr);
0N/A return intGlyphImages[glyphCode] & INTMASK;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A /* must be using long (8 byte) addresses */
0N/A else if ((glyphPtr = getLongGlyphImages()[glyphCode]) != 0L) {
0N/A return glyphPtr;
0N/A } else {
0N/A glyphPtr = nativeFont.getGlyphImage(pScalerContext, glyphCode);
0N/A
0N/A synchronized (this) {
0N/A if (longGlyphImages[glyphCode] == 0L) {
0N/A longGlyphImages[glyphCode] = glyphPtr;
0N/A return glyphPtr;
0N/A } else {
0N/A StrikeCache.freeLongPointer(glyphPtr);
0N/A return longGlyphImages[glyphCode];
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* This is used when a FileFont uses the native names to create a
0N/A * delegate NativeFont/Strike to get images from native. This is used
0N/A * because Solaris TrueType fonts have external PCF bitmaps rather than
0N/A * embedded bitmaps. This is really only important for CJK fonts as
0N/A * for most scripts the external X11 bitmaps aren't much better - if
0N/A * at all - than the results from hinting the outlines.
0N/A */
0N/A long getGlyphImagePtrNoCache(int glyphCode) {
0N/A return nativeFont.getGlyphImageNoDefault(pScalerContext, glyphCode);
0N/A }
0N/A
0N/A void getGlyphImageBounds(int glyphcode, Point2D.Float pt,
0N/A Rectangle result) {
0N/A }
0N/A
0N/A Point2D.Float getGlyphMetrics(int glyphCode) {
0N/A Point2D.Float pt = new Point2D.Float(getGlyphAdvance(glyphCode), 0f);
0N/A return pt;
0N/A }
0N/A
0N/A float getGlyphAdvance(int glyphCode) {
0N/A return nativeFont.getGlyphAdvance(pScalerContext, glyphCode);
0N/A }
0N/A
0N/A Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {
0N/A return nativeFont.getGlyphOutlineBounds(pScalerContext, glyphCode);
0N/A }
0N/A
0N/A GeneralPath getGlyphOutline(int glyphCode, float x, float y) {
0N/A return new GeneralPath();
0N/A }
0N/A
0N/A GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {
0N/A return new GeneralPath();
0N/A }
0N/A
0N/A}
0N/A
0N/A/* Returned instead of a NativeStrike.
0N/A * It can intercept any request it wants, but mostly
0N/A * passes them on to its delegate strike. It is important that
0N/A * it override all the inherited FontStrike methods to delegate them
0N/A * appropriately.
0N/A */
0N/A
0N/Aclass DelegateStrike extends NativeStrike {
0N/A
0N/A private FontStrike delegateStrike;
0N/A
0N/A DelegateStrike(NativeFont nativeFont, FontStrikeDesc desc,
0N/A FontStrike delegate) {
0N/A super(nativeFont, desc);
0N/A this.delegateStrike = delegate;
0N/A }
0N/A
0N/A /* We want the native font to be responsible for reporting the
0N/A * font metrics, even if it often delegates to another font.
0N/A * The code here isn't yet implementing exactly that. If the glyph
0N/A * transform was something native couldn't handle, there's no native
0N/A * context from which to obtain metrics. Need to revise this to obtain
0N/A * the metrics and transform them. But currently in such a case it
0N/A * gets the metrics from a different font - its glyph delegate font.
0N/A */
0N/A StrikeMetrics getFontMetrics() {
0N/A if (strikeMetrics == null) {
0N/A if (pScalerContext != 0) {
0N/A strikeMetrics = super.getFontMetrics();
0N/A }
0N/A if (strikeMetrics == null) {
0N/A strikeMetrics = delegateStrike.getFontMetrics();
0N/A }
0N/A }
0N/A return strikeMetrics;
0N/A }
0N/A
0N/A void getGlyphImagePtrs(int[] glyphCodes, long[] images,int len) {
0N/A delegateStrike.getGlyphImagePtrs(glyphCodes, images, len);
0N/A }
0N/A
0N/A long getGlyphImagePtr(int glyphCode) {
0N/A return delegateStrike.getGlyphImagePtr(glyphCode);
0N/A }
0N/A
0N/A void getGlyphImageBounds(int glyphCode,
0N/A Point2D.Float pt, Rectangle result) {
0N/A delegateStrike.getGlyphImageBounds(glyphCode, pt, result);
0N/A }
0N/A
0N/A Point2D.Float getGlyphMetrics(int glyphCode) {
0N/A return delegateStrike.getGlyphMetrics(glyphCode);
0N/A }
0N/A
0N/A float getGlyphAdvance(int glyphCode) {
0N/A return delegateStrike.getGlyphAdvance(glyphCode);
0N/A }
0N/A
0N/A Point2D.Float getCharMetrics(char ch) {
0N/A return delegateStrike.getCharMetrics(ch);
0N/A }
0N/A
0N/A float getCodePointAdvance(int cp) {
0N/A if (cp < 0 || cp >= 0x10000) {
0N/A cp = 0xffff;
0N/A }
0N/A return delegateStrike.getGlyphAdvance(cp);
0N/A }
0N/A
0N/A Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {
0N/A return delegateStrike.getGlyphOutlineBounds(glyphCode);
0N/A }
0N/A
0N/A GeneralPath getGlyphOutline(int glyphCode, float x, float y) {
0N/A return delegateStrike.getGlyphOutline(glyphCode, x, y);
0N/A }
0N/A
0N/A GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {
0N/A return delegateStrike.getGlyphVectorOutline(glyphs, x, y);
0N/A }
0N/A
0N/A}