public Shape modelToView(GlyphView v, int pos, Position.Bias bias, Shape a) throws BadLocationException { int offs = pos - v.getStartOffset(); Rectangle2D alloc = a.getBounds2D(); TextHitInfo hit = (bias == Position.Bias.Forward) ? TextHitInfo.afterOffset(offs) : TextHitInfo.beforeOffset(offs); float[] locs = layout.getCaretInfo(hit); // vertical at the baseline, should use slope and check if glyphs // are being rendered vertically. alloc.setRect(alloc.getX() + locs[0], alloc.getY(), 1, alloc.getHeight()); return alloc; }
/** * Provides a mapping from the view coordinate space to the logical coordinate space of the model. * * @param v the view containing the view coordinates * @param x the X coordinate * @param y the Y coordinate * @param a the allocated region to render into * @param biasReturn either <code>Position.Bias.Forward</code> or <code>Position.Bias.Backward * </code> is returned as the zero-th element of this array * @return the location within the model that best represents the given point of view * @see View#viewToModel */ public int viewToModel(GlyphView v, float x, float y, Shape a, Position.Bias[] biasReturn) { Rectangle2D alloc = (a instanceof Rectangle2D) ? (Rectangle2D) a : a.getBounds2D(); // Move the y co-ord of the hit onto the baseline. This is because TextLayout supports // italic carets and we do not. TextHitInfo hit = layout.hitTestChar(x - (float) alloc.getX(), 0); int pos = hit.getInsertionIndex(); if (pos == v.getEndOffset()) { pos--; } biasReturn[0] = hit.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward; return pos + v.getStartOffset(); }
/** * Paint the glyphs for the given view. This is implemented to only render if the Graphics is of * type Graphics2D which is required by TextLayout (and this should be the case if running on the * JDK). */ public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) { if (g instanceof Graphics2D) { Rectangle2D alloc = a.getBounds2D(); Graphics2D g2d = (Graphics2D) g; float y = (float) alloc.getY() + layout.getAscent() + layout.getLeading(); float x = (float) alloc.getX(); if (p0 > v.getStartOffset() || p1 < v.getEndOffset()) { try { // TextLayout can't render only part of it's range, so if a // partial range is required, add a clip region. Shape s = v.modelToView(p0, Position.Bias.Forward, p1, Position.Bias.Backward, a); Shape savedClip = g.getClip(); g2d.clip(s); layout.draw(g2d, x, y); g.setClip(savedClip); } catch (BadLocationException e) { } } else { layout.draw(g2d, x, y); } } }