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