glyphview.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,079 行 · 第 1/3 页

JAVA
1,079
字号
    try      {        getDocument().getText(p0, p1 - p0, txt);      }    catch (BadLocationException ex)      {	AssertionError ae;        ae = new AssertionError("BadLocationException should not be "				+ "thrown here. p0 = " + p0 + ", p1 = " + p1);	ae.initCause(ex);	throw ae;      }    return txt;  }  /**   * Returns the font for the text run for which this <code>GlyphView</code>   * is responsible.   *   * @return the font for the text run for which this <code>GlyphView</code>   *         is responsible   */  public Font getFont()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    String family = StyleConstants.getFontFamily(atts);    int size = StyleConstants.getFontSize(atts);    int style = Font.PLAIN;    if (StyleConstants.isBold(atts))        style |= Font.BOLD;    if (StyleConstants.isItalic(atts))      style |= Font.ITALIC;    Font font = new Font(family, style, size);    return font;  }  /**   * Returns the foreground color which should be used to paint the text.   * This is fetched from the associated element's text attributes using   * {@link StyleConstants#getForeground}.   *   * @return the foreground color which should be used to paint the text   */  public Color getForeground()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    return StyleConstants.getForeground(atts);  }  /**   * Returns the background color which should be used to paint the text.   * This is fetched from the associated element's text attributes using   * {@link StyleConstants#getBackground}.   *   * @return the background color which should be used to paint the text   */  public Color getBackground()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    // We cannot use StyleConstants.getBackground() here, because that returns    // BLACK as default (when background == null). What we need is the    // background setting of the text component instead, which is what we get    // when background == null anyway.    return (Color) atts.getAttribute(StyleConstants.Background);  }  /**   * Determines whether the text should be rendered strike-through or not. This   * is determined using the method   * {@link StyleConstants#isStrikeThrough(AttributeSet)} on the element of   * this view.   *   * @return whether the text should be rendered strike-through or not   */  public boolean isStrikeThrough()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    return StyleConstants.isStrikeThrough(atts);  }  /**   * Determines whether the text should be rendered as subscript or not. This   * is determined using the method   * {@link StyleConstants#isSubscript(AttributeSet)} on the element of   * this view.   *   * @return whether the text should be rendered as subscript or not   */  public boolean isSubscript()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    return StyleConstants.isSubscript(atts);  }  /**   * Determines whether the text should be rendered as superscript or not. This   * is determined using the method   * {@link StyleConstants#isSuperscript(AttributeSet)} on the element of   * this view.   *   * @return whether the text should be rendered as superscript or not   */  public boolean isSuperscript()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    return StyleConstants.isSuperscript(atts);  }  /**   * Determines whether the text should be rendered as underlined or not. This   * is determined using the method   * {@link StyleConstants#isUnderline(AttributeSet)} on the element of   * this view.   *   * @return whether the text should be rendered as underlined or not   */  public boolean isUnderline()  {    Element el = getElement();    AttributeSet atts = el.getAttributes();    return StyleConstants.isUnderline(atts);  }  /**   * Creates and returns a shallow clone of this GlyphView. This is used by   * the {@link #createFragment} and {@link #breakView} methods.   *   * @return a shallow clone of this GlyphView   */  protected final Object clone()  {    try      {        return super.clone();      }    catch (CloneNotSupportedException ex)      {        AssertionError err = new AssertionError("CloneNotSupportedException "                                                + "must not be thrown here");        err.initCause(ex);        throw err;      }  }  /**   * Tries to break the view near the specified view span <code>len</code>.   * The glyph view can only be broken in the X direction. For Y direction it   * returns itself.   *   * @param axis the axis for breaking, may be {@link View#X_AXIS} or   *        {@link View#Y_AXIS}   * @param p0 the model location where the fragment should start   * @param pos the view position along the axis where the fragment starts   * @param len the desired length of the fragment view   *   * @return the fragment view, or <code>this</code> if breaking was not   *         possible   */  public View breakView(int axis, int p0, float pos, float len)  {    if (axis == Y_AXIS)      return this;    checkPainter();    GlyphPainter painter = getGlyphPainter();    // Try to find a suitable line break.    BreakIterator lineBreaker = BreakIterator.getLineInstance();    Segment txt = new Segment();    try      {        int start = getStartOffset();        int length = getEndOffset() - start;        getDocument().getText(start, length, txt);      }    catch (BadLocationException ex)      {        AssertionError err = new AssertionError("BadLocationException must not "                                                + "be thrown here.");        err.initCause(ex);        throw err;      }    int breakLocation =      Utilities.getBreakLocation(txt, getContainer().getFontMetrics(getFont()),                                 (int) pos, (int) (pos + len),                                 getTabExpander(), p0);    View brokenView = createFragment(p0, breakLocation);    return brokenView;  }  /**   * Determines how well the specified view location is suitable for inserting   * a line break. If <code>axis</code> is <code>View.Y_AXIS</code>, then   * this method forwards to the superclass, if <code>axis</code> is   * <code>View.X_AXIS</code> then this method returns   * {@link View#ExcellentBreakWeight} if there is a suitable break location   * (usually whitespace) within the specified view span, or   * {@link View#GoodBreakWeight} if not.   *   * @param axis the axis along which the break weight is requested   * @param pos the starting view location   * @param len the length of the span at which the view should be broken   *   * @return the break weight   */  public int getBreakWeight(int axis, float pos, float len)  {    int weight;    if (axis == Y_AXIS)      weight = super.getBreakWeight(axis, pos, len);    else      {        // FIXME: Commented out because the Utilities.getBreakLocation method        // is still buggy. The GoodBreakWeight is a reasonable workaround for        // now.//        int startOffset = getStartOffset();//        int endOffset = getEndOffset() - 1;//        Segment s = getText(startOffset, endOffset);//        Container c = getContainer();//        FontMetrics fm = c.getFontMetrics(c.getFont());//        int x0 = (int) pos;//        int x = (int) (pos + len);//        int breakLoc = Utilities.getBreakLocation(s, fm, x0, x,//                                                  getTabExpander(),//                                                  startOffset);//        if (breakLoc == startOffset || breakLoc == endOffset)//          weight = GoodBreakWeight;//        else//          weight = ExcellentBreakWeight;        weight = GoodBreakWeight;      }    return weight;  }  /**   * Receives notification that some text attributes have changed within the   * text fragment that this view is responsible for. This calls   * {@link View#preferenceChanged(View, boolean, boolean)} on the parent for   * both width and height.   *   * @param e the document event describing the change; not used here   * @param a the view allocation on screen; not used here   * @param vf the view factory; not used here   */  public void changedUpdate(DocumentEvent e, Shape a, ViewFactory vf)  {    preferenceChanged(this, true, true);  }  /**   * Receives notification that some text has been inserted within the   * text fragment that this view is responsible for. This calls   * {@link View#preferenceChanged(View, boolean, boolean)} for the   * direction in which the glyphs are rendered.   *   * @param e the document event describing the change; not used here   * @param a the view allocation on screen; not used here   * @param vf the view factory; not used here   */  public void insertUpdate(DocumentEvent e, Shape a, ViewFactory vf)  {    preferenceChanged(this, true, false);  }  /**   * Receives notification that some text has been removed within the   * text fragment that this view is responsible for. This calls   * {@link View#preferenceChanged(View, boolean, boolean)} on the parent for   * width.   *   * @param e the document event describing the change; not used here   * @param a the view allocation on screen; not used here   * @param vf the view factory; not used here   */  public void removeUpdate(DocumentEvent e, Shape a, ViewFactory vf)  {    preferenceChanged(this, true, false);  }  /**   * Creates a fragment view of this view that starts at <code>p0</code> and   * ends at <code>p1</code>.   *   * @param p0 the start location for the fragment view   * @param p1 the end location for the fragment view   *   * @return the fragment view   */  public View createFragment(int p0, int p1)  {    GlyphView fragment = (GlyphView) clone();    if (p0 != getStartOffset())      fragment.startOffset = p0;    if (p1 != getEndOffset())      fragment.endOffset = p1;    return fragment;  }  /**   * Returns the alignment of this view along the specified axis. For the Y   * axis this is <code>(height - descent) / height</code> for the used font,   * so that it is aligned along the baseline.   * For the X axis the superclass is called.   */  public float getAlignment(int axis)  {    float align;    if (axis == Y_AXIS)      {        checkPainter();        GlyphPainter painter = getGlyphPainter();        float height = painter.getHeight(this);        float descent = painter.getDescent(this);        align = (height - descent) / height;       }    else      align = super.getAlignment(axis);    return align;  }  /**   * Returns the model location that should be used to place a caret when   * moving the caret through the document.   *   * @param pos the current model location   * @param bias the bias for <code>p</code>   * @param a the allocated region for the glyph view   * @param direction the direction from the current position; Must be one of   *        {@link SwingConstants#EAST}, {@link SwingConstants#WEST},   *        {@link SwingConstants#NORTH} or {@link SwingConstants#SOUTH}   * @param biasRet filled with the bias of the resulting location when method   *        returns   *   * @return the location within the document that should be used to place the   *         caret when moving the caret around the document   *   * @throws BadLocationException if <code>pos</code> is an invalid model   *         location   * @throws IllegalArgumentException if <code>d</code> is invalid   */  public int getNextVisualPositionFrom(int pos, Position.Bias bias, Shape a,                                       int direction, Position.Bias[] biasRet)    throws BadLocationException  {    checkPainter();    GlyphPainter painter = getGlyphPainter();    return painter.getNextVisualPositionFrom(this, pos, bias, a, direction,                                             biasRet);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?