diff --git a/Sources/SwiftUIMath/Internal/Display/CGContext+DisplayNode.swift b/Sources/SwiftUIMath/Internal/Display/CGContext+DisplayNode.swift index 9916469..9fb4345 100644 --- a/Sources/SwiftUIMath/Internal/Display/CGContext+DisplayNode.swift +++ b/Sources/SwiftUIMath/Internal/Display/CGContext+DisplayNode.swift @@ -5,8 +5,8 @@ import Foundation extension CGContext { func draw(_ displayNode: Math.DisplayNode, foregroundColor: CGColor) { let foregroundColor = - displayNode.localTextColor?.cgColor - ?? displayNode.textColor?.cgColor + displayNode.localTextColor + ?? displayNode.textColor ?? foregroundColor switch displayNode { diff --git a/Sources/SwiftUIMath/Internal/Display/DisplayNode.swift b/Sources/SwiftUIMath/Internal/Display/DisplayNode.swift index 8332cdb..34defbe 100644 --- a/Sources/SwiftUIMath/Internal/Display/DisplayNode.swift +++ b/Sources/SwiftUIMath/Internal/Display/DisplayNode.swift @@ -9,9 +9,9 @@ extension Math { var position: CGPoint = .zero var range: NSRange = NSRange(location: 0, length: 0) var hasScript: Bool = false - var textColor: PlatformColor? - var localTextColor: PlatformColor? - var localBackgroundColor: PlatformColor? + var textColor: CGColor? + var localTextColor: CGColor? + var localBackgroundColor: CGColor? func bounds() -> CGRect { CGRect(x: position.x, y: position.y - descent, width: width, height: ascent + descent) diff --git a/Sources/SwiftUIMath/Internal/Display/Typesetter.swift b/Sources/SwiftUIMath/Internal/Display/Typesetter.swift index b0934ae..2b28095 100644 --- a/Sources/SwiftUIMath/Internal/Display/Typesetter.swift +++ b/Sources/SwiftUIMath/Internal/Display/Typesetter.swift @@ -1,3 +1,4 @@ +import CoreGraphics import CoreText import Foundation @@ -423,7 +424,9 @@ extension Math { return line } - static var placeholderColor: PlatformColor { PlatformColor.blue } + static var placeholderColor: CGColor { + CGColor(srgbRed: 0, green: 0, blue: 1, alpha: 1) + } init( withFont font: PlatformFont?, style: Style.Level, cramped: Bool, spaced: Bool, @@ -986,7 +989,7 @@ extension Math { let colorAtom = atom as! Color let display = Typesetter.createLineForMathList( colorAtom.innerList, font: font, style: style, maxWidth: maxWidth) - display!.localTextColor = PlatformColor(fromHexString: colorAtom.colorString) + display!.localTextColor = CGColor.fromHexString(colorAtom.colorString) // Check if we need to break before adding this colored content let shouldBreak = shouldBreakBeforeDisplay( @@ -1013,7 +1016,7 @@ extension Math { let colorAtom = atom as! TextColor let display = Typesetter.createLineForMathList( colorAtom.innerList, font: font, style: style, maxWidth: maxWidth) - display!.localTextColor = PlatformColor(fromHexString: colorAtom.colorString) + display!.localTextColor = CGColor.fromHexString(colorAtom.colorString) // Check if we need to break before adding this colored content let shouldBreak = shouldBreakBeforeDisplay( @@ -1051,7 +1054,7 @@ extension Math { let display = Typesetter.createLineForMathList( colorboxAtom.innerList, font: font, style: style, maxWidth: maxWidth) - display!.localBackgroundColor = PlatformColor(fromHexString: colorboxAtom.colorString) + display!.localBackgroundColor = CGColor.fromHexString(colorboxAtom.colorString) // Check if we need to break before adding this colorbox let shouldBreak = shouldBreakBeforeDisplay( @@ -1387,7 +1390,7 @@ extension Math { let color = Typesetter.placeholderColor current = NSAttributedString( string: atom.nucleus, - attributes: [kCTForegroundColorAttributeName as NSAttributedString.Key: color.cgColor] + attributes: [kCTForegroundColorAttributeName as NSAttributedString.Key: color] ) } else { current = NSAttributedString(string: atom.nucleus) diff --git a/Sources/SwiftUIMath/Internal/Helpers/CGColor+HexString.swift b/Sources/SwiftUIMath/Internal/Helpers/CGColor+HexString.swift new file mode 100644 index 0000000..8c4beb6 --- /dev/null +++ b/Sources/SwiftUIMath/Internal/Helpers/CGColor+HexString.swift @@ -0,0 +1,20 @@ +import CoreGraphics +import Foundation + +extension CGColor { + static func fromHexString(_ hexString: String) -> CGColor? { + guard !hexString.isEmpty, hexString.hasPrefix("#") else { return nil } + + var rgbValue = UInt64(0) + let scanner = Scanner(string: hexString) + scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#") + guard scanner.scanHexInt64(&rgbValue) else { return nil } + + return CGColor( + srgbRed: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, + green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, + blue: CGFloat(rgbValue & 0x0000FF) / 255.0, + alpha: 1.0 + ) + } +} diff --git a/Sources/SwiftUIMath/Internal/Helpers/Platform.swift b/Sources/SwiftUIMath/Internal/Helpers/Platform.swift deleted file mode 100644 index e9bc9a8..0000000 --- a/Sources/SwiftUIMath/Internal/Helpers/Platform.swift +++ /dev/null @@ -1,31 +0,0 @@ -import SwiftUI - -#if canImport(UIKit) - typealias PlatformColor = UIColor - typealias PlatformBezierPath = UIBezierPath -#elseif canImport(AppKit) - typealias PlatformColor = NSColor - typealias PlatformBezierPath = NSBezierPath -#endif - -extension PlatformColor { - convenience init?(fromHexString hexString: String) { - self.init(hexString: hexString) - } - - convenience init?(hexString: String) { - guard !hexString.isEmpty, hexString.hasPrefix("#") else { return nil } - - var rgbValue = UInt64(0) - let scanner = Scanner(string: hexString) - scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#") - guard scanner.scanHexInt64(&rgbValue) else { return nil } - - self.init( - red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, - green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, - blue: CGFloat(rgbValue & 0x0000FF) / 255.0, - alpha: 1.0 - ) - } -}