| Ticket UUID: | adb6352846319ff0ba4761bb46391c5ea284fd7c | |||
| Title: | space-newline shows as double newline in message | |||
| Type: | Bug | Version: | 8.6.12 on MacOS | |
| Submitter: | jal_frezie | Created on: | 2022-05-31 11:54:12 | |
| Subsystem: | 14. [message] | Assigned To: | nobody | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Open | Last Modified: | 2022-06-19 16:37:14 | |
| Resolution: | None | Closed By: | nobody | |
| Closed on: | ||||
| Description: |
If the text of a message widget contains a space followed by a newline, it shows in the widget as two newlines, e.g., % pack [message .m -text foo\ \nbar] produces text that looks like foo bar | |||
| User Comments: |
fvogel added on 2022-06-19 16:37:14:
Marc, this doesn't fix the bug I think. The message window still renders in three lines instead of the expected two. No test of the test suite fails except the one I have added this morning specifically to test this bug, i.e. message-4.3 (see my previous message). About TK_ISOLATE_END: I believe this is an old unused relic. While it is passed by TkTextCharLayoutProc() no use is made if this flag anywhere in the Tk source code up to your commit [790b8182]. This means in particular that if a flag is needed to solve the present ticket then it can have the name you want, not necessarily TK_ISOLATE_END. That said, TK_ISOLATE_END is documented a bit at the beginning of TkpMeasureCharsInContext in tkMacOSXFont.c and what is said there seems related to our problem. marc_culler (claiming to be Marc Culler) added on 2022-06-19 13:41:17: So the logic is slowly revealing itself, in spite of the opaque code. There are situations when end-of-line characters should be ignored but trailing whitespace should not be ignored (such as the case of this ticket). There are other situations when both should be ignored (such as when text is being wrapped, causing whitespace to be modified). I committed a change to the bugfix branch which fixes this ticket and does not cause failures in textDisp.test or font.test. This is based on trial-and-error, since I cannot actually follow how the various flags are being used. TK_ISOLATE_END is only used by the Text widget, so it has a special role with respect to textDisp.test. fvogel added on 2022-06-19 05:01:59: I have committed a new test (message-4.3) in the bugfix branch. This test demonstrates the issue (it fails without the fix) and passes with the fix, as expected. The bad news is that the proposed fix triggers new failures in font.test, namely font-24.11, -24.12, -30.6 and -31.6. marc_culler (claiming to be Marc Culler) added on 2022-06-19 02:41:11: I could not figure out why trailing whitespace was being stripped, along with line endings. And that seemed to be the issue with this example. So I removed the code that strips whitespace. After doing that the example above works as expected and there are no textDisp failures. I may be missing something. Or maybe it was just an old bug to strip whitespace. I committed a bugfix branch. kevin_walzer added on 2022-06-18 22:32:17: @marc: The layout mechanism for text line-by-line is found in TkpMeasureCharsInContext() in tkMacOSXFont.c. The function makes heavy use of Core Font functions, including when to break lines. Around line 970 is where the code attempts to trim whitespace and line ending characters from the calculation, looking for characters that are in the [NSCharacterSet whitespaceAndNewlineCharacterSet] and decrementing the index used for calculating the line width until no further characters in that character set are found. The behavior appears to depend on how blocks of text are interpreted. A newline character inserted at the end of a line or logical text block is stripped out; a newline character inserted at the beginning of a line or logical text block is preserved and rendered as a distinct line. In the OP's example, the spaces are stripped out after "foo," which is treated as a logical text block; the newline is NOT stripped out. Instead, it is treated as the start of a new line or logical text block and a new line is inserted, followed by "bar" on the next line. Again, I do not know if this can be addressed without causing extensive breakage elsewhere. marc_culler (claiming to be Marc Culler) added on 2022-06-18 14:20:00: @Kevin: do you have any idea what logic the Text widget is using when it decides to add a newline? I am guessing that it is comparing the length of the line to the length of a block of text, and that the space character increases the length of the block of text (as measured by mac-specific code) but not the length of the line (as measured by Tk, using previously computed lengths of words, but not not using *actual* widths for space characters). The Mac code for measuring a block of text may very well use different sizes of spaces due to kerning and other fancy stuff. Perhaps Tk is making an invalid assumption, such as assuming that every space character has the same size, perhaps equal to the size of an en-space. kevin_walzer added on 2022-06-18 01:59:59: <p>After a close review of the code here, and attempting several different approaches at a fix, I've concluded it that the Mac font rendering system in Tk simply does not handle this specific case correctly: a space followed by a newline. I have observed the same additional line inserted when inserting text into a text widget with a narrow width. Francois' sketch of a possible fix addresses this use case but winds up causing severe breakage elsewhere in text display, because the Mac code in my view is correct in virtually all other cases besides this; I have tried different variations of Francois' suggestion and encountered roughly the same results. This leads me back to my earlier response: this is an edge case that that we are unable to fix without causing worse problems elsewhere. <p>I will leave this ticket open for a bit in hopes that someone else might be able to arrive at a better solution than I've been able to find, but I'm inclined to close this as "won't fix." <p>Francois, thanks again for your analysis - it would have been impossible for me to trace this otherwise. kevin_walzer added on 2022-06-15 11:42:02: Francois, my mistake! I will look again at core-8-6-branch. And thank you for the patch. fvogel added on 2022-06-15 05:51:23: Of course my patch is wrong, it just showed where IMO the bug is (and it applies to core-8-6-branch). kevin_walzer added on 2022-06-15 01:56:50: This patch fails to apply to tkMacOSXFont.c in trunk. Also, I am not at all sure that a fix for this small bug is worth 40 test suite failures. fvogel added on 2022-06-12 20:01:29: The attached patch1.txt makes the message window render as expected (i.e. in two lines). However ~40 textDisp tests fail with this patch. fvogel added on 2022-06-12 09:00:28: From a quick look at the code I believe analysis could start at the loop in ComputeMessageGeometry() (tkMessage.c). This loop calls Tk_ComputeTextLayout() in tkFont.c, which in turn calls Tk_MeasureChars, which has a platform-dependent implementation. On macOS, in each iteration of that loop, Tk_ComputeTextLayout returns a thisHeight value of 48 pixels, which is exactly the height of three lines (it is three times the value of [font metrics [.m cget -font] -linespace]). In contrast, on Windows Tk_ComputeTextLayout returns a thisHeight value of twice the linespace only (i.e. two lines height). kevin_walzer added on 2022-06-12 01:39:54: I can also confirm this, but cannot find anywhere in the Mac source tree where messages are rendered, except for the defaults in a header file - and those match the other platforms. This text layout, in any other widget, does not insert an extra newline. So I am not sure where to look to fix it. fvogel added on 2022-05-31 18:58:33: Windows : no problem. macOS with aqua : I can reproduce. | |||
Attachments:
- Patch1.txt [download] added by fvogel on 2022-06-12 20:04:40. [details]
