On Fri, 9 Feb 2024 12:54:17 GMT, John Hendrikx <jhendr...@openjdk.org> wrote:

>> There are a number of tickets open related to text rendering:
>> 
>> https://bugs.openjdk.org/browse/JDK-8314215
>> 
>> https://bugs.openjdk.org/browse/JDK-8145496
>> 
>> https://bugs.openjdk.org/browse/JDK-8129014
>> 
>> They have in common that wrapped text is taking the trailing spaces on each 
>> wrapped line into account when calculating where to wrap.  This looks okay 
>> for text that is left aligned (as the spaces will be trailing the lines and 
>> generally aren't a problem, but looks weird with CENTER and RIGHT 
>> alignments.  Even with LEFT alignment there are artifacts of this behavior, 
>> where a line like `AAA  BBB  CCC` (note the **double** spaces) gets split up 
>> into `AAA  `, `BBB  ` and `CCC`, but if space reduces further, it will wrap 
>> **too** early because the space is taken into account (ie. `AAA` may still 
>> have fit just fine, but `AAA  ` doesn't, so the engine wraps it to `AA` + `A 
>>  ` or something).
>> 
>> The fix for this is two fold; first the individual lines of text should not 
>> include any trailing spaces into their widths; second, the code that is 
>> taking the trailing space into account when wrapping should ignore all 
>> trailing spaces (currently it is ignoring all but one trailing space).  With 
>> these two fixes, the layout in LEFT/CENTER/RIGHT alignments all look great, 
>> and there is no more early wrapping due to a space being taking into account 
>> while the actual text still would have fit (this is annoying in tight 
>> layouts, where a line can be wrapped early even though it looks like it 
>> would have fit).
>> 
>> If it were that simple, we'd be done, but there may be another issue here 
>> that needs solving: wrapped aligned TextArea's.
>> 
>> TextArea don't directly support text alignment (via a setTextAlignment 
>> method like Label) but you can change it via CSS.
>> 
>> For Left alignment + wrapping, TextArea will ignore any spaces typed before 
>> a line that was wrapped.  In other words, you can type spaces as much as you 
>> want, and they won't show up and the cursor won't move.  The spaces are all 
>> getting appended to the previous line.  When you cursor through these 
>> spaces, the cursor can be rendered out of the control's bounds.  To 
>> illustrate, if you have the text `AAA                 BBB CCC`, and the text 
>> gets wrapped to `AAA`, `BBB`, `CCC`, typing spaces before `BBB` will not 
>> show up.  If you cursor back, the cursor may be outside the control bounds 
>> because so many spaces are trailing `AAA`.
>> 
>> The above behavior has NOT changed, is pretty standard for wrapped text 
>> controls,...
>
> John Hendrikx has updated the pull request incrementally with two additional 
> commits since the last revision:
> 
>  - Add some clarifying documentation
>  - Do not collapse trailing spaces of last line (where no soft wrap occurs)

> So we treat trailing spaces before \n as hard wrap, does it mean the same 
> treatment is applied to the leading space (i.e. leading spaces in the first 
> line), as seen here with TextAlignment=CENTER
> 
> ```
>    aaa    bbb     bbbb    
> ccc   dddd   eee   ffffff
>     aaa     b     c  
> ```
> 
> ![Screenshot 2024-02-09 at 09 59 
> 31](https://private-user-images.githubusercontent.com/107069028/303737487-9541c449-0c25-44ee-b85e-827b760d4710.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MDc1MTUyODIsIm5iZiI6MTcwNzUxNDk4MiwicGF0aCI6Ii8xMDcwNjkwMjgvMzAzNzM3NDg3LTk1NDFjNDQ5LTBjMjUtNDRlZS1iODVlLTgyN2I3NjBkNDcxMC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwMjA5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDIwOVQyMTQzMDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00OWY4ODZhYWQ3Mzg4MjliNDRjNmY5ZDY3NGUxOTdhMGJlZjIyMzJjZTExY2IzYjE0MmNlOTI0NmI1NDBmY2M0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.Ww_efTpd0AxSa_QVMjmKH2z8K1BPcVd8Kqp5vJtHE7o)

Yes, white space is only collapsed when a soft wrap is inserted to keep it 
within `wrappingWidth` -- this is because a user can't predict where such 
wrapping occurs and would have to strip spaces there themselves (never mind 
that it wouldn't wrap anymore there then as there is no space).  The other 
cases the user can easily solve themselves by ensuring that the strings they 
supply are `trim`ed.

I think removing leading/trailing spaces (and double spaces) is a different 
feature (browsers like to do this) but not really suitable as a good default 
for FX.  It could be made optional, but I think it is easy enough for a user to 
sanitize the strings they deliver a bit, while it would be impossible for the 
user to do so at the soft wrap locations.

-------------

PR Comment: https://git.openjdk.org/jfx/pull/1236#issuecomment-1936652246

Reply via email to