Tk Source Code

View Ticket
Login
Ticket UUID: b38e430e2613b762df28a19e2436689d5f1141cd
Title: Labels are rendered incorrectly on MacOS when a line starts with \n, and -wraplength is set.
Type: Bug Version: 9.0.2
Submitter: SVIDevelopment Created on: 2025-09-23 08:08:15
Subsystem: 03. [*button] and [label] Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2025-10-10 20:15:21
Resolution: None Closed By: nobody
    Closed on:
Description:
When using a label of which

1. The text has a line which a line starts with "\n";
2. Having wraplength set to some value,

an extra new line is rendered on macOS (aqua), while this does not happen on Linux. Example script:

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

set top    [toplevel .top]
wm geometry $top 600x600

set txt \
    "New lines in the middle\nof lines behave correctly\
     \nbut those at the start do not"

# Normal behavior:
set goodLabel [label $top.goodLabel -text $txt -justify left]
grid $goodLabel

set sep    [ttk::separator $top.sep]
grid $sep -sticky ew -pady 5

# Bugged behavior on MacOS:
set badLabel [label $top.badLabel -text $txt -justify left -wraplength 20000]
grid $badLabel

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

I'll attach some screenshots
User Comments: chrstphrchvz added on 2025-10-10 20:15:21:

I find it hard to tell exactly what the expected behavior is…

I do not mean to blame current Tk Aqua maintainers for lack of comments/explanations of this code, particularly since this ticket's issue is present in much older versions of Tk Cocoa. Maybe -wraplength is just not used very often for this issue to be noticeable. And the Aqua, Win32, Xft, and non-Xft implementations of Tk_MeasureChars() all differ somewhat and are not easy to compare.


chrstphrchvz added on 2025-10-10 17:52:41:

I wonder if the problem is in here:

	/*
	 * Trim right whitespace/lineending characters.
	 */
cs = (index <= len && (flags & TK_WHOLE_WORDS)) ? whitespaceCharacterSet : lineendingCharacterSet; while (index > start && [cs characterIsMember:[string characterAtIndex:(index - 1)]]) { index--; }

Decrementing index leaves the trailing whitespace “unconsumed” during this call to Tk_MeasureCharsInContext(). I find it hard to tell exactly what the expected behavior is, but I wonder if the trailing whitespace is supposed to be consumed instead, while still not being considered part of the string during width measurement.


chrstphrchvz added on 2025-10-10 16:19:41:

See https://www.tcl-lang.org/man/tcl9.0/TclCmd/Tcl.html#M24

\<newline>whiteSpace

A single space character replaces the backslash, newline, and all spaces and tabs after the newline.


marc_culler (claiming to be Marc Culler) added on 2025-10-10 13:45:33:
I was able to verify that the string has only one space before the
newline.  But I don't know the mechanism which causes that.  I guess
it doesn't matter.

marc_culler (claiming to be Marc Culler) added on 2025-10-10 13:40:15:
Christopher, you say that there is a single space before the newline,
but to a naive observer like me it looks like there are 5 spaces before
that newline.  What causes the 5 spaces to be replaced by a singe space?

chrstphrchvz added on 2025-09-24 22:39:04:

I have verified that this problem does not occur on Windows.

The reporter's example string is equal to:

set txt "New lines in the middle\nof lines behave correctly \nbut those at the start do not"
i.e. there is a single space before the misbehaving newline. So I think having a space before the newline is what matters, rather than whether the newline is at the beginning of a line.

TkpComputeButtonGeometry() (tkMacOSXButton.c) passes the label's configured wraplength to Tk_ComputeTextLayout() (tkFont.c), which in turn uses wrapLength to compute the maxLength it passes to Tk_MeasureChars()→ Tk_MeasureCharsInContext() (tkMacOSXFont.c). The problem seems like an inconsistency between the if (maxLength < 0)and else cases of Tk_MeasureCharsInContext().


Attachments: