Path: blob/master/src/java.desktop/share/classes/sun/font/FontRunIterator.java
41155 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425/*26*27* (C) Copyright IBM Corp. 2003 - All Rights Reserved28*/2930package sun.font;3132/**33* Iterates over runs of fonts in a CompositeFont, optionally taking script runs into account.34*/35public final class FontRunIterator {36CompositeFont font;37char[] text;38int start;39int limit;4041CompositeGlyphMapper mapper; // handy cache4243int slot = -1;44int pos;4546public void init(CompositeFont font, char[] text, int start, int limit) {47if (font == null || text == null || start < 0 || limit < start || limit > text.length) {48throw new IllegalArgumentException();49}5051this.font = font;52this.text = text;53this.start = start;54this.limit = limit;5556this.mapper = (CompositeGlyphMapper)font.getMapper();57this.slot = -1;58this.pos = start;59}6061public PhysicalFont getFont() {62return slot == -1 ? null : font.getSlotFont(slot);63}6465public int getGlyphMask() {66return slot << 24;67}6869public int getPos() {70return pos;71}7273/*74* characters that are in the 'common' script become part of the75* surrounding script run. we want to fetch these from the same font76* used to get surrounding characters, where possible. but we don't77* want to force non-common characters to come from other than their78* standard font.79*80* what we really want to do is this:81* 1) fetch a code point from the text.82* 2) get its 'native' script code83* 3) determine its 'resolved' script code84* 4) if its native script is COMMON, and its resolved script is the same as the previous85* code point's, then see if the previous font supports this code point. if so, use it.86* 5) otherwise resolve the font as usual87* 6) break the run when either the physical font or the resolved script changes.88*89* problems: we optimize latin-1 and cjk text assuming a fixed90* width for each character. since latin-1 digits and punctuation91* are common, following this algorithm they will change to match92* the fonts used for the preceding text, and potentially change metrics.93*94* this also seems to have the potential for changing arbitrary runs of text, e.g.95* any number of digits and spaces can change depending on the preceding (or following!)96* non-COMMON character's font assignment. this is not good.97*98* since the goal is to enable layout to be performed using as few physical fonts as99* possible, and the primary cause of switching fonts is to handle spaces, perhaps100* we should just special-case spaces and assign them from the current font, whatever101* it may be.102*103* One could also argue that the job of the composite font is to assign physical fonts104* to text runs, however it wishes. we don't necessarily have to provide script info105* to let it do this. it can determine based on whatever. so having a special 'next'106* function that takes script (and limit) is redundant. It can fetch the script again107* if need be.108*109* both this and the script iterator are turning char sequences into code point110* sequences. maybe it would be better to feed a single code point into each iterator-- push111* the data instead of pull it?112*/113114public boolean next(int script, int lim) {115if (pos == lim) {116return false;117}118119int ch = nextCodePoint(lim);120int sl = mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK;121slot = sl >>> 24;122while ((ch = nextCodePoint(lim)) != DONE && (mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK) == sl);123pushback(ch);124125return true;126}127128public boolean next() {129return next(Script.COMMON, limit);130}131132static final int SURROGATE_START = 0x10000;133static final int LEAD_START = 0xd800;134static final int LEAD_LIMIT = 0xdc00;135static final int TAIL_START = 0xdc00;136static final int TAIL_LIMIT = 0xe000;137static final int LEAD_SURROGATE_SHIFT = 10;138static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START;139140static final int DONE = -1;141142int nextCodePoint() {143return nextCodePoint(limit);144}145146int nextCodePoint(int lim) {147if (pos >= lim) {148return DONE;149}150int ch = text[pos++];151if (ch >= LEAD_START && ch < LEAD_LIMIT && pos < lim) {152int nch = text[pos];153if (nch >= TAIL_START && nch < TAIL_LIMIT) {154++pos;155ch = (ch << LEAD_SURROGATE_SHIFT) + nch + SURROGATE_OFFSET;156}157}158return ch;159}160161void pushback(int ch) {162if (ch >= 0) {163if (ch >= 0x10000) {164pos -= 2;165} else {166pos -= 1;167}168}169}170}171172173