📄 glyphview.java
字号:
/* GlyphView.java -- A view to render styled text Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.Shape;import java.awt.Toolkit;import java.text.BreakIterator;import javax.swing.SwingConstants;import javax.swing.event.DocumentEvent;import javax.swing.text.Position.Bias;/** * Renders a run of styled text. This {@link View} subclass paints the * characters of the <code>Element</code> it is responsible for using * the style information from that <code>Element</code>. * * @author Roman Kennke (roman@kennke.org) */public class GlyphView extends View implements TabableView, Cloneable{ /** * An abstract base implementation for a glyph painter for * <code>GlyphView</code>. */ public abstract static class GlyphPainter { /** * Creates a new <code>GlyphPainer</code>. */ public GlyphPainter() { // Nothing to do here. } /** * Returns the ascent of the font that is used by this glyph painter. * * @param v the glyph view * * @return the ascent of the font that is used by this glyph painter */ public abstract float getAscent(GlyphView v); /** * Returns the descent of the font that is used by this glyph painter. * * @param v the glyph view * * @return the descent of the font that is used by this glyph painter */ public abstract float getDescent(GlyphView v); /** * Returns the full height of the rendered text. * * @return the full height of the rendered text */ public abstract float getHeight(GlyphView view); /** * Determines the model offset, so that the text between <code>p0</code> * and this offset fits within the span starting at <code>x</code> with * the length of <code>len</code>. * * @param v the glyph view * @param p0 the starting offset in the model * @param x the start location in the view * @param len the length of the span in the view */ public abstract int getBoundedPosition(GlyphView v, int p0, float x, float len); /** * Paints the glyphs. * * @param view the glyph view to paint * @param g the graphics context to use for painting * @param a the allocation of the glyph view * @param p0 the start position (in the model) from which to paint * @param p1 the end position (in the model) to which to paint */ public abstract void paint(GlyphView view, Graphics g, Shape a, int p0, int p1); /** * Maps a position in the document into the coordinate space of the View. * The output rectangle usually reflects the font height but has a width * of zero. * * @param view the glyph view * @param pos the position of the character in the model * @param a the area that is occupied by the view * @param b either {@link Position.Bias#Forward} or * {@link Position.Bias#Backward} depending on the preferred * direction bias. If <code>null</code> this defaults to * <code>Position.Bias.Forward</code> * * @return a rectangle that gives the location of the document position * inside the view coordinate space * * @throws BadLocationException if <code>pos</code> is invalid * @throws IllegalArgumentException if b is not one of the above listed * valid values */ public abstract Shape modelToView(GlyphView view, int pos, Position.Bias b, Shape a) throws BadLocationException; /** * Maps a visual position into a document location. * * @param v the glyph view * @param x the X coordinate of the visual position * @param y the Y coordinate of the visual position * @param a the allocated region * @param biasRet filled with the bias of the model location on method exit * * @return the model location that represents the specified view location */ public abstract int viewToModel(GlyphView v, float x, float y, Shape a, Position.Bias[] biasRet); /** * Determine the span of the glyphs from location <code>p0</code> to * location <code>p1</code>. If <code>te</code> is not <code>null</code>, * then TABs are expanded using this <code>TabExpander</code>. * The parameter <code>x</code> is the location at which the view is * located (this is important when using TAB expansion). * * @param view the glyph view * @param p0 the starting location in the document model * @param p1 the end location in the document model * @param te the tab expander to use * @param x the location at which the view is located * * @return the span of the glyphs from location <code>p0</code> to * location <code>p1</code>, possibly using TAB expansion */ public abstract float getSpan(GlyphView view, int p0, int p1, TabExpander te, float x); /** * Returns the model location that should be used to place a caret when * moving the caret through the document. * * @param v the glyph view * @param pos the current model location * @param b 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(GlyphView v, int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet) throws BadLocationException { int result = pos; switch (direction) { case SwingConstants.EAST: result = pos + 1; break; case SwingConstants.WEST: result = pos - 1; break; case SwingConstants.NORTH: case SwingConstants.SOUTH: default: // This should be handled in enclosing view, since the glyph view // does not layout vertically. break; } return result; } /** * Returns a painter that can be used to render the specified glyph view. * If this glyph painter is stateful, then it should return a new instance. * However, if this painter is stateless it should return itself. The * default behaviour is to return itself. * * @param v the glyph view for which to create a painter * @param p0 the start offset of the rendered area * @param p1 the end offset of the rendered area * * @return a painter that can be used to render the specified glyph view */ public GlyphPainter getPainter(GlyphView v, int p0, int p1) { return this; } } /** * The default <code>GlyphPainter</code> used in <code>GlyphView</code>. */ static class DefaultGlyphPainter extends GlyphPainter { /** * Returns the full height of the rendered text. * * @return the full height of the rendered text */ public float getHeight(GlyphView view) { Font font = view.getFont(); FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font); float height = metrics.getHeight(); return height; } /** * Paints the glyphs. * * @param view the glyph view to paint * @param g the graphics context to use for painting * @param a the allocation of the glyph view * @param p0 the start position (in the model) from which to paint * @param p1 the end position (in the model) to which to paint */ public void paint(GlyphView view, Graphics g, Shape a, int p0, int p1) { int height = (int) getHeight(view); Segment txt = view.getText(p0, p1); Rectangle bounds = a.getBounds(); TabExpander tabEx = null; View parent = view.getParent(); if (parent instanceof TabExpander) tabEx = (TabExpander) parent; // Fill the background of the text run. Color background = view.getBackground(); g.setColor(background); int width = Utilities.getTabbedTextWidth(txt, g.getFontMetrics(), bounds.x, tabEx, txt.offset); g.fillRect(bounds.x, bounds.y, width, height); // Draw the actual text. g.setColor(view.getForeground()); g.setFont(view.getFont()); if (view.isSuperscript()) // TODO: Adjust font for superscripting. Utilities.drawTabbedText(txt, bounds.x, bounds.y - height / 2, g, tabEx, txt.offset); else if (view.isSubscript()) // TODO: Adjust font for subscripting. Utilities.drawTabbedText(txt, bounds.x, bounds.y + height / 2, g, tabEx, txt.offset); else Utilities.drawTabbedText(txt, bounds.x, bounds.y, g, tabEx, txt.offset); if (view.isStikeThrough()) { int strikeHeight = (int) (getAscent(view) / 2); g.drawLine(bounds.x, bounds.y + strikeHeight, bounds.height + width, bounds.y + strikeHeight); } if (view.isUnderline()) { int lineHeight = (int) getAscent(view); g.drawLine(bounds.x, bounds.y + lineHeight, bounds.height + width, bounds.y + lineHeight); } } /** * Maps a position in the document into the coordinate space of the View. * The output rectangle usually reflects the font height but has a width * of zero. * * @param view the glyph view * @param pos the position of the character in the model * @param a the area that is occupied by the view * @param b either {@link Position.Bias#Forward} or * {@link Position.Bias#Backward} depending on the preferred * direction bias. If <code>null</code> this defaults to * <code>Position.Bias.Forward</code> * * @return a rectangle that gives the location of the document position * inside the view coordinate space * * @throws BadLocationException if <code>pos</code> is invalid * @throws IllegalArgumentException if b is not one of the above listed * valid values */ public Shape modelToView(GlyphView view, int pos, Position.Bias b, Shape a) throws BadLocationException { Element el = view.getElement(); Font font = view.getFont(); FontMetrics fm = view.getContainer().getFontMetrics(font); Segment txt = view.getText(el.getStartOffset(), pos); int width = fm.charsWidth(txt.array, txt.offset, txt.count); int height = fm.getHeight(); Rectangle bounds = a.getBounds(); Rectangle result = new Rectangle(bounds.x + width, bounds.y, bounds.x + width, height); return result; } /** * Determine the span of the glyphs from location <code>p0</code> to * location <code>p1</code>. If <code>te</code> is not <code>null</code>, * then TABs are expanded using this <code>TabExpander</code>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -