Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/font/CompositeStrike.java
38829 views
/*1* Copyright (c) 2003, 2005, 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.Font;28import java.awt.Rectangle;29import java.awt.geom.GeneralPath;30import java.awt.geom.Point2D;31import java.awt.geom.Rectangle2D;3233/*34* performance:35* it seems expensive that when using a composite font for36* every char you have to find which "slot" can display it.37* Just the fact that you need to check at all ..38* A composite glyph code ducks this by encoding the slot into the39* glyph code, but you still need to get from char to glyph code.40*/41public final class CompositeStrike extends FontStrike {4243static final int SLOTMASK = 0xffffff;4445private CompositeFont compFont;46private PhysicalStrike[] strikes;47int numGlyphs = 0;4849CompositeStrike(CompositeFont font2D, FontStrikeDesc desc) {50this.compFont = font2D;51this.desc = desc;52this.disposer = new FontStrikeDisposer(compFont, desc);53if (desc.style != compFont.style) {54algoStyle = true;55if ((desc.style & Font.BOLD) == Font.BOLD &&56((compFont.style & Font.BOLD) == 0)) {57boldness = 1.33f;58}59if ((desc.style & Font.ITALIC) == Font.ITALIC &&60(compFont.style & Font.ITALIC) == 0) {61italic = 0.7f;62}63}64strikes = new PhysicalStrike[compFont.numSlots];65}6667/* do I need this (see Strike::compositeStrikeForGlyph) */68PhysicalStrike getStrikeForGlyph(int glyphCode) {69return getStrikeForSlot(glyphCode >>> 24);70}7172PhysicalStrike getStrikeForSlot(int slot) {7374if (slot >= strikes.length) {75slot = 0;76}7778PhysicalStrike strike = strikes[slot];79if (strike == null) {80strike =81(PhysicalStrike)(compFont.getSlotFont(slot).getStrike(desc));8283strikes[slot] = strike;84}85return strike;86}8788public int getNumGlyphs() {89return compFont.getNumGlyphs();90}9192StrikeMetrics getFontMetrics() {93if (strikeMetrics == null) {94StrikeMetrics compMetrics = new StrikeMetrics();95for (int s=0; s<compFont.numMetricsSlots; s++) {96compMetrics.merge(getStrikeForSlot(s).getFontMetrics());97}98strikeMetrics = compMetrics;99}100return strikeMetrics;101}102103104/* Performance tweak: Slot 0 can often return all the glyphs105* Note slot zero doesn't need to be masked.106* Could go a step further and support getting a run of glyphs.107* This would help many locales a little.108*109* Note that if a client constructs an invalid a composite glyph that110* references an invalid slot, that the behaviour is currently111* that this slot index falls through to CompositeFont.getSlotFont(int)112* which will substitute a default font, from which to obtain the113* strike. If its an invalid glyph code for a valid slot, then the114* physical font for that slot will substitute the missing glyph.115*/116void getGlyphImagePtrs(int[] glyphCodes, long[] images, int len) {117PhysicalStrike strike = getStrikeForSlot(0);118int numptrs = strike.getSlot0GlyphImagePtrs(glyphCodes, images, len);119if (numptrs == len) {120return;121}122for (int i=numptrs; i< len; i++) {123strike = getStrikeForGlyph(glyphCodes[i]);124images[i] = strike.getGlyphImagePtr(glyphCodes[i] & SLOTMASK);125}126}127128129long getGlyphImagePtr(int glyphCode) {130PhysicalStrike strike = getStrikeForGlyph(glyphCode);131return strike.getGlyphImagePtr(glyphCode & SLOTMASK);132}133134void getGlyphImageBounds(int glyphCode, Point2D.Float pt, Rectangle result) {135PhysicalStrike strike = getStrikeForGlyph(glyphCode);136strike.getGlyphImageBounds(glyphCode & SLOTMASK, pt, result);137}138139Point2D.Float getGlyphMetrics(int glyphCode) {140PhysicalStrike strike = getStrikeForGlyph(glyphCode);141return strike.getGlyphMetrics(glyphCode & SLOTMASK);142}143144Point2D.Float getCharMetrics(char ch) {145return getGlyphMetrics(compFont.getMapper().charToGlyph(ch));146}147148float getGlyphAdvance(int glyphCode) {149PhysicalStrike strike = getStrikeForGlyph(glyphCode);150return strike.getGlyphAdvance(glyphCode & SLOTMASK);151}152153/* REMIND where to cache?154* The glyph advance is already cached by physical strikes and that's a lot155* of the work.156* Also FontDesignMetrics maintains a latin char advance cache, so don't157* cache advances here as apps tend to hold onto metrics objects when158* performance is sensitive to it. Revisit this assumption later.159*/160float getCodePointAdvance(int cp) {161return getGlyphAdvance(compFont.getMapper().charToGlyph(cp));162}163164Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {165PhysicalStrike strike = getStrikeForGlyph(glyphCode);166return strike.getGlyphOutlineBounds(glyphCode & SLOTMASK);167}168169GeneralPath getGlyphOutline(int glyphCode, float x, float y) {170171PhysicalStrike strike = getStrikeForGlyph(glyphCode);172GeneralPath path = strike.getGlyphOutline(glyphCode & SLOTMASK, x, y);173if (path == null) {174return new GeneralPath();175} else {176return path;177}178}179180/* The physical font slot for each glyph is encoded in the glyph ID181* To be as efficient as possible we find a run of glyphs from the182* same slot and create a temporary array of these glyphs decoded183* to the slot. The slot font is then queried for the GeneralPath184* for that run of glyphs. GeneralPaths from each run are appended185* to create the shape for the whole glyph array.186*/187GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {188GeneralPath path = null;189GeneralPath gp;190int glyphIndex = 0;191int[] tmpGlyphs;192193while (glyphIndex < glyphs.length) {194int start = glyphIndex;195int slot = glyphs[glyphIndex] >>> 24;196while (glyphIndex < glyphs.length &&197(glyphs[glyphIndex+1] >>> 24) == slot) {198glyphIndex++;199}200int tmpLen = glyphIndex-start+1;201tmpGlyphs = new int[tmpLen];202for (int i=0;i<tmpLen;i++) {203tmpGlyphs[i] = glyphs[i] & SLOTMASK;204}205gp = getStrikeForSlot(slot).getGlyphVectorOutline(tmpGlyphs, x, y);206if (path == null) {207path = gp;208} else if (gp != null) {209path.append(gp, false);210}211}212if (path == null) {213return new GeneralPath();214} else {215return path;216}217}218}219220221