📄 glyphview.java
字号:
* 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(); return StyleConstants.getBackground(atts); } /** * 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 isStikeThrough() { 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(); int breakLocation = painter.getBoundedPosition(this, p0, pos, len); // Try to find a suitable line break. BreakIterator lineBreaker = BreakIterator.getLineInstance(); Segment txt = new Segment(); try { getDocument().getText(getStartOffset(), getEndOffset(), txt); } catch (BadLocationException ex) { AssertionError err = new AssertionError("BadLocationException must not " + "be thrown here."); err.initCause(ex); throw err; } lineBreaker.setText(txt); int goodBreakLocation = lineBreaker.previous(); if (goodBreakLocation != BreakIterator.DONE) breakLocation = goodBreakLocation; 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 { // Determine the model locations at pos and pos + len. int spanX = (int) getPreferredSpan(X_AXIS); int spanY = (int) getPreferredSpan(Y_AXIS); Rectangle dummyAlloc = new Rectangle(0, 0, spanX, spanY); Position.Bias[] biasRet = new Position.Bias[1]; int offset1 = viewToModel(pos, spanY / 2, dummyAlloc, biasRet); int offset2 = viewToModel(pos, spanY / 2, dummyAlloc, biasRet); Segment txt = getText(offset1, offset2); BreakIterator lineBreaker = BreakIterator.getLineInstance(); lineBreaker.setText(txt); int breakLoc = lineBreaker.previous(); if (breakLoc == offset1) weight = View.BadBreakWeight; else if(breakLoc == BreakIterator.DONE) weight = View.GoodBreakWeight; else weight = View.ExcellentBreakWeight; } 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) { getParent().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)} 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 insertUpdate(DocumentEvent e, Shape a, ViewFactory vf) { getParent().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) { getParent().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(); fragment.startOffset = p0; 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); } /** * Returns the document position that is (visually) nearest to the given * document position <code>pos</code> in the given direction <code>d</code>. * * @param c the text component * @param pos the document position * @param b the bias for <code>pos</code> * @param d the direction, must be either {@link SwingConstants#NORTH}, * {@link SwingConstants#SOUTH}, {@link SwingConstants#WEST} or * {@link SwingConstants#EAST} * @param biasRet an array of {@link Position.Bias} that can hold at least * one element, which is filled with the bias of the return position * on method exit * * @return the document position that is (visually) nearest to the given * document position <code>pos</code> in the given direction * <code>d</code> * * @throws BadLocationException if <code>pos</code> is not a valid offset in * the document model */ public int getNextVisualPositionFrom(JTextComponent c, int pos, Position.Bias b, int d, Position.Bias[] biasRet) throws BadLocationException { // TODO: Implement this properly. throw new AssertionError("Not implemented yet."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -