Browse Source

Make font a property that can be configured at any time, not just at startup

Miguel de Icaza 5 years ago
parent
commit
0d099506e2

+ 15 - 15
Sources/SwiftTerm/Apple/AppleTerminalView.swift

@@ -142,14 +142,14 @@ extension TerminalView {
     // Computes the font dimensions once font.normal has been set
     func computeFontDimensions () -> CellDimension
     {
-        let lineAscent = CTFontGetAscent (options.font.normal)
-        let lineDescent = CTFontGetDescent (options.font.normal)
-        let lineLeading = CTFontGetLeading (options.font.normal)
+        let lineAscent = CTFontGetAscent (font.normal)
+        let lineDescent = CTFontGetDescent (font.normal)
+        let lineLeading = CTFontGetLeading (font.normal)
         let cellHeight = ceil(lineAscent + lineDescent + lineLeading)
         #if os(macOS)
-        let cellWidth = options.font.normal.maximumAdvancement.width
+        let cellWidth = font.normal.maximumAdvancement.width
         #else
-        let fontAttributes = [NSAttributedString.Key.font: options.font.normal]
+        let fontAttributes = [NSAttributedString.Key.font: font.normal]
         let cellWidth = "W".size(withAttributes: fontAttributes).width
         #endif
         return CellDimension(width: cellWidth, height: cellHeight)
@@ -259,23 +259,23 @@ extension TerminalView {
             return result
         }
         
-        var font: TTFont
+        var tf: TTFont
         let isBold = flags.contains(.bold)
         if isBold {
             if flags.contains (.italic) {
-                font = options.font.boldItalic
+                tf = font.boldItalic
             } else {
-                font = options.font.bold
+                tf = font.bold
             }
         } else if flags.contains (.italic) {
-            font = options.font.italic
+            tf = font.italic
         } else {
-            font = options.font.normal
+            tf = font.normal
         }
         
         let fgColor = mapColor (color: fg, isFg: true, isBold: isBold)
         var nsattr: [NSAttributedString.Key:Any] = [
-            .font: font,
+            .font: tf,
             .foregroundColor: fgColor,
             .backgroundColor: mapColor(color: bg, isFg: false, isBold: false)
         ]
@@ -404,13 +404,13 @@ extension TerminalView {
             // draw underline at font.normal.underlinePosition baseline
             let underlineStyle = NSUnderlineStyle(rawValue: attributes[.underlineStyle] as? NSUnderlineStyle.RawValue ?? 0)
             let underlineColor = attributes[.underlineColor] as? TTColor ?? nativeForegroundColor
-            let underlinePosition = options.font.underlinePosition ()
+            let underlinePosition = font.underlinePosition ()
 
             // draw line at the baseline
             currentContext.setShouldAntialias(false)
             currentContext.setStrokeColor(underlineColor.cgColor)
 
-            let underlineThickness = max(round(scale * options.font.underlineThickness ()) / scale, 0.5)
+            let underlineThickness = max(round(scale * font.underlineThickness ()) / scale, 0.5)
             for p in positions {
                 switch underlineStyle {
                 case let style where style.contains(.single):
@@ -459,8 +459,8 @@ extension TerminalView {
     // TODO: this should not render any lines outside the dirtyRect
     func drawTerminalContents (dirtyRect: TTRect, context: CGContext)
     {
-        let lineDescent = CTFontGetDescent(options.font.normal)
-        let lineLeading = CTFontGetLeading(options.font.normal)
+        let lineDescent = CTFontGetDescent(font.normal)
+        let lineLeading = CTFontGetLeading(font.normal)
 
         // draw lines
         for row in terminal.buffer.yDisp..<terminal.rows + terminal.buffer.yDisp {

+ 37 - 49
Sources/SwiftTerm/Mac/MacTerminalView.swift

@@ -36,54 +36,37 @@ import CoreGraphics
  * defaults, otherwise, this uses its own set of defaults colors.
  */
 open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations {
-    // User facing, customizable view options
-    public struct Options {
+    public struct Font {
+        public let normal: NSFont
+        let bold: NSFont
+        let italic: NSFont
+        let boldItalic: NSFont
         
-        public struct Font {
-            public let normal: NSFont
-            let bold: NSFont
-            let italic: NSFont
-            let boldItalic: NSFont
-            
-            static var defaultFont: NSFont {
-                if #available(OSX 10.15, *)  {
-                    return NSFont.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular)
-                } else {
-                    return NSFont(name: "Menlo Regular", size: NSFont.systemFontSize) ?? NSFont(name: "Courier", size: NSFont.systemFontSize)!
-                }
-            }
-            
-            public init(font baseFont: NSFont, fontSize: CGFloat? = nil) {
-                self.normal = baseFont
-                self.bold = NSFontManager.shared.convert(baseFont, toHaveTrait: [.boldFontMask])
-                self.italic = NSFontManager.shared.convert(baseFont, toHaveTrait: [.italicFontMask])
-                self.boldItalic = NSFontManager.shared.convert(baseFont, toHaveTrait: [.italicFontMask, .boldFontMask])
-            }
-
-            // Expected by the shared rendering code
-            func underlinePosition () -> CGFloat
-            {
-                return normal.underlinePosition
-            }
-
-            // Expected by the shared rendering code
-            func underlineThickness () -> CGFloat
-            {
-                return normal.underlineThickness
+        static var defaultFont: NSFont {
+            if #available(OSX 10.15, *)  {
+                return NSFont.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular)
+            } else {
+                return NSFont(name: "Menlo Regular", size: NSFont.systemFontSize) ?? NSFont(name: "Courier", size: NSFont.systemFontSize)!
             }
         }
         
-        public let font: Font
-        public static let `default` = Options(font: Font(font: Font.defaultFont))
-        
-        public init(font: Font) {
-            self.font = font
+        public init(font baseFont: NSFont, fontSize: CGFloat? = nil) {
+            self.normal = baseFont
+            self.bold = NSFontManager.shared.convert(baseFont, toHaveTrait: [.boldFontMask])
+            self.italic = NSFontManager.shared.convert(baseFont, toHaveTrait: [.italicFontMask])
+            self.boldItalic = NSFontManager.shared.convert(baseFont, toHaveTrait: [.italicFontMask, .boldFontMask])
         }
-    }
-    
-    public private(set) var options: Options {
-        didSet {
-            self.setupOptions()
+
+        // Expected by the shared rendering code
+        func underlinePosition () -> CGFloat
+        {
+            return normal.underlinePosition
+        }
+
+        // Expected by the shared rendering code
+        func underlineThickness () -> CGFloat
+        {
+            return normal.underlineThickness
         }
     }
     
@@ -115,8 +98,13 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations {
     var trueColors: [Attribute.Color:NSColor] = [:]
     var transparent = TTColor.transparent ()
     
-    public init(frame: CGRect, options: Options) {
-        self.options = options
+    /// This font structure represents the font to be used for the terminal
+    public var font: Font {
+        didSet { setupOptions() }
+    }
+    
+    public init(frame: CGRect, font: NSFont?) {
+        self.font = Font (font: font ?? Font.defaultFont)
 
         super.init (frame: frame)
         setup()
@@ -124,14 +112,14 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations {
     
     public override init (frame: CGRect)
     {
-        self.options = Options.default
+        self.font = Font (font: Font.defaultFont)
         super.init (frame: frame)
         setup()
     }
     
     public required init? (coder: NSCoder)
     {
-        self.options = Options.default
+        self.font = Font (font: Font.defaultFont)
         super.init (coder: coder)
         setup()
     }
@@ -973,13 +961,13 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations {
     
     public func resetFontSize ()
     {
-        options = Options.`default`
+        font = Font (font: Font.defaultFont)
     }
     
     let fontScale = [9, 10, 11, 13, 14, 18, 24, 36, 48, 64, 72, 96, 144, 288]
     public func biggerFontSize ()
     {
-        let current = options.font.normal.pointSize
+        let current = font.normal.pointSize
         for x in fontScale {
             if current < CGFloat (x) {
                 // Set the font size here

+ 34 - 45
Sources/SwiftTerm/iOS/iOSTerminalView.swift

@@ -33,50 +33,33 @@ import CoreGraphics
  * defaults, otherwise, this uses its own set of defaults colors.
  */
 open class TerminalView: UIScrollView, UITextInputTraits, UIKeyInput, UIScrollViewDelegate {
-    // User facing, customizable view options
-    public struct Options {
+    public struct Font {
+        public let normal: UIFont
+        let bold: UIFont
+        let italic: UIFont
+        let boldItalic: UIFont
         
-        public struct Font {
-            public let normal: UIFont
-            let bold: UIFont
-            let italic: UIFont
-            let boldItalic: UIFont
-            
-            static var defaultFont: UIFont {
-                UIFont.monospacedSystemFont (ofSize: 12, weight: .regular)
-            }
-            
-            public init(font baseFont: UIFont) {
-                self.normal = baseFont
-                self.bold = UIFont (descriptor: baseFont.fontDescriptor.withSymbolicTraits ([.traitBold])!, size: 0)
-                self.italic = UIFont (descriptor: baseFont.fontDescriptor.withSymbolicTraits ([.traitItalic])!, size: 0)
-                self.boldItalic = UIFont (descriptor: baseFont.fontDescriptor.withSymbolicTraits ([.traitItalic, .traitBold])!, size: 0)
-            }
-            
-            // Expected by the shared rendering code
-            func underlinePosition () -> CGFloat
-            {
-                return -1.2
-            }
-            
-            // Expected by the shared rendering code
-            func underlineThickness () -> CGFloat
-            {
-                return 0.63
-            }
+        static var defaultFont: UIFont {
+            UIFont.monospacedSystemFont (ofSize: 12, weight: .regular)
         }
         
-        public let font: Font
-        public static let `default` = Options(font: Font(font: Font.defaultFont))
+        public init(font baseFont: UIFont) {
+            self.normal = baseFont
+            self.bold = UIFont (descriptor: baseFont.fontDescriptor.withSymbolicTraits ([.traitBold])!, size: 0)
+            self.italic = UIFont (descriptor: baseFont.fontDescriptor.withSymbolicTraits ([.traitItalic])!, size: 0)
+            self.boldItalic = UIFont (descriptor: baseFont.fontDescriptor.withSymbolicTraits ([.traitItalic, .traitBold])!, size: 0)
+        }
         
-        public init(font: Font) {
-            self.font = font
+        // Expected by the shared rendering code
+        func underlinePosition () -> CGFloat
+        {
+            return -1.2
         }
-    }
-    
-    public private(set) var options: Options {
-        didSet {
-            self.setupOptions()
+        
+        // Expected by the shared rendering code
+        func underlineThickness () -> CGFloat
+        {
+            return 0.63
         }
     }
     
@@ -106,27 +89,33 @@ open class TerminalView: UIScrollView, UITextInputTraits, UIKeyInput, UIScrollVi
     var trueColors: [Attribute.Color:UIColor] = [:]
     var transparent = TTColor.transparent ()
     
-    public init(frame: CGRect, options: Options) {
-        self.options = options
+    /// This font structure represents the font to be used for the terminal
+    public var font: Font {
+        didSet {
+            setupOptions()
+        }
+    }
+    
+    public init(frame: CGRect, font: UIFont?) {
+        self.font = Font (font: font ?? Font.defaultFont)
         super.init (frame: frame)
         setup()
     }
     
     public override init (frame: CGRect)
     {
-        self.options = Options.default
-
+        self.font = Font (font: Font.defaultFont)
         super.init (frame: frame)
         setup()
     }
     
     public required init? (coder: NSCoder)
     {
-        self.options = Options.default
+        self.font = Font (font: Font.defaultFont)
         super.init (coder: coder)
         setup()
     }
-    
+        
     func setup()
     {
         setupOptions ()