From f05f97748476bea65eab547a9947c17a21073686 Mon Sep 17 00:00:00 2001 From: LEOYoon-Tsaw Date: Sun, 4 Apr 2021 12:06:24 -0400 Subject: [PATCH 1/5] Remove horizontal padding (cherry picked from commit 421f3bd7b04bb24f66943e1027ffb99d7265c1aa) --- SquirrelPanel.m | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 6d3585427..4cb869b3f 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -172,7 +172,6 @@ @interface SquirrelView : NSView @property(nonatomic, readonly) NSRect contentRect; @property(nonatomic, readonly) BOOL isDark; @property(nonatomic, strong, readonly) SquirrelTheme *currentTheme; -@property(nonatomic, readonly) CGFloat textFrameWidth; @property(nonatomic, assign) CGFloat seperatorWidth; - (BOOL)isFlipped; @@ -218,6 +217,7 @@ - (instancetype)initWithFrame:(NSRect)frameRect { } // Use textStorage to store text and manage all text layout and draws NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:NSZeroSize]; + textContainer.lineFragmentPadding = 0.0; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [layoutManager addTextContainer:textContainer]; _text = [[NSTextStorage alloc] init]; @@ -230,18 +230,10 @@ - (instancetype)initWithFrame:(NSRect)frameRect { return self; } -// The textStorage layout will have a 5px empty edge on both sides -- (CGFloat)textFrameWidth { - return [_text.layoutManagers[0] boundingRectForGlyphRange:NSMakeRange(0, 0) inTextContainer:_text.layoutManagers[0].textContainers[0]].origin.x; -} - // Get the rectangle containing entire contents, expensive to calculate - (NSRect)contentRect { NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForTextContainer:_text.layoutManagers[0].textContainers[0]]; NSRect rect = [_text.layoutManagers[0] boundingRectForGlyphRange:glyphRange inTextContainer:_text.layoutManagers[0].textContainers[0]]; - CGFloat frameWidth = self.textFrameWidth; - rect.origin.x -= frameWidth; - rect.size.width += frameWidth * 2; return rect; } @@ -359,7 +351,7 @@ - (void)multilineRectForRange:(NSRange)charRange leadingRect:(NSRect *)leadingRe *leadingRect = NSZeroRect; *bodyRect = boundingRect; *trailingRect = NSZeroRect; - if (boundingRect.origin.x <= self.textFrameWidth +1 && fullRangeInBoundingRect.location < glyphRange.location) { + if (boundingRect.origin.x <= 1 && fullRangeInBoundingRect.location < glyphRange.location) { *leadingRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(fullRangeInBoundingRect.location, glyphRange.location-fullRangeInBoundingRect.location) inTextContainer:textContainer]; if (!nearEmptyRect(*leadingRect)) { bodyRect->size.height -= leadingRect->size.height; @@ -480,7 +472,6 @@ - (void)addGapBetweenHorizontalCandidates:(NSRect *)rect { // All draws happen here - (void)drawRect:(NSRect)dirtyRect { - double textFrameWidth = self.textFrameWidth; NSBezierPath *backgroundPath; NSBezierPath *borderPath; NSBezierPath *highlightedPath; @@ -515,8 +506,8 @@ - (void)drawRect:(NSRect)dirtyRect { // Draw highlighted Rect if (_highlightedRange.length > 0 && theme.highlightedStripColor != nil) { NSRect innerBox = backgroundRect; - innerBox.size.width -= (theme.edgeInset.width + 1 + textFrameWidth) * 2; - innerBox.origin.x += theme.edgeInset.width + 1 + textFrameWidth; + innerBox.size.width -= (theme.edgeInset.width + 1) * 2; + innerBox.origin.x += theme.edgeInset.width + 1; if (_preeditRange.length == 0) { innerBox.origin.y += theme.edgeInset.height + 1; innerBox.size.height -= (theme.edgeInset.height + 1) * 2; @@ -593,8 +584,8 @@ - (void)drawRect:(NSRect)dirtyRect { [self multilineRectForRange:_highlightedPreeditRange leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect]; NSRect innerBox = preeditRect; - innerBox.size.width -= (theme.edgeInset.width + 1 + textFrameWidth) * 2; - innerBox.origin.x += theme.edgeInset.width + 1 + textFrameWidth; + innerBox.size.width -= (theme.edgeInset.width + 1) * 2; + innerBox.origin.x += theme.edgeInset.width + 1; innerBox.origin.y += theme.edgeInset.height + 1; if (_highlightedRange.length == 0) { innerBox.size.height -= (theme.edgeInset.height + 1) * 2; @@ -876,7 +867,7 @@ - (void)show { } //Break line if the text is too long, based on screen size. - CGFloat textWidth = _view.text.size.width + _view.textFrameWidth * 2; + CGFloat textWidth = _view.text.size.width; NSFont *currentFont = theme.attrs[NSFontAttributeName]; CGFloat fontScale = currentFont.pointSize / 12; CGFloat textWidthRatio = MIN(1.0, 1.0 / (theme.vertical ? 4 : 3) + fontScale / 12); From d4cd139f4ddd180468a807cdbf31961f8abe7d9e Mon Sep 17 00:00:00 2001 From: LEOYoon-Tsaw Date: Mon, 5 Apr 2021 03:43:14 -0400 Subject: [PATCH 2/5] Fix issue of different line height with Emoji (cherry picked from commit 34504169b3c14c788855e24bae7ed960848d4376) --- SquirrelPanel.m | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 4cb869b3f..0771927a3 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -746,15 +746,18 @@ void convertToVerticalGlyph(NSMutableAttributedString *originalText, NSRange str } } -void changeEmojiSize(NSMutableAttributedString *text, CGFloat emojiFontSize) { +void fixDefaultFont(NSMutableAttributedString *text, NSSet *fonts) { [text fixFontAttributeInRange:NSMakeRange(0, text.length)]; - NSFont *emojiFont = [NSFont fontWithName:@"AppleColorEmoji" size:emojiFontSize]; NSRange currentFontRange = NSMakeRange(NSNotFound, 0); long i = 0; while (i < text.length) { - NSFont *charFont = [text attributesAtIndex:i effectiveRange:¤tFontRange][NSFontAttributeName]; - if ([charFont.fontName isEqualTo: @"AppleColorEmoji"]) { - [text addAttributes:@{NSFontAttributeName: emojiFont} range:currentFontRange]; + NSDictionary *charAttr = [text attributesAtIndex:i effectiveRange:¤tFontRange]; + NSFont *charFont = charAttr[NSFontAttributeName]; + if (![fonts containsObject:charFont]) { + NSMutableDictionary *defaultAttributes = [charAttr mutableCopy]; + NSFont *defaultFont = [NSFont systemFontOfSize:charFont.pointSize]; + [defaultAttributes setValue:defaultFont forKey:NSFontAttributeName]; + [text setAttributes:defaultAttributes range:currentFontRange]; } i = currentFontRange.location + currentFontRange.length; } @@ -1155,9 +1158,12 @@ - (void)showPreedit:(NSString *)preedit [text appendAttributedString:line]; } - // Change Emoji font size - NSFont *currentFont = theme.attrs[NSFontAttributeName]; - changeEmojiSize(text, MAX(round(currentFont.pointSize * 0.8), currentFont.pointSize - 2)); + // Fix font rendering + fixDefaultFont(text, [NSSet setWithArray:@[ + theme.attrs[NSFontAttributeName], + theme.labelAttrs[NSFontAttributeName], + theme.commentAttrs[NSFontAttributeName] + ]]); // text done! [_view setText:text]; From 1fe8281ec04bae0396f2aa44204e83eb73a39eb9 Mon Sep 17 00:00:00 2001 From: LEOYoon-Tsaw Date: Mon, 5 Apr 2021 11:13:20 -0400 Subject: [PATCH 3/5] optimize `fixDefaultFont` code (cherry picked from commit 0d26585ab166e0ddac90966ad1996267000f18ec) --- SquirrelPanel.m | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 0771927a3..fb56800bc 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -751,13 +751,10 @@ void fixDefaultFont(NSMutableAttributedString *text, NSSet *fonts) { NSRange currentFontRange = NSMakeRange(NSNotFound, 0); long i = 0; while (i < text.length) { - NSDictionary *charAttr = [text attributesAtIndex:i effectiveRange:¤tFontRange]; - NSFont *charFont = charAttr[NSFontAttributeName]; + NSFont *charFont = [text attributesAtIndex:i effectiveRange:¤tFontRange][NSFontAttributeName]; if (![fonts containsObject:charFont]) { - NSMutableDictionary *defaultAttributes = [charAttr mutableCopy]; NSFont *defaultFont = [NSFont systemFontOfSize:charFont.pointSize]; - [defaultAttributes setValue:defaultFont forKey:NSFontAttributeName]; - [text setAttributes:defaultAttributes range:currentFontRange]; + [text addAttribute:NSFontAttributeName value:defaultFont range:currentFontRange]; } i = currentFontRange.location + currentFontRange.length; } From e108f79f0086a1a52401dddcaab80bef63772199 Mon Sep 17 00:00:00 2001 From: LEOYoon-Tsaw Date: Mon, 5 Apr 2021 11:58:19 -0400 Subject: [PATCH 4/5] Fix a small bug in preedit highlighted rect without candidate (cherry picked from commit 6edcd56acf10614d3d408c37d1a183b06faac7fe) --- SquirrelPanel.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SquirrelPanel.m b/SquirrelPanel.m index fb56800bc..938087908 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -496,7 +496,7 @@ - (void)drawRect:(NSRect)dirtyRect { preeditRect.size.height += theme.edgeInset.height + theme.preeditLinespace / 2 + theme.hilitedCornerRadius / 2; preeditRect.origin = NSMakePoint(textField.origin.x - theme.edgeInset.width, textField.origin.y - theme.edgeInset.height); if (_highlightedRange.length == 0) { - preeditRect.size.height += theme.edgeInset.height - theme.preeditLinespace / 2; + preeditRect.size.height += theme.edgeInset.height - theme.preeditLinespace / 2 - theme.hilitedCornerRadius / 2; } if (theme.preeditBackgroundColor != nil) { preeditPath = drawSmoothLines(rectVertex(preeditRect), 0, 0); From 309d5d2558707a428fa0c28769f28b1fffea0aec Mon Sep 17 00:00:00 2001 From: LEOYoon-Tsaw Date: Mon, 5 Apr 2021 19:07:57 -0400 Subject: [PATCH 5/5] Limit font replacement to Emoji only (cherry picked from commit 4375a9f3f7a85b1e01f99d733111d0edd18438a9) --- SquirrelPanel.m | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 938087908..48b6e8ff0 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -746,13 +746,13 @@ void convertToVerticalGlyph(NSMutableAttributedString *originalText, NSRange str } } -void fixDefaultFont(NSMutableAttributedString *text, NSSet *fonts) { +void fixDefaultFont(NSMutableAttributedString *text) { [text fixFontAttributeInRange:NSMakeRange(0, text.length)]; NSRange currentFontRange = NSMakeRange(NSNotFound, 0); long i = 0; while (i < text.length) { - NSFont *charFont = [text attributesAtIndex:i effectiveRange:¤tFontRange][NSFontAttributeName]; - if (![fonts containsObject:charFont]) { + NSFont *charFont = [text attribute:NSFontAttributeName atIndex:i effectiveRange:¤tFontRange]; + if ([charFont.fontName isEqualToString:@"AppleColorEmoji"]) { NSFont *defaultFont = [NSFont systemFontOfSize:charFont.pointSize]; [text addAttribute:NSFontAttributeName value:defaultFont range:currentFontRange]; } @@ -1156,11 +1156,7 @@ - (void)showPreedit:(NSString *)preedit } // Fix font rendering - fixDefaultFont(text, [NSSet setWithArray:@[ - theme.attrs[NSFontAttributeName], - theme.labelAttrs[NSFontAttributeName], - theme.commentAttrs[NSFontAttributeName] - ]]); + fixDefaultFont(text); // text done! [_view setText:text];