Skip to content

Commit

Permalink
Dynamically compute font size and position
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamik423 committed Feb 14, 2022
1 parent e2082f7 commit 880f7f4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
Binary file not shown.
50 changes: 24 additions & 26 deletions Padbury Clock Revived/ClockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
// >>> [email protected] <<<
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
Expand Down Expand Up @@ -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()))
Expand Down
5 changes: 5 additions & 0 deletions Padbury Clock Revived/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified screenshots/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 880f7f4

Please sign in to comment.