adds first version of the app
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import CoreText
|
||||
import Foundation
|
||||
|
||||
/// Registers the bundled OpenDyslexic faces for this process.
|
||||
public enum FontRegistry {
|
||||
public static let resourceBundleName = "Plak_PlakUI.bundle"
|
||||
|
||||
/// The Fonts folder, found without `Bundle.module`: its generated accessor looks next to
|
||||
/// the .app (not in Contents/Resources) and falls back to an absolute build path.
|
||||
public static func fontsDirectory() -> URL? {
|
||||
let fm = FileManager.default
|
||||
let candidates = [
|
||||
Bundle.main.resourceURL,
|
||||
Bundle.main.bundleURL,
|
||||
Bundle.main.executableURL?.deletingLastPathComponent(),
|
||||
// Under `swift test` the code lives in an .xctest bundle beside the resource bundle.
|
||||
Bundle(for: BundleToken.self).bundleURL.deletingLastPathComponent(),
|
||||
]
|
||||
for base in candidates.compactMap({ $0 }) {
|
||||
let fonts = base.appendingPathComponent(resourceBundleName).appendingPathComponent("Fonts")
|
||||
if fm.fileExists(atPath: fonts.path) { return fonts }
|
||||
let flat = base.appendingPathComponent(resourceBundleName).appendingPathComponent("Contents/Resources/Fonts")
|
||||
if fm.fileExists(atPath: flat.path) { return flat }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Registers every font file once; returns the PostScript names now available. Safe to call repeatedly.
|
||||
@discardableResult
|
||||
public static func registerBundledFonts() -> Set<String> {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
if let done = registered { return done }
|
||||
var names = Set<String>()
|
||||
guard let dir = fontsDirectory(),
|
||||
let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) else {
|
||||
NSLog("plak: bundled fonts not found; using the system fonts")
|
||||
registered = names
|
||||
return names
|
||||
}
|
||||
for url in files where ["otf", "ttf"].contains(url.pathExtension.lowercased()) {
|
||||
var error: Unmanaged<CFError>?
|
||||
let ok = CTFontManagerRegisterFontsForURL(url as CFURL, .process, &error)
|
||||
let already = (error?.takeRetainedValue()).map { CFErrorGetCode($0) == CTFontManagerError.alreadyRegistered.rawValue } ?? false
|
||||
if ok || already {
|
||||
let descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor] ?? []
|
||||
for d in descriptors {
|
||||
if let name = CTFontDescriptorCopyAttribute(d, kCTFontNameAttribute) as? String { names.insert(name) }
|
||||
}
|
||||
} else {
|
||||
NSLog("plak: cannot register font %@", url.lastPathComponent)
|
||||
}
|
||||
}
|
||||
registered = names
|
||||
return names
|
||||
}
|
||||
|
||||
private final class BundleToken {}
|
||||
private static let lock = NSLock()
|
||||
nonisolated(unsafe) private static var registered: Set<String>?
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import AppKit
|
||||
|
||||
/// OpenDyslexic for all text; the system font when the bundled face is missing.
|
||||
public enum PlakFonts {
|
||||
public static let regular = "OpenDyslexic-Regular"
|
||||
|
||||
public static func body(_ size: CGFloat) -> NSFont {
|
||||
FontRegistry.registerBundledFonts()
|
||||
return NSFont(name: regular, size: size) ?? .systemFont(ofSize: size)
|
||||
}
|
||||
|
||||
/// The height of one line of this font with no paragraph style.
|
||||
static func naturalLineHeight(_ font: NSFont) -> CGFloat {
|
||||
ceil(font.ascender - font.descender + font.leading)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import AppKit
|
||||
|
||||
/// The colour tokens of the paper look.
|
||||
public enum Theme {
|
||||
/// Window and page background.
|
||||
public static let paper = NSColor(hex: 0xD8D0C0)
|
||||
/// Text.
|
||||
public static let ink = NSColor(hex: 0x2B2621)
|
||||
/// Dates, `image missing`.
|
||||
public static let inkMuted = NSColor(hex: 0x6E655A)
|
||||
/// The caret.
|
||||
public static let accent = NSColor(hex: 0x8A4B2A)
|
||||
/// Selected text, and the save error message.
|
||||
public static let selection = NSColor(hex: 0xB9AE98)
|
||||
}
|
||||
|
||||
public extension NSColor {
|
||||
convenience init(hex: UInt32, alpha: CGFloat = 1) {
|
||||
self.init(
|
||||
srgbRed: CGFloat((hex >> 16) & 0xFF) / 255,
|
||||
green: CGFloat((hex >> 8) & 0xFF) / 255,
|
||||
blue: CGFloat(hex & 0xFF) / 255,
|
||||
alpha: alpha
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The body text size that ⌘+ / ⌘− / ⌘0 change.
|
||||
public struct TextSize: Equatable, Sendable {
|
||||
public static let standard: CGFloat = 16
|
||||
public static let range: ClosedRange<CGFloat> = 10...32
|
||||
public static let step: CGFloat = 2
|
||||
|
||||
public private(set) var body: CGFloat
|
||||
|
||||
public init(body: CGFloat = TextSize.standard) {
|
||||
self.body = min(max(body, TextSize.range.lowerBound), TextSize.range.upperBound)
|
||||
}
|
||||
|
||||
public mutating func bigger() { self = TextSize(body: body + TextSize.step) }
|
||||
public mutating func smaller() { self = TextSize(body: body - TextSize.step) }
|
||||
public mutating func reset() { self = TextSize() }
|
||||
}
|
||||
|
||||
/// Sizes of the page layout that follow the body size.
|
||||
public enum PageMetrics {
|
||||
public static let columnCharacters = 68
|
||||
public static let lineHeightMultiple: CGFloat = 1.6
|
||||
public static let dateScale: CGFloat = 0.75
|
||||
/// Room under the transparent title bar.
|
||||
public static let topInset: CGFloat = 52
|
||||
public static let sideMargin: CGFloat = 28
|
||||
/// The narrowest left margin that shows created dates.
|
||||
public static let dateMarginMinimum: CGFloat = 120
|
||||
/// Room between a margin date and the column.
|
||||
public static let dateSpacing: CGFloat = 16
|
||||
|
||||
/// The width of about 68 characters of body text.
|
||||
public static func columnWidth(bodySize: CGFloat) -> CGFloat {
|
||||
let sample = String(String(repeating: "the quick brown fox jumps over a lazy dog ", count: 2).prefix(columnCharacters))
|
||||
let width = NSAttributedString(string: sample, attributes: [.font: PlakFonts.body(bodySize)]).size().width
|
||||
return ceil(width)
|
||||
}
|
||||
|
||||
/// The height of one text line: 1.6 times the size, or more when the font needs it.
|
||||
public static func lineHeight(_ font: NSFont) -> CGFloat {
|
||||
max(round(font.pointSize * lineHeightMultiple), PlakFonts.naturalLineHeight(font))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user