import CoreText import Foundation #if os(macOS) import AppKit typealias DocsPlatformFont = NSFont #else import UIKit typealias DocsPlatformFont = UIFont #endif @MainActor enum DocsFonts { private static let registered: Void = { for family in ["Sans", "Serif", "Mono"] { for face in ["Regular", "Bold", "Italic", "BoldItalic"] { let name = "Liberation\(family)-\(face)" if let url = DocsResources.bundle.url(forResource: name, withExtension: "ttf") { CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil) } } } }() static func font(marks: [String: JSONValue], size baseSize: CGFloat, heading: Bool) -> DocsPlatformFont { _ = registered var size = baseSize for key in marks.keys.sorted() where key.hasPrefix("size_") { if let value = Double(key.dropFirst(5)), [8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72].contains(value) { size = value } else if key == "size_sm" { size = 10 } else if key == "size_lg" { size = 14 } else if key == "size_xl" { size = 18 } } if marks["code"] != nil { size *= 0.85 } let family = marks["font_mono"] != nil || marks["code"] != nil ? "Mono" : marks["font_serif"] != nil ? "Serif" : "Sans" let bold = marks["strong"] != nil || heading let italic = marks["em"] != nil let face = bold ? (italic ? "BoldItalic" : "Bold") : (italic ? "Italic" : "Regular") let postScriptName = "Liberation\(family)" + (face == "Regular" ? "" : "-\(face)") return DocsPlatformFont(name: postScriptName, size: size) ?? DocsPlatformFont.systemFont(ofSize: size) } }