JDK-8314215 : Trailing Spaces before Line Breaks Affect the Center Alignment of Text
  • Type: Bug
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: jfx11,8,jfx21
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2023-08-11
  • Updated: 2025-02-24
  • Resolved: 2024-04-28
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
jfx23 b16Fixed
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
Trailing spaces before line breaks should not affect center alignment as Photoshop does not. You can find a video of Photoshop here: https://github.com/xulihang/ImageTrans-docs/issues/482

And here is a screenshot of Text in JavaFX. The first image displays the following text: "This is a paragraph" while the second displays the following text: "This is a
paragraph".

https://github.com/xulihang/JavaFX-samples/blob/main/alignment/screenshot.jpg



Comments
A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jfx/pull/1236 Date: 2023-09-10 20:50:18 +0000
24-02-2025

Changeset: 398f104d Author: John Hendrikx <jhendrikx@openjdk.org> Date: 2024-04-28 18:22:23 +0000 URL: https://git.openjdk.org/jfx/commit/398f104d6ba721f4534d6e7afdc903b2384e147f
28-04-2024

I've done some experimenting with the code that calculates where the breaks are located. There is (IMHO) another problem, and that's the fact that the breaks will take spaces into account currently. Assume the underscore (_) represents spaces, and you have a text like "A A A _ _"; the wrapping algorithm will not wrap when the 2nd space won't fit, but it will wrap when the first space doesn't fit anymore (there is a comment in the code about that "only keep one white space in the line before wrapping"). I think this (for Labels at least) is not what you want. When you have a label that wraps to three lines: "A A A _ _", "B B B _ _ " and "C C C" then once the spaces don't fit anymore it will start doing crazy stuff like splitting up "AAA" into "AA" and "A". Instead it should really not take the spaces into account at all for these cases. Fixing this by A) adjusting the line widths to exclude trailing spaces, and B) removing the `break` in the `while` loop near the above comment makes all alignments look correct. Now, I'm guessing this same code will also be used for editable fields. Here there is an additional problem -- where do you put the cursor when it is located in one the the trailing whitespaces on a wrapped line? For LEFT alignment (and CENTER alignment most of the time) there will be space to put the cursor. For RIGHT alignment the cursor would be outside the bounds. I checked Microsoft Word, and when RIGHT alignment is used, they simply render the cursor beyond the right margin. When there are sufficient trailing spaces, the cursor can even disappear off the page. Still, this is a common problem with editors, and even with LEFT alignment there will be artifacts. For example, here in JIRA typing spaces before a line break will simply render the cursor at the maximum possible right position, and no further, even if there are more spaces being typed. So I think that for editable fields, the cursor should be placed at the given space, if that space is actually visible, or at the most right allowed position if the space was cut off. This will look quite natural for LEFT alignment. For CENTER alignment, this works pretty well too. For RIGHT alignment the cursor will stick to the last visible character, until a non-space character is typed.
09-09-2023

Looks like https://bugs.openjdk.org/browse/JDK-8129014 and https://bugs.openjdk.org/browse/JDK-8145496 are related.
09-09-2023

Also added right justification screenshot to more clearly show that those spaces really need to be stripped. Only left justification works as you'd expect at the moment.
09-09-2023

Attached a screenshot that shows that center justification doesn't work as you'd expect for this code; Label label = new Label("AAA AAA AAA"); label.setFont(Font.font("courier new", 120)); label.setWrapText(true); label.setTextAlignment(TextAlignment.CENTER); stage.setScene(new Scene(label)); stage.show();
09-09-2023

Well .. spaces have an advance, they just don't colour any pixels. So this would require special handling. And why just trailing spaces ? What about leading spaces, why would that be different ? And should it look like if you try to edit the text ? There are probably differing views on that.
16-08-2023