Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove horizontal padding and fix emoji line height #559

Merged
merged 5 commits into from
Nov 23, 2021
Merged
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
40 changes: 15 additions & 25 deletions SquirrelPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -505,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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -755,15 +746,15 @@ void convertToVerticalGlyph(NSMutableAttributedString *originalText, NSRange str
}
}

void changeEmojiSize(NSMutableAttributedString *text, CGFloat emojiFontSize) {
void fixDefaultFont(NSMutableAttributedString *text) {
[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:&currentFontRange][NSFontAttributeName];
if ([charFont.fontName isEqualTo: @"AppleColorEmoji"]) {
[text addAttributes:@{NSFontAttributeName: emojiFont} range:currentFontRange];
NSFont *charFont = [text attribute:NSFontAttributeName atIndex:i effectiveRange:&currentFontRange];
if ([charFont.fontName isEqualToString:@"AppleColorEmoji"]) {
NSFont *defaultFont = [NSFont systemFontOfSize:charFont.pointSize];
[text addAttribute:NSFontAttributeName value:defaultFont range:currentFontRange];
}
i = currentFontRange.location + currentFontRange.length;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1164,9 +1155,8 @@ - (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);

// text done!
[_view setText:text];
Expand Down