Path: blob/master/src/java.desktop/share/classes/sun/font/CompositeStrike.java
41154 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) {73if (slot >= strikes.length) {74slot = 0;75}76PhysicalStrike strike = strikes[slot];77if (strike == null) {78strike =79(PhysicalStrike)(compFont.getSlotFont(slot).getStrike(desc));8081strikes[slot] = strike;82}83return strike;84}8586public int getNumGlyphs() {87return compFont.getNumGlyphs();88}8990StrikeMetrics getFontMetrics() {91if (strikeMetrics == null) {92StrikeMetrics compMetrics = new StrikeMetrics();93for (int s=0; s<compFont.numMetricsSlots; s++) {94compMetrics.merge(getStrikeForSlot(s).getFontMetrics());95}96strikeMetrics = compMetrics;97}98return strikeMetrics;99}100101102/* Performance tweak: Slot 0 can often return all the glyphs103* Note slot zero doesn't need to be masked.104* Could go a step further and support getting a run of glyphs.105* This would help many locales a little.106*107* Note that if a client constructs an invalid a composite glyph that108* references an invalid slot, that the behaviour is currently109* that this slot index falls through to CompositeFont.getSlotFont(int)110* which will substitute a default font, from which to obtain the111* strike. If its an invalid glyph code for a valid slot, then the112* physical font for that slot will substitute the missing glyph.113*/114void getGlyphImagePtrs(int[] glyphCodes, long[] images, int len) {115PhysicalStrike strike = getStrikeForSlot(0);116int numptrs = strike.getSlot0GlyphImagePtrs(glyphCodes, images, len);117if (numptrs == len) {118return;119}120for (int i=numptrs; i< len; i++) {121strike = getStrikeForGlyph(glyphCodes[i]);122images[i] = strike.getGlyphImagePtr(glyphCodes[i] & SLOTMASK);123}124}125126127long getGlyphImagePtr(int glyphCode) {128PhysicalStrike strike = getStrikeForGlyph(glyphCode);129return strike.getGlyphImagePtr(glyphCode & SLOTMASK);130}131132void getGlyphImageBounds(int glyphCode, Point2D.Float pt, Rectangle result) {133PhysicalStrike strike = getStrikeForGlyph(glyphCode);134strike.getGlyphImageBounds(glyphCode & SLOTMASK, pt, result);135}136137Point2D.Float getGlyphMetrics(int glyphCode) {138PhysicalStrike strike = getStrikeForGlyph(glyphCode);139return strike.getGlyphMetrics(glyphCode & SLOTMASK);140}141142Point2D.Float getCharMetrics(char ch) {143return getGlyphMetrics(compFont.getMapper().charToGlyph(ch));144}145146float getGlyphAdvance(int glyphCode) {147PhysicalStrike strike = getStrikeForGlyph(glyphCode);148return strike.getGlyphAdvance(glyphCode & SLOTMASK);149}150151/* REMIND where to cache?152* The glyph advance is already cached by physical strikes and that's a lot153* of the work.154* Also FontDesignMetrics maintains a latin char advance cache, so don't155* cache advances here as apps tend to hold onto metrics objects when156* performance is sensitive to it. Revisit this assumption later.157*/158float getCodePointAdvance(int cp) {159return getGlyphAdvance(compFont.getMapper().charToGlyph(cp));160}161162Rectangle2D.Float getGlyphOutlineBounds(int glyphCode) {163PhysicalStrike strike = getStrikeForGlyph(glyphCode);164return strike.getGlyphOutlineBounds(glyphCode & SLOTMASK);165}166167GeneralPath getGlyphOutline(int glyphCode, float x, float y) {168169PhysicalStrike strike = getStrikeForGlyph(glyphCode);170GeneralPath path = strike.getGlyphOutline(glyphCode & SLOTMASK, x, y);171if (path == null) {172return new GeneralPath();173} else {174return path;175}176}177178/* The physical font slot for each glyph is encoded in the glyph ID179* To be as efficient as possible we find a run of glyphs from the180* same slot and create a temporary array of these glyphs decoded181* to the slot. The slot font is then queried for the GeneralPath182* for that run of glyphs. GeneralPaths from each run are appended183* to create the shape for the whole glyph array.184*/185GeneralPath getGlyphVectorOutline(int[] glyphs, float x, float y) {186GeneralPath path = null;187GeneralPath gp;188int glyphIndex = 0;189int[] tmpGlyphs;190191while (glyphIndex < glyphs.length) {192int start = glyphIndex;193int slot = glyphs[glyphIndex] >>> 24;194while (glyphIndex < glyphs.length &&195(glyphs[glyphIndex+1] >>> 24) == slot) {196glyphIndex++;197}198int tmpLen = glyphIndex-start+1;199tmpGlyphs = new int[tmpLen];200for (int i=0;i<tmpLen;i++) {201tmpGlyphs[i] = glyphs[i] & SLOTMASK;202}203gp = getStrikeForSlot(slot).getGlyphVectorOutline(tmpGlyphs, x, y);204if (path == null) {205path = gp;206} else if (gp != null) {207path.append(gp, false);208}209}210if (path == null) {211return new GeneralPath();212} else {213return path;214}215}216}217218219