@@ -60,32 +60,56 @@ 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 deg = Math .abs (getRotation ());
84+ var rad = Math .toRadians (deg );
85+ var sin = Math .sin (rad );
86+ var cos = Math .cos (rad );
87+ this .height = (w * sin ) + (h * cos );
88+ this .width = (w * cos ) + (h * sin );
89+ }
90+
91+ if (DEBUG ) {
92+ // Note: getBoundsInParent() takes into account the rotation,
93+ // but it's a more expensive call.
94+ style .setText (text );
95+ var parent = style .getBoundsInParent ();
96+ var hp = parent .getHeight ();
97+ var wp = parent .getWidth ();
98+
99+ System .out .printf ("[Label dimensions] Text: '%s' | Rotation: %.1f°%n" +
100+ " Manual: W: %8.4f | H: %8.4f%n" +
101+ " Layout: W: %8.4f | H: %8.4f%n" +
102+ " Delta: W: %8.4f | H: %8.4f%n" ,
103+ text , getRotation (),
104+ width , height ,
105+ wp , hp ,
106+ Math .abs (width - wp ), Math .abs (height - hp ));
107+ }
108+
87109 }
88110
111+ private static final boolean DEBUG = false ;
112+
89113 /**
90114 * @return the style applied to this tickmark
91115 */
0 commit comments