From 21440aebb021f6c78b315347d99ef87e98fdb717 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 8 Dec 2019 12:42:05 -0500 Subject: [PATCH] Centralize font selection via +[BSSourceView sourceFont]. --- Source/BSLineNumberRulerView.mm | 13 +++++-------- Source/BSSourceView.h | 2 ++ Source/BSSourceView.mm | 23 +++++++++++++++++------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Source/BSLineNumberRulerView.mm b/Source/BSLineNumberRulerView.mm index 7b1d253..b530bf0 100644 --- a/Source/BSLineNumberRulerView.mm +++ b/Source/BSLineNumberRulerView.mm @@ -235,14 +235,11 @@ const CGFloat kRulerRightPadding = 2.5; */ - (NSDictionary*)fontAttributes { - NSFont* font = [NSFont fontWithName:@"Menlo" size:10.0]; - if (!font) - font = [NSFont fontWithName:@"Monaco" size:10.0]; - return [NSDictionary dictionaryWithObjectsAndKeys: - font, NSFontAttributeName, - [NSColor grayColor], NSForegroundColorAttributeName, - nil - ]; + NSFont* font = [NSFont fontWithDescriptor:[[BSSourceView sourceFont] fontDescriptor] size:11.0]; + return @{ + NSFontAttributeName : font, + NSForegroundColorAttributeName : [NSColor grayColor], + }; } /** diff --git a/Source/BSSourceView.h b/Source/BSSourceView.h index ac50431..a9359da 100644 --- a/Source/BSSourceView.h +++ b/Source/BSSourceView.h @@ -52,6 +52,8 @@ - (void)setString:(NSString*)source asFile:(NSString*)path; - (void)scrollToLine:(NSUInteger)line; ++ (NSFont*)sourceFont; + @end // Delegate //////////////////////////////////////////////////////////////////// diff --git a/Source/BSSourceView.mm b/Source/BSSourceView.mm index 8892801..e7bf17b 100644 --- a/Source/BSSourceView.mm +++ b/Source/BSSourceView.mm @@ -131,6 +131,20 @@ [scrollView_ setNeedsDisplay:YES]; } +/** + * Returns the preferred font for source views. + */ ++ (NSFont*)sourceFont +{ + static NSFont* font = nil; + if (!font) { + font = [NSFont fontWithName:@"Menlo" size:12]; + if (!font) + font = [NSFont fontWithName:@"Monaco" size:12.0]; + } + return font; +} + /** * Setup all the subviews for the source metaview */ @@ -153,7 +167,7 @@ textView_ = [[BSSourceViewTextView alloc] initWithFrame:textFrame]; [textView_ setSourceView:self]; [textView_ setEditable:NO]; - [textView_ setFont:[NSFont fontWithName:@"Monaco" size:10.0]]; + [textView_ setFont:[[self class] sourceFont]]; [textView_ setHorizontallyResizable:YES]; [textView_ setVerticallyResizable:YES]; [textView_ setMinSize:textFrame.size]; @@ -207,11 +221,8 @@ [stringData replaceOccurrencesOfString:@"\u00A0" withString:@" " options:0 range:NSMakeRange(0, stringData.length)]; // Override the default font from Courier. - NSFont* menloFont = [NSFont fontWithName:@"Menlo" size:12]; - if (menloFont) { - [source addAttributes:@{ NSFontAttributeName : menloFont } - range:NSMakeRange(0, source.length)]; - } + [source addAttributes:@{ NSFontAttributeName : [[self class] sourceFont] } + range:NSMakeRange(0, source.length)]; } else { NSLog(@"Failed to highlight PHP file %@: %@", filePath, [[errPipe fileHandleForReading] readDataToEndOfFile]); } -- 2.22.5