Tk Source Code

View Ticket
Login
Ticket UUID: 6437e8d00d12f938d1136184a62f50c48a3d2b53
Title: font-24.5 test fails on macOS and Linux
Type: Bug Version: core-8-6-branch
Submitter: fvogel Created on: 2018-07-24 21:55:46
Subsystem: 45. Aqua Fonts Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2018-10-09 09:38:56
Resolution: Fixed Closed By: fvogel
    Closed on: 2018-10-09 09:38:56
Description:

font-24.5 test fails on macOS and Linux as follows:

==== font-24.5 Tk_ComputeTextLayout: break line FAILED
==== Contents of test case:

    .t.l config -text "000\t00000" -wrap [expr 9 * $ax]
	update
	list [expr {[winfo reqwidth .t.l] eq [expr {$ax * 8}]}]  [expr {[winfo reqheight .t.l] eq [expr {$ay * 2}]}]

---- Result was:
0 0
---- Result should have been (exact matching):
1 1
==== font-24.5 FAILED

User Comments: fvogel added on 2018-10-09 09:38:56:
Merged to core-8-6-branch and trunk.

fvogel added on 2018-10-09 06:00:33:
No new failure with the test suite, on any platform (Windows Vista, macOS 10.12 and Linux Debian 8). Will merge in a few hours.

fvogel added on 2018-10-08 20:55:02:
Fixed for macOS in the bugfix branch. There was no problem on Windows nor on linux.

fvogel added on 2018-10-07 19:13:37:

Reopening.

The fixes in the bugfix branch really made font-24.5 pass, however they triggered new failures with canvText-17.1 and canvText-19.1 on macOS (at least).

This needs to be sorted out before changing the code, thus I have reverted the fix in core-8-6-branch and trunk and will keep working in the bugfix branch.

For the records, here is the log of the newly failing tests on macOS:

==== canvText-17.1 TextToPostscript procedure FAILED
==== Contents of test case:

    set font {Courier 12 italic}
    set ax [font measure $font 0]
    set ay [font metrics $font -linespace]
    .c config -height 300 -highlightthickness 0 -bd 0
    update
    .c create text 100 100 -tags test
    .c itemconfig test -font $font -text "00000000" -width [expr 3*$ax]
    .c itemconfig test -anchor n -fill black
    set x [.c postscript]
    set x [string range $x [string first "findfont " $x] end]
    expr {$x eq [subst $result] ? "ok" : $x}

---- Result was:
findfont 12 scalefont ISOEncode setfont
0.000 0.000 0.000 setrgbcolor AdjustColor
0 100 200 [
[(0)]
[(0)]
[(0)]
[(0)]
[(0)]
[(000)]
] 13 -0.5 0 0 false DrawText
grestore
restore showpage

%%Trailer
end
%%EOF

---- Result should have been (exact matching):
ok
==== canvText-17.1 FAILED



==== canvText-19.1 patch 1006286, leading space caused wrap under Win32 FAILED
==== Contents of test case:

    set f {Arial 28 bold}
    set s1 {    Yeah-ah-ah-ah-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-Yow}
    set s2 {    Yeah ah ah ah oh oh oh oh oh oh oh oh oh oh oh oh oh oh oh oh Yow}
    $c create text 21 18  -font $f  -text $s1  -fill white  -width 922  -anchor nw  -tags tbox1
    $c create rect {*}[$c bbox tbox1] -outline red
    $c create text 21 160  -font $f  -text $s2  -fill white  -width 922  -anchor nw  -tags tbox2
    $c create rect {*}[$c bbox tbox2] -outline red
    after 500 "set done 1" ; vwait done
    set results [list]
    $c select from tbox2 4
    $c select to tbox2 8
    lappend results [selection get]
    $c select from tbox1 4
    $c select to tbox1 8
    lappend results [selection get]
    array set metrics [font metrics $f]
    set x [expr {21 + [font measure $f "    "]  + ([font measure {Arial 28 bold} "Y"] / 2)}]
    set y1 [expr {18 + ($metrics(-linespace) / 2)}]
    set y2 [expr {160 + ($metrics(-linespace) / 2)}]
    lappend results [$c index tbox1 @$x,$y1]
    lappend results [$c index tbox2 @$x,$y2]

