Repository Public
1// SP Notes iOS2// File: Editor/AtomRenderer.swift3// Description: Native rendering of document atoms as attachment images: pictures, files, tables, equations, rules, embeds, mentions,4// and a neutral chip for node types this app does not edit (they are kept untouched in the document).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import UIKit9import SwiftMath10import SPNotesCore1112@MainActor13final class AtomRenderer {14 let theme: EditorTheme15 /// Returns the local picture for an image `src` (`/api/files/<id>`), or nil when it is not on the device yet.16 var imageProvider: (String) -> UIImage? = { _ in nil }17 /// Called once per missing picture so the owner can download it and then call `invalidate(src:)`.18 var onMissingImage: (String) -> Void = { _ in }19 private var requested = Set<String>()20 /// 2.0: what the cards of the 3.0 blocks draw from outside the document (see `BlockCardBuilder`).21 let cards = BlockCardStore()22 /// Appearance and Dynamic Type size the atoms are drawn for (the text view's); nil = the current trait collection.23 var traits: UITraitCollection?2425 init(theme: EditorTheme = .shared) { self.theme = theme }2627 func forgetRequests() { requested.removeAll(); cards.forgetRequests() }2829 /// Sets the attachment image and bounds for the given width. Cheap when nothing changed.30 func prepare(_ atom: AtomAttachment, availableWidth: CGFloat, baseFont: UIFont) {31 (traits ?? .current).performAsCurrent { prepareNow(atom, availableWidth: availableWidth, baseFont: baseFont) }32 }3334 private func prepareNow(_ atom: AtomAttachment, availableWidth: CGFloat, baseFont: UIFont) {35 if atom.isBlock {36 if atom.image != nil, abs(atom.renderedWidth - availableWidth) < 0.5, !needsReload(atom) { return }37 let image = renderBlock(atom.node, width: availableWidth)38 atom.image = image39 atom.bounds = CGRect(origin: .zero, size: image.size)40 atom.renderedWidth = availableWidth41 atom.isPlaceholder = atom.node.type == "image" && imageProvider(atom.node.stringAttr("src") ?? "") == nil42 } else {43 let (image, descent) = renderInline(atom.node, font: baseFont)44 atom.image = image45 atom.bounds = CGRect(x: 0, y: -descent, width: image.size.width, height: image.size.height)46 }47 }4849 /// An image atom that was drawn as a placeholder and whose picture has arrived since.50 private func needsReload(_ atom: AtomAttachment) -> Bool {51 guard atom.node.type == "image", let src = atom.node.stringAttr("src") else { return false }52 return atom.isPlaceholder && imageProvider(src) != nil53 }5455 // MARK: block atoms5657 private func renderBlock(_ node: PMNode, width: CGFloat) -> UIImage {58 switch node.type {59 case "image": return renderImage(node, width: width)60 case "fileBlock": return chip(icon: Self.fileIcon(mime: node.stringAttr("mime") ?? "", name: node.stringAttr("name") ?? ""), title: node.stringAttr("name") ?? String(localized: "Fichier"), subtitle: Self.byteCount(node.intAttr("size")), width: width)61 case "horizontalRule": return rule(width: width)62 case "blockMath": return math(node.stringAttr("latex") ?? "", width: width)63 case "table": return table(node, width: width)64 case "youtube": return chip(icon: "play.rectangle.fill", title: String(localized: "Vidéo"), subtitle: node.stringAttr("src") ?? "", width: width)65 default:66 // 2.0: the blocks of SPEC §2 are drawn as designed cards (Editor/BlockCards.swift); anything else stays a neutral chip67 if let card = renderCard(node, width: width) { return card }68 return chip(icon: "square.dashed", title: String(localized: "Bloc « \(node.type) »"), subtitle: String(localized: "Conservé tel quel — modifiable sur le web"), width: width)69 }70 }7172 private func renderImage(_ node: PMNode, width: CGFloat) -> UIImage {73 let src = node.stringAttr("src") ?? ""74 if let picture = imageProvider(src) {75 let natural = picture.size76 guard natural.width > 0, natural.height > 0 else { return placeholder(width: width, symbol: "photo") }77 var size = natural.width > width ? CGSize(width: width, height: natural.height * width / natural.width) : natural78 let maxHeight: CGFloat = 52079 if size.height > maxHeight { size = CGSize(width: size.width * maxHeight / size.height, height: maxHeight) }80 size = CGSize(width: max(1, size.width.rounded()), height: max(1, size.height.rounded()))81 let renderer = UIGraphicsImageRenderer(size: CGSize(width: size.width, height: size.height + 8))82 return renderer.image { _ in83 let rect = CGRect(x: 0, y: 4, width: size.width, height: size.height)84 UIBezierPath(roundedRect: rect, cornerRadius: 10).addClip()85 picture.draw(in: rect)86 }87 }88 if !src.isEmpty, !requested.contains(src) { requested.insert(src); onMissingImage(src) }89 return placeholder(width: width, symbol: "photo")90 }9192 private func placeholder(width: CGFloat, symbol: String) -> UIImage {93 let size = CGSize(width: width, height: min(200, width * 0.56))94 return UIGraphicsImageRenderer(size: size).image { _ in95 let rect = CGRect(origin: .zero, size: size).insetBy(dx: 0, dy: 4)96 theme.surface.setFill(); UIBezierPath(roundedRect: rect, cornerRadius: 10).fill()97 if let icon = UIImage(systemName: symbol, withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .regular))?.withTintColor(theme.muted, renderingMode: .alwaysOriginal) {98 icon.draw(at: CGPoint(x: rect.midX - icon.size.width / 2, y: rect.midY - icon.size.height / 2))99 }100 }101 }102103 private func chip(icon: String, title: String, subtitle: String, width: CGFloat) -> UIImage {104 let height: CGFloat = 60105 let size = CGSize(width: width, height: height + 8)106 return UIGraphicsImageRenderer(size: size).image { _ in107 let rect = CGRect(x: 0.5, y: 4.5, width: width - 1, height: height - 1)108 let path = UIBezierPath(roundedRect: rect, cornerRadius: 12)109 theme.surface.setFill(); path.fill()110 theme.border.setStroke(); path.lineWidth = 1; path.stroke()111 if let image = UIImage(systemName: icon, withConfiguration: UIImage.SymbolConfiguration(pointSize: 22, weight: .medium))?.withTintColor(theme.accent, renderingMode: .alwaysOriginal) {112 image.draw(at: CGPoint(x: rect.minX + 14, y: rect.midY - image.size.height / 2))113 }114 let textX = rect.minX + 52115 let textWidth = rect.maxX - textX - 12116 let titleFont = UIFont.preferredFont(forTextStyle: .subheadline).withTraits(.traitBold)117 let paragraph = NSMutableParagraphStyle(); paragraph.lineBreakMode = .byTruncatingMiddle118 (title as NSString).draw(in: CGRect(x: textX, y: rect.minY + (subtitle.isEmpty ? 20 : 10), width: textWidth, height: 22), withAttributes: [.font: titleFont, .foregroundColor: theme.text, .paragraphStyle: paragraph])119 if !subtitle.isEmpty {120 (subtitle as NSString).draw(in: CGRect(x: textX, y: rect.minY + 32, width: textWidth, height: 18), withAttributes: [.font: UIFont.preferredFont(forTextStyle: .caption1), .foregroundColor: theme.secondary, .paragraphStyle: paragraph])121 }122 }123 }124125 private func rule(width: CGFloat) -> UIImage {126 UIGraphicsImageRenderer(size: CGSize(width: width, height: 22)).image { _ in127 theme.border.setFill()128 UIBezierPath(roundedRect: CGRect(x: 0, y: 10, width: width, height: 1.5), cornerRadius: 0.75).fill()129 }130 }131132 private func math(_ latex: String, width: CGFloat) -> UIImage {133 let fontSize = UIFontMetrics(forTextStyle: .body).scaledValue(for: 19)134 var typeset = MathImage(latex: MathSource.normalize(latex), fontSize: fontSize, textColor: theme.text, labelMode: .display, textAlignment: .center)135 let (error, image, _) = typeset.asImage()136 guard error == nil, let image, image.size.width > 0, image.size.height > 0 else {137 // Not parseable: show the source so nothing is hidden.138 return chip(icon: "sum", title: latex.isEmpty ? String(localized: "Équation vide") : latex, subtitle: String(localized: "LaTeX non interprété"), width: width)139 }140 let scale = min(1, width / image.size.width)141 let drawn = CGSize(width: image.size.width * scale, height: image.size.height * scale)142 return UIGraphicsImageRenderer(size: CGSize(width: width, height: drawn.height + 20)).image { _ in143 image.draw(in: CGRect(x: (width - drawn.width) / 2, y: 10, width: drawn.width, height: drawn.height))144 }145 }146147 private func table(_ node: PMNode, width: CGFloat) -> UIImage {148 let grid = TableGrid(node: node)149 guard grid.columnCount > 0, !grid.rows.isEmpty else { return chip(icon: "tablecells", title: String(localized: "Tableau vide"), subtitle: "", width: width) }150 let font = UIFont.preferredFont(forTextStyle: .footnote)151 let bold = font.withTraits(.traitBold)152 let padding: CGFloat = 8153 let columnWidth = max(64, (width - 1) / CGFloat(grid.columnCount))154 let fits = columnWidth * CGFloat(grid.columnCount) <= width + 0.5155 let totalWidth = fits ? width : columnWidth * CGFloat(grid.columnCount)156 // row heights from the tallest cell157 var heights: [CGFloat] = []158 for row in grid.rows {159 var h: CGFloat = font.lineHeight160 for cell in row {161 let bounds = (cell.text as NSString).boundingRect(with: CGSize(width: columnWidth - 2 * padding, height: 400), options: [.usesLineFragmentOrigin], attributes: [.font: cell.isHeader ? bold : font], context: nil)162 h = max(h, ceil(bounds.height))163 }164 heights.append(min(h, 160) + 2 * padding)165 }166 let totalHeight = heights.reduce(0, +)167 let scale = fits ? 1 : width / totalWidth // a table wider than the screen is shown reduced; the table editor shows it at full size168 let size = CGSize(width: width, height: totalHeight * scale + 10)169 return UIGraphicsImageRenderer(size: size).image { context in170 context.cgContext.translateBy(x: 0, y: 5)171 context.cgContext.scaleBy(x: scale, y: scale)172 let outer = UIBezierPath(roundedRect: CGRect(x: 0.5, y: 0.5, width: totalWidth - 1, height: totalHeight - 1), cornerRadius: 8)173 context.cgContext.saveGState(); outer.addClip()174 var y: CGFloat = 0175 for (r, row) in grid.rows.enumerated() {176 for (c, cell) in row.enumerated() {177 let rect = CGRect(x: CGFloat(c) * columnWidth, y: y, width: columnWidth, height: heights[r])178 if cell.isHeader { theme.surface.setFill(); UIRectFill(rect) }179 (cell.text as NSString).draw(with: rect.insetBy(dx: padding, dy: padding), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font: cell.isHeader ? bold : font, .foregroundColor: theme.text], context: nil)180 }181 y += heights[r]182 }183 context.cgContext.restoreGState()184 theme.border.setStroke()185 let lines = UIBezierPath()186 y = 0187 for h in heights.dropLast() { y += h; lines.move(to: CGPoint(x: 0, y: y)); lines.addLine(to: CGPoint(x: totalWidth, y: y)) }188 for c in 1..<max(1, grid.columnCount) { let x = CGFloat(c) * columnWidth; lines.move(to: CGPoint(x: x, y: 0)); lines.addLine(to: CGPoint(x: x, y: totalHeight)) }189 lines.lineWidth = 1; lines.stroke()190 outer.lineWidth = 1; outer.stroke()191 }192 }193194 // MARK: inline atoms195196 private func renderInline(_ node: PMNode, font: UIFont) -> (UIImage, CGFloat) {197 switch node.type {198 case "mention":199 return pill("@" + (node.stringAttr("label") ?? node.stringAttr("id") ?? ""), font: font, color: theme.accent, background: theme.accent.withAlphaComponent(0.12))200 case "inlineMath", "math":201 let latex = node.stringAttr("latex") ?? ""202 var typeset = MathImage(latex: MathSource.normalize(latex), fontSize: font.pointSize, textColor: theme.text, labelMode: .text, textAlignment: .left)203 let (error, image, info) = typeset.asImage()204 if error == nil, let image, image.size.width > 0 { return (image, info?.descent ?? 0) }205 return pill("$" + latex + "$", font: theme.code, color: theme.secondary, background: theme.codeBackground)206 case "citation":207 return citationChip(node, font: font)208 case "footnoteRef":209 return pill("[" + (node.stringAttr("id") ?? "?") + "]", font: font.withSize(font.pointSize * 0.75), color: theme.accent, background: .clear)210 default:211 return pill("⟨" + node.type + "⟩", font: font, color: theme.secondary, background: theme.surface)212 }213 }214215 private func pill(_ text: String, font: UIFont, color: UIColor, background: UIColor) -> (UIImage, CGFloat) {216 let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color]217 let textSize = (text as NSString).size(withAttributes: attributes)218 let size = CGSize(width: ceil(textSize.width) + 8, height: ceil(font.lineHeight))219 let image = UIGraphicsImageRenderer(size: size).image { _ in220 background.setFill(); UIBezierPath(roundedRect: CGRect(origin: .zero, size: size).insetBy(dx: 0, dy: 1), cornerRadius: 5).fill()221 (text as NSString).draw(at: CGPoint(x: 4, y: (size.height - textSize.height) / 2), withAttributes: attributes)222 }223 return (image, -font.descender)224 }225226 // MARK: helpers227228 static func fileIcon(mime: String, name: String) -> String {229 let lower = name.lowercased()230 if lower.hasSuffix(".pkdrawing") { return "pencil.and.scribble" }231 if mime == "application/pdf" || lower.hasSuffix(".pdf") { return "doc.richtext.fill" }232 if mime.hasPrefix("image/") { return "photo.fill" }233 if mime.hasPrefix("audio/") { return "waveform" }234 if mime.hasPrefix("video/") { return "film.fill" }235 if mime.hasPrefix("text/") || lower.hasSuffix(".md") || lower.hasSuffix(".txt") { return "doc.text.fill" }236 if lower.hasSuffix(".zip") { return "doc.zipper" }237 return "doc.fill"238 }239240 static func byteCount(_ bytes: Int?) -> String {241 guard let bytes, bytes > 0 else { return "" }242 return ByteCountFormatter.string(fromByteCount: Int64(bytes), countStyle: .file)243 }244}245246/// Read-only view of a table node as a grid of plain-text cells (for drawing and for the table editor).247struct TableGrid {248 struct Cell { var text: String; var isHeader: Bool }249 var rows: [[Cell]] = []250 var columnCount: Int { rows.map(\.count).max() ?? 0 }251252 init(node: PMNode) {253 for row in node.content ?? [] where row.type == "tableRow" {254 rows.append((row.content ?? []).map { Cell(text: Self.text(of: $0), isHeader: $0.type == "tableHeader") })255 }256 }257258 static func text(of node: PMNode) -> String {259 if let text = node.text { return text }260 if node.type == "hardBreak" { return "\n" }261 if node.type == "mention" { return "@" + (node.stringAttr("label") ?? "") }262 if node.type == "inlineMath" { return node.stringAttr("latex") ?? "" }263 let children = node.content ?? []264 let isBlockContainer = children.contains { $0.text == nil && $0.type != "hardBreak" && $0.type != "mention" && $0.type != "inlineMath" && $0.type != "footnoteRef" }265 return children.map(text(of:)).joined(separator: isBlockContainer ? "\n" : "")266 }267}268269enum MathSource {270 /// A few constructs the typesetter does not know are mapped to equivalents.271 static func normalize(_ source: String) -> String {272 var t = source.trimmingCharacters(in: .whitespacesAndNewlines)273 for (a, b) in [("\\dfrac", "\\frac"), ("\\tfrac", "\\frac"), ("\\displaystyle", ""), ("\\textstyle", ""), ("\\nonumber", ""), ("\\notag", ""), ("\\lvert", "|"), ("\\rvert", "|"), ("\\lVert", "\\|"), ("\\rVert", "\\|"), ("\\operatorname", "\\mathrm"), ("\\boldsymbol", "\\mathbf"), ("\\bm", "\\mathbf")] {274 t = t.replacingOccurrences(of: a, with: b)275 }276 for env in ["align*", "align", "equation*", "equation", "gather*", "gather"] {277 t = t.replacingOccurrences(of: "\\begin{\(env)}", with: "\\begin{aligned}").replacingOccurrences(of: "\\end{\(env)}", with: "\\end{aligned}")278 }279 return t280 }281}282283extension PMNode {284 func stringAttr(_ key: String) -> String? { if case .string(let s)? = attrs?[key] { return s } else { return nil } }285 func intAttr(_ key: String) -> Int? {286 switch attrs?[key] { case .int(let i)?: return Int(i); case .double(let d)?: return Int(d); default: return nil }287 }288 func boolAttr(_ key: String) -> Bool? { if case .bool(let b)? = attrs?[key] { return b } else { return nil } }289}290291extension UIFont {292 func withTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont {293 guard let descriptor = fontDescriptor.withSymbolicTraits(fontDescriptor.symbolicTraits.union(traits)) else { return self }294 return UIFont(descriptor: descriptor, size: pointSize)295 }296}297