Add display model
This commit is contained in:
44
Sources/SwiftUIMath/Internal/Display/DisplayList.swift
Normal file
44
Sources/SwiftUIMath/Internal/Display/DisplayList.swift
Normal file
@@ -0,0 +1,44 @@
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
|
||||
extension Math {
|
||||
final class DisplayList: DisplayNode {
|
||||
enum LinePosition: Int {
|
||||
case regular
|
||||
case `subscript`
|
||||
case superscript
|
||||
}
|
||||
|
||||
var linePosition: LinePosition = .regular
|
||||
var children: [DisplayNode] = []
|
||||
var index: Int = NSNotFound
|
||||
|
||||
init(children: [DisplayNode], range: NSRange) {
|
||||
super.init()
|
||||
self.children = children
|
||||
self.position = .zero
|
||||
self.index = NSNotFound
|
||||
self.range = range
|
||||
recomputeDimensions()
|
||||
}
|
||||
|
||||
func recomputeDimensions() {
|
||||
var maxAscent: CGFloat = 0
|
||||
var maxDescent: CGFloat = 0
|
||||
var maxWidth: CGFloat = 0
|
||||
for node in children {
|
||||
let ascent = max(0, node.position.y + node.ascent)
|
||||
maxAscent = max(maxAscent, ascent)
|
||||
|
||||
let descent = max(0, 0 - (node.position.y - node.descent))
|
||||
maxDescent = max(maxDescent, descent)
|
||||
|
||||
let width = node.width + node.position.x
|
||||
maxWidth = max(maxWidth, width)
|
||||
}
|
||||
self.ascent = maxAscent
|
||||
self.descent = maxDescent
|
||||
self.width = maxWidth
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user