---- Result was:
{Yeah } Yeah- 3 4
---- Result should have been (exact matching):
{Yeah } Yeah- 4 4
==== canvText-19.1 FAILED


fvogel added on 2018-10-06 09:47:29:
Merged to core-8-6-branch and trunk.

fvogel added on 2018-09-30 15:38:02:
On Linux with --disable-xft there is no issue to fix: font.test already passes 100% OK, and the rendering of the test script is the expected one (i.e. the same as on Windows).

fvogel added on 2018-09-30 14:56:51:

I have now proposed a fix for macOS ([94cfd2f6]) and for Linux with Xft ([1235434d]).

With these fixes, the output of the test suite with TESTFLAGS="-file *ont.test" (i.e. font.test, unixFont.test and winFont.test):

- is 100% pass on macOS, including font-24.5 which is the subject of the present ticket
- is unchanged on Linux Debian 8, except that font-24.5 now passes (there are still 18 other failing tests, but no new failure)

The rendering of the test script I provided on 2018-07-24 22:00:52, which mimics what font-24.5 is doing, is now the same on Linux (with Xft) and macOS as it is on Windows. (note that I made a mistake in that script on the line just before the last one, which should read ...expr {$ax * 8}... instead of ...expr {$ax * 4}...).

Remains to be done: check Linux with --disable-xft


fvogel added on 2018-09-29 22:31:33:

Test font-24.5 intends to check that the label widget will word-wrap its text according to the allotted -wraplength.

The label text content is "000\t00000" and the -wraplength is the width in pixels occupied by 9 characters ($ax having been measured previously to be the width of one char in pixels).

For the sake of simplifying the explanation and in order to easily write example calculations, let's say that the font used for this test is fixed width with a character width of 7 pixels (this is what I can observe on all systems at my disposal: Win Vista, Linux Debian 8, and macOS).

The -wraplength is 9*7 = 63 pixels. The tab char \t is 8 characters wide, that is 56 pixels (this is fontPtr->tabWidth).

Tk_ComputeTextLayout operates by chunks of characters separated by "special" characters (tabs, newlines, carriage returns). It therefore first sends the three first characters "000" to Tk_MeasureChars, which discovers that this string measures 3*7 = 21 pixels. Tk_ComputeTextLayout happily concludes that this fits in the allotted 63 pixels.

Tk_ComputeTextLayout then handles the \t character by adding just enough pixels to arrive at the tab stop, that is at 56 pixels from the left border of the label widget.

Since this position is still less than the allotted 63 pixels, there is still space left on the right before the wrap point, and Tk_ComputeTextLayout sends the next string ("00000") to Tk_MeasureChars for measurement purposes of how much of it fits in the remaining space on the line before the wrap point.

The important point is that wrapping shall occur at word boundaries (see the man page for -wraplength). When called by Tk_ComputeTextLayout, Tk_MeasureChars therefore always receives the TK_WHOLE_WORDS flag.

At this point, when measuring this second string, on Windows Tk_MeasureChars answers that no character of this string can fit on the remaining space. This IS correct and is due to the TK_WHOLE_WORDS flag being correctly honored. Indeed, the first word of the string "00000" is "00000" and this is too long to fit in the remaining 63-56 = 7 pixels on the label line.

In contrast, on macOS and Linux, at this point Tk_MeasureChars answers that exactly one character of the string "00000" can fit. This could have been correct (63-56 = 7 pixels, that is exactly one char), however this answer does not honor the TK_WHOLE_WORDS flag.

Conclusion: this failing test font-24.5 reveals that on macOS and Linux Tk_MeasureChars does not correctly honor the TK_WHOLE_WORDS flag in the specific case exercised by the test.


fvogel added on 2018-08-04 15:46:38:

