JDK-8090775 : Need font/text measurement API
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: fx2.0
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2010-03-17
  • Updated: 2018-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Duplicate :  
Description
Presently the only way to measure text is to create a Text node, insert it into a scene, and
ask for its bounds : logical or visual. To make matters a bit worse, those bounds will be
padded because they are overloaded to handle dirty regions.
To help lay out etc, a client will benefit from being able to ask for exact advance of at
least a single line string (perhaps with styling ?). The client will need to specify
a suitable render context (graphics transform and font) as well as the specific
text for this to be valid.
Comments
Thanks Felipe for that tip ��� quite useful!
17-04-2013

Adding an FontMetrics API would not be hard. On the other hand, text metrics is a bit more complicated (specially if you consider rich text). See also RT-29257 & RT-29077, these deal with selection support which includes hit test API (not sure what you are trying to do, but hit test is quite useful). Finally, if you need a workaround as soon as possible, see javafx-ui-controls/src/com/sun/javafx/scene/control/skin/Utils.java
16-04-2013

This issue was raised in 2010. Is there any hope of progress during 2013? This issue is important to us.
16-04-2013

I think it would be terrific if access to FontMetrics is provided via the corresponding Font as described by Richard Bair above. As I look over com.sun.javafx.tk.FontMetrics, it seems to be missing a bunch of the helper methods found in java.awt.FontMetrics. If the plan is to promote it from the toolkit to the public API, it would be nice to see methods like charWidth(), getWidths(), getStringBounds() etc., all returning doubles to be consistent with classes like Bounds.
27-06-2012

For those of us who require precise positioning of fully kerned text, a JavaFX equivalent of java.awt.FontMetrics would be invaluable. My current workaround involves creating a 'shadow' java.awt.Font for each JavaFX font, then extracting FontMetrics from a small offscreen BufferedImage. I would greatly prefer not to need to do this. (my first crack at inserting code into a JIRA comment - please forgive if not formatted properly): // create a JavaFX Font for use in Text nodes Font font = Font.font("Arial", FontWeight.NORMAL, 18); // let's see if we can line these up String s1 = "the quick sly fox jump"; String s2 = "ed over the lazy brown dog"; Text firstPart = new Text(s1); firstPart.setFont(font); Text secondPart = new Text(s2); secondPart.setFont(font); // kludge: create a 'shadow' Java font java.awt.Font shadowFont = new java.awt.Font ("Arial", java.awt.Font.PLAIN, 18); // create a small offscreen BufferedImage BufferedImage bi = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(32,32); // get a Graphics2D object, set the java.awt.Font, extract the FontMetrics Graphics2D g = bi.createGraphics(); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g.setFont(shadowFont); java.awt.FontMetrics fm = g.getFontMetrics(); // lay out the first Text node firstPart.setLayoutX(100); firstPart.setLayoutY(100); // use the FontMetrics to position the second Text node adjacent to the first one secondPart.setLayoutX(100 + fm.stringWidth(s1)); secondPart.setLayoutY(100); // add Text nodes to a pane Pane pane = new Pane(); pane.getChildren().addAll(firstPart,secondPart);
15-06-2012

FWIW, this comment was in our FontMetrics class (Obviously the use of FX Script isn't applicable but the general idea of making it part of the Font class and lazily evaluated is still relevant) * TODO Ultimately I'd like to make this public and accessible from the * Font class, perhaps with a simple lazy bind. So something like: * public class Font { * public-read var metrics = bind lazy Toolkit.getToolkit().getFontMetrics(this); * ... * }
28-02-2012

I am not sure we actually need this as public API for presidio. If the controls need this support or additional support. we can add it internally for presidio and make it public later when we have solid external use cases and more time to get it done properly. There's already some basic internal measurement support attached to the toolkit and if it needs more that's information that would be very useful. Ah I see this was resurrected because of the recent external RT-11655 and it was closed as a dup of this old RFE. Copying the external EA user's text : "To create custom nodes with advanced text features, there is need for font metrics. Something like getStringBounds, getCharWidth, getOutline, getCharAdvance and additional functions to calculate glyph sizes." That's a longish list and whilst not unreasonable, and likely pretty key to creating custom nodes, we'd likely want to think about it rather than rush it. It also misses the visual bounds which I expect someone would want. A number of the controls workaround this quite satisfactorily by creating off screen Text Nodes (ie not added to a scene) and querying their bounds
25-03-2011