Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/font/FontScaler.java
38829 views
/*1* Copyright (c) 2007, 2011, 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;31import java.lang.reflect.Constructor;3233import sun.java2d.Disposer;34import sun.java2d.DisposerRecord;3536/* FontScaler is "internal interface" to font rasterizer library.37*38* Access to native rasterizers without going through this interface is39* strongly discouraged. In particular, this is important because native40* data could be disposed due to runtime font processing error at any time.41*42* FontScaler represents combination of particular rasterizer implementation43* and particular font. It does not include rasterization attributes such as44* transform. These attributes are part of native scalerContext object.45* This approach allows to share same scaler for different requests related46* to the same font file.47*48* Note that scaler may throw FontScalerException on any operation.49* Generally this means that runtime error had happened and scaler is not50* usable. Subsequent calls to this scaler should not cause crash but will51* likely cause exceptions to be thrown again.52*53* It is recommended that callee should replace its reference to the scaler54* with something else. For instance it could be FontManager.getNullScaler().55* Note that NullScaler is trivial and will not actually rasterize anything.56*57* Alternatively, callee can use more sophisticated error recovery strategies58* and for instance try to substitute failed scaler with new scaler instance59* using another font.60*61* Note that in case of error there is no need to call dispose(). Moreover,62* dispose() generally is called by Disposer thread and explicit calls to63* dispose might have unexpected sideeffects because scaler can be shared.64*65* Current disposing logic is the following:66* - scaler is registered in the Disposer by the FontManager (on creation)67* - scalers are disposed when associated Font2D object (e.g. TruetypeFont)68* is garbage collected. That's why this object implements DisposerRecord69* interface directly (as it is not used as indicator when it is safe70* to release native state) and that's why we have to use WeakReference71* to Font internally.72* - Majority of Font2D objects are linked from various mapping arrays73* (e.g. FontManager.localeFullNamesToFont). So, they are not collected.74* This logic only works for fonts created with Font.createFont()75*76* Notes:77* - Eventually we may consider releasing some of the scaler resources if78* it was not used for a while but we do not want to be too aggressive on79* this (and this is probably more important for Type1 fonts).80*/81public abstract class FontScaler implements DisposerRecord {8283private static FontScaler nullScaler = null;84private static Constructor<FontScaler> scalerConstructor = null;8586//Find preferred font scaler87//88//NB: we can allow property based preferences89// (theoretically logic can be font type specific)90static {91Class scalerClass = null;92Class arglst[] = new Class[] {Font2D.class, int.class,93boolean.class, int.class};9495try {96if (FontUtilities.isOpenJDK) {97scalerClass = Class.forName("sun.font.FreetypeFontScaler");98} else {99scalerClass = Class.forName("sun.font.T2KFontScaler");100}101} catch (ClassNotFoundException e) {102scalerClass = NullFontScaler.class;103}104105//NB: rewrite using factory? constructor is ugly way106try {107scalerConstructor = scalerClass.getConstructor(arglst);108} catch (NoSuchMethodException e) {109//should not happen110}111}112113/* This is the only place to instantiate new FontScaler.114* Therefore this is very convinient place to register115* scaler with Disposer as well as trigger deregistring bad font116* in case when scaler reports this.117*/118public static FontScaler getScaler(Font2D font,119int indexInCollection,120boolean supportsCJK,121int filesize) {122FontScaler scaler = null;123124try {125Object args[] = new Object[] {font, indexInCollection,126supportsCJK, filesize};127scaler = scalerConstructor.newInstance(args);128Disposer.addObjectRecord(font, scaler);129} catch (Throwable e) {130scaler = nullScaler;131132//if we can not instantiate scaler assume bad font133//NB: technically it could be also because of internal scaler134// error but here we are assuming scaler is ok.135FontManager fm = FontManagerFactory.getInstance();136fm.deRegisterBadFont(font);137}138return scaler;139}140141/*142* At the moment it is harmless to create 2 null scalers so, technically,143* syncronized keyword is not needed.144*145* But it is safer to keep it to avoid subtle problems if we will be adding146* checks like whether scaler is null scaler.147*/148public static synchronized FontScaler getNullScaler() {149if (nullScaler == null) {150nullScaler = new NullFontScaler();151}152return nullScaler;153}154155protected WeakReference<Font2D> font = null;156protected long nativeScaler = 0; //used by decendants157//that have native state158protected boolean disposed = false;159160abstract StrikeMetrics getFontMetrics(long pScalerContext)161throws FontScalerException;162163abstract float getGlyphAdvance(long pScalerContext, int glyphCode)164throws FontScalerException;165166abstract void getGlyphMetrics(long pScalerContext, int glyphCode,167Point2D.Float metrics)168throws FontScalerException;169170/*171* Returns pointer to native GlyphInfo object.172* Callee is responsible for freeing this memory.173*174* Note:175* currently this method has to return not 0L but pointer to valid176* GlyphInfo object. Because Strike and drawing releated logic does177* expect that.178* In the future we may want to rework this to allow 0L here.179*/180abstract long getGlyphImage(long pScalerContext, int glyphCode)181throws FontScalerException;182183abstract Rectangle2D.Float getGlyphOutlineBounds(long pContext,184int glyphCode)185throws FontScalerException;186187abstract GeneralPath getGlyphOutline(long pScalerContext, int glyphCode,188float x, float y)189throws FontScalerException;190191abstract GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs,192int numGlyphs, float x, float y)193throws FontScalerException;194195/* Used by Java2D disposer to ensure native resources are released.196Note: this method does not release any of created197scaler context objects! */198public void dispose() {}199200/**201* Used when the native resources held by the scaler need202* to be released before the 2D disposer runs.203*/204public void disposeScaler() {}205206/* At the moment these 3 methods are needed for Type1 fonts only.207* For Truetype fonts we extract required info outside of scaler208* on java layer.209*/210abstract int getNumGlyphs() throws FontScalerException;211abstract int getMissingGlyphCode() throws FontScalerException;212abstract int getGlyphCode(char charCode) throws FontScalerException;213214/* This method returns table cache used by native layout engine.215* This cache is essentially just small collection of216* pointers to various truetype tables. See definition of TTLayoutTableCache217* in the fontscalerdefs.h for more details.218*219* Note that tables themselves have same format as defined in the truetype220* specification, i.e. font scaler do not need to perform any preprocessing.221*222* Probably it is better to have API to request pointers to each table223* separately instead of requesting pointer to some native structure.224* (then there is not need to share its definition by different225* implementations of scaler).226* However, this means multiple JNI calls and potential impact on performance.227*228* Note: return value 0 is legal.229* This means tables are not available (e.g. type1 font).230*/231abstract long getLayoutTableCache() throws FontScalerException;232233/* Used by the OpenType engine for mark positioning. */234abstract Point2D.Float getGlyphPoint(long pScalerContext,235int glyphCode, int ptNumber)236throws FontScalerException;237238abstract long getUnitsPerEm();239240/* Returns pointer to native structure describing rasterization attributes.241Format of this structure is scaler-specific.242243Callee is responsible for freeing scaler context (using free()).244245Note:246Context is tightly associated with strike and it is actually247freed when corresponding strike is being released.248*/249abstract long createScalerContext(double[] matrix,250int aa, int fm,251float boldness, float italic,252boolean disableHinting);253254/* Marks context as invalid because native scaler is invalid.255Notes:256- pointer itself is still valid and has to be released257- if pointer to native scaler was cached it258should not be neither disposed nor used.259it is very likely it is already disposed by this moment. */260abstract void invalidateScalerContext(long ppScalerContext);261}262263264