Centralize font selection via +[BSSourceView sourceFont].
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 8 Dec 2019 17:42:05 +0000 (12:42 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 15 Dec 2019 17:08:04 +0000 (12:08 -0500)
Source/BSLineNumberRulerView.mm
Source/BSSourceView.h
Source/BSSourceView.mm

index 7b1d253d1e2b4a06420cfcedd7356f60b6e53fff..b530bf04143f719630c4bf930310750d6240c732 100644 (file)
@@ -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],
+  };
 }
 
 /**
index ac504317be388e7f5d4a6f2999323ee8309ecb0f..a9359da7f00d53fb4cbaae67096db550f248b5dc 100644 (file)
@@ -52,6 +52,8 @@
 - (void)setString:(NSString*)source asFile:(NSString*)path;
 - (void)scrollToLine:(NSUInteger)line;
 
++ (NSFont*)sourceFont;
+
 @end
 
 // Delegate ////////////////////////////////////////////////////////////////////
index 88928016bd5e1db14d4246d2c27e9ef7045534c1..e7bf17bfe00b441cdeccc7396c1eecb3d1ff3d3c 100644 (file)
   [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
  */
   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];
         [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]);
       }