Tk Source Code

View Ticket
Login
Ticket UUID: 89354dae316b7a16abe96d2bce1877d646f6f44d
Title: Aqua: neighboring foreground color gets applied to very edge of nearby text
Type: Bug Version: 8.6.9.1
Submitter: chrstphrchvz Created on: 2019-08-17 14:46:31
Subsystem: 18. [text] Assigned To: marc_culler
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2020-04-15 16:38:05
Resolution: Fixed Closed By: nab
    Closed on: 2020-04-15 16:38:05
Description:

Foreground colors of text might be applied to few pixels of neighboring text, and becomes noticeable when those foreground colors differ.

The issue seems mostly noticeable when using colored subpixel antialiasing, which is no longer the default in macOS 10.14 Mojave, but can be re-enabled using defaults write -g CGFontRenderingFontSmoothingDisabled -bool NO or a utility like TinkerTool. (I personally have the old antialiasing re-enabled, since it looks better for non-retina displays.) The issue is still present with the default antialiasing in Mojave and later, but it is less noticeable.

One place to observe this issue in the widget demo under dark mode: the rightmost edge of the demo name will have faint non-blue edges.

With Mojave default antialiasing:

With subpixel antialiasing:

Setting the demo tag's foreground to the text background makes only the errant edges apparent (the neighboring demospace tag does not have a set foreground, and so is using the text widget foreground):

.t configure -background black -foreground white
.t tag configure demo -foreground black

With Mojave default antialiasing:

With subpixel antialiasing:

.t configure -background white -foreground black
.t tag configure demo -foreground white

With Mojave default antialiasing:

With subpixel antialiasing:

This issue appears in 8.5.9 as well. I wonder if this bug is the same as or is related to [3607259], where that issue had just become difficult to notice (with the given test case) rather than truly fixed. Here is what its test case looks like after doing .f.t configure -background black:

User Comments: nab added on 2020-04-15 16:38:05:
https://vimeo.com/408044743
this is a part on what my app is doing (just to let you know I use -itemconfigure for canvas text items a lot...)

I've compiled latest merge to mac_styles branch and I didn't notice any improvements  in terms of CPU usage.
I'm using Helvetica font

so sad... :)
++

nab added on 2020-04-15 14:33:36:
I will report as soon as I can.
++

marc_culler (claiming to be Marc Culler) added on 2020-04-15 14:26:57:
Excellent point! The issue is much more extreme when the kerning is negative.
This also reveals that it really is an intrinsic problem which need not be
related to anti-aliasing at all.

I will close this ticket and merge the fix, hoping to make a small dent in
the growing backlog of Aqua tickets.

@nab - you and @stevenaaus are probably the people best qualified to decide
if there is a noticeable performance change.  Let us know!

chrstphrchvz added on 2020-04-15 14:24:33:

In terms of performances will canvas text benefit of those changes?

Very likely. DisplayCanvText() uses TkDrawAngledTextLayout() which in turn uses the DrawCharsInContext() being improved here.

One of the slower TkTable examples that I tried for [685ac30727] (maxsize.tcl) appears to redraw about twice as fast/often with [39c3f57220].


nab added on 2020-04-15 13:49:53:
Hi,
I'm very impressed by your proposal/fixes...
In terms of performances will canvas text benefit of those changes?

Best regards, 
Nicolas

chrstphrchvz added on 2020-04-15 13:23:03:

I believe I'm satisfied with the fix in [39c3f57220].

Marc, thank you for your work on this, and great observation how kerning is exactly why bounds clipping won't work between adjacent chunks. Here's a more obvious example illustrating this (notice leading portions of f, g, and j were clipped):

pack [text .t -font {{Times Italic} 64} -height 1 -width 10]
.t insert end "asdfghjkl"
.t tag add redtext 1.3 1.6
.t tag add greentext 1.4
.t tag configure redtext -foreground red
.t tag configure greentext -foreground green

Before/After:

And, drawing text might be slightly faster now since CTLineGetTypographicBounds() does less work than CTLineGetImageBounds() (which I wonder might be doing as much work as actually drawing text), and text before start isn't needlessly redrawn.

Maybe if more performance tweaks are desired in this area, one idea might be to check if any Core Text objects can be cached between TkpMeasureCharsInContext() and DrawCharsInContext().


marc_culler (claiming to be Marc Culler) added on 2020-04-14 19:52:41:
Thank you Christopher!!!

I pushed a fix to the bugfix branch.  I tried to make all type conversions
between floats and ints completely explicit.

