37 lines
1.2 KiB
Swift
37 lines
1.2 KiB
Swift
import AppKit
|
|
import CoreText
|
|
import SwiftUI
|
|
|
|
struct DiagramError: Error {
|
|
let reason: String
|
|
}
|
|
|
|
func textSize(_ text: String, _ font: CTFont) -> CGSize {
|
|
let lines = text.components(separatedBy: "\n")
|
|
var width: CGFloat = 0
|
|
for l in lines {
|
|
let line = CTLineCreateWithAttributedString(NSAttributedString(string: l, attributes: [.font: font as NSFont]))
|
|
width = max(width, CGFloat(CTLineGetTypographicBounds(line, nil, nil, nil)))
|
|
}
|
|
let lineHeight = CTFontGetAscent(font) + CTFontGetDescent(font) + CTFontGetLeading(font) + 3
|
|
return CGSize(width: ceil(width), height: ceil(lineHeight * CGFloat(lines.count)))
|
|
}
|
|
|
|
struct DiagramFailure: View {
|
|
let source: String
|
|
let reason: String
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(verbatim: source)
|
|
.textSelection(.enabled)
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 6)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.background(Palette.surface, in: RoundedRectangle(cornerRadius: 6))
|
|
Text(verbatim: "diagram not rendered: \(reason)")
|
|
.foregroundStyle(Palette.muted)
|
|
}
|
|
}
|
|
}
|