<div dir="ltr">Missing floating-point-precision method in font measuring API<br><br>Hello,<br><br>It looks like there's currently a gap in text measuring API with respect to<br>methods working with floating point coordinates. It's not something new, but<br>with HiDPI support added in Java 9, it seems to become more significant.<br><br>Consider the following simplified use case of custom text component which<br>supports highlighting. Suppose we initially draw a couple of characters<br>(in paintComponent method) like this<br><br>  char[] text = new char[] {'a', 'b'};<br>  int x = ..., y = ...;<br>  g.drawChars(text, 0, 2, x, y);<br>  <br>But then we need draw the second character in a different color, and we'd like<br>the glyph positions to be the same. We assume the text is simple (e.g. Latin, <br>without ligatures, etc). The simplest way to do it is<br><br>  g.drawChars(text, 0, 1, x, y);<br>  g.setColor(...);<br>  int advance = g.getFontMetrics().charWidth(text[0]);<br>  g.drawChars(text, 1, 1, x + advance, y);<br><br>This will not work though, if advance is not integer. We wouldn't be affected<br>by this issue, if we didn't use fractional metrics, but in HiDPI case <br>the advance can fractional even if fractional metrics are not used - <br>glyph is positioned at integer coordinates in device space, so e.g. with <br>200% scale, in user space it can contain half-pixels. Current solution is<br><br> float advance = (float)g.getFontMetrics().getStringBounds(text, 0, 1, g).x;<br> g.drawString(new String(text, 1, 1), x + advance, (float)y);<br> <br>Alternatively, we can use font.createGlyphVector() instead of getStringBounds<br>(this is done internally anyway), but in any case an instance of GlyphVector<br>will be created in the process, which seems unnecessary - all we need is advance<br>which is already available, we just cannot get it directly without it being<br>rounded. Creation of new String instance, copying underlying text, also doesn't<br>seem to be necessary, but there's no other method to draw part of existing text<br>using floating point coordinates.<br><br>Do you think adding corresponding new floating-point-based method to FontMetrics<br>(and maybe to Graphics2D) makes sense? Maybe it's planned already as part of<br>HiDPI-related activity? Should I raise an RFE via <a href="http://bugs.java.com">bugs.java.com</a> for this?<br><br>Thanks,<br>Dmitry Batrak<br><br><br></div>