Tk Source Code

View Ticket
Login
Ticket UUID: 478568
Title: problems displaying some italic fonts
Type: Bug Version: obsolete: 8.5a0
Submitter: boakley Created on: 2001-11-06 05:43:31
Subsystem: 18. [text] Assigned To: hobbs
Priority: 6 Severity:
Status: Closed Last Modified: 2003-12-04 21:36:31
Resolution: Fixed Closed By: vincentdarley
    Closed on: 2003-12-04 14:36:31
Description:
This may be a windows-specific problem, and might be 
the same as bug 220086, or at least related.

I'm seeing this bug on Win2K, with the most recent 
versions of 8.2, 8.3 and 8.4. I do *NOT* see this 
problem with 8.0.5.

The nutshell summary is that if I configure a text tag 
to have a slant of italic but all other font metrics 
identical to other text, and the family is {MS Sans 
Serif}, I get extra visual whitespace between the 
italic and non-italic text. And if I move the 
insertion cursor through the italicized text (using 
arrow keys, for example) the whitespace shifts around. 

It's almost as if the physical x location of non-
italic characters and the insertion cursor is based on 
a different font metric than that used to compute the 
physical x location of the italic characters.  

As I've said, this happens with {MS Sans Serif}. It 
also seems to happen with "Fixedsys" I've tested with 
a family of "Helvetica" and with "Arial" 
and "Microsoft Sans Serif" and the problem goes away, 
so it's somewhat font-specific. I didn't have the 
patience to try every font on my system, but would be 
willing to do so if called upon.

I wouldn't make a big deal about this, and chalk it up 
to Microsoft nonsense, but the problem doesn't exist 
in 8.0.5 so it appears to be a regression on the part 
of tk.

Also, considering the default font for text widgets on 
windows (at least on my box) is {MS Sans Serif}, this 
should probably be addressed. Either fix the bug or 
pick a better default font (Arial? Personally I vote 
for Verdana, but I don't know how ubiquitous it is 
yet).

Here's a script you can use to see the problem:

pack [text .t]
eval font create romanFont [font actual [.t cget -
font]]
eval font create italicFont [font actual [.t cget -
font]]

font configure italicFont -slant italic -family {MS 
Sans Serif}
font configure romanFont  -slant roman -family {MS 
Sans Serif}

.t tag configure italic -font italicFont
.t tag configure roman  -font romanFont

.t insert 1.0 "this is italic" italic " and this is 
not." roman
User Comments: cc_benny added on 2003-12-02 22:31:25:
Logged In: YES 
user_id=143885

See patch #852669.

nobody added on 2003-12-02 18:20:22:
Logged In: NO 

The extra width needs to be added on once per string, but it
seems to be added on once per char.

There are other measurement related issues (which pertain to
dkf's point about the use of widths) which need to be
resolved (see discussions on mactcl list), since width(A) +
width (B) != width (AB) in general, but that's somewhat
tangential since what we have here is at least partly a
local bug in a single function.

dkf added on 2003-12-02 16:00:59:
Logged In: YES 
user_id=79902

Surely it depends on what you are using the widths for?  If
you're positioning the cursor or the next character, you may
want to remove the extra width.  If you're calculating the
bounding box for deciding whether to wrap the line, you
probably want that extra width...

(Thus, I demonstrate my lack of knowledge of the Tk font and
text-handling guts, which must know something like this
already.)

vincentdarley added on 2003-12-02 04:19:35:
Logged In: YES 
user_id=32170

Given this problem is much worse with bold+italic fonts, I'm
99% sure my previous diagnosis is correct.  The code needs
stuff like this:

GetTextMetrics(hdc, &tm);
curX -= tm.tmOverhang;

put in here and there (the above taken from a MSDN code sample).

nobody added on 2003-12-02 04:16:16:
Logged In: NO 

Looking at the code, it appears not to make use of the
TEXTMETRIC.tmOverhang field, which is only relevant for
synthesised bitmap fonts (which might explain the different
between Tahoma and MS Sans Serif).  Here's the relevant doc:

tmOverhang
    Specifies the extra width per string that may be added
to some synthesized fonts. When synthesizing some
attributes, such as bold or italic, graphics device
interface (GDI) or a device may have to add width to a
string on both a per-character and per-string basis. For
example, GDI makes a string bold by expanding the spacing of
each character and overstriking by an offset value; it
italicizes a font by shearing the string. In either case,
there is an overhang past the basic string. For bold
strings, the overhang is the distance by which the
overstrike is offset. For italic strings, the overhang is
the amount the top of the font is sheared past the bottom of
the font.

    The tmOverhang member enables the application to
determine how much of the character width returned by a
GetTextExtentPoint32 function call on a single character is
the actual character width and how much is the per-string
extra width. The actual width is the extent minus the overhang.

hobbs added on 2003-12-02 02:25:07:
Logged In: YES 
user_id=72656

See my original notes on this.  It does appear to be a font-
specific issue.  MS Sans Serif offends, but Tahoma does not.  
I wonder if there are more reasons why MS changed the 
default Windows font to Tahoma?

vincentdarley added on 2003-12-02 01:57:20:
Logged In: YES 
user_id=32170

If you move the insertion cursor through the bold-italic
text you'll see all sorts of strange wobbling -- clearly the
character-width calculation used here is simply wrong for
italic text, and this comment in Tk_MeasureChars:

    /*
     * According to Microsoft tech support, Windows does not
use kerning
     * or fractional character widths when displaying text
on the screen.
     * So that means we can safely measure individual
characters or spans
     * of characters and add up the widths w/o any
"off-by-one-pixel" 
     * errors.  
     */

is quite wrong.  Italic characters do use kerning quite clearly.

This means there may be 1 or 2 problems:

(i) the width calculation is wrong for these italic characters.
(ii) when there is kerning involved the width calculation is
even more incorrect.

dkf added on 2003-11-15 01:03:35:
Logged In: YES 
user_id=79902

The effect is even stronger with bold and italic text!  (Add
'font configure bold -slant italic' to the example below...)

This is *way* beyond what I know how to fix BTW...

nobody added on 2003-11-14 15:35:18:
Logged In: NO 

Similar problem with bold:

pack [text .t]
eval font create bold [font actual [.t cget -font]]
font configure bold -weight bold
.t tag configure bold -font bold
.t insert end PRE {} BOLD bold POST {}

verified in tk8.5a0

hobbs added on 2002-10-09 03:25:45:
Logged In: YES 
user_id=72656

also see dup at 563866

boakley added on 2002-03-06 03:25:42:
Logged In: YES 
user_id=249075

What is the status of this bug? Is it on anyone's plate to 
be fixed for 8.4?

hobbs added on 2001-11-07 03:39:27:
Logged In: YES 
user_id=72656

Yes, this is the same as 220086.  That is marked dup, but 
of what I can't seem to find.  Also see 220818 which may be 
related.  I would like to have that old bug report, because 
I had made notes there.  In any case, I believe that 
tkWinFont.c:Tk_MeasureChars is not really *correct*.  I 
added a "TODO" in that file that explains why I question 
some earlier commented logic.