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.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * 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 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * 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 preceding 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 preceding (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         int secondPosition = pos;
 123         int preChar = ch;
 124         boolean consumed = false;
 125         slot = sl >>> 24;
 126         while ((ch = nextCodePoint(lim)) != DONE ) {
 127             if (CharToGlyphMapper.isVariationSelector(ch)
 128                 && CharToGlyphMapper.isBaseChar(preChar)
 129                 && consumed == false) {
 130                 consumed = true;
 131                 int[] chars = {preChar, ch};
 132                 int[] glyphs = {0, 0};
 133                 mapper.charsToGlyphs(2, chars, glyphs);
 134                 int vsSize = 1;
 135                 if (ch >= 0x10000) vsSize = 2;
 136                 if (secondPosition + vsSize == pos){ // Real slot
 137                     sl = glyphs[0] & CompositeGlyphMapper.SLOTMASK;
 138                     slot = sl >>> 24;
 139                 }
 140                 if ((glyphs[0] &  CompositeGlyphMapper.SLOTMASK) != sl) {
 141                     pushback(ch);
 142                     pushback(preChar);
 143                     return true;
 144                 }
 145             }else{
 146                 consumed = false;
 147                 if ((mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK) != sl) {
 148                     break;
 149                 }
 150             }
 151             preChar = ch;
 152         }
 153         pushback(ch);
 154 
 155         return true;
 156     }
 157 
 158     public boolean next() {
 159         return next(Script.COMMON, limit);
 160     }
 161 
 162     static final int SURROGATE_START = 0x10000;
 163     static final int LEAD_START = 0xd800;
 164     static final int LEAD_LIMIT = 0xdc00;
 165     static final int TAIL_START = 0xdc00;
 166     static final int TAIL_LIMIT = 0xe000;
 167     static final int LEAD_SURROGATE_SHIFT = 10;
 168     static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START;
 169 
 170     static final int DONE = -1;
 171 
 172     int nextCodePoint() {
 173         return nextCodePoint(limit);
 174     }
 175 
 176     int nextCodePoint(int lim) {
 177         if (pos >= lim) {
 178             return DONE;
 179         }
 180         int ch = text[pos++];
 181         if (ch >= LEAD_START && ch < LEAD_LIMIT && pos < lim) {
 182             int nch = text[pos];
 183             if (nch >= TAIL_START && nch < TAIL_LIMIT) {
 184                 ++pos;
 185                 ch = (ch << LEAD_SURROGATE_SHIFT) + nch + SURROGATE_OFFSET;
 186             }
 187         }
 188         return ch;
 189     }
 190 
 191     void pushback(int ch) {
 192         if (ch >= 0) {
 193             if (ch >= 0x10000) {
 194                 pos -= 2;
 195             } else {
 196                 pos -= 1;
 197             }
 198         }
 199     }
 200 }