From 30a26f61555216d86cb35b5662fe9d970f38f76e Mon Sep 17 00:00:00 2001 From: culler Date: Mon, 13 Apr 2020 22:06:35 +0000 Subject: Fix [89354dae31]: Text display artifacts can be produced by antialiasing. --- macosx/tkMacOSXFont.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c index c3b0b66..6bb7b51 100644 --- a/macosx/tkMacOSXFont.c +++ b/macosx/tkMacOSXFont.c @@ -1293,6 +1293,12 @@ DrawCharsInContext( line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, start)); startBounds = CTLineGetImageBounds(line, context); + + /* + * Expand the rectangle by half a pixel to allow room for anitaliasing. + */ + + startBounds = CGRectInset(startBounds, -0.5, -0.5); CFRelease(line); clipRect.origin.x = startBounds.origin.x + startBounds.size.width; CGContextClipToRect(context, clipRect); -- cgit v0.12 From 9faa66c0ab0bc765706825d27e157d920f34000a Mon Sep 17 00:00:00 2001 From: marc_culler Date: Tue, 14 Apr 2020 16:42:34 +0000 Subject: Clipping can never work! Try again. --- macosx/tkMacOSXFont.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c index 6bb7b51..3a9e36f 100644 --- a/macosx/tkMacOSXFont.c +++ b/macosx/tkMacOSXFont.c @@ -1244,18 +1244,19 @@ DrawCharsInContext( NSAttributedString *attributedString; CTTypesetterRef typesetter; CFIndex start, len; - CTLineRef line; + CTLineRef line, full=nil; MacDrawable *macWin = (MacDrawable *) drawable; TkMacOSXDrawingContext drawingContext; CGContextRef context; CGColorRef fg; NSFont *nsFont; CGAffineTransform t; + CGFloat width; int h; - if (rangeStart < 0 || rangeLength <= 0 || - rangeStart + rangeLength > numBytes || - !TkMacOSXSetupDrawingContext(drawable, gc, 1, &drawingContext)) { + if (rangeStart < 0 || rangeLength <= 0 || + rangeStart + rangeLength > numBytes || + !TkMacOSXSetupDrawingContext(drawable, gc, 1, &drawingContext)) { return; } string = TkUtfToNSString((const char *)source, numBytes); @@ -1285,25 +1286,24 @@ DrawCharsInContext( CGAffineTransformTranslate(t, x, y), angle*PI/180.0), -x, -y); } CGContextConcatCTM(context, t); - CGContextSetTextPosition(context, x, y); start = Tcl_NumUtfChars(source, rangeStart); - len = Tcl_NumUtfChars(source, rangeStart + rangeLength); + len = Tcl_NumUtfChars(source, rangeStart + rangeLength) - start; + line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, len)); if (start > 0) { - CGRect clipRect = CGRectInfinite, startBounds; - - line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, start)); - startBounds = CTLineGetImageBounds(line, context); - + /* - * Expand the rectangle by half a pixel to allow room for anitaliasing. + * We are only drawing part of the string. To compute the x coordinate + * of the part we are drawing we subtract its typographical length from + * the typographical length of the full string. This accounts for the + * kerning after the initial part of the string. */ - startBounds = CGRectInset(startBounds, -0.5, -0.5); - CFRelease(line); - clipRect.origin.x = startBounds.origin.x + startBounds.size.width; - CGContextClipToRect(context, clipRect); + full = CTTypesetterCreateLine(typesetter, CFRangeMake(0, start + len)); + width = CTLineGetTypographicBounds(full, NULL, NULL, NULL); + CFRelease(full); + x += (width - CTLineGetTypographicBounds(line, NULL, NULL, NULL)); } - line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, len)); + CGContextSetTextPosition(context, x, y); CTLineDraw(line, context); CFRelease(line); CFRelease(typesetter); -- cgit v0.12 From b4f276f52906366f518ba93803324d4e78db4f8e Mon Sep 17 00:00:00 2001 From: culler Date: Tue, 14 Apr 2020 19:51:15 +0000 Subject: Fix a glitch caused by coercion of a float to an int. Thanks to Christoper Chavez for finding and analyzing the bug. --- macosx/tkMacOSXFont.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c index 3a9e36f..a91cbe4 100644 --- a/macosx/tkMacOSXFont.c +++ b/macosx/tkMacOSXFont.c @@ -1243,7 +1243,7 @@ DrawCharsInContext( NSMutableDictionary *attributes; NSAttributedString *attributedString; CTTypesetterRef typesetter; - CFIndex start, len; + CFIndex start, length; CTLineRef line, full=nil; MacDrawable *macWin = (MacDrawable *) drawable; TkMacOSXDrawingContext drawingContext; @@ -1251,8 +1251,7 @@ DrawCharsInContext( CGColorRef fg; NSFont *nsFont; CGAffineTransform t; - CGFloat width; - int h; + CGFloat width, height, textX = (CGFloat) x, textY = (CGFloat) y; if (rangeStart < 0 || rangeLength <= 0 || rangeStart + rangeLength > numBytes || @@ -1276,19 +1275,20 @@ DrawCharsInContext( attributes:attributes]; typesetter = CTTypesetterCreateWithAttributedString( (CFAttributedStringRef)attributedString); - x += macWin->xOff; - y += macWin->yOff; - h = drawingContext.portBounds.size.height; - y = h - y; - t = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, h); + textX += (CGFloat) macWin->xOff; + textY += (CGFloat) macWin->yOff; + height = drawingContext.portBounds.size.height; + textY = height - textY; + t = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, height); if (angle != 0.0) { t = CGAffineTransformTranslate(CGAffineTransformRotate( - CGAffineTransformTranslate(t, x, y), angle*PI/180.0), -x, -y); + CGAffineTransformTranslate(t, textX, textY), angle*PI/180.0), + -textX, -textY); } CGContextConcatCTM(context, t); start = Tcl_NumUtfChars(source, rangeStart); - len = Tcl_NumUtfChars(source, rangeStart + rangeLength) - start; - line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, len)); + length = Tcl_NumUtfChars(source, rangeStart + rangeLength) - start; + line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, length)); if (start > 0) { /* @@ -1298,12 +1298,12 @@ DrawCharsInContext( * kerning after the initial part of the string. */ - full = CTTypesetterCreateLine(typesetter, CFRangeMake(0, start + len)); + full = CTTypesetterCreateLine(typesetter, CFRangeMake(0, start + length)); width = CTLineGetTypographicBounds(full, NULL, NULL, NULL); CFRelease(full); - x += (width - CTLineGetTypographicBounds(line, NULL, NULL, NULL)); + textX += (width - CTLineGetTypographicBounds(line, NULL, NULL, NULL)); } - CGContextSetTextPosition(context, x, y); + CGContextSetTextPosition(context, textX, textY); CTLineDraw(line, context); CFRelease(line); CFRelease(typesetter); -- cgit v0.12