import AppKit /// Decoded images by file path, shared by every image of the page. public final class ImageCache { private var images: [String: NSImage] = [:] public init() {} /// The image at `url`; a missing or unreadable file is not kept, so it shows once it exists. func image(at url: URL) -> NSImage? { if let image = images[url.path] { return image } guard let image = NSImage(contentsOf: url), image.isValid, image.size.width > 0, image.size.height > 0 else { return nil } images[url.path] = image return image } } /// An image in the page: one character that knows the path of its image line. final class ImageAttachment: NSTextAttachment { /// Relative to the page file folder, as `images/`. let path: String init(path: String, image: NSImage?, font: NSFont) { self.path = path super.init(data: nil, ofType: nil) attachmentCell = image.map { PageImageCell(imageCell: $0) } ?? MissingImageCell(font: font) } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") } var isMissing: Bool { attachmentCell is MissingImageCell } } /// An image at its own size, scaled down to the width of the line, never up. final class PageImageCell: NSTextAttachmentCell { static func fitted(_ size: NSSize, width: CGFloat) -> NSSize { guard size.width > width, size.width > 0, width > 0 else { return size } return NSSize(width: floor(width), height: floor(size.height * width / size.width)) } override func cellSize() -> NSSize { image?.size ?? .zero } override func cellBaselineOffset() -> NSPoint { .zero } override func cellFrame(for textContainer: NSTextContainer, proposedLineFragment lineFrag: NSRect, glyphPosition position: NSPoint, characterIndex charIndex: Int) -> NSRect { NSRect(origin: .zero, size: Self.fitted(cellSize(), width: lineFrag.width - max(0, position.x))) } override func draw(withFrame cellFrame: NSRect, in controlView: NSView?) { image?.draw(in: cellFrame, from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: [.interpolation: NSImageInterpolation.high.rawValue]) } override func draw(withFrame cellFrame: NSRect, in controlView: NSView?, characterIndex charIndex: Int, layoutManager: NSLayoutManager) { draw(withFrame: cellFrame, in: controlView) } override func wantsToTrackMouse() -> Bool { false } } /// The `image missing` text in place of an image whose file is gone. final class MissingImageCell: NSTextAttachmentCell { static let label = "image missing" private(set) var text: NSAttributedString init(font: NSFont) { text = Self.text(font) super.init(textCell: "") } @available(*, unavailable) required init(coder: NSCoder) { fatalError("init(coder:) is not supported") } func setFont(_ font: NSFont) { text = Self.text(font) } private static func text(_ font: NSFont) -> NSAttributedString { NSAttributedString(string: label, attributes: [.font: font, .foregroundColor: Theme.inkMuted]) } override func cellSize() -> NSSize { let size = text.size() return NSSize(width: ceil(size.width), height: ceil(size.height)) } /// Puts the label's baseline on the line's baseline. override func cellBaselineOffset() -> NSPoint { let font = text.attribute(.font, at: 0, effectiveRange: nil) as? NSFont return NSPoint(x: 0, y: font?.descender ?? 0) } override func draw(withFrame cellFrame: NSRect, in controlView: NSView?) { text.draw(in: cellFrame) } override func draw(withFrame cellFrame: NSRect, in controlView: NSView?, characterIndex charIndex: Int, layoutManager: NSLayoutManager) { draw(withFrame: cellFrame, in: controlView) } override func wantsToTrackMouse() -> Bool { false } }