| 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
 | 
 | 
/*  | 
 | 
 *  | 
 | 
 * (C) Copyright IBM Corp. 2003 - All Rights Reserved  | 
 | 
 */  | 
 | 
 | 
 | 
package sun.font;  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
public final class FontRunIterator { | 
 | 
    CompositeFont font;  | 
 | 
    char[] text;  | 
 | 
    int start;  | 
 | 
    int limit;  | 
 | 
 | 
 | 
    CompositeGlyphMapper mapper;   | 
 | 
 | 
 | 
    int slot = -1;  | 
 | 
    int pos;  | 
 | 
 | 
 | 
    public void init(CompositeFont font, char[] text, int start, int limit) { | 
 | 
        if (font == null || text == null || start < 0 || limit < start || limit > text.length) { | 
 | 
            throw new IllegalArgumentException();  | 
 | 
        }  | 
 | 
 | 
 | 
        this.font = font;  | 
 | 
        this.text = text;  | 
 | 
        this.start = start;  | 
 | 
        this.limit = limit;  | 
 | 
 | 
 | 
        this.mapper = (CompositeGlyphMapper)font.getMapper();  | 
 | 
        this.slot = -1;  | 
 | 
        this.pos = start;  | 
 | 
    }  | 
 | 
 | 
 | 
    public PhysicalFont getFont() { | 
 | 
        return slot == -1 ? null : font.getSlotFont(slot);  | 
 | 
    }  | 
 | 
 | 
 | 
    public int getGlyphMask() { | 
 | 
        return slot << 24;  | 
 | 
    }  | 
 | 
 | 
 | 
    public int getPos() { | 
 | 
        return pos;  | 
 | 
    }  | 
 | 
 | 
 | 
    /*  | 
 | 
     * characters that are in the 'common' script become part of the  | 
 | 
     * surrounding script run.  we want to fetch these from the same font  | 
 | 
     * used to get surrounding characters, where possible.  but we don't  | 
 | 
     * want to force non-common characters to come from other than their  | 
 | 
     * standard font.  | 
 | 
     *  | 
 | 
     * what we really want to do is this:  | 
 | 
     * 1) fetch a code point from the text.  | 
 | 
     * 2) get its 'native' script code  | 
 | 
     * 3) determine its 'resolved' script code  | 
 | 
     * 4) if its native script is COMMON, and its resolved script is the same as the previous  | 
 | 
     *    code point's, then see if the previous font supports this code point.  if so, use it.  | 
 | 
     * 5) otherwise resolve the font as usual  | 
 | 
     * 6) break the run when either the physical font or the resolved script changes.  | 
 | 
     *  | 
 | 
     * problems: we optimize latin-1 and cjk text assuming a fixed  | 
 | 
     * width for each character.  since latin-1 digits and punctuation  | 
 | 
     * are common, following this algorithm they will change to match  | 
 | 
     * the fonts used for the preceding text, and potentially change metrics.  | 
 | 
     *  | 
 | 
     * this also seems to have the potential for changing arbitrary runs of text, e.g.  | 
 | 
     * any number of digits and spaces can change depending on the preceding (or following!)  | 
 | 
     * non-COMMON character's font assignment.  this is not good.  | 
 | 
     *  | 
 | 
     * since the goal is to enable layout to be performed using as few physical fonts as  | 
 | 
     * possible, and the primary cause of switching fonts is to handle spaces, perhaps  | 
 | 
     * we should just special-case spaces and assign them from the current font, whatever  | 
 | 
     * it may be.  | 
 | 
     *  | 
 | 
     * One could also argue that the job of the composite font is to assign physical fonts  | 
 | 
     * to text runs, however it wishes.  we don't necessarily have to provide script info  | 
 | 
     * to let it do this.  it can determine based on whatever.  so having a special 'next'  | 
 | 
     * function that takes script (and limit) is redundant.  It can fetch the script again  | 
 | 
     * if need be.  | 
 | 
     *  | 
 | 
     * both this and the script iterator are turning char sequences into code point  | 
 | 
     * sequences.  maybe it would be better to feed a single code point into each iterator-- push  | 
 | 
     * the data instead of pull it?  | 
 | 
     */  | 
 | 
 | 
 | 
    public boolean next(int script, int lim) { | 
 | 
        if (pos == lim) { | 
 | 
            return false;  | 
 | 
        }  | 
 | 
 | 
 | 
        int ch = nextCodePoint(lim);  | 
 | 
        int sl = mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK;  | 
 | 
        slot = sl >>> 24;  | 
 | 
        while ((ch = nextCodePoint(lim)) != DONE && (mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK) == sl);  | 
 | 
        pushback(ch);  | 
 | 
 | 
 | 
        return true;  | 
 | 
    }  | 
 | 
 | 
 | 
    public boolean next() { | 
 | 
        return next(Script.COMMON, limit);  | 
 | 
    }  | 
 | 
 | 
 | 
    static final int SURROGATE_START = 0x10000;  | 
 | 
    static final int LEAD_START = 0xd800;  | 
 | 
    static final int LEAD_LIMIT = 0xdc00;  | 
 | 
    static final int TAIL_START = 0xdc00;  | 
 | 
    static final int TAIL_LIMIT = 0xe000;  | 
 | 
    static final int LEAD_SURROGATE_SHIFT = 10;  | 
 | 
    static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START;  | 
 | 
 | 
 | 
    static final int DONE = -1;  | 
 | 
 | 
 | 
    final int nextCodePoint() { | 
 | 
        return nextCodePoint(limit);  | 
 | 
    }  | 
 | 
 | 
 | 
    final int nextCodePoint(int lim) { | 
 | 
        if (pos >= lim) { | 
 | 
            return DONE;  | 
 | 
        }  | 
 | 
        int ch = text[pos++];  | 
 | 
        if (ch >= LEAD_START && ch < LEAD_LIMIT && pos < lim) { | 
 | 
            int nch = text[pos];  | 
 | 
            if (nch >= TAIL_START && nch < TAIL_LIMIT) { | 
 | 
                ++pos;  | 
 | 
                ch = (ch << LEAD_SURROGATE_SHIFT) + nch + SURROGATE_OFFSET;  | 
 | 
            }  | 
 | 
        }  | 
 | 
        return ch;  | 
 | 
    }  | 
 | 
 | 
 | 
    final void pushback(int ch) { | 
 | 
        if (ch >= 0) { | 
 | 
            if (ch >= 0x10000) { | 
 | 
                pos -= 2;  | 
 | 
            } else { | 
 | 
                pos -= 1;  | 
 | 
            }  | 
 | 
        }  | 
 | 
    }  | 
 | 
}  |