0N/A/*
3909N/A * Copyright (c) 2007, 2011, 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.GeneralPath;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.lang.ref.WeakReference;
0N/A
0N/A/* This is Freetype based implementation of FontScaler.
0N/A *
0N/A * Note that in case of runtime error it is expected that
0N/A * native code will release all native resources and
0N/A * call invalidateScaler() (that will throw FontScalerException).
0N/A *
0N/A * Note that callee is responsible for releasing native scaler context.
0N/A */
0N/Aclass FreetypeFontScaler extends FontScaler {
0N/A /* constants aligned with native code */
0N/A private static final int TRUETYPE_FONT = 1;
0N/A private static final int TYPE1_FONT = 2;
0N/A
0N/A static {
0N/A /* At the moment fontmanager library depends on freetype library
0N/A and therefore no need to load it explicitly here */
0N/A FontManagerNativeLibrary.load();
0N/A initIDs(FreetypeFontScaler.class);
0N/A }
0N/A
0N/A private static native void initIDs(Class FFS);
0N/A
0N/A private void invalidateScaler() throws FontScalerException {
0N/A nativeScaler = 0;
0N/A font = null;
0N/A throw new FontScalerException();
0N/A }
0N/A
0N/A public FreetypeFontScaler(Font2D font, int indexInCollection,
0N/A boolean supportsCJK, int filesize) {
0N/A int fonttype = TRUETYPE_FONT;
0N/A if (font instanceof Type1Font) {
0N/A fonttype = TYPE1_FONT;
0N/A }
0N/A nativeScaler = initNativeScaler(font,
0N/A fonttype,
0N/A indexInCollection,
0N/A supportsCJK,
0N/A filesize);
0N/A this.font = new WeakReference(font);
0N/A }
0N/A
0N/A synchronized StrikeMetrics getFontMetrics(long pScalerContext)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getFontMetricsNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler);
0N/A }
1686N/A return FontScaler.getNullScaler().getFontMetrics(0L);
0N/A }
0N/A
0N/A synchronized float getGlyphAdvance(long pScalerContext, int glyphCode)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphAdvanceNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler,
0N/A glyphCode);
0N/A }
1686N/A return FontScaler.getNullScaler().
1686N/A getGlyphAdvance(0L, glyphCode);
0N/A }
0N/A
0N/A synchronized void getGlyphMetrics(long pScalerContext,
0N/A int glyphCode, Point2D.Float metrics)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A getGlyphMetricsNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler,
0N/A glyphCode,
0N/A metrics);
0N/A return;
0N/A }
1686N/A FontScaler.getNullScaler().
1686N/A getGlyphMetrics(0L, glyphCode, metrics);
0N/A }
0N/A
0N/A synchronized long getGlyphImage(long pScalerContext, int glyphCode)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphImageNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler,
0N/A glyphCode);
0N/A }
1686N/A return FontScaler.getNullScaler().
1686N/A getGlyphImage(0L, glyphCode);
0N/A }
0N/A
0N/A synchronized Rectangle2D.Float getGlyphOutlineBounds(
0N/A long pScalerContext, int glyphCode)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphOutlineBoundsNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler,
0N/A glyphCode);
0N/A }
1686N/A return FontScaler.getNullScaler().
1686N/A getGlyphOutlineBounds(0L,glyphCode);
0N/A }
0N/A
0N/A synchronized GeneralPath getGlyphOutline(
0N/A long pScalerContext, int glyphCode, float x, float y)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphOutlineNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler,
0N/A glyphCode,
0N/A x, y);
0N/A }
1686N/A return FontScaler.getNullScaler().
1686N/A getGlyphOutline(0L, glyphCode, x,y);
0N/A }
0N/A
0N/A synchronized GeneralPath getGlyphVectorOutline(
0N/A long pScalerContext, int[] glyphs, int numGlyphs,
0N/A float x, float y) throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphVectorOutlineNative(font.get(),
0N/A pScalerContext,
0N/A nativeScaler,
0N/A glyphs,
0N/A numGlyphs,
0N/A x, y);
0N/A }
1686N/A return FontScaler
1686N/A .getNullScaler().getGlyphVectorOutline(0L, glyphs, numGlyphs, x, y);
0N/A }
0N/A
0N/A synchronized long getLayoutTableCache() throws FontScalerException {
0N/A return getLayoutTableCacheNative(nativeScaler);
0N/A }
0N/A
0N/A public synchronized void dispose() {
0N/A if (nativeScaler != 0L) {
0N/A disposeNativeScaler(nativeScaler);
0N/A nativeScaler = 0L;
0N/A }
0N/A }
0N/A
0N/A synchronized int getNumGlyphs() throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getNumGlyphsNative(nativeScaler);
0N/A }
1686N/A return FontScaler.getNullScaler().getNumGlyphs();
0N/A }
0N/A
0N/A synchronized int getMissingGlyphCode() throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getMissingGlyphCodeNative(nativeScaler);
0N/A }
1686N/A return FontScaler.getNullScaler().getMissingGlyphCode();
0N/A }
0N/A
0N/A synchronized int getGlyphCode(char charCode) throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphCodeNative(nativeScaler, charCode);
0N/A }
1686N/A return FontScaler.getNullScaler().getGlyphCode(charCode);
0N/A }
0N/A
0N/A synchronized Point2D.Float getGlyphPoint(long pScalerContext,
0N/A int glyphCode, int ptNumber)
0N/A throws FontScalerException {
0N/A if (nativeScaler != 0L) {
0N/A return getGlyphPointNative(font.get(), pScalerContext,
0N/A nativeScaler, glyphCode, ptNumber);
0N/A }
1686N/A return FontScaler.getNullScaler().getGlyphPoint(
0N/A pScalerContext, glyphCode, ptNumber);
0N/A }
0N/A
0N/A synchronized long getUnitsPerEm() {
0N/A return getUnitsPerEMNative(nativeScaler);
0N/A }
0N/A
4257N/A long createScalerContext(double[] matrix,
3351N/A int aa, int fm, float boldness, float italic,
3351N/A boolean disableHinting) {
0N/A if (nativeScaler != 0L) {
0N/A return createScalerContextNative(nativeScaler, matrix,
4257N/A aa, fm, boldness, italic);
0N/A }
0N/A return NullFontScaler.getNullScalerContext();
0N/A }
0N/A
0N/A //Note: native methods can throw RuntimeException if processing fails
0N/A private native long initNativeScaler(Font2D font, int type,
0N/A int indexInCollection, boolean supportsCJK, int filesize);
0N/A private native StrikeMetrics getFontMetricsNative(Font2D font,
0N/A long pScalerContext, long pScaler);
0N/A private native float getGlyphAdvanceNative(Font2D font,
0N/A long pScalerContext, long pScaler, int glyphCode);
0N/A private native void getGlyphMetricsNative(Font2D font,
0N/A long pScalerContext, long pScaler,
0N/A int glyphCode, Point2D.Float metrics);
0N/A private native long getGlyphImageNative(Font2D font,
0N/A long pScalerContext, long pScaler, int glyphCode);
0N/A private native Rectangle2D.Float getGlyphOutlineBoundsNative(Font2D font,
0N/A long pScalerContext, long pScaler, int glyphCode);
0N/A private native GeneralPath getGlyphOutlineNative(Font2D font,
0N/A long pScalerContext, long pScaler,
0N/A int glyphCode, float x, float y);
0N/A private native GeneralPath getGlyphVectorOutlineNative(Font2D font,
0N/A long pScalerContext, long pScaler,
0N/A int[] glyphs, int numGlyphs, float x, float y);
0N/A native Point2D.Float getGlyphPointNative(Font2D font,
0N/A long pScalerContext, long pScaler, int glyphCode, int ptNumber);
0N/A
0N/A private native long getLayoutTableCacheNative(long pScaler);
0N/A
0N/A private native void disposeNativeScaler(long pScaler);
0N/A
0N/A private native int getGlyphCodeNative(long pScaler, char charCode);
0N/A private native int getNumGlyphsNative(long pScaler);
0N/A private native int getMissingGlyphCodeNative(long pScaler);
0N/A
0N/A private native long getUnitsPerEMNative(long pScaler);
0N/A
0N/A native long createScalerContextNative(long pScaler, double[] matrix,
4257N/A int aa, int fm, float boldness, float italic);
0N/A
0N/A /* Freetype scaler context does not contain any pointers that
0N/A has to be invalidated if native scaler is bad */
0N/A void invalidateScalerContext(long pScalerContext) {}
0N/A}