-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2DContext.swift
More file actions
147 lines (105 loc) · 5.19 KB
/
Solution2DContext.swift
File metadata and controls
147 lines (105 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Solution2DContext.swift
// Advent of Code 2024 Visualization
//
// Created by Stephen H. Gerstacker on 2023-11-26.
// SPDX-License-Identifier: MIT
//
import AVFoundation
import Combine
import Foundation
open class Solution2DContext: SolutionContext {
// MARK: - Properties
private var lastPixelBuffer: CVPixelBuffer? = nil
// MARK: - Drawing
public func discard(context: CGContext, pixelBuffer: CVPixelBuffer) {
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
}
public func nextContext() throws -> (CGContext, CVPixelBuffer) {
guard let pool = writerAdaptor?.pixelBufferPool else {
throw SolutionError.apiError("No pixel buffer pool available")
}
var pixelBuffer: CVPixelBuffer? = nil
CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBuffer)
guard let pixelBuffer else {
throw SolutionError.apiError("Pixel buffer pool is out of pixel buffers")
}
CVPixelBufferLockBaseAddress(pixelBuffer, [])
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let stride = CVPixelBufferGetBytesPerRow(pixelBuffer)
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(
data: baseAddress,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: stride,
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue + CGBitmapInfo.byteOrder32Little.rawValue
) else {
throw SolutionError.apiError("Failed to create context from pixel buffer")
}
context.setAllowsFontSmoothing(true)
context.setShouldSmoothFonts(true)
return (context, pixelBuffer)
}
public func repeatLastFrame() {
guard let pixelBuffer = lastPixelBuffer else { return }
submit(pixelBuffer: pixelBuffer)
}
public func submit(context: CGContext, pixelBuffer: CVPixelBuffer) {
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
submit(pixelBuffer: pixelBuffer)
}
override func submit(pixelBuffer: CVPixelBuffer) {
self.lastPixelBuffer = pixelBuffer
super.submit(pixelBuffer: pixelBuffer)
}
// MARK: - Drawing Utilities
public func draw(text: String, color: CGColor, font: NativeFont, rect: CGRect, in context: CGContext) {
let finalRect = CGRect(x: rect.origin.x, y: CGFloat(context.height) - rect.origin.y - rect.size.height, width: rect.size.width, height: rect.size.height)
let textAttributed = NSAttributedString(string: text)
let cfText = CFAttributedStringCreateMutableCopy(kCFAllocatorDefault, text.count, textAttributed)!
let cfTextLength = CFAttributedStringGetLength(cfText)
let textRange = CFRange(location: 0, length: cfTextLength)
CFAttributedStringSetAttribute(cfText, textRange, kCTFontAttributeName, font)
CFAttributedStringSetAttribute(cfText, textRange, kCTForegroundColorAttributeName, color)
let maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
let frameSetter = CTFramesetterCreateWithAttributedString(cfText)
let textSize = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, textRange, nil, maxSize, nil)
let xOffset = (finalRect.width - textSize.width) / 2.0
let yOffset = (finalRect.height - textSize.height) / 2.0
let centeredFrame = finalRect.insetBy(dx: xOffset, dy: yOffset)
let path = CGMutablePath()
path.addRect(centeredFrame)
let ctFrame = CTFramesetterCreateFrame(frameSetter, textRange, path, nil)
CTFrameDraw(ctFrame, context)
}
public func offscreenImage(draw: (CGContext) -> Void) -> CGImage? {
guard let context = CGContext(
data: nil,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: 0, space:
CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue + CGBitmapInfo.byteOrder32Little.rawValue
) else {
return nil
}
draw(context)
return context.makeImage()
}
public func fill(rect: CGRect, color: CGColor, in context: CGContext) {
let finalRect = CGRect(x: rect.origin.x, y: CGFloat(context.height) - rect.origin.y - rect.size.height, width: rect.size.width, height: rect.size.height)
context.setFillColor(color)
context.fill(finalRect)
}
public func stroke(rect: CGRect, color: CGColor, in context: CGContext) {
let finalRect = CGRect(x: rect.origin.x, y: CGFloat(context.height) - rect.origin.y - rect.size.height, width: rect.size.width, height: rect.size.height)
context.setStrokeColor(color)
context.stroke(finalRect)
}
}