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;
1686N/Aimport java.lang.reflect.Constructor;
1686N/A
1686N/Aimport sun.java2d.Disposer;
0N/Aimport sun.java2d.DisposerRecord;
0N/A
0N/A/* FontScaler is "internal interface" to font rasterizer library.
0N/A *
0N/A * Access to native rasterizers without going through this interface is
0N/A * strongly discouraged. In particular, this is important because native
0N/A * data could be disposed due to runtime font processing error at any time.
0N/A *
0N/A * FontScaler represents combination of particular rasterizer implementation
0N/A * and particular font. It does not include rasterization attributes such as
0N/A * transform. These attributes are part of native scalerContext object.
0N/A * This approach allows to share same scaler for different requests related
0N/A * to the same font file.
0N/A *
0N/A * Note that scaler may throw FontScalerException on any operation.
0N/A * Generally this means that runtime error had happened and scaler is not
0N/A * usable. Subsequent calls to this scaler should not cause crash but will
0N/A * likely cause exceptions to be thrown again.
0N/A *
0N/A * It is recommended that callee should replace its reference to the scaler
0N/A * with something else. For instance it could be FontManager.getNullScaler().
0N/A * Note that NullScaler is trivial and will not actually rasterize anything.
0N/A *
0N/A * Alternatively, callee can use more sophisticated error recovery strategies
0N/A * and for instance try to substitute failed scaler with new scaler instance
0N/A * using another font.
0N/A *
0N/A * Note that in case of error there is no need to call dispose(). Moreover,
0N/A * dispose() generally is called by Disposer thread and explicit calls to
0N/A * dispose might have unexpected sideeffects because scaler can be shared.
0N/A *
0N/A * Current disposing logic is the following:
0N/A * - scaler is registered in the Disposer by the FontManager (on creation)
0N/A * - scalers are disposed when associated Font2D object (e.g. TruetypeFont)
0N/A * is garbage collected. That's why this object implements DisposerRecord
0N/A * interface directly (as it is not used as indicator when it is safe
0N/A * to release native state) and that's why we have to use WeakReference
0N/A * to Font internally.
0N/A * - Majority of Font2D objects are linked from various mapping arrays
0N/A * (e.g. FontManager.localeFullNamesToFont). So, they are not collected.
0N/A * This logic only works for fonts created with Font.createFont()
0N/A *
0N/A * Notes:
0N/A * - Eventually we may consider releasing some of the scaler resources if
0N/A * it was not used for a while but we do not want to be too aggressive on
0N/A * this (and this is probably more important for Type1 fonts).
0N/A */
0N/Apublic abstract class FontScaler implements DisposerRecord {
1686N/A
1686N/A private static FontScaler nullScaler = null;
1686N/A private static Constructor<FontScaler> scalerConstructor = null;
1686N/A
1686N/A //Find preferred font scaler
1686N/A //
1686N/A //NB: we can allow property based preferences
1686N/A // (theoretically logic can be font type specific)
1686N/A static {
1686N/A Class scalerClass = null;
1686N/A Class arglst[] = new Class[] {Font2D.class, int.class,
1686N/A boolean.class, int.class};
1686N/A
1686N/A try {
1686N/A if (FontUtilities.isOpenJDK) {
1686N/A scalerClass = Class.forName("sun.font.FreetypeFontScaler");
1686N/A } else {
1686N/A scalerClass = Class.forName("sun.font.T2KFontScaler");
1686N/A }
1686N/A } catch (ClassNotFoundException e) {
1686N/A scalerClass = NullFontScaler.class;
1686N/A }
1686N/A
1686N/A //NB: rewrite using factory? constructor is ugly way
1686N/A try {
1686N/A scalerConstructor = scalerClass.getConstructor(arglst);
1686N/A } catch (NoSuchMethodException e) {
1686N/A //should not happen
1686N/A }
1686N/A }
1686N/A
1686N/A /* This is the only place to instantiate new FontScaler.
1686N/A * Therefore this is very convinient place to register
1686N/A * scaler with Disposer as well as trigger deregistring bad font
1686N/A * in case when scaler reports this.
1686N/A */
1686N/A public static FontScaler getScaler(Font2D font,
1686N/A int indexInCollection,
1686N/A boolean supportsCJK,
1686N/A int filesize) {
1686N/A FontScaler scaler = null;
1686N/A
1686N/A try {
1686N/A Object args[] = new Object[] {font, indexInCollection,
1686N/A supportsCJK, filesize};
1686N/A scaler = scalerConstructor.newInstance(args);
1686N/A Disposer.addObjectRecord(font, scaler);
1686N/A } catch (Throwable e) {
1686N/A scaler = nullScaler;
1686N/A
1686N/A //if we can not instantiate scaler assume bad font
1686N/A //NB: technically it could be also because of internal scaler
1686N/A // error but here we are assuming scaler is ok.
1686N/A FontManager fm = FontManagerFactory.getInstance();
1686N/A fm.deRegisterBadFont(font);
1686N/A }
1686N/A return scaler;
1686N/A }
1686N/A
1686N/A /*
1686N/A * At the moment it is harmless to create 2 null scalers so, technically,
1686N/A * syncronized keyword is not needed.
1686N/A *
1686N/A * But it is safer to keep it to avoid subtle problems if we will be adding
1686N/A * checks like whether scaler is null scaler.
1686N/A */
1686N/A public static synchronized FontScaler getNullScaler() {
1686N/A if (nullScaler == null) {
1686N/A nullScaler = new NullFontScaler();
1686N/A }
1686N/A return nullScaler;
1686N/A }
1686N/A
0N/A protected WeakReference<Font2D> font = null;
0N/A protected long nativeScaler = 0; //used by decendants
0N/A //that have native state
0N/A protected boolean disposed = false;
0N/A
0N/A abstract StrikeMetrics getFontMetrics(long pScalerContext)
0N/A throws FontScalerException;
0N/A
0N/A abstract float getGlyphAdvance(long pScalerContext, int glyphCode)
0N/A throws FontScalerException;
0N/A
0N/A abstract void getGlyphMetrics(long pScalerContext, int glyphCode,
0N/A Point2D.Float metrics)
0N/A throws FontScalerException;
0N/A
0N/A /*
0N/A * Returns pointer to native GlyphInfo object.
0N/A * Callee is responsible for freeing this memory.
0N/A *
0N/A * Note:
0N/A * currently this method has to return not 0L but pointer to valid
0N/A * GlyphInfo object. Because Strike and drawing releated logic does
0N/A * expect that.
0N/A * In the future we may want to rework this to allow 0L here.
0N/A */
0N/A abstract long getGlyphImage(long pScalerContext, int glyphCode)
0N/A throws FontScalerException;
0N/A
0N/A abstract Rectangle2D.Float getGlyphOutlineBounds(long pContext,
0N/A int glyphCode)
0N/A throws FontScalerException;
0N/A
0N/A abstract GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,
0N/A float x, float y)
0N/A throws FontScalerException;
0N/A
0N/A abstract GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs,
0N/A int numGlyphs, float x, float y)
0N/A throws FontScalerException;
0N/A
0N/A /* Used by Java2D disposer to ensure native resources are released.
0N/A Note: this method does not release any of created
0N/A scaler context objects! */
0N/A public void dispose() {}
0N/A
0N/A /* At the moment these 3 methods are needed for Type1 fonts only.
0N/A * For Truetype fonts we extract required info outside of scaler
0N/A * on java layer.
0N/A */
0N/A abstract int getNumGlyphs() throws FontScalerException;
0N/A abstract int getMissingGlyphCode() throws FontScalerException;
0N/A abstract int getGlyphCode(char charCode) throws FontScalerException;
0N/A
0N/A /* This method returns table cache used by native layout engine.
0N/A * This cache is essentially just small collection of
0N/A * pointers to various truetype tables. See definition of TTLayoutTableCache
0N/A * in the fontscalerdefs.h for more details.
0N/A *
0N/A * Note that tables themselves have same format as defined in the truetype
0N/A * specification, i.e. font scaler do not need to perform any preprocessing.
0N/A *
0N/A * Probably it is better to have API to request pointers to each table
0N/A * separately instead of requesting pointer to some native structure.
0N/A * (then there is not need to share its definition by different
0N/A * implementations of scaler).
0N/A * However, this means multiple JNI calls and potential impact on performance.
0N/A *
0N/A * Note: return value 0 is legal.
0N/A * This means tables are not available (e.g. type1 font).
0N/A */
0N/A abstract long getLayoutTableCache() throws FontScalerException;
0N/A
0N/A /* Used by the OpenType engine for mark positioning. */
0N/A abstract Point2D.Float getGlyphPoint(long pScalerContext,
0N/A int glyphCode, int ptNumber)
0N/A throws FontScalerException;
0N/A
0N/A abstract long getUnitsPerEm();
0N/A
0N/A /* Returns pointer to native structure describing rasterization attributes.
0N/A Format of this structure is scaler-specific.
0N/A
0N/A Callee is responsible for freeing scaler context (using free()).
0N/A
0N/A Note:
0N/A Context is tightly associated with strike and it is actually
0N/A freed when corresponding strike is being released.
0N/A */
0N/A abstract long createScalerContext(double[] matrix,
3351N/A int aa, int fm,
3351N/A float boldness, float italic,
3351N/A boolean disableHinting);
0N/A
0N/A /* Marks context as invalid because native scaler is invalid.
0N/A Notes:
0N/A - pointer itself is still valid and has to be released
0N/A - if pointer to native scaler was cached it
0N/A should not be neither disposed nor used.
0N/A it is very likely it is already disposed by this moment. */
0N/A abstract void invalidateScalerContext(long ppScalerContext);
0N/A}