📄 glyphview.java
字号:
* 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 float getSpan(GlyphView view, int p0, int p1, TabExpander te, float x) { Element el = view.getElement(); Font font = view.getFont(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font); Segment txt = view.getText(p0, p1); int span = Utilities.getTabbedTextWidth(txt, fm, (int) x, te, p0); return span; } /** * Returns the ascent of the text run that is rendered by this * <code>GlyphPainter</code>. * * @param v the glyph view * * @return the ascent of the text run that is rendered by this * <code>GlyphPainter</code> * * @see FontMetrics#getAscent() */ public float getAscent(GlyphView v) { Font font = v.getFont(); FontMetrics fm = v.getContainer().getFontMetrics(font); return fm.getAscent(); } /** * Returns the descent of the text run that is rendered by this * <code>GlyphPainter</code>. * * @param v the glyph view * * @return the descent of the text run that is rendered by this * <code>GlyphPainter</code> * * @see FontMetrics#getDescent() */ public float getDescent(GlyphView v) { Font font = v.getFont(); FontMetrics fm = v.getContainer().getFontMetrics(font); return fm.getDescent(); } /** * 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 int getBoundedPosition(GlyphView v, int p0, float x, float len) { TabExpander te = v.getTabExpander(); Segment txt = v.getText(p0, v.getEndOffset()); Font font = v.getFont(); FontMetrics fm = v.getContainer().getFontMetrics(font); int pos = Utilities.getTabbedTextOffset(txt, fm, (int) x, (int) (x + len), te, p0, false); return pos; } /** * 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 int viewToModel(GlyphView v, float x, float y, Shape a, Bias[] biasRet) { Rectangle b = a.getBounds(); int pos = getBoundedPosition(v, v.getStartOffset(), b.x, x - b.x); return pos; } } /** * The GlyphPainer used for painting the glyphs. */ GlyphPainter glyphPainter; /** * The start offset within the document for this view. */ int startOffset; /** * The end offset within the document for this view. */ int endOffset; /** * Creates a new <code>GlyphView</code> for the given <code>Element</code>. * * @param element the element that is rendered by this GlyphView */ public GlyphView(Element element) { super(element); startOffset = element.getStartOffset(); endOffset = element.getEndOffset(); } /** * Returns the <code>GlyphPainter</code> that is used by this * <code>GlyphView</code>. If no <code>GlyphPainer</code> has been installed * <code>null</code> is returned. * * @return the glyph painter that is used by this * glyph view or <code>null</code> if no glyph painter has been * installed */ public GlyphPainter getGlyphPainter() { return glyphPainter; } /** * Sets the {@link GlyphPainter} to be used for this <code>GlyphView</code>. * * @param painter the glyph painter to be used for this glyph view */ public void setGlyphPainter(GlyphPainter painter) { glyphPainter = painter; } /** * Checks if a <code>GlyphPainer</code> is installed. If this is not the * case, a default painter is installed. */ protected void checkPainter() { if (glyphPainter == null) glyphPainter = new DefaultGlyphPainter(); } /** * Renders the <code>Element</code> that is associated with this * <code>View</code>. * * @param g the <code>Graphics</code> context to render to * @param a the allocated region for the <code>Element</code> */ public void paint(Graphics g, Shape a) { Element el = getElement(); checkPainter(); getGlyphPainter().paint(this, g, a, el.getStartOffset(), el.getEndOffset()); } /** * Returns the preferred span of the content managed by this * <code>View</code> along the specified <code>axis</code>. * * @param axis the axis * * @return the preferred span of this <code>View</code>. */ public float getPreferredSpan(int axis) { float span = 0; checkPainter(); GlyphPainter painter = getGlyphPainter(); if (axis == X_AXIS) { Element el = getElement(); TabExpander tabEx = null; View parent = getParent(); if (parent instanceof TabExpander) tabEx = (TabExpander) parent; span = painter.getSpan(this, getStartOffset(), getEndOffset(), tabEx, 0.F); } else span = painter.getHeight(this); return span; } /** * 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 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(int pos, Shape a, Position.Bias b) throws BadLocationException { GlyphPainter p = getGlyphPainter(); return p.modelToView(this, pos, b, a); } /** * Maps coordinates from the <code>View</code>'s space into a position * in the document model. * * @param x the x coordinate in the view space * @param y the y coordinate in the view space * @param a the allocation of this <code>View</code> * @param b the bias to use * * @return the position in the document that corresponds to the screen * coordinates <code>x, y</code> */ public int viewToModel(float x, float y, Shape a, Position.Bias[] b) { checkPainter(); GlyphPainter painter = getGlyphPainter(); return painter.viewToModel(this, x, y, a, b); } /** * Return the {@link TabExpander} to use. * * @return the {@link TabExpander} to use */ public TabExpander getTabExpander() { TabExpander te = null; View parent = getParent(); if (parent instanceof TabExpander) te = (TabExpander) parent; return te; } /** * Returns the preferred span of this view for tab expansion. * * @param x the location of the view * @param te the tab expander to use * * @return the preferred span of this view for tab expansion */ public float getTabbedSpan(float x, TabExpander te) { Element el = getElement(); return getGlyphPainter().getSpan(this, el.getStartOffset(), el.getEndOffset(), te, x); } /** * Returns the span of a portion of the view. This is used in TAB expansion * for fragments that don't contain TABs. * * @param p0 the start index * @param p1 the end index * * @return the span of the specified portion of the view */ public float getPartialSpan(int p0, int p1) { Element el = getElement(); Document doc = el.getDocument(); Segment seg = new Segment(); try { doc.getText(p0, p1 - p0, seg); } catch (BadLocationException ex) { AssertionError ae; ae = new AssertionError("BadLocationException must not be thrown " + "here"); ae.initCause(ex); throw ae; } FontMetrics fm = null; // Fetch font metrics somewhere. return Utilities.getTabbedTextWidth(seg, fm, 0, null, p0); } /** * Returns the start offset in the document model of the portion * of text that this view is responsible for. * * @return the start offset in the document model of the portion * of text that this view is responsible for */ public int getStartOffset() { return startOffset; } /** * Returns the end offset in the document model of the portion * of text that this view is responsible for. * * @return the end offset in the document model of the portion * of text that this view is responsible for */ public int getEndOffset() { return endOffset; } /** * Returns the text segment that this view is responsible for. * * @param p0 the start index in the document model * @param p1 the end index in the document model * * @return the text segment that this view is responsible for */ public Segment getText(int p0, int p1) { Segment txt = new Segment(); 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -