J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 it |
| 5 | * under the terms of the GNU General Public License version 2 only, as |
| 6 | * published by the Free Software Foundation. Sun designates this |
| 7 | * particular file as subject to the "Classpath" exception as provided |
| 8 | * by Sun in the LICENSE file that accompanied this code. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | * accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License version |
| 17 | * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any questions. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * |
| 28 | * (C) Copyright IBM Corp. 2003 - All Rights Reserved |
| 29 | */ |
| 30 | |
| 31 | package sun.font; |
| 32 | |
| 33 | /** |
| 34 | * Iterates over runs of fonts in a CompositeFont, optionally taking script runs into account. |
| 35 | */ |
| 36 | public final class FontRunIterator { |
| 37 | CompositeFont font; |
| 38 | char[] text; |
| 39 | int start; |
| 40 | int limit; |
| 41 | |
| 42 | CompositeGlyphMapper mapper; // handy cache |
| 43 | |
| 44 | int slot = -1; |
| 45 | int pos; |
| 46 | |
| 47 | public void init(CompositeFont font, char[] text, int start, int limit) { |
| 48 | if (font == null || text == null || start < 0 || limit < start || limit > text.length) { |
| 49 | throw new IllegalArgumentException(); |
| 50 | } |
| 51 | |
| 52 | this.font = font; |
| 53 | this.text = text; |
| 54 | this.start = start; |
| 55 | this.limit = limit; |
| 56 | |
| 57 | this.mapper = (CompositeGlyphMapper)font.getMapper(); |
| 58 | this.slot = -1; |
| 59 | this.pos = start; |
| 60 | } |
| 61 | |
| 62 | public PhysicalFont getFont() { |
| 63 | return slot == -1 ? null : font.getSlotFont(slot); |
| 64 | } |
| 65 | |
| 66 | public int getGlyphMask() { |
| 67 | return slot << 24; |
| 68 | } |
| 69 | |
| 70 | public int getPos() { |
| 71 | return pos; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * characters that are in the 'common' script become part of the |
| 76 | * surrounding script run. we want to fetch these from the same font |
| 77 | * used to get surrounding characters, where possible. but we don't |
| 78 | * want to force non-common characters to come from other than their |
| 79 | * standard font. |
| 80 | * |
| 81 | * what we really want to do is this: |
| 82 | * 1) fetch a code point from the text. |
| 83 | * 2) get its 'native' script code |
| 84 | * 3) determine its 'resolved' script code |
| 85 | * 4) if its native script is COMMON, and its resolved script is the same as the previous |
| 86 | * code point's, then see if the previous font supports this code point. if so, use it. |
| 87 | * 5) otherwise resolve the font as usual |
| 88 | * 6) break the run when either the physical font or the resolved script changes. |
| 89 | * |
| 90 | * problems: we optimize latin-1 and cjk text assuming a fixed |
| 91 | * width for each character. since latin-1 digits and punctuation |
| 92 | * are common, following this algorithm they will change to match |
| 93 | * the fonts used for the preceeding text, and potentially change metrics. |
| 94 | * |
| 95 | * this also seems to have the potential for changing arbitrary runs of text, e.g. |
| 96 | * any number of digits and spaces can change depending on the preceeding (or following!) |
| 97 | * non-COMMON character's font assignment. this is not good. |
| 98 | * |
| 99 | * since the goal is to enable layout to be performed using as few physical fonts as |
| 100 | * possible, and the primary cause of switching fonts is to handle spaces, perhaps |
| 101 | * we should just special-case spaces and assign them from the current font, whatever |
| 102 | * it may be. |
| 103 | * |
| 104 | * One could also argue that the job of the composite font is to assign physical fonts |
| 105 | * to text runs, however it wishes. we don't necessarily have to provide script info |
| 106 | * to let it do this. it can determine based on whatever. so having a special 'next' |
| 107 | * function that takes script (and limit) is redundant. It can fetch the script again |
| 108 | * if need be. |
| 109 | * |
| 110 | * both this and the script iterator are turning char sequences into code point |
| 111 | * sequences. maybe it would be better to feed a single code point into each iterator-- push |
| 112 | * the data instead of pull it? |
| 113 | */ |
| 114 | |
| 115 | public boolean next(int script, int lim) { |
| 116 | if (pos == lim) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | int ch = nextCodePoint(lim); |
| 121 | int sl = mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK; |
| 122 | slot = sl >>> 24; |
| 123 | while ((ch = nextCodePoint(lim)) != DONE && (mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK) == sl); |
| 124 | pushback(ch); |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | public boolean next() { |
| 130 | return next(Script.COMMON, limit); |
| 131 | } |
| 132 | |
| 133 | static final int SURROGATE_START = 0x10000; |
| 134 | static final int LEAD_START = 0xd800; |
| 135 | static final int LEAD_LIMIT = 0xdc00; |
| 136 | static final int TAIL_START = 0xdc00; |
| 137 | static final int TAIL_LIMIT = 0xe000; |
| 138 | static final int LEAD_SURROGATE_SHIFT = 10; |
| 139 | static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START; |
| 140 | |
| 141 | static final int DONE = -1; |
| 142 | |
| 143 | final int nextCodePoint() { |
| 144 | return nextCodePoint(limit); |
| 145 | } |
| 146 | |
| 147 | final int nextCodePoint(int lim) { |
| 148 | if (pos >= lim) { |
| 149 | return DONE; |
| 150 | } |
| 151 | int ch = text[pos++]; |
| 152 | if (ch >= LEAD_START && ch < LEAD_LIMIT && pos < lim) { |
| 153 | int nch = text[pos]; |
| 154 | if (nch >= TAIL_START && nch < TAIL_LIMIT) { |
| 155 | ++pos; |
| 156 | ch = (ch << LEAD_SURROGATE_SHIFT) + nch + SURROGATE_OFFSET; |
| 157 | } |
| 158 | } |
| 159 | return ch; |
| 160 | } |
| 161 | |
| 162 | final void pushback(int ch) { |
| 163 | if (ch >= 0) { |
| 164 | if (ch >= 0x10000) { |
| 165 | pos -= 2; |
| 166 | } else { |
| 167 | pos -= 1; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |