Tk Source Code

View Ticket
Login
2025-06-06
13:39 Ticket [c81e0cce] Named font late creation status still Open with 4 other changes artifact: db8ba3d7 user: oehhar
13:29 Ticket [c81e0cce]: 3 changes artifact: 43d1088d user: chw
12:47 Ticket [c81e0cce]: 4 changes artifact: c5e010cf user: oehhar
12:45
[c81e0cce] Add proposed test (in my simple language, sorry). Thanks, Christian Leaf check-in: 560de125 user: oehhar tags: bug-c81e0cce-named-font-late-creation
12:16 Ticket [c81e0cce] Named font late creation status still Open with 3 other changes artifact: 23d0dd0c user: chw
11:36 Ticket [c81e0cce]: 4 changes artifact: ef6e369e user: oehhar
11:23 Ticket [c81e0cce]: 3 changes artifact: caa1799f user: chw
10:59 Ticket [c81e0cce]: 4 changes artifact: 28473d14 user: oehhar
10:00 Ticket [c81e0cce]: 4 changes artifact: 30387eef user: oehhar
09:59
bug [c81e0cce] provided patch (VC6 optimized): named font not honored if defied after reference in a widget check-in: ed0d5d23 user: oehhar tags: bug-c81e0cce-named-font-late-creation
08:36 Ticket [c81e0cce] Named font late creation status still Open with 3 other changes artifact: 486b61fb user: chw
05:46 Add attachment tkFont.diff to ticket [c81e0cce] artifact: 32851b5a user: chw
05:46 New ticket [c81e0cce] Named font late creation. artifact: 21200d47 user: chw

Ticket UUID: c81e0ccee2b971b01f58a9adb8bfa42ae66b785e
Title: Named font late creation
Type: Bug Version: >= 8.0
Submitter: chw Created on: 2025-06-06 05:46:08
Subsystem: 44. Generic Fonts Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2025-06-06 13:39:30
Resolution: None Closed By: nobody
    Closed on:
Description:

Named fonts are not configured properly when the font name is used in a widget prior to the creation of the named font by the font create command as shown in the following snippet.

proc step1 {} {
    after 1000 step2
    ttk::style configure TLabel -font ANamedFont
    label .l1 -text "A normal label" -font ANamedFont
    ttk::label .l2 -text "A ttk::label"
    pack .l1 .l2
}
proc step2 {} {
    after 1000 step3
    font create ANamedFont -family Helvetica -size 30
}
proc step3 {} {
    ttk::label .l3 -text "Another ttk::label"
    pack .l3
}
step1

The expected outcome would be the first two labels having Helvetica 30 after a second, and the third label having that same font after two seconds. However, no font change happens both on Win32 and POSIX (Mac untested), i.e. the system chosen fallback font remains in use. In contrast, this snippet does what one expects:

proc step1 {} {
    after 1000 step2
    ttk::style configure TLabel -font ANamedFont
    font create ANamedFont -family Helvetica -size 30
    label .l1 -text "A normal label" -font ANamedFont
    ttk::label .l2 -text "A ttk::label"
    pack .l1 .l2
}
proc step2 {} {
    after 1000 step3
    font delete ANamedFont
    font create ANamedFont -family Courier -size 30
}
proc step3 {} {
    ttk::label .l3 -text "Another ttk::label"
    pack .l3
}
step1

So there's some confusion in the font subsystem regarding caching of named and unnamed fonts and when to perform updates. Therefore I attach a proposed fix as a patch against core-8-6-branch.

User Comments: oehhar added on 2025-06-06 13:39:30:

Christian,

I see your reson. But then, most tests should do so.

AFAIK, we have a test start argument, to run tests individually. I suppose, the reason why this is not done systematically is test time. Don Ported does that and finds dependencies between the tests.

Thanks for all, Harald


chw added on 2025-06-06 13:29:37:

Harald,

while I can live with the test, doing this in another interp serves a purpose: to get a ultra clean (Mr Proper) start condition for the test, since that interp loads and initializes its own little Tk world. Just a thought.


oehhar added on 2025-06-06 12:47:49:

Thanks ! Added test by commit [c81e0cce].

I simplified the test (removing the interpreter) and commented it, so even I do understand it ;-).

If you resist in writing to branches directly, you have to live with my stupid commits....

Thanks for all, Harald


chw added on 2025-06-06 12:16:21:

Without further instrumentation it might be possible to test it somehow unsharp with this approach

test font-x.x {Named font late creation} -setup {
    catch {interp delete foo}
} -body {
    interp create foo
    foo eval {
        load {} Tk
        font create F1 -family Helvetica -size 100
        label .l -font F1 -text LARGE
        pack .l
        update
        set W1 [winfo width .l]
        destroy .l
        font delete F1
        label .l -font F2 -text LARGE
        pack .l
        update
        font create F2 -family Helvetica -size 100
        update
        set W2 [winfo width .l]
        destroy .l
        font delete F2
    }
    set result [foo eval {expr {$W1 == $W2}}]
    foo eval {destroy .}
    interp delete foo
} -result 1

but this is a sketch only. Please refine.


oehhar added on 2025-06-06 11:36:54:

Yes. Could you provide a test case? I think, with a test case, this may quickly be merged.

Thanks, Harald


chw added on 2025-06-06 11:23:41:

Harald,

thanks for testing. Indeed is this problem as old as Tk 8.0 as far as can be seen in tkFont.c. In the second snippet of the ticket, the font delete is essential, since it proves that on re-creation of the same named font an update is performed. But the fix addresses the initial case, i.e. named font non-existent, font subsystem uses a fallback, and after that the named font is created in the expectation to be immediately used.


oehhar added on 2025-06-06 10:59:59:

I can confirm, that the patch is effective, and that the additional "font delete" is not required any more.

My environment: win32+MS-VC6 on MS-Win11 64 bit GER.

I can also confirm, that the bug is also present in Tk 9.0.1

Comments and tests welcome

Great job, Christian ! Harald


oehhar added on 2025-06-06 10:00:11:

Pathch now in commit [ed0d5d23], VC6 optimized. Tests pass with MS-VC6 Thanks, Harald


Attachments: