Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Source/MarkupText.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
98 changes: 91 additions & 7 deletions Source/MarkupText.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,21 +78,63 @@ - (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<NSAttributedStringKey, id> *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<NSString *> *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 {
_boxSize = NSMakeSize(MAX(1.0f, boxSize.width), MAX(1.0f, boxSize.height));
}

- (void)updateMeasuredSize {
self.zoomedTextRect = NSZeroRect;
NSAttributedString *attr = [self attributedString];
if (attr.length == 0) {
CGFloat lineHeight = STTextLineHeight(self.font);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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];
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Source/ScreenshotCanvasView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions Tests/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Loading
Loading