Some more notes:

- On macOS the test fails because (the second time) TkpMeasureCharsInContext called with the source string "00000" returns 1: it believes one char fits in the allotted 7 pixels width. This looks right: font measure "Courier -12" "0" returns 7, i.e. exactly the given maxLength width. In contrast, on Windows, despite that command also returns 7, (the second call to) Tk_MeasureChars returns zero, i.e. no character from that same string "00000" will fit in the maxLength width given. This seems wrong on Windows, not on macOS...
- However the above may be explained by a fix, on Windows only, that impacted the relevant part of the source code. See ticket [478568ffff] and its associated patch [852669ffff], committed in [7f0387ce5efe5ce1]. This could perhaps be the cause for the difference in behavior between platforms (needs further analysis).
- font-24.5 also fails on Linux Debian 8. However, rendering is again different from the two other platforms (see attachment).


fvogel added on 2018-07-25 20:49:46:

First of all, analysis of this needs the fix for [59dde8a675] (removal of the 4 pixels artifical horizontal padding from the width seen by Tk) - otherwise the value passed by the test to the -wrap option is 9*4=36 pixels too large, which prevents any wrapping from occurring.

Once this is fixed the real analysis can begin. When executing:

    .t.l config -text "000\t00000" -wrap [expr 9 * $ax]

Tk_ComputeTextLayout calls Tk_MeasureChars several times here.

On Windows, a debug output produces:

  Tk_ComputeTextLayout: wrapLength = 63
    Tk_MeasureChars: numBytes = 3, maxLength = 63, flags = 3
  Tk_ComputeTextLayout: Tk_MeasureChars returned: bytesThisChunk = 3
    Tk_MeasureChars: numBytes = 5, maxLength = 7, flags = 1
  Tk_ComputeTextLayout: Tk_MeasureChars returned: bytesThisChunk = 0
    Tk_MeasureChars: numBytes = 5, maxLength = 63, flags = 3
  Tk_ComputeTextLayout: Tk_MeasureChars returned: bytesThisChunk = 5
    Tk_MeasureChars: numBytes = 52, maxLength = -1, flags = 0

Compare this (correct) output to what macOS is spitting:

  Tk_ComputeTextLayout: wrapLength = 63
    TkpMeasureCharsInContext: numBytes = 3, maxLength = 63, flags = 3
  Tk_ComputeTextLayout: Tk_MeasureChars returned: bytesThisChunk = 3
    TkpMeasureCharsInContext: numBytes = 5, maxLength = 7, flags = 1
  Tk_ComputeTextLayout: Tk_MeasureChars returned: bytesThisChunk = 1
    TkpMeasureCharsInContext: numBytes = 4, maxLength = 63, flags = 3
  Tk_ComputeTextLayout: Tk_MeasureChars returned: bytesThisChunk = 4
    TkpMeasureCharsInContext: numBytes = 1, maxLength = -1, flags = 0

Therefore the return value of the second call to TkpMeasureCharsInContext is 1 instead of 0. The bug really seems to be in TkpMeasureCharsInContext.

I have made changes in that function but so far I couldn't let font-24.5 pass without generating new failures with other tests from font.test


fvogel added on 2018-07-24 22:00:52:

Quick analysis suggests that \t is incorrectly taken into account in TkpMeasureCharsInContext (tkMacOSXFont.c)

When running the test content in wish one can see that the label text in fact does not wrap on a second line on macOS (whereas it does on Windows for instance).

Test code:

package require Tk
toplevel .t
label .t.l -padx 0 -pady 0 -bd 0 -highlightthickness 0 -justify left \
	-text "0" -font "Courier -12"
pack .t.l
set ax [winfo reqwidth .t.l]
set ay [winfo reqheight .t.l]
.t.l config -text "000\t00000" -wrap [expr 9 * $ax]
update
puts "[winfo reqwidth  .t.l] eq? [expr {$ax * 4}]"
puts "[winfo reqheight .t.l] eq? [expr {$ay * 2}]"


Attachments: