Path: blob/master/src/java.desktop/share/classes/sun/font/FontScaler.java
41154 views
/*1* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.font;2627import java.awt.geom.GeneralPath;28import java.awt.geom.Point2D;29import java.awt.geom.Rectangle2D;30import java.lang.ref.WeakReference;3132import sun.java2d.Disposer;33import sun.java2d.DisposerRecord;3435/* FontScaler is "internal interface" to font rasterizer library.36*37* Access to native rasterizers without going through this interface is38* strongly discouraged. In particular, this is important because native39* data could be disposed due to runtime font processing error at any time.40*41* FontScaler represents combination of particular rasterizer implementation42* and particular font. It does not include rasterization attributes such as43* transform. These attributes are part of native scalerContext object.44* This approach allows to share same scaler for different requests related45* to the same font file.46*47* Note that scaler may throw FontScalerException on any operation.48* Generally this means that runtime error had happened and scaler is not49* usable. Subsequent calls to this scaler should not cause crash but will50* likely cause exceptions to be thrown again.51*52* It is recommended that callee should replace its reference to the scaler53* with something else. For instance it could be FontManager.getNullScaler().54* Note that NullScaler is trivial and will not actually rasterize anything.55*56* Alternatively, callee can use more sophisticated error recovery strategies57* and for instance try to substitute failed scaler with new scaler instance58* using another font.59*60* Note that in case of error there is no need to call dispose(). Moreover,61* dispose() generally is called by Disposer thread and explicit calls to62* dispose might have unexpected sideeffects because scaler can be shared.63*64* Current disposing logic is the following:65* - scaler is registered in the Disposer by the FontManager (on creation)66* - scalers are disposed when associated Font2D object (e.g. TruetypeFont)67* is garbage collected. That's why this object implements DisposerRecord68* interface directly (as it is not used as indicator when it is safe69* to release native state) and that's why we have to use WeakReference70* to Font internally.71* - Majority of Font2D objects are linked from various mapping arrays72* (e.g. FontManager.localeFullNamesToFont). So, they are not collected.73* This logic only works for fonts created with Font.createFont()74*75* Notes:76* - Eventually we may consider releasing some of the scaler resources if77* it was not used for a while but we do not want to be too aggressive on78* this (and this is probably more important for Type1 fonts).79*/80public abstract class FontScaler implements DisposerRecord {8182private static FontScaler nullScaler = null;8384//Find preferred font scaler85//86//NB: we can allow property based preferences87// (theoretically logic can be font type specific)8889/* This is the only place to instantiate new FontScaler.90* Therefore this is very convinient place to register91* scaler with Disposer as well as trigger deregistering a bad font92* when the scaler reports this.93*/94public static FontScaler getScaler(Font2D font,95int indexInCollection,96boolean supportsCJK,97int filesize) {98FontScaler scaler = null;99100try {101scaler = new FreetypeFontScaler(font, indexInCollection,102supportsCJK, filesize);103Disposer.addObjectRecord(font, scaler);104} catch (Throwable e) {105scaler = getNullScaler();106107//if we can not instantiate scaler assume a bad font108//NB: technically it could be also because of internal scaler109// error but here we are assuming scaler is ok.110FontManager fm = FontManagerFactory.getInstance();111fm.deRegisterBadFont(font);112}113return scaler;114}115116/*117* At the moment it is harmless to create 2 null scalers so, technically,118* syncronized keyword is not needed.119*120* But it is safer to keep it to avoid subtle problems if we will be adding121* checks like whether scaler is null scaler.122*/123public static synchronized FontScaler getNullScaler() {124if (nullScaler == null) {125nullScaler = new NullFontScaler();126}127return nullScaler;128}129130protected WeakReference<Font2D> font = null;131protected long nativeScaler = 0; //used by decendants132//that have native state133protected boolean disposed = false;134135abstract StrikeMetrics getFontMetrics(long pScalerContext)136throws FontScalerException;137138abstract float getGlyphAdvance(long pScalerContext, int glyphCode)139throws FontScalerException;140141abstract void getGlyphMetrics(long pScalerContext, int glyphCode,142Point2D.Float metrics)143throws FontScalerException;144145/*146* Returns pointer to native GlyphInfo object.147* Callee is responsible for freeing this memory.148*149* Note:150* currently this method has to return not 0L but pointer to valid151* GlyphInfo object. Because Strike and drawing releated logic does152* expect that.153* In the future we may want to rework this to allow 0L here.154*/155abstract long getGlyphImage(long pScalerContext, int glyphCode)156throws FontScalerException;157158abstract Rectangle2D.Float getGlyphOutlineBounds(long pContext,159int glyphCode)160throws FontScalerException;161162abstract GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,163float x, float y)164throws FontScalerException;165166abstract GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs,167int numGlyphs, float x, float y)168throws FontScalerException;169170/* Used by Java2D disposer to ensure native resources are released.171Note: this method does not release any of created172scaler context objects! */173public void dispose() {}174175/**176* Used when the native resources held by the scaler need177* to be released before the 2D disposer runs.178*/179public void disposeScaler() {}180181/* At the moment these 3 methods are needed for Type1 fonts only.182* For Truetype fonts we extract required info outside of scaler183* on java layer.184*/185abstract int getNumGlyphs() throws FontScalerException;186abstract int getMissingGlyphCode() throws FontScalerException;187abstract int getGlyphCode(char charCode) throws FontScalerException;188189/* Used by the OpenType engine for mark positioning. */190abstract Point2D.Float getGlyphPoint(long pScalerContext,191int glyphCode, int ptNumber)192throws FontScalerException;193194abstract long getUnitsPerEm();195196/* Returns pointer to native structure describing rasterization attributes.197Format of this structure is scaler-specific.198199Callee is responsible for freeing scaler context (using free()).200201Note:202Context is tightly associated with strike and it is actually203freed when corresponding strike is being released.204*/205abstract long createScalerContext(double[] matrix,206int aa, int fm,207float boldness, float italic);208209/* Marks context as invalid because native scaler is invalid.210Notes:211- pointer itself is still valid and has to be released212- if pointer to native scaler was cached it213should not be neither disposed nor used.214it is very likely it is already disposed by this moment. */215abstract void invalidateScalerContext(long ppScalerContext);216}217218219