diff --git a/Source/MarkupText.h b/Source/MarkupText.h index e78c9fc..c6831cb 100644 --- a/Source/MarkupText.h +++ b/Source/MarkupText.h @@ -69,7 +69,11 @@ typedef NS_ENUM(NSInteger, MarkupTextStyle) { - (CGFloat)decorationOutset; /// The text plus its outline, shadow or background (not the pointer). - (NSRect)decoratedTextBounds; -/// Everything drawn, pointer included: what export and selection outlines cover. +/// The same, where the canvas last drew it: a zoomed font sets the text a little wider or narrower. +/// For hit testing and selection outlines. Export uses decoratedBounds. +- (NSRect)decoratedTextBoundsOnCanvas; +- (NSRect)decoratedBoundsOnCanvas; +/// Everything drawn, pointer included: what export covers. - (NSRect)decoratedBounds; /// Where the pointer leaves the label: the point on its edge nearest the target. - (NSPoint)pointerAnchor; diff --git a/Source/MarkupText.m b/Source/MarkupText.m index e73a78d..d6d2ac5 100644 --- a/Source/MarkupText.m +++ b/Source/MarkupText.m @@ -31,6 +31,9 @@ static inline CGFloat STTextLineHeight(NSFont *font) { @interface MarkupText () @property (nonatomic, assign, readwrite) NSSize measuredSize; +/// Where the canvas last drew the text at a zoom other than 100%, relative to the origin, in image +/// points. Empty at 100%, where the text is where it's measured to be. +@property (nonatomic, assign) NSRect zoomedTextRect; @end @implementation MarkupText @@ -75,14 +78,55 @@ - (NSAttributedString *)attributedStringWithFont:(NSFont *)font { } - (NSAttributedString *)attributedStringWithFont:(NSFont *)font color:(NSColor *)color { + return [self attributedStringWithText:self.text ?: @"" font:font color:color alignment:self.alignment]; +} + +- (NSAttributedString *)attributedStringWithText:(NSString *)text font:(NSFont *)font color:(NSColor *)color alignment:(NSTextAlignment)alignment { NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; attributes[NSFontAttributeName] = font; attributes[NSForegroundColorAttributeName] = color; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.lineBreakMode = NSLineBreakByWordWrapping; - style.alignment = self.alignment; + style.alignment = alignment; attributes[NSParagraphStyleAttributeName] = style; - return [[NSAttributedString alloc] initWithString:self.text ?: @"" attributes:attributes]; + return [[NSAttributedString alloc] initWithString:text attributes:attributes]; +} + +/// The text with a newline wherever it wraps at the label's own size, so a zoomed font, whose +/// hinted advances differ, breaks its lines in the same places as Copy and Save. +- (NSString *)textWithLinesBrokenAsAtFullSize { + NSTextStorage *storage = [[NSTextStorage alloc] initWithAttributedString:[self attributedString]]; + NSLayoutManager *layout = [[NSLayoutManager alloc] init]; + NSTextContainer *container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(MAX(1.0f, self.boxSize.width), FLT_MAX)]; + container.lineFragmentPadding = 0.0; + [layout addTextContainer:container]; + [storage addLayoutManager:layout]; + NSString *whole = storage.string; + NSMutableArray *lines = [[NSMutableArray alloc] init]; + NSUInteger glyph = 0, glyphs = [layout numberOfGlyphs]; + while (glyph < glyphs) { + NSRange lineGlyphs = NSMakeRange(0, 0); + [layout lineFragmentRectForGlyphAtIndex:glyph effectiveRange:&lineGlyphs]; + if (lineGlyphs.length == 0) { + break; + } + NSRange lineCharacters = [layout characterRangeForGlyphRange:lineGlyphs actualGlyphRange:NULL]; + NSString *line = [whole substringWithRange:lineCharacters]; + if ([line hasSuffix:@"\n"]) { + line = [line substringToIndex:line.length - 1]; + } else { + // The spaces a soft wrap leaves at the end of a line take no room when drawn. + NSUInteger end = line.length; + while (end > 0 && [line characterAtIndex:end - 1] == ' ') { + end--; + } + line = [line substringToIndex:end]; + } + [lines addObject:line]; + glyph = NSMaxRange(lineGlyphs); + } + NSString *broken = [lines componentsJoinedByString:@"\n"]; + return [whole hasSuffix:@"\n"] ? [broken stringByAppendingString:@"\n"] : broken; } - (void)setBoxSize:(NSSize)boxSize { @@ -90,6 +134,7 @@ - (void)setBoxSize:(NSSize)boxSize { } - (void)updateMeasuredSize { + self.zoomedTextRect = NSZeroRect; NSAttributedString *attr = [self attributedString]; if (attr.length == 0) { CGFloat lineHeight = STTextLineHeight(self.font); @@ -150,6 +195,7 @@ - (NSSize)measuredSizeForWrapWidth:(CGFloat)wrapWidth { } - (BOOL)fitToTextWithinCanvasSize:(NSSize)canvasSize { + self.zoomedTextRect = NSZeroRect; CGFloat lineHeight = STTextLineHeight(self.font); CGFloat pointSize = self.font.pointSize > 0.0 ? self.font.pointSize : 18.0; NSPoint origin = self.origin; @@ -217,8 +263,23 @@ - (NSRect)decoratedTextBounds { return NSInsetRect([self textBounds], -outset, -outset); } +- (NSRect)decoratedTextBoundsOnCanvas { + if (NSIsEmptyRect(self.zoomedTextRect)) { + return [self decoratedTextBounds]; + } + CGFloat outset = [self decorationOutset]; + return NSInsetRect(NSOffsetRect(self.zoomedTextRect, self.origin.x, self.origin.y), -outset, -outset); +} + - (NSRect)decoratedBounds { - NSRect bounds = [self decoratedTextBounds]; + return [self decoratedBoundsAround:[self decoratedTextBounds]]; +} + +- (NSRect)decoratedBoundsOnCanvas { + return [self decoratedBoundsAround:[self decoratedTextBoundsOnCanvas]]; +} + +- (NSRect)decoratedBoundsAround:(NSRect)bounds { if (!self.hasPointer) { return bounds; } @@ -344,6 +405,31 @@ - (void)drawAtScale:(CGFloat)scale unflippedHeight:(CGFloat)unflippedHeight deco NSRect box = [self textRectForCanvas]; NSRect textRect = NSMakeRect(box.origin.x * scale, box.origin.y * scale, box.size.width * scale, box.size.height * scale); + NSRect used = NSMakeRect((box.origin.x + [self alignmentOffset]) * scale, box.origin.y * scale, + self.measuredSize.width * scale, self.measuredSize.height * scale); + NSString *drawnText = self.text; + if (font != baseFont) { + // A zoomed font's lines aren't the label's lines times the zoom, so what's drawn fits the + // text as this font sets it: same line breaks, its own widths. + drawnText = [self textWithLinesBrokenAsAtFullSize]; + NSRect drawn = [[self attributedStringWithText:drawnText font:font color:[NSColor blackColor] alignment:NSTextAlignmentLeft] + boundingRectWithSize:NSMakeSize(1000000.0, FLT_MAX) + options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)]; + used.size = NSMakeSize(ceil(MAX(drawn.size.width, 1.0)), ceil(MAX(drawn.size.height, STTextLineHeight(font)))); + CGFloat spare = textRect.size.width - used.size.width; + if (spare < 0.0) { + textRect.origin.x += (self.alignment == NSTextAlignmentRight) ? spare : (self.alignment == NSTextAlignmentCenter) ? spare * 0.5 : 0.0; + textRect.size.width = used.size.width; + spare = 0.0; + } + used.origin.x = NSMinX(textRect) + ((self.alignment == NSTextAlignmentRight) ? spare : (self.alignment == NSTextAlignmentCenter) ? floor(spare * 0.5) : 0.0); + textRect.size.height = MAX(textRect.size.height, used.size.height); + } + if (unflippedHeight <= 0.0) { + self.zoomedTextRect = (font != baseFont) ? NSMakeRect(NSMinX(used) / scale - self.origin.x, NSMinY(used) / scale - self.origin.y, + NSWidth(used) / scale, NSHeight(used) / scale) + : NSZeroRect; + } // Rects are worked out top-down, as on the canvas, then mapped for unflipped contexts. NSRect (^map)(NSRect) = ^NSRect(NSRect rect) { if (unflippedHeight > 0.0) { @@ -353,7 +439,7 @@ - (void)drawAtScale:(CGFloat)scale unflippedHeight:(CGFloat)unflippedHeight deco }; void (^drawGlyphs)(NSColor *, CGFloat, CGFloat) = ^(NSColor *color, CGFloat dx, CGFloat dy) { NSRect rect = NSOffsetRect(textRect, dx, dy); - [[self attributedStringWithFont:font color:color] drawInRect:map(rect)]; + [[self attributedStringWithText:drawnText font:font color:color alignment:self.alignment] drawInRect:map(rect)]; }; if (self.hasPointer) { @@ -383,8 +469,6 @@ - (void)drawAtScale:(CGFloat)scale unflippedHeight:(CGFloat)unflippedHeight deco } case MarkupTextStyleBackground: { CGFloat padding = [self backgroundPadding] * scale; - NSRect used = NSMakeRect((box.origin.x + [self alignmentOffset]) * scale, box.origin.y * scale, - self.measuredSize.width * scale, self.measuredSize.height * scale); NSRect pill = NSInsetRect(used, -padding, -padding); CGFloat radius = MIN(padding * 1.2, pill.size.height * 0.5); [(self.color ?: [NSColor whiteColor]) setFill]; @@ -411,7 +495,7 @@ - (void)renderInContext:(NSGraphicsContext *)context canvasSize:(NSSize)canvasSi - (BOOL)containsPoint:(NSPoint)point { // Hit the text itself (with a little tolerance), not empty space in a wide box. - if (NSPointInRect(point, NSInsetRect([self decoratedTextBounds], -4.0, -4.0))) { + if (NSPointInRect(point, NSInsetRect([self decoratedTextBoundsOnCanvas], -4.0, -4.0))) { return YES; } if (!self.hasPointer) { diff --git a/Source/ScreenshotCanvasView.m b/Source/ScreenshotCanvasView.m index 8963a88..2934523 100644 --- a/Source/ScreenshotCanvasView.m +++ b/Source/ScreenshotCanvasView.m @@ -3120,7 +3120,7 @@ - (nullable id)annotationAtImagePoint:(NSPoint)point { - (NSRect)boundsOfAnnotation:(id)annotation { if ([annotation isKindOfClass:[MarkupText class]]) { - return [(MarkupText *)annotation decoratedBounds]; + return [(MarkupText *)annotation decoratedBoundsOnCanvas]; } if ([annotation isKindOfClass:[MarkupStroke class]]) { return [(MarkupStroke *)annotation bounds]; diff --git a/Tests/GNUmakefile b/Tests/GNUmakefile index dca873c..dbdcfd2 100644 --- a/Tests/GNUmakefile +++ b/Tests/GNUmakefile @@ -53,6 +53,7 @@ ScreenshotToolTests_OBJC_FILES = \ EmptyStateProbe_test.m \ LaunchCostProbe_test.m \ TextMoveProbe_test.m \ + TextLabelZoomProbe_test.m \ MultiWindowProbe_test.m \ PasteAvailabilityProbe_test.m \ PopoverFocusProbe_test.m \ diff --git a/Tests/TextLabelZoomProbe_test.m b/Tests/TextLabelZoomProbe_test.m new file mode 100644 index 0000000..e025066 --- /dev/null +++ b/Tests/TextLabelZoomProbe_test.m @@ -0,0 +1,320 @@ +/* + * TextLabelZoomProbe_test.m + * Copyright (C) 2026 Daniel Boyd + * + * A label's background fits the text as drawn at every zoom. The canvas draws text with a font + * scaled to the zoom, whose hinted advances don't scale evenly, while Copy draws at 1x. + */ + +#import +#import +#import "AppDelegate.h" +#import "MarkupText.h" +#import "ScreenshotCanvasView.h" +#import "ScreenshotToolSettings.h" +#import "TestEnvironmentHelpers.h" + + +@interface AppDelegate (TextLabelZoomTesting) +- (void)setupWindowAndContent; +- (void)layoutContentSubviews; +- (NSWindow *)window; +- (ScreenshotCanvasView *)canvasView; +@end + +@interface ScreenshotCanvasView (TextLabelZoomTesting) +@property (nonatomic, strong) NSMutableArray *texts; +- (NSArray *)selectedAnnotationObjects; +- (NSRect)boundsOfAnnotation:(id)annotation; +@end + +@interface TextLabelZoomProbeTests : XCTestCase +@end + +@implementation TextLabelZoomProbeTests + ++ (void)load { + STConfigureTestDefaults(); +} + +- (void)setUp { + [super setUp]; + [NSApplication sharedApplication]; +} + +/// The label drawn at `scale` on a white canvas of `canvas` points, read back one pixel per canvas point. Drawn +/// and read in strips STCaptureStripWidth points wide: on a CI runner with a small desktop, GNUstep's Windows +/// backend read back columns past about 1050 px as non-white, which the scans below took for the pill. Each +/// strip keeps the canvas's full height, so the unflipped y the label draws at is unchanged. +static const CGFloat STCaptureStripWidth = 512.0; + +static NSBitmapImageRep *STCaptureLabel(MarkupText *label, CGFloat scale, NSSize canvas) { + NSInteger width = (NSInteger)canvas.width, height = (NSInteger)canvas.height; + NSBitmapImageRep *whole = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:width pixelsHigh:height + bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO + colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0]; + // One image, drawn and read once per strip. + NSInteger fullStrip = MIN((NSInteger)STCaptureStripWidth, width); + NSImage *strip = [[NSImage alloc] initWithSize:NSMakeSize(fullStrip, height)]; + for (NSInteger x0 = 0; x0 < width; x0 += (NSInteger)STCaptureStripWidth) { + NSInteger stripWidth = MIN((NSInteger)STCaptureStripWidth, width - x0); + [strip lockFocus]; + [[NSColor whiteColor] setFill]; + NSRectFill(NSMakeRect(0.0, 0.0, fullStrip, height)); + // Saved and restored: GNUstep doesn't reset the transform on the next lockFocus of this image. + [NSGraphicsContext saveGraphicsState]; + NSAffineTransform *shift = [NSAffineTransform transform]; + [shift translateXBy:-(CGFloat)x0 yBy:0.0]; + [shift concat]; + [label drawAtScale:scale unflippedHeight:canvas.height decorationsOnly:NO]; + [NSGraphicsContext restoreGraphicsState]; + NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, stripWidth, height)]; + [strip unlockFocus]; + // Pixels per canvas point: 1 unless the backend backs the image at a higher density, when each point is + // sampled at its pixel's centre so the scans still count canvas points. + CGFloat densityX = (CGFloat)rep.pixelsWide / stripWidth, densityY = (CGFloat)rep.pixelsHigh / height; + if (x0 == 0) { + NSLog(@"STCaptureLabel: scale %.3f canvas %ldx%ld, strip %ld pt read back %ldx%ld px (density %.2f x %.2f)", + scale, (long)width, (long)height, (long)stripWidth, (long)rep.pixelsWide, (long)rep.pixelsHigh, densityX, densityY); + } + if (rep.pixelsWide < stripWidth || rep.pixelsHigh < height || rep.pixelsWide > STCaptureStripWidth * 2.0) { + NSLog(@"STCaptureLabel: strip at %ld read back %ldx%ld for %ldx%ld points", (long)x0, (long)rep.pixelsWide, + (long)rep.pixelsHigh, (long)stripWidth, (long)height); + return nil; + } + for (NSInteger y = 0; y < height; y++) { + NSInteger py = MIN((NSInteger)floor((y + 0.5) * densityY), rep.pixelsHigh - 1); + for (NSInteger x = 0; x < stripWidth; x++) { + NSInteger px = MIN((NSInteger)floor((x + 0.5) * densityX), rep.pixelsWide - 1); + NSColor *c = [[rep colorAtX:px y:py] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; + [whole setColor:c atX:x0 + x y:y]; + } + } + } + return whole; +} + +/// The capture's four corners are the white it was filled with: the readback is whole. +static BOOL STCornersAreWhite(NSBitmapImageRep *rep) { + if (!rep) { + return NO; + } + NSInteger w = rep.pixelsWide - 1, h = rep.pixelsHigh - 1; + BOOL white = YES; + for (NSArray *p in @[ @[ @0, @0 ], @[ @(w), @0 ], @[ @0, @(h) ], @[ @(w), @(h) ] ]) { + NSColor *c = [[rep colorAtX:p[0].integerValue y:p[1].integerValue] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; + // Written so a transparent pixel, whose components read back as NaN, isn't taken for white. + if (!(c.redComponent >= 0.95 && c.greenComponent >= 0.95 && c.blueComponent >= 0.95 && c.alphaComponent >= 0.95)) { + NSLog(@"STCornersAreWhite: corner %@,%@ is %.2f %.2f %.2f a %.2f", p[0], p[1], c.redComponent, c.greenComponent, + c.blueComponent, c.alphaComponent); + white = NO; + } + } + return white; +} + +/// Space, in pixels, between the ends of the pill and the ends of the glyphs. +static void STMeasureGaps(XCTestCase *self, MarkupText *label, CGFloat scale, CGFloat *left, CGFloat *right, CGFloat *height) { + NSSize canvas = NSMakeSize(ceil(1100.0 * scale), ceil(200.0 * scale)); + NSBitmapImageRep *rep = STCaptureLabel(label, scale, canvas); + XCTAssertNotNil(rep, @"capture at %.3f", scale); + XCTAssertTrue(STCornersAreWhite(rep), @"capture at %.3f isn't white at its corners", scale); + NSInteger pillFirst = -1, pillLast = -1, glyphFirst = -1, glyphLast = -1, top = -1, bottom = -1; + for (NSInteger x = 0; x < rep.pixelsWide; x++) { + for (NSInteger y = 0; y < rep.pixelsHigh; y++) { + NSColor *c = [[rep colorAtX:x y:y] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; + if (c.blueComponent > 0.9) { + continue; // The white around the pill. + } + pillFirst = pillFirst < 0 ? x : pillFirst; + pillLast = x; + top = (top < 0 || y < top) ? y : top; + bottom = MAX(bottom, y); + if (c.redComponent < 0.8) { + glyphFirst = glyphFirst < 0 ? x : glyphFirst; + glyphLast = x; + } + } + } + // Found, and inside the capture: a scan that reaches its edge measured the background. + XCTAssertTrue(pillFirst > 0 && glyphFirst > pillFirst, @"pill %ld glyph %ld at %.3f", (long)pillFirst, (long)glyphFirst, scale); + XCTAssertTrue(pillLast < rep.pixelsWide - 1 && glyphLast < pillLast, @"pill ends %ld of %ld, glyphs %ld at %.3f", + (long)pillLast, (long)rep.pixelsWide, (long)glyphLast, scale); + XCTAssertTrue(top > 0 && bottom < rep.pixelsHigh - 1, @"pill rows %ld..%ld of %ld at %.3f", (long)top, (long)bottom, + (long)rep.pixelsHigh, scale); + *left = glyphFirst - pillFirst; + *right = pillLast - glyphLast; + if (height) { + *height = bottom - top + 1; + } +} + +- (void)testThePillFitsTheTextAtEveryZoom { + NSArray *zooms = @[ @1.0, @0.947, @0.75, @0.5, @2.0, @1.5, @0.33, @0.8, @1.1 ]; + NSString *words = @"when in the course of human events"; + for (NSNumber *size in @[ @14, @18, @24, @36 ]) { + NSFont *font = [NSFont systemFontOfSize:size.doubleValue]; + MarkupText *label = [[MarkupText alloc] initWithText:words font:font color:[NSColor yellowColor] + origin:NSMakePoint(40.0, 40.0) boxSize:NSMakeSize(200.0, 40.0)]; + label.style = MarkupTextStyleBackground; + [label fitToTextWithinCanvasSize:NSMakeSize(1100.0, 200.0)]; + CGFloat pad = label.decorationOutset; + for (NSNumber *zoom in zooms) { + CGFloat scale = zoom.doubleValue, left = 0.0, right = 0.0; + STMeasureGaps(self, label, scale, &left, &right, NULL); + NSLog(@"LABELGAP %@ pt zoom %.3f: left %.0f right %.0f (padding %.1f)", size, scale, left, right, pad * scale); + XCTAssertLessThanOrEqual(fabs(left - right), MAX(3.0, ceil(size.doubleValue * scale * 0.2)), @"%@ pt at %.3f: left %.0f right %.0f", size, scale, left, right); + } + } +} + +- (void)testAlignedLabelsStayEvenInAWideBox { + NSFont *font = [NSFont systemFontOfSize:18.0]; + for (NSNumber *alignment in @[ @(NSTextAlignmentLeft), @(NSTextAlignmentCenter), @(NSTextAlignmentRight) ]) { + MarkupText *label = [[MarkupText alloc] initWithText:@"when in the course of human events" font:font color:[NSColor yellowColor] + origin:NSMakePoint(40.0, 40.0) boxSize:NSMakeSize(700.0, 40.0)]; + label.style = MarkupTextStyleBackground; + label.alignment = alignment.integerValue; + label.widthIsFixed = YES; + [label fitToTextWithinCanvasSize:NSMakeSize(1100.0, 200.0)]; + for (NSNumber *zoom in @[ @0.947, @0.75, @0.5, @2.0 ]) { + CGFloat left = 0.0, right = 0.0; + STMeasureGaps(self, label, zoom.doubleValue, &left, &right, NULL); + XCTAssertLessThanOrEqual(fabs(left - right), 3.0, @"alignment %@ at %@: left %.0f right %.0f", alignment, zoom, left, right); + } + } +} + +- (void)testAWrappedLabelKeepsItsLinesAtEveryZoom { + NSFont *font = [NSFont systemFontOfSize:18.0]; + MarkupText *label = [[MarkupText alloc] initWithText:@"when in the course of human events\nit becomes necessary for one people" font:font color:[NSColor yellowColor] + origin:NSMakePoint(40.0, 10.0) boxSize:NSMakeSize(230.0, 40.0)]; + label.style = MarkupTextStyleBackground; + label.widthIsFixed = YES; + [label fitToTextWithinCanvasSize:NSMakeSize(1100.0, 200.0)]; + CGFloat full = 0.0, left = 0.0, right = 0.0; + STMeasureGaps(self, label, 1.0, &left, &right, &full); + XCTAssertGreaterThan(full, label.lineHeight * 3.0, @"The label should wrap for this test"); + for (NSNumber *zoom in @[ @0.947, @0.75, @0.5, @1.1, @1.5, @2.0 ]) { + CGFloat height = 0.0; + STMeasureGaps(self, label, zoom.doubleValue, &left, &right, &height); + XCTAssertEqualWithAccuracy(height, full * zoom.doubleValue, label.lineHeight * zoom.doubleValue * 0.5, @"at %@", zoom); + } +} + +/// The ends of the pill as drawn at `scale`, in image points. +static void STPillEnds(XCTestCase *self, MarkupText *label, CGFloat scale, CGFloat *minX, CGFloat *maxX) { + NSSize canvas = NSMakeSize(ceil(1100.0 * scale), ceil(200.0 * scale)); + NSBitmapImageRep *rep = STCaptureLabel(label, scale, canvas); + XCTAssertNotNil(rep, @"capture at %.3f", scale); + XCTAssertTrue(STCornersAreWhite(rep), @"capture at %.3f isn't white at its corners", scale); + NSInteger first = -1, last = -1; + for (NSInteger x = 0; x < rep.pixelsWide; x++) { + for (NSInteger y = 0; y < rep.pixelsHigh; y++) { + if ([[rep colorAtX:x y:y] colorUsingColorSpaceName:NSDeviceRGBColorSpace].blueComponent <= 0.9) { + first = first < 0 ? x : first; + last = x; + break; + } + } + } + XCTAssertTrue(first > 0 && last < rep.pixelsWide - 1, @"pill %ld..%ld of %ld at %.3f", (long)first, (long)last, + (long)rep.pixelsWide, scale); + *minX = first / scale; + *maxX = (last + 1) / scale; +} + +/// One paint of the canvas, the way the window asks for it. +static void STDrawCanvas(ScreenshotCanvasView *canvas) { + [canvas lockFocus]; + [canvas drawRect:canvas.bounds]; + [canvas unlockFocus]; +} + +static NSEvent *STMouse(NSEventType type, NSPoint viewPoint, ScreenshotCanvasView *canvas) { + return [NSEvent mouseEventWithType:type location:[canvas convertPoint:viewPoint toView:nil] modifierFlags:0 timestamp:0 + windowNumber:[canvas.window windowNumber] context:nil eventNumber:0 clickCount:1 pressure:1.0]; +} + +/// Every part of the pill you can see selects and drags the label, and its outline surrounds the pill. +/// A 36 pt label at 94.7% draws wider than it measures at full size. +- (void)testThePillAsDrawnIsWhatYouClickAndWhatsOutlined { + AppDelegate *app = [[AppDelegate alloc] init]; + [app setupWindowAndContent]; + [app.window makeKeyAndOrderFront:nil]; + ScreenshotCanvasView *canvas = app.canvasView; + NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(1100.0, 200.0)]; + [image lockFocus]; + [[NSColor whiteColor] setFill]; + NSRectFill(NSMakeRect(0.0, 0.0, 1100.0, 200.0)); + [image unlockFocus]; + NSFont *font = [NSFont fontWithName:@"DejaVuSans" size:36.0] ?: [NSFont systemFontOfSize:36.0]; + for (NSNumber *zoom in @[ @0.947, @0.75, @0.5, @1.5, @2.0 ]) { + [canvas loadImage:image]; + [app layoutContentSubviews]; + canvas.fitToWindow = NO; + canvas.activeTool = ScreenshotCanvasToolSelect; + MarkupText *label = [[MarkupText alloc] initWithText:@"when in the course of human events" font:font color:[NSColor yellowColor] + origin:NSMakePoint(40.0, 40.0) boxSize:NSMakeSize(200.0, 40.0)]; + label.style = MarkupTextStyleBackground; + [label fitToTextWithinCanvasSize:image.size]; + if (!canvas.texts) { + canvas.texts = [[NSMutableArray alloc] init]; + } + [canvas.texts addObject:label]; + NSRect exported = label.decoratedBounds; + canvas.zoomScale = zoom.doubleValue; + STDrawCanvas(canvas); + + CGFloat minX = 0.0, maxX = 0.0; + STPillEnds(self, label, zoom.doubleValue, &minX, &maxX); + NSRect outline = [canvas boundsOfAnnotation:label]; + XCTAssertLessThanOrEqual(NSMinX(outline), minX + 1.0, @"at %@", zoom); + XCTAssertGreaterThanOrEqual(NSMaxX(outline), maxX - 1.0, @"at %@", zoom); + XCTAssertTrue(NSEqualRects(label.decoratedBounds, exported), @"Export's bounds don't follow the zoom"); + + // A press on the pill's last pixel selects, and a drag from it moves the label. + CGFloat y = NSMidY(label.decoratedBounds) * zoom.doubleValue; + NSPoint end = NSMakePoint((maxX - 1.0) * zoom.doubleValue, y); + NSPoint moved = NSMakePoint(end.x + 20.0, end.y + 10.0); + [canvas mouseDown:STMouse(NSLeftMouseDown, end, canvas)]; + XCTAssertTrue([canvas selectedAnnotationObjects].firstObject == label, @"at %@: the pill's right end should select it", zoom); + NSPoint before = label.origin; + [canvas mouseDragged:STMouse(NSLeftMouseDragged, moved, canvas)]; + [canvas mouseUp:STMouse(NSLeftMouseUp, moved, canvas)]; + XCTAssertGreaterThan(label.origin.x, before.x, @"at %@: a drag from it should move the label", zoom); + + // Well past the pill, nothing. + STDrawCanvas(canvas); + CGFloat newMin = 0.0, newMax = 0.0; + STPillEnds(self, label, zoom.doubleValue, &newMin, &newMax); + XCTAssertFalse([label containsPoint:NSMakePoint(newMax + 8.0, NSMidY(label.decoratedBounds))], @"at %@", zoom); + canvas.zoomScale = 1.0; + } +} + +/// A capture wider than the cutoff seen on CI reads back white all the way across. +- (void)testAWideCaptureReadsBackWhole { + MarkupText *label = [[MarkupText alloc] initWithText:@"x" font:[NSFont systemFontOfSize:14.0] color:[NSColor yellowColor] + origin:NSMakePoint(40.0, 40.0) boxSize:NSMakeSize(40.0, 40.0)]; + label.style = MarkupTextStyleBackground; + [label fitToTextWithinCanvasSize:NSMakeSize(2600.0, 200.0)]; + NSBitmapImageRep *rep = STCaptureLabel(label, 1.0, NSMakeSize(2600.0, 200.0)); + XCTAssertNotNil(rep); + XCTAssertEqual(rep.pixelsWide, 2600); + XCTAssertTrue(STCornersAreWhite(rep)); + NSInteger lastPainted = -1; + for (NSInteger x = 0; x < rep.pixelsWide; x++) { + for (NSInteger y = 0; y < rep.pixelsHigh; y += 4) { + NSColor *c = [[rep colorAtX:x y:y] colorUsingColorSpaceName:NSDeviceRGBColorSpace]; + if (!(c.blueComponent > 0.9 && c.alphaComponent > 0.9)) { + lastPainted = x; + break; + } + } + } + XCTAssertGreaterThan(lastPainted, 0, @"the label should be found"); + XCTAssertLessThan(lastPainted, 200, @"nothing past the label should read back as painted"); +} + +@end