src/java.desktop/macosx/classes/sun/font/CFont.java

Print this page




  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 package sun.font;
  27 
  28 import java.awt.Font;
  29 import java.awt.font.FontRenderContext;
  30 import java.awt.geom.AffineTransform;
  31 import java.awt.geom.GeneralPath;;
  32 import java.awt.geom.Point2D;
  33 import java.awt.geom.Rectangle2D;

  34 
  35 // Right now this class is final to avoid a problem with native code.
  36 // For some reason the JNI IsInstanceOf was not working correctly
  37 // so we are checking the class specifically. If we subclass this
  38 // we need to modify the native code in CFontWrapper.m
  39 public final class CFont extends PhysicalFont {
  40 
  41     /* CFontStrike doesn't call these methods so they are unimplemented.
  42      * They are here to meet the requirements of PhysicalFont, needed
  43      * because a CFont can sometimes be returned where a PhysicalFont
  44      * is expected.
  45      */
  46     StrikeMetrics getFontMetrics(long pScalerContext) {
  47        throw new InternalError("Not implemented");
  48     }
  49 
  50     float getGlyphAdvance(long pScalerContext, int glyphCode) {
  51        throw new InternalError("Not implemented");
  52     }
  53 
  54     void getGlyphMetrics(long pScalerContext, int glyphCode,
  55                                   Point2D.Float metrics) {
  56        throw new InternalError("Not implemented");
  57     }
  58 
  59     long getGlyphImage(long pScalerContext, int glyphCode) {
  60        throw new InternalError("Not implemented");
  61     }
  62 
  63     Rectangle2D.Float getGlyphOutlineBounds(long pScalerContext,
  64                                                      int glyphCode) {
  65        throw new InternalError("Not implemented");
  66     }
  67 
  68     GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,
  69                                          float x, float y) {
  70        throw new InternalError("Not implemented");
  71     }
  72 
  73     GeneralPath getGlyphVectorOutline(long pScalerContext,
  74                                                int[] glyphs, int numGlyphs,
  75                                                float x, float y) {
  76        throw new InternalError("Not implemented");
  77     }
  78 














  79     private static native long createNativeFont(final String nativeFontName,
  80                                                 final int style);
  81     private static native void disposeNativeFont(final long nativeFontPtr);
  82 
  83     private boolean isFakeItalic;
  84     private String nativeFontName;
  85     private long nativeFontPtr;
  86 
  87     private native float getWidthNative(final long nativeFontPtr);
  88     private native float getWeightNative(final long nativeFontPtr);
  89 
  90     private int fontWidth = -1;
  91     private int fontWeight = -1;
  92 
  93     @Override
  94     public int getWidth() {
  95         if (fontWidth == -1) {
  96             // Apple use a range of -1 -> +1, where 0.0 is normal
  97             // OpenType uses a % range from 50% -> 200% where 100% is normal
  98             // and maps these onto the integer values 1->9.


 162         fullName = logicalFamilyName;
 163         familyName = logicalFamilyName;
 164         nativeFontName = other.nativeFontName;
 165         style = other.style;
 166         isFakeItalic = other.isFakeItalic;
 167     }
 168 
 169     public CFont createItalicVariant() {
 170         CFont font = new CFont(this, familyName);
 171         font.nativeFontName = fullName;
 172         font.fullName =
 173             fullName + (style == Font.BOLD ? "" : "-") + "Italic-Derived";
 174         font.style |= Font.ITALIC;
 175         font.isFakeItalic = true;
 176         return font;
 177     }
 178 
 179     protected synchronized long getNativeFontPtr() {
 180         if (nativeFontPtr == 0L) {
 181             nativeFontPtr = createNativeFont(nativeFontName, style);
 182 }
 183         return nativeFontPtr;
 184     }
 185 









































 186     protected synchronized void finalize() {
 187         if (nativeFontPtr != 0) {
 188             disposeNativeFont(nativeFontPtr);
 189         }
 190         nativeFontPtr = 0;
 191     }
 192 
 193     protected CharToGlyphMapper getMapper() {
 194         if (mapper == null) {
 195             mapper = new CCharToGlyphMapper(this);
 196         }
 197         return mapper;
 198     }
 199 
 200     protected FontStrike createStrike(FontStrikeDesc desc) {
 201         if (isFakeItalic) {
 202             desc = new FontStrikeDesc(desc);
 203             desc.glyphTx.concatenate(AffineTransform.getShearInstance(-0.2, 0));
 204         }
 205         return new CStrike(this, desc);


  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 package sun.font;
  27 
  28 import java.awt.Font;
  29 import java.awt.font.FontRenderContext;
  30 import java.awt.geom.AffineTransform;
  31 import java.awt.geom.GeneralPath;;
  32 import java.awt.geom.Point2D;
  33 import java.awt.geom.Rectangle2D;
  34 import java.util.ArrayList;
  35 
  36 // Right now this class is final to avoid a problem with native code.
  37 // For some reason the JNI IsInstanceOf was not working correctly
  38 // so we are checking the class specifically. If we subclass this
  39 // we need to modify the native code in CFontWrapper.m
  40 public final class CFont extends PhysicalFont implements FontSubstitution {
  41 
  42     /* CFontStrike doesn't call these methods so they are unimplemented.
  43      * They are here to meet the requirements of PhysicalFont, needed
  44      * because a CFont can sometimes be returned where a PhysicalFont
  45      * is expected.
  46      */
  47     StrikeMetrics getFontMetrics(long pScalerContext) {
  48        throw new InternalError("Not implemented");
  49     }
  50 
  51     float getGlyphAdvance(long pScalerContext, int glyphCode) {
  52        throw new InternalError("Not implemented");
  53     }
  54 
  55     void getGlyphMetrics(long pScalerContext, int glyphCode,
  56                                   Point2D.Float metrics) {
  57        throw new InternalError("Not implemented");
  58     }
  59 
  60     long getGlyphImage(long pScalerContext, int glyphCode) {
  61        throw new InternalError("Not implemented");
  62     }
  63 
  64     Rectangle2D.Float getGlyphOutlineBounds(long pScalerContext,
  65                                                      int glyphCode) {
  66        throw new InternalError("Not implemented");
  67     }
  68 
  69     GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,
  70                                          float x, float y) {
  71        throw new InternalError("Not implemented");
  72     }
  73 
  74     GeneralPath getGlyphVectorOutline(long pScalerContext,
  75                                                int[] glyphs, int numGlyphs,
  76                                                float x, float y) {
  77        throw new InternalError("Not implemented");
  78     }
  79 
  80     @Override
  81     protected long getLayoutTableCache() {
  82         return getLayoutTableCacheNative(getNativeFontPtr());
  83     }
  84 
  85     @Override
  86     protected byte[] getTableBytes(int tag) {
  87         return getTableBytesNative(getNativeFontPtr(), tag);
  88     }
  89 
  90     private native synchronized long getLayoutTableCacheNative(long nativeFontPtr);
  91 
  92     private native byte[] getTableBytesNative(long nativeFontPtr, int tag);
  93 
  94     private static native long createNativeFont(final String nativeFontName,
  95                                                 final int style);
  96     private static native void disposeNativeFont(final long nativeFontPtr);
  97 
  98     private boolean isFakeItalic;
  99     private String nativeFontName;
 100     private long nativeFontPtr;
 101 
 102     private native float getWidthNative(final long nativeFontPtr);
 103     private native float getWeightNative(final long nativeFontPtr);
 104 
 105     private int fontWidth = -1;
 106     private int fontWeight = -1;
 107 
 108     @Override
 109     public int getWidth() {
 110         if (fontWidth == -1) {
 111             // Apple use a range of -1 -> +1, where 0.0 is normal
 112             // OpenType uses a % range from 50% -> 200% where 100% is normal
 113             // and maps these onto the integer values 1->9.


 177         fullName = logicalFamilyName;
 178         familyName = logicalFamilyName;
 179         nativeFontName = other.nativeFontName;
 180         style = other.style;
 181         isFakeItalic = other.isFakeItalic;
 182     }
 183 
 184     public CFont createItalicVariant() {
 185         CFont font = new CFont(this, familyName);
 186         font.nativeFontName = fullName;
 187         font.fullName =
 188             fullName + (style == Font.BOLD ? "" : "-") + "Italic-Derived";
 189         font.style |= Font.ITALIC;
 190         font.isFakeItalic = true;
 191         return font;
 192     }
 193 
 194     protected synchronized long getNativeFontPtr() {
 195         if (nativeFontPtr == 0L) {
 196             nativeFontPtr = createNativeFont(nativeFontName, style);
 197         }
 198         return nativeFontPtr;
 199     }
 200 
 201     static native void getCascadeList(long nativeFontPtr, ArrayList<String> listOfString);
 202 
 203     private CompositeFont createCompositeFont() {
 204         ArrayList<String> listOfString = new ArrayList<String>();
 205         getCascadeList(nativeFontPtr, listOfString);
 206          
 207         FontManager fm = FontManagerFactory.getInstance();
 208         int numFonts = 1 + listOfString.size();
 209         PhysicalFont[] fonts = new PhysicalFont[numFonts];
 210         fonts[0] = this;
 211         int idx = 1;
 212         for (String s : listOfString) {
 213             if (s.equals(".AppleSymbolsFB"))  {
 214                 // Don't know why we get the weird name above .. replace.
 215                 s = "AppleSymbols";
 216             }
 217             Font2D f2d = fm.findFont2D(s, Font.PLAIN, FontManager.NO_FALLBACK);
 218             if (f2d == null || f2d == this) {
 219                 continue;
 220             }
 221             fonts[idx++] = (PhysicalFont)f2d;
 222         } 
 223         if (idx < fonts.length) {
 224             PhysicalFont[] orig = fonts;
 225             fonts = new PhysicalFont[idx];
 226             System.arraycopy(orig, 0, fonts, 0, idx);
 227         }
 228         CompositeFont compFont = new CompositeFont(fonts);
 229         compFont.mapper = new CCompositeGlyphMapper(compFont); 
 230         return compFont;
 231     }
 232 
 233     private CompositeFont compFont;
 234 
 235     public CompositeFont getCompositeFont2D() {
 236         if (compFont == null) {
 237            compFont = createCompositeFont();
 238         }
 239         return compFont;
 240     }
 241 
 242     protected synchronized void finalize() {
 243         if (nativeFontPtr != 0) {
 244             disposeNativeFont(nativeFontPtr);
 245         }
 246         nativeFontPtr = 0;
 247     }
 248 
 249     protected CharToGlyphMapper getMapper() {
 250         if (mapper == null) {
 251             mapper = new CCharToGlyphMapper(this);
 252         }
 253         return mapper;
 254     }
 255 
 256     protected FontStrike createStrike(FontStrikeDesc desc) {
 257         if (isFakeItalic) {
 258             desc = new FontStrikeDesc(desc);
 259             desc.glyphTx.concatenate(AffineTransform.getShearInstance(-0.2, 0));
 260         }
 261         return new CStrike(this, desc);