Tk Source Code

View Ticket
Login
Ticket UUID: 220849
Title: bug in font actual
Type: Bug Version: final: 8.1.1, 8.6.18, 9.0.5, 9.1b1
Submitter: welch Created on: 2000-11-01 01:29:57
Subsystem: 46. Unix Fonts Assigned To: hobbs
Priority: 5 Medium Severity:
Status: Open Last Modified: 2026-09-29 19:38:45
Resolution: None Closed By:
    Closed on:
Description:
OriginalBugID: 2216 Bug
Version: 8.1.1
SubmitDate: '1999-06-15'
LastModified: 
Severity: LOW
Status: UnAssn
Submitter: techsupp
OS: Other
OSVersion: NetBSD-1.3.3/i386  XFree-?
Machine: NA


Name:
Hume Smith

ReproducibleScript:
(copy of interactive session)
    aurora> wish
    % label .a -text Howdy -font {times -12}
    .a
    % label .b -text Howdy -font {times -13}
    .b
    % pack .a .b
    % font actual [.a cget -font]
    -family times -size 12 -weight normal -slant roman -underline 0
    -overstrike 0
    % font actual [.b cget -font]
    -family times -size 12 -weight normal -slant roman -underline 0
    -overstrike 0
    % info patchlevel
    8.1.1
    % winfo reqwidth .a
    40
    % winfo reqwidth .b
    46
    %

ObservedBehavior:
font actual reports the same sizes for the two fonts; but .b has a
    larger label than .a, as perhaps you can see from the requested widths.
    This is obviously quite sensitive to things like X server, the font
    implementation, dot pitch...

DesiredBehavior:
font atual should report different sizes
User Comments: serhiy.storchaka added on 2026-09-29 19:38:45:

Still present in 8.6 and 9.x, on all platforms; also reported in python/cpython#143990. font actual returns the size rounded to an integer number of points. If the size was specified in pixels, different sizes can give the same result, and font create {*}[font actual $font] creates a different font. For example, -12 and -13 pixels both give 12 at 75 DPI. At 96 DPI (100%), -10/-11, -14/-15, -18/-19 and -22/-23 collide, and more pairs at 125%.

How the actual size is calculated:

  • X11 with Xft: a size in points is passed to Fontconfig as XFT_SIZE as is; a size in pixels is first converted to points with tk scaling. Fontconfig converts it to pixels with its own DPI (Xft.dpi, which can differ slightly from tk scaling), and scalable fonts are rendered at a fractional pixel size. The actual size is the XFT_SIZE of the matched pattern, i.e. the requested size in points, not measured: 12.5 for 12.5 points, 12.466 for 13 pixels at 75 DPI.
  • X11 without Xft: a size in points is converted to pixels with tk scaling; a size in pixels is used as is. The font is chosen by an XLFD with the nearest integer pixel size (the nearest available size for bitmap fonts). The actual size is the pixel size of the chosen font, converted to points: 12.466 for both 12.5 points and 13 pixels at 75 DPI.
  • Windows: a size in points is converted to pixels with tk scaling and rounded; a size in pixels is rounded. It is used as the character height in LOGFONT. The actual size is tmHeight - tmInternalLeading from GetTextMetrics(), converted to points: 9.75 for 10 points and for 13 pixels at 96 DPI, 12.75 for 12.5 points.
  • macOS: a size in points is converted to Cocoa points (logical pixels) with the fixed factor 4/3, not with tk scaling, and rounded; a size in pixels is converted with tk scaling (4/3 by default) and rounded. The actual size is the size of the NSFont multiplied by the fixed factor 3/4: 9.75 for 10 points and for 13 pixels, 12.75 for 12.5 points.

In all cases the actual size is a fractional number of points, but it is rounded to an integer when returned.

There are two possible solutions; both change the result of font actual:

  1. Return the size in pixels (a negative number) if it was specified in pixels, for example -13 instead of 12. It is exact, and the result can be passed back to font create. But code that uses the size to calculate proportional sizes or to sort fonts by size does not work with a negative size, and code that expects a positive size breaks. Test winfont-2.1 fails.
  2. Return the size in points as a fractional number if it was specified in pixels or as a fractional number, for example 12.466 instead of 12. The result is always in points, so calculating proportional sizes and comparing sizes still work. Sizes specified as integer points are still rounded, so the result for them is unchanged, even on Windows and macOS. -size now accepts fractional numbers, so the result can be passed back to font create. But code that expects an integer size breaks (incr, format %d, string is integer), and the error message for an invalid size changes.

I implemented both variants, but choosing between them needs a wide discussion.


pcaffin added on 2008-03-13 17:08:24:
Logged In: YES 
user_id=2029956
Originator: NO

Whoops. In the above, change "the technical discrepancy (-7 vs 12, -13 vs 10)" to "the technical discrepancy (9 vs -12, 10 vs -13)".

pcaffin added on 2008-03-13 17:02:27:
Logged In: YES 
user_id=2029956
Originator: NO

I've tested this with Tk 8.5.2.0.284581 (teapot) on Linux and the bug's still present.

Tested with Tk 8.5.2.0.284581 (teapot) on Windows XP Pro SP2 (using the same values which the reporter used) and got the following:

(bin) 4 % font actual [.a cget -font]
-family {Times New Roman} -size 9 -weight normal -slant roman -underline 0 -overstrike 0
(bin) 5 % font actual [.b cget -font]
-family {Times New Roman} -size 10 -weight normal -slant roman -underline 0 -overstrike 0

Seems okay, but, the technical discrepancy (-7 vs 12, -13 vs 10) might be useful in tracking down the issue on X11.

hobbs added on 2001-12-28 03:32:24:
Logged In: YES 
user_id=72656

Note also:

[ #220838 ] font size taken wrong from X font

[ #220836 ] Font {times 14} is larger than {times 15}

[ #220886 ] possible "font actual" bug

hobbs added on 2001-12-28 02:16:20:
Logged In: YES 
user_id=72656

This seems to be a Unix-only problem, confirmed in 8.3.4.