chrstphrchvz added on 2020-04-14 19:06:07:

I've briefly tried [c7c0e6ab03]…However moving the insertion cursor causes glyphs to shift slightly…

This appears to be caused by (width - CTLineGetTypographicBounds(line, NULL, NULL, NULL)) being truncated when added to int x; using a CGFloat instead to hold the result and pass into CGContextSetTextPosition() seems to resolve this.


chrstphrchvz added on 2020-04-14 18:49:13:

I've briefly tried [c7c0e6ab03]; it looked good at first glance—no more redrawing, so no more artifacts. However moving the insertion cursor causes glyphs to shift slightly, so maybe the alignment needs some refinement.


marc_culler (claiming to be Marc Culler) added on 2020-04-14 17:01:51:
I think I can explain what is going on here.  The core fact is this: when
drawing two adjacent anti-aliased glyphs there *can* be pixels which get
colored by both glyphs.  The corollary of this is that there is no way that
clipping can possibly work since that assumes that the sets of affected
pixels are disjoint.

Your solution of drawing transparent glyphs avoids clipping, and it works.

I think there is another approach which avoids redrawing glyphs an arbitrary
number of times.  The natural thing to do would be to just draw the glyphs
that we are being asked to draw.  The complication with doing that is that
you need to compute the x-coordinate where the part of the line which we
are drawing should start.  It does not start at the end of the initial part
which is not being drawn.  The last glyph in the initial part typically has
some kerning following it, so the x-coordinate needs to be a little larger
than the x-coordinate of the rightmost pixel in the initial part.  However,
I found that you can compute the x-coordinate by computing the typographical
length of the full string and subtracting the typographical length of the
part being drawn.

I have pushed an implementation of this scheme to the bug-fix branch.  As
far as I have been able to tell it works correctly.  What do you think?

chrstphrchvz added on 2020-04-14 15:53:29:

Some other thoughts:

I have not looked very deep into what CTLineGetImageBounds() is up to or other aspects of Core Text and antialiasing. This function seems sparsely used in open source projects. I came across https://developer.apple.com/videos/play/wwdc2012/226/ (12:45) which at a minimum said that CTLineGetImageBounds() is "pretty difficult to actually use", though also mentions a modern equivalent CTLineGetBoundsWithOptions(…, kCTLineBoundsUseGlyphPathBounds).

I had wondered if rounding or some other miscalculation of startBounds → clipRect.origin.x might have been responsible, as I thought may have been for [3607259]. But the calculations in DrawCharsInContext are nearly as they were for the original Tk Cocoa, so I wonder if all that has changed are subtle differences in newer macOS or fonts, such that [3607259] doesn't look as bad as it once did. And when looking at this issue again recently, it didn't appear that rounding could be an issue, because querying the context for its bounds with CGContextGetClipBoundingBox(context) after clipping with clipRect did not reveal any rounding, and I didn't think there would be any rounding performed by the Core Text operations that immediately followed it (since they are Retina-aware and can even produce vectorized output). Unless there is still some implicit internal rounding I'm not aware of, such as antialiasing that Marc mentions.

Is there a way to verify that the image bounds of glyphs drawn after start doesn't overlap startBounds? Does CTTypesetterCreateLine() guarantee it will use exactly the same layout for a prefix of a string both when it is laid out alone and with the the rest of the string?

Is it not possible to compute the bounds of the text from start to start + len, and clip to those bounds instead of startBounds?

I would also want to take a step back and understand why Tk Aqua is drawing text the way it currently does. I'm under the impression it is trying to cooperate with how text is drawn under X11, drawing a single chunk of glyphs of a single formatted style at a time. Since contiguous text looks best on Mac only when it can render text from the beginning of a line/string, it currently redraws already-drawn glyphs and expects to be able to clip them away. But I wonder if NSAttributedString is capable enough to allow drawing all chunks of text for a single line/string in one pass and look right (and avoiding issues like this one by requiring intermediate clipping). Has this approach already been considered and rejected, or just not attempted due to the effort that would be needed for it to get along with the generic code?

Unless there is a more direct way of instructing Core Text to not redraw certain glpyhs yet still use the layout as if they are being drawn, setting already-drawn glyphs to transparent when redrawing them seems to be the closest thing to doing so.

Here's why I think it that approach sufficient and correct:

Given a string "abcdef" ("abc" is red and "def" is blue), first "abc" is drawn. (It has no text prior to start so there is no startBounds clipping.) Assume "abc" is allowed to be drawn as far right as necessary without interference from any clipping regions. Then "abc" will be have been completely* drawn before proceeding to draw "def". So when drawing "def", if the color for "abc" being redrawn is set to transparent, nothing of "abc" already drawn should be lost visually because it was drawn completely. Clipping to startBounds is then not strictly necessary, but still maybe useful for safety and optimization.

