diff --git a/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate b/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate index e130e9d..a0a9134 100644 Binary files a/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate and b/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Padbury Clock Revived/ClockView.swift b/Padbury Clock Revived/ClockView.swift index 9c994c9..9aeaff5 100644 --- a/Padbury Clock Revived/ClockView.swift +++ b/Padbury Clock Revived/ClockView.swift @@ -31,6 +31,7 @@ final class ClockView: ScreenSaverView { var fontSize: CGFloat = 0 var vOffset: CGFloat = 0 + var lineHeight: CGFloat = 0 var backgroundColor: NSColor = .blue var foregroundColor: NSColor = .green @@ -76,34 +77,29 @@ final class ClockView: ScreenSaverView { dateFormatter.dateFormat = "\(hour)\(minute)\(second)\(suffix)" dateFormatter.amSymbol = "AM" dateFormatter.pmSymbol = "PM" - - // The font gets a font size relative to the screen width as not to clip. - // I don't know how to determine this correctly, these are ugly hard coded - // numbers and they MIGHT fail for long strings (bold + AM/PM). If you know - // of a better system please make a pull request or write me an email! - // >>> contact.kamik423@gmail.com <<< - let fontSizeScaleFactor : CGFloat - switch preferences.fontFamily { - case .sanFrancisco, .neueHelvetica: - fontSizeScaleFactor = 0.20 - case .sanFranciscoMono: - fontSizeScaleFactor = 0.13 - case .newYork: - fontSizeScaleFactor = 0.17 + + // Compute fontsize to fit screen. + // Sample string is measured. It is contained in <> to give it some consistent space + // at the edge of the screen. + var sizingString = "<11" + sizingString += preferences.showTimeSeparators ? ":" : " " + sizingString += "59" + if preferences.showSeconds { + sizingString += preferences.showTimeSeparators ? ":" : " " + sizingString += "59" + sizingString += preferences.useAmPm ? " AM" : "" + } else { + // If no seconds are used and no AM the font size should still be decreased a bit + // because otherwise it will be very gimongous and not very minimalist + sizingString += preferences.useAmPm ? " AM" : "X" } - fontSize = fontSizeScaleFactor * bounds.width - - // Vertical offset of the font. This is manually determined. - // The 0.15 is to move it down a bit since the center of the font rect is - // lower than the center of the numbers as the rect accounts for lower case - // letter that might extend below the baseline. - // - // 0.5: align center of target rect with center of screen - // 0.15: offset so font is visually centered - vOffset = fontSize * 0.5 - 0.15 * fontSize - + sizingString += ">" + fontSize = 100 * bounds.width / NSString(string: sizingString).size(withAttributes: [.font: preferences.nsFont(ofSize: 100)]).width + // Load the correct font let font = preferences.nsFont(ofSize: fontSize) + lineHeight = font.ascender - font.descender - font.leading + vOffset = (font.descender + font.leading) - 0.5 * font.capHeight // Set the paragraph style with the correct font, centering and color. let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center @@ -140,11 +136,13 @@ final class ClockView: ScreenSaverView { if preferences.mainScreenOnly && !isMainScreen { return } // The font rect. vOffset has been described above - let targetRect = NSRect(x: 0, y: bounds.height * 0.5 - vOffset, width: bounds.width, height: fontSize) + let targetRect = NSRect(x: 0, y: bounds.height / 2 + vOffset, width: bounds.width, height: lineHeight) ///// For debugging the font rect can be drawn now. // NSColor.blue.setFill() // targetRect.fill() + // NSColor.green.setFill() + // NSRect(x: 0, y: bounds.height / 2, width: bounds.width, height: 1).fill() // Get the time string let time = NSString(string: dateFormatter.string(from: Date())) diff --git a/Padbury Clock Revived/Preferences.swift b/Padbury Clock Revived/Preferences.swift index 3519ebb..c8bd97b 100644 --- a/Padbury Clock Revived/Preferences.swift +++ b/Padbury Clock Revived/Preferences.swift @@ -42,6 +42,11 @@ class Preferences: NSObject { // Alternate Punctuation (rounded, raised colon) featureSettings.append([.typeIdentifier: kCharacterAlternativesType, .selectorIdentifier: 1]) } + // Alternative fontfeatures can be determined using + // CTFontCopyFeatures(NSFont(name: ".AppleSystemUIFont", size: 10)!) + // SF supports open 4, straight 6 and 9 + // NY supports old style + // Apply the attributes font = NSFont(descriptor: font.fontDescriptor.addingAttributes([.featureSettings: featureSettings]), size: 0.0) ?? font return font diff --git a/README.md b/README.md index 7b288ac..b0d7b23 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,12 @@ Download [the latest version](https://github.com/Kamik423/Padbury-Clock-Revived/ ## Changelog +### 1.1.2 (2022-02-14) + +* Dynamically load font weights and display them in the dropdown +* Compute font size dynamically based on enabled and disabled features +* Compute correct vertical centering instead of guesstimating it + ### 1.1.1 (2022-02-10) * Added option for main screen only diff --git a/screenshots/settings.png b/screenshots/settings.png index 73a2ce9..f40faa6 100644 Binary files a/screenshots/settings.png and b/screenshots/settings.png differ