forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSFont+RulerFont.swift
More file actions
42 lines (35 loc) · 1.4 KB
/
NSFont+RulerFont.swift
File metadata and controls
42 lines (35 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// NSFont+RulerFont.swift
// CodeEditSourceEditor
//
// Created by Elias Wahl on 17.03.23.
//
import Foundation
import AppKit
extension NSFont {
var rulerFont: NSFont {
let fontSize: Double = (self.pointSize - 1) + 0.25
let fontAdvance: Double = self.pointSize * 0.49 + 0.1
let fontWeight = NSFont.Weight(rawValue: self.pointSize * 0.00001 + 0.0001)
let fontWidth = NSFont.Width(rawValue: -0.13)
let font = NSFont.systemFont(ofSize: fontSize, weight: fontWeight, width: fontWidth)
/// Set the open four
let alt4: [NSFontDescriptor.FeatureKey: Int] = [
.selectorIdentifier: kStylisticAltOneOnSelector,
.typeIdentifier: kStylisticAlternativesType
]
/// Set alternate styling for 6 and 9
let alt6and9: [NSFontDescriptor.FeatureKey: Int] = [
.selectorIdentifier: kStylisticAltTwoOnSelector,
.typeIdentifier: kStylisticAlternativesType
]
/// Make all digits monospaced
let monoSpaceDigits: [NSFontDescriptor.FeatureKey: Int] = [
.selectorIdentifier: 0,
.typeIdentifier: kNumberSpacingType
]
let features = [alt4, alt6and9, monoSpaceDigits]
let descriptor = font.fontDescriptor.addingAttributes([.featureSettings: features, .fixedAdvance: fontAdvance])
return NSFont(descriptor: descriptor, size: 0) ?? font
}
}