fixed UIFont and NSFont issue, their names are different from the otf file names
This commit is contained in:
@@ -5,8 +5,13 @@
|
|||||||
// Created by Peter Tang on 10/9/2023.
|
// Created by Peter Tang on 10/9/2023.
|
||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
#if os(iOS)
|
||||||
import CoreText
|
import UIKit
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
import AppKit
|
||||||
|
#endif
|
||||||
|
|
||||||
public enum MathFont: String, CaseIterable {
|
public enum MathFont: String, CaseIterable {
|
||||||
|
|
||||||
@@ -16,12 +21,40 @@ public enum MathFont: String, CaseIterable {
|
|||||||
case xitsFont = "xits-math"
|
case xitsFont = "xits-math"
|
||||||
case termesFont = "texgyretermes-math"
|
case termesFont = "texgyretermes-math"
|
||||||
|
|
||||||
|
var fontFamilyName: String {
|
||||||
|
switch self {
|
||||||
|
case .latinModernFont: return "Latin Modern Math"
|
||||||
|
case .kpMathLightFont: return "KpMath"
|
||||||
|
case .kpMathSansFont: return "KpMath"
|
||||||
|
case .xitsFont: return "XITS Math"
|
||||||
|
case .termesFont: return "TeX Gyre Termes Math"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var fontName: String {
|
||||||
|
switch self {
|
||||||
|
case .latinModernFont: return "LatinModernMath-Regular"
|
||||||
|
case .kpMathLightFont: return "KpMath-Light"
|
||||||
|
case .kpMathSansFont: return "KpMath-Sans"
|
||||||
|
case .xitsFont: return "XITSMath"
|
||||||
|
case .termesFont: return "TeXGyreTermesMath-Regular"
|
||||||
|
}
|
||||||
|
}
|
||||||
public func cgFont() -> CGFont? {
|
public func cgFont() -> CGFont? {
|
||||||
BundleManager.manager.obtainCGFont(font: self)
|
BundleManager.manager.obtainCGFont(font: self)
|
||||||
}
|
}
|
||||||
public func ctFont(withSize size: CGFloat) -> CTFont? {
|
public func ctFont(withSize size: CGFloat) -> CTFont? {
|
||||||
BundleManager.manager.obtainCTFont(font: self, withSize: size)
|
BundleManager.manager.obtainCTFont(font: self, withSize: size)
|
||||||
}
|
}
|
||||||
|
#if os(iOS)
|
||||||
|
public func uiFont(withSize size: CGFloat) -> UIFont? {
|
||||||
|
UIFont(name: fontName, size: size)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if os(macOS)
|
||||||
|
public func nsFont(withSize size: CGFloat) -> NSFont? {
|
||||||
|
NSFont(name: fontName, size: size)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
internal func mathTable() -> NSDictionary? {
|
internal func mathTable() -> NSDictionary? {
|
||||||
BundleManager.manager.obtainMathTable(font: self)
|
BundleManager.manager.obtainMathTable(font: self)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,6 @@ import XCTest
|
|||||||
|
|
||||||
final class MathFontTests: XCTestCase {
|
final class MathFontTests: XCTestCase {
|
||||||
func testMathFontScript() throws {
|
func testMathFontScript() throws {
|
||||||
// for family in UIFont.familyNames.sorted() {
|
|
||||||
// let names = UIFont.fontNames(forFamilyName: family)
|
|
||||||
// print("Family: \(family) Font names: \(names)")
|
|
||||||
// }
|
|
||||||
let size = Int.random(in: 20 ... 40)
|
let size = Int.random(in: 20 ... 40)
|
||||||
MathFont.allCases.forEach {
|
MathFont.allCases.forEach {
|
||||||
// print("\(#function) cgfont \($0.cgFont())")
|
// print("\(#function) cgfont \($0.cgFont())")
|
||||||
@@ -22,5 +18,27 @@ final class MathFontTests: XCTestCase {
|
|||||||
XCTAssertNotNil($0.ctFont(withSize: CGFloat(size)))
|
XCTAssertNotNil($0.ctFont(withSize: CGFloat(size)))
|
||||||
XCTAssertEqual($0.ctFont(withSize: CGFloat(size))?.fontSize, CGFloat(size), "ctFont fontSize test")
|
XCTAssertEqual($0.ctFont(withSize: CGFloat(size))?.fontSize, CGFloat(size), "ctFont fontSize test")
|
||||||
}
|
}
|
||||||
|
#if os(iOS)
|
||||||
|
// for family in UIFont.familyNames.sorted() {
|
||||||
|
// let names = UIFont.fontNames(forFamilyName: family)
|
||||||
|
// print("Family: \(family) Font names: \(names)")
|
||||||
|
// }
|
||||||
|
fontNames.forEach { name in
|
||||||
|
let font = UIFont(name: name, size: CGFloat(size))
|
||||||
|
XCTAssertNotNil(font)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if os(macOS)
|
||||||
|
fontNames.forEach { name in
|
||||||
|
let font = NSFont(name: name, size: CGFloat(size))
|
||||||
|
XCTAssertNotNil(font)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
var fontNames: [String] {
|
||||||
|
MathFont.allCases.map { $0.fontName }
|
||||||
|
}
|
||||||
|
var fontFamilyNames: [String] {
|
||||||
|
MathFont.allCases.map { $0.fontFamilyName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user