diff options
Diffstat (limited to 'javax/swing/text/GlyphView.java')
-rw-r--r-- | javax/swing/text/GlyphView.java | 71 |
1 files changed, 44 insertions, 27 deletions
diff --git a/javax/swing/text/GlyphView.java b/javax/swing/text/GlyphView.java index cb7f8f05d..35c8dd5d7 100644 --- a/javax/swing/text/GlyphView.java +++ b/javax/swing/text/GlyphView.java @@ -252,6 +252,8 @@ public class GlyphView extends View implements TabableView, Cloneable */ static class DefaultGlyphPainter extends GlyphPainter { + FontMetrics fontMetrics; + /** * Returns the full height of the rendered text. * @@ -259,9 +261,8 @@ public class GlyphView extends View implements TabableView, Cloneable */ public float getHeight(GlyphView view) { - Font font = view.getFont(); - FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font); - float height = metrics.getHeight(); + updateFontMetrics(view); + float height = fontMetrics.getHeight(); return height; } @@ -341,16 +342,16 @@ public class GlyphView extends View implements TabableView, Cloneable Shape a) throws BadLocationException { + updateFontMetrics(view); Element el = view.getElement(); - Font font = view.getFont(); - FontMetrics fm = view.getContainer().getFontMetrics(font); Segment txt = view.getText(el.getStartOffset(), pos); Rectangle bounds = a instanceof Rectangle ? (Rectangle) a : a.getBounds(); TabExpander expander = view.getTabExpander(); - int width = Utilities.getTabbedTextWidth(txt, fm, bounds.x, expander, + int width = Utilities.getTabbedTextWidth(txt, fontMetrics, bounds.x, + expander, view.getStartOffset()); - int height = fm.getHeight(); + int height = fontMetrics.getHeight(); Rectangle result = new Rectangle(bounds.x + width, bounds.y, 0, height); return result; @@ -375,10 +376,10 @@ public class GlyphView extends View implements TabableView, Cloneable public float getSpan(GlyphView view, int p0, int p1, TabExpander te, float x) { - Font font = view.getFont(); - FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font); + updateFontMetrics(view); Segment txt = view.getText(p0, p1); - int span = Utilities.getTabbedTextWidth(txt, fm, (int) x, te, p0); + int span = Utilities.getTabbedTextWidth(txt, fontMetrics, (int) x, te, + p0); return span; } @@ -395,9 +396,8 @@ public class GlyphView extends View implements TabableView, Cloneable */ public float getAscent(GlyphView v) { - Font font = v.getFont(); - FontMetrics fm = v.getContainer().getFontMetrics(font); - return fm.getAscent(); + updateFontMetrics(v); + return fontMetrics.getAscent(); } /** @@ -413,9 +413,8 @@ public class GlyphView extends View implements TabableView, Cloneable */ public float getDescent(GlyphView v) { - Font font = v.getFont(); - FontMetrics fm = v.getContainer().getFontMetrics(font); - return fm.getDescent(); + updateFontMetrics(v); + return fontMetrics.getDescent(); } /** @@ -430,16 +429,10 @@ public class GlyphView extends View implements TabableView, Cloneable */ public int getBoundedPosition(GlyphView v, int p0, float x, float len) { + updateFontMetrics(v); TabExpander te = v.getTabExpander(); Segment txt = v.getText(p0, v.getEndOffset()); - Font font = v.getFont(); - Container c = v.getContainer(); - FontMetrics fm; - if (c != null) - fm = c.getFontMetrics(font); - else - fm = Toolkit.getDefaultToolkit().getFontMetrics(font); - int pos = Utilities.getTabbedTextOffset(txt, fm, (int) x, + int pos = Utilities.getTabbedTextOffset(txt, fontMetrics, (int) x, (int) (x + len), te, p0, false); return pos + p0; } @@ -458,9 +451,33 @@ public class GlyphView extends View implements TabableView, Cloneable 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 + v.getStartOffset(); + Rectangle r = a instanceof Rectangle ? (Rectangle) a : a.getBounds(); + int p0 = v.getStartOffset(); + int p1 = v.getEndOffset(); + TabExpander te = v.getTabExpander(); + Segment s = v.getText(p0, p1); + int offset = Utilities.getTabbedTextOffset(s, fontMetrics, r.x, (int) x, + te, p0); + int ret = p0 + offset; + if (ret == p1) + ret--; + biasRet[0] = Position.Bias.Forward; + return ret; + } + + private void updateFontMetrics(GlyphView v) + { + Font font = v.getFont(); + if (fontMetrics == null || ! font.equals(fontMetrics.getFont())) + { + Container c = v.getContainer(); + FontMetrics fm; + if (c != null) + fm = c.getFontMetrics(font); + else + fm = Toolkit.getDefaultToolkit().getFontMetrics(font); + fontMetrics = fm; + } } } |