@@ -60,32 +60,55 @@ protected void updateTextSize() {
6060 }
6161
6262 protected void updateBounds () {
63- // N.B. important: usage of getBoundsInParent() which also takes into account text rotations.
64- //
6563 // Checking text bounds via a node is incredibly wasteful, so we try to use an internal API
6664 // that is available with appropriate jvm flags. The two results can differ by tiny amounts
67- // as the width may depend on the actual character sequence (e.g. 4 followed by 3), but in
68- // initial tests the diff was usually below 1px. This should not matter in practice.
65+ // as the width may depend on the actual character sequence (e.g. 4 followed by 3). In our
66+ // tests the diff was generally within 1px, so this should not matter in practice.
67+ final double w , h ;
6968 if (FxFontMetrics .isAvailable ()) {
70- this .height = FxFontMetrics .getLineHeight (style .getFont ());
71- this .width = FxFontMetrics .getWidth (style .getFont (), text );
72- if (getRotation () != 0 ) {
73- var rad = Math .toRadians (getRotation ());
74- var sin = Math .sin (rad );
75- var cos = Math .cos (rad );
76- var w = width ;
77- var h = height ;
78- this .width = (w * cos ) + (h * sin ) + 0.5 ; // account for AA
79- this .height = (w * sin ) + (h * cos ) + 0.5 ; // account for AA
80- }
69+ h = FxFontMetrics .getLineHeight (style .getFont ());
70+ w = FxFontMetrics .getWidth (style .getFont (), text );
8171 } else {
8272 style .setText (text );
83- var bounds = style .getBoundsInParent ();
84- height = bounds .getHeight ();
85- width = bounds .getWidth ();
73+ var bounds = style .getLayoutBounds ();
74+ h = bounds .getHeight ();
75+ w = bounds .getWidth ();
8676 }
77+
78+ // Account for rotation
79+ if (getRotation () == 0 ) {
80+ this .width = w ;
81+ this .height = h ;
82+ } else {
83+ var rad = Math .toRadians (getRotation ());
84+ var sin = Math .abs (Math .sin (rad ));
85+ var cos = Math .abs (Math .cos (rad ));
86+ this .height = (w * sin ) + (h * cos );
87+ this .width = (w * cos ) + (h * sin );
88+ }
89+
90+ if (DEBUG ) {
91+ // Note: getBoundsInParent() takes into account the rotation,
92+ // but it's a more expensive call.
93+ style .setText (text );
94+ var parent = style .getBoundsInParent ();
95+ var hp = parent .getHeight ();
96+ var wp = parent .getWidth ();
97+
98+ System .out .printf ("[Label dimensions] Text: '%s' | Rotation: %.1f°%n" +
99+ " Manual: W: %8.4f | H: %8.4f%n" +
100+ " Layout: W: %8.4f | H: %8.4f%n" +
101+ " Delta: W: %8.4f | H: %8.4f%n" ,
102+ text , getRotation (),
103+ width , height ,
104+ wp , hp ,
105+ Math .abs (width - wp ), Math .abs (height - hp ));
106+ }
107+
87108 }
88109
110+ private static final boolean DEBUG = false ;
111+
89112 /**
90113 * @return the style applied to this tickmark
91114 */
0 commit comments