*(Unless CTTypesetterCreateLine() has somehow intentionally clipped part of 'c' to be drawn as part of "def".)

I think trying to wrangle startBounds/clipRect rather than making clipped text transparent has a greater likelihood that future tinkering in macOS itself requires paying this issue a visit in the future. I also wonder whether expanding the size of startBounds could potentially clip into the glyphs after start (though unlikely if only by a pixel more).

Though adjusting startBounds/clipRect is likely more performant than making clipped text transparent.


chrstphrchvz added on 2020-04-14 04:43:20:

Marc, your workaround in [e872348af7] is an improvement, but the test case from [3607259] still reveal extremely faint artifacts on the right edge of the '2' characters, most easily detectable after setting only tag1 text ("3 4 5 6") to be visible, e.g.:

.f.t configure -background white -foreground white
.f.t tag configure tag1 -foreground black

Initial test:

Black tag1 text on white background/white tag1 text on black background:

Above tests after using MSPaint-style fill (only completely black/white pixels are filled) to fill background with dark blue/light yellow respectively:

So far, setting the clipped text to transparent is the only approach I've found to completely eliminate the artifacts:



marc_culler (claiming to be Marc Culler) added on 2020-04-13 22:15:21:
By following my hunch about rounding I seem to have stumbled on a fix for this
problem.  I opened a bug-fix branch that implements it.  All it does is to expand
the startBounds by 0.5 pixels on each side.  I believe that the anti-aliasing
algorithm requires a clipping region that is 0.5 pixel larger than the bounding
box of the text.  I don't know if this is documented somewhere but I would not
be surprised to learn that it is not.

Can you please test the bugfix branch and see if you agree that it fixes the
problem?  Thanks.

marc_culler (claiming to be Marc Culler) added on 2020-04-13 21:06:53:
I am sure you are right that those are "prompt marks".  Maybe if I had known
such a thing existed I would have had an easier time seeing them.

chrstphrchvz added on 2020-04-13 20:42:53:

I will upload a screenshot showing the thin vertical black lines that appear at along the right side of the Terminal window.
Do you mean the gray square brackets on both the left and right edges?

If so, those are the automatic prompt line marks.


marc_culler (claiming to be Marc Culler) added on 2020-04-13 20:42:26:
It occurs to me that this might be due to rounding.  All of Apple's screen
dimensions are floating point numbers and all of Tk's screen dimensions are
integers.  I am wondering if Tk might be rounding the dimensions of some
rectangles to integers when it should be leaving them as floats.

marc_culler (claiming to be Marc Culler) added on 2020-04-13 20:26:34:
Actually, when you zoom in they look like right square brackets.

marc_culler (claiming to be Marc Culler) added on 2020-04-13 20:21:25:
I will upload a screenshot showing the thin vertical black lines that appear at
along the right side of the Terminal window.  This was made on a laptop with
a Retina display, but I have seen the same thing on a Mac mini with a Dell
monitor.

chrstphrchvz added on 2020-04-13 19:57:38:

I see very similar artifacts in Apple's Terminal app. I use a white background and a black foreground in the Terminal, and I see thin vertical black lines along the right hand edge of the text.

I never managed to observe this in Terminal.app or any other app, at least on a non-Retina display.

My best guess is that in DrawCharsInContext() (tkMacOSXFont.c), startBounds = CTLineGetImageBounds() provides precise subpixel dimensions, which sounds fine, but whether due to some slight inaccuracy in the font or somewhere in Core Text, startBounds does not perfectly cover the text prior to start by a subpixel amount, leaving behind the 1-pixel-wide artifacts I observe.

Workaround: set the text prior to start to be completely transparent (I have attached a patch with one possible approach). Unless, it is argued, that an imperfect startBounds is a font/macOS issue, and therefore Tk has no business working around it.

Yes, there are bigger warts in Tk to try removing.


marc_culler (claiming to be Marc Culler) added on 2019-08-18 02:29:47:
I see very similar artifacts in Apple's Terminal app.  I use a white background
and a black foreground in the Terminal, and I see thin vertical black lines along
the right hand edge of the text.  So I think this is an Apple bug, not a Tk bug.
I don't see that we have any control over the text anti-aliasing, beyond
choosing one of the handful of anti-aliasing options that Apple makes available.

Attachments: