1040 lines · 35.4 KB · Java
RawDownload
/* * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *//* * * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved */ package sun.font; import java.awt.Font;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.Shape; import java.awt.font.FontRenderContext;import java.awt.font.GlyphJustificationInfo;import java.awt.font.GlyphMetrics;import java.awt.font.LineMetrics;import java.awt.font.TextAttribute; import java.awt.geom.AffineTransform;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D; import java.util.Map; /** * Default implementation of ExtendedTextLabel. */ // {jbr} I made this class package-private to keep the// Decoration.Label API package-private. /* public */class ExtendedTextSourceLabel extends ExtendedTextLabel implements Decoration.Label {   TextSource source;  private Decoration decorator;   // caches  private Font font;  private AffineTransform baseTX;  private CoreMetrics cm;   Rectangle2D lb;  Rectangle2D ab;  Rectangle2D vb;  Rectangle2D ib;  StandardGlyphVector gv;  float[] charinfo;   /**   * Create from a TextSource.   */  public ExtendedTextSourceLabel(TextSource source, Decoration decorator) {    this.source = source;    this.decorator = decorator;    finishInit();  }   /**   * Create from a TextSource, optionally using cached data from oldLabel starting at the offset.   * If present oldLabel must have been created from a run of text that includes the text used in   * the new label.  Start in source corresponds to logical character offset in oldLabel.   */  public ExtendedTextSourceLabel(TextSource source, ExtendedTextSourceLabel oldLabel, int offset) {    // currently no optimization.    this.source = source;    this.decorator = oldLabel.decorator;    finishInit();  }   private void finishInit() {    font = source.getFont();     Map<TextAttribute, ?> atts = font.getAttributes();    baseTX = AttributeValues.getBaselineTransform(atts);    if (baseTX == null){        cm = source.getCoreMetrics();    } else {      AffineTransform charTX = AttributeValues.getCharTransform(atts);      if (charTX == null) {          charTX = new AffineTransform();      }      font = font.deriveFont(charTX);       LineMetrics lm = font.getLineMetrics(source.getChars(), source.getStart(),          source.getStart() + source.getLength(), source.getFRC());      cm = CoreMetrics.get(lm);    }  }    // TextLabel API   public Rectangle2D getLogicalBounds() {    return getLogicalBounds(0, 0);  }   public Rectangle2D getLogicalBounds(float x, float y) {    if (lb == null) {      lb = createLogicalBounds();    }    return new Rectangle2D.Float((float)(lb.getX() + x),                                 (float)(lb.getY() + y),                                 (float)lb.getWidth(),                                 (float)lb.getHeight());  }     public float getAdvance() {        if (lb == null) {            lb = createLogicalBounds();        }        return (float)lb.getWidth();    }   public Rectangle2D getVisualBounds(float x, float y) {    if (vb == null) {      vb = decorator.getVisualBounds(this);    }    return new Rectangle2D.Float((float)(vb.getX() + x),                                 (float)(vb.getY() + y),                                 (float)vb.getWidth(),                                 (float)vb.getHeight());  }   public Rectangle2D getAlignBounds(float x, float y) {    if (ab == null) {      ab = createAlignBounds();    }    return new Rectangle2D.Float((float)(ab.getX() + x),                                 (float)(ab.getY() + y),                                 (float)ab.getWidth(),                                 (float)ab.getHeight());   }   public Rectangle2D getItalicBounds(float x, float y) {    if (ib == null) {      ib = createItalicBounds();    }    return new Rectangle2D.Float((float)(ib.getX() + x),                                 (float)(ib.getY() + y),                                 (float)ib.getWidth(),                                 (float)ib.getHeight());   }   public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {      return getGV().getPixelBounds(frc, x, y);  }   public boolean isSimple() {      return decorator == Decoration.getPlainDecoration() &&             baseTX == null;  }   public AffineTransform getBaselineTransform() {      return baseTX; // passing internal object, caller must not modify!  }   public Shape handleGetOutline(float x, float y) {    return getGV().getOutline(x, y);  }   public Shape getOutline(float x, float y) {    return decorator.getOutline(this, x, y);  }   public void handleDraw(Graphics2D g, float x, float y) {    g.drawGlyphVector(getGV(), x, y);  }   public void draw(Graphics2D g, float x, float y) {    decorator.drawTextAndDecorations(this, g, x, y);  }   /**   * The logical bounds extends from the origin of the glyphvector to the   * position at which a following glyphvector's origin should be placed.   * We always assume glyph vectors are rendered from left to right, so   * the origin is always to the left.   * <p> On a left-to-right run, combining marks and 'ligatured away'   * characters are to the right of their base characters.  The charinfo   * array will record the character positions for these 'missing' characters   * as being at the origin+advance of the base glyph, with zero advance.   * (This is not necessarily the same as the glyph position, for example,   * an umlaut glyph may have a position to the left of this point, it depends   * on whether the font was designed so that such glyphs overhang to the left   * of their origin, or whether it presumes some kind of kerning to position   * the glyphs).  Anyway, the left of the bounds is the origin of the first   * logical (leftmost) character, and the right is the origin + advance of the   * last logical (rightmost) character.   * <p> On a right-to-left run, these special characters are to the left   * of their base characters.  Again, since 'glyph position' has been abstracted   * away, we can use the origin of the leftmost character, and the origin +   * advance of the rightmost character.   * <p> On a mixed run (hindi) we can't rely on the first logical character   * being the leftmost character.  However we can again rely on the leftmost   * character origin and the rightmost character + advance.   */  protected Rectangle2D createLogicalBounds() {    return getGV().getLogicalBounds();  }   public Rectangle2D handleGetVisualBounds() {    return getGV().getVisualBounds();  }   /**   * Like createLogicalBounds except ignore leading and logically trailing white space.   * this assumes logically trailing whitespace is also visually trailing.   * Whitespace is anything that has a zero visual width, regardless of its advance.   * <p> We make the same simplifying assumptions as in createLogicalBounds, namely   * that we can rely on the charinfo to shield us from any glyph positioning oddities   * in the font that place the glyph for a character at other than the pos + advance   * of the character to its left.  So we no longer need to skip chars with zero   * advance, as their bounds (right and left) are already correct.   */  protected Rectangle2D createAlignBounds() {    float[] info = getCharinfo();     float al = 0f;    float at = -cm.ascent;    float aw = 0f;    float ah = cm.ascent + cm.descent;     boolean lineIsLTR = (source.getLayoutFlags() & 0x8) == 0;    int rn = info.length - numvals;    if (lineIsLTR) {      while (rn > 0 && info[rn+visw] == 0) {        rn -= numvals;      }    }     if (rn >= 0) {      int ln = 0;      while (ln < rn && ((info[ln+advx] == 0) || (!lineIsLTR && info[ln+visw] == 0))) {        ln += numvals;      }       al = Math.max(0f, info[ln+posx]);      aw = info[rn+posx] + info[rn+advx] - al;    }     /*      boolean lineIsLTR = source.lineIsLTR();      int rn = info.length - numvals;      while (rn > 0 && ((info[rn+advx] == 0) || (lineIsLTR && info[rn+visw] == 0))) {      rn -= numvals;      }       if (rn >= 0) {      int ln = 0;      while (ln < rn && ((info[ln+advx] == 0) || (!lineIsLTR && info[ln+visw] == 0))) {      ln += numvals;      }       al = Math.max(0f, info[ln+posx]);      aw = info[rn+posx] + info[rn+advx] - al;      }      */     return new Rectangle2D.Float(al, at, aw, ah);  }   public Rectangle2D createItalicBounds() {    float ia = cm.italicAngle;     Rectangle2D lb = getLogicalBounds();    float l = (float)lb.getMinX();    float t = -cm.ascent;    float r = (float)lb.getMaxX();    float b = cm.descent;    if (ia != 0) {        if (ia > 0) {            l -= ia * (b - cm.ssOffset);            r -= ia * (t - cm.ssOffset);        } else {            l -= ia * (t - cm.ssOffset);            r -= ia * (b - cm.ssOffset);        }    }    return new Rectangle2D.Float(l, t, r - l, b - t);  }   private final StandardGlyphVector getGV() {    if (gv == null) {      gv = createGV();    }     return gv;  }   protected StandardGlyphVector createGV() {    FontRenderContext frc = source.getFRC();    int flags = source.getLayoutFlags();    char[] context = source.getChars();    int start = source.getStart();    int length = source.getLength();     GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines    gv = gl.layout(font, frc, context, start, length, flags, null); // ??? use textsource    GlyphLayout.done(gl);     return gv;  }   // ExtendedTextLabel API   private static final int posx = 0,    posy = 1,    advx = 2,    advy = 3,    visx = 4,    visy = 5,    visw = 6,    vish = 7;  private static final int numvals = 8;   public int getNumCharacters() {    return source.getLength();  }   public CoreMetrics getCoreMetrics() {    return cm;  }   public float getCharX(int index) {    validate(index);    return getCharinfo()[l2v(index) * numvals + posx];  }   public float getCharY(int index) {    validate(index);    return getCharinfo()[l2v(index) * numvals + posy];  }   public float getCharAdvance(int index) {    validate(index);    return getCharinfo()[l2v(index) * numvals + advx];  }   public Rectangle2D handleGetCharVisualBounds(int index) {    validate(index);    float[] charinfo = getCharinfo();    index = l2v(index) * numvals;    return new Rectangle2D.Float(                                 charinfo[index + visx],                                 charinfo[index + visy],                                 charinfo[index + visw],                                 charinfo[index + vish]);  }   public Rectangle2D getCharVisualBounds(int index, float x, float y) {     Rectangle2D bounds = decorator.getCharVisualBounds(this, index);    if (x != 0 || y != 0) {        bounds.setRect(bounds.getX()+x,                       bounds.getY()+y,                       bounds.getWidth(),                       bounds.getHeight());    }    return bounds;  }   private void validate(int index) {    if (index < 0) {      throw new IllegalArgumentException("index " + index + " < 0");    } else if (index >= source.getLength()) {      throw new IllegalArgumentException("index " + index + " < " + source.getLength());    }  }   /*    public int hitTestChar(float x, float y) {    // !!! return index of char hit, for swing    // result is negative for trailing-edge hits    // no italics so no problem at margins.    // for now, ignore y since we assume horizontal text     // find non-combining char origin to right of x    float[] charinfo = getCharinfo();     int n = 0;    int e = source.getLength();    while (n < e && charinfo[n + advx] != 0 && charinfo[n + posx] > x) {    n += numvals;    }    float rightx = n < e ? charinfo[n+posx] : charinfo[e - numvals + posx] + charinfo[e - numvals + advx];     // find non-combining char to left of that char    n -= numvals;    while (n >= 0 && charinfo[n+advx] == 0) {    n -= numvals;    }    float leftx = n >= 0 ? charinfo[n+posx] : 0;    float lefta = n >= 0 ? charinfo[n+advx] : 0;     n /= numvals;     boolean left = true;    if (x < leftx + lefta / 2f) {    // left of prev char    } else if (x < (leftx + lefta + rightx) / 2f) {    // right of prev char    left = false;    } else {    // left of follow char    n += 1;    }     if ((source.getLayoutFlags() & 0x1) != 0) {    n = getNumCharacters() - 1 - n;    left = !left;    }     return left ? n : -n;    }    */   public int logicalToVisual(int logicalIndex) {    validate(logicalIndex);    return l2v(logicalIndex);  }   public int visualToLogical(int visualIndex) {    validate(visualIndex);    return v2l(visualIndex);  }   public int getLineBreakIndex(int start, float width) {    float[] charinfo = getCharinfo();    int length = source.getLength();    --start;    while (width >= 0 && ++start < length) {      float adv = charinfo[l2v(start) * numvals + advx];      width -= adv;    }     return start;  }   public float getAdvanceBetween(int start, int limit) {    float a = 0f;     float[] charinfo = getCharinfo();    --start;    while (++start < limit) {      a += charinfo[l2v(start) * numvals + advx];    }     return a;  }   public boolean caretAtOffsetIsValid(int offset) {      // REMIND: improve this implementation       // Ligature formation can either be done in logical order,      // with the ligature glyph logically preceding the null      // chars;  or in visual order, with the ligature glyph to      // the left of the null chars.  This method's implementation      // must reflect which strategy is used.       if (offset == 0 || offset == source.getLength()) {          return true;      }      char c = source.getChars()[source.getStart() + offset];      if (c == '\t' || c == '\n' || c == '\r') { // hack          return true;      }      int v = l2v(offset);       // If ligatures are always to the left, do this stuff:      //if (!(source.getLayoutFlags() & 0x1) == 0) {      //    v += 1;      //    if (v == source.getLength()) {      //        return true;

Lines 1–500 of 1040.