Scripts/screenshots-iphone-compose.swift (3631B)
1
2
3import AppKit
4import CoreGraphics
5import CoreText
6import Foundation
7import ImageIO
8
9guard CommandLine.arguments.count >= 4 else {
10 fputs("Usage: screenshots-iphone-compose.swift <framed.png> <output.png> <caption>\n", stderr)
11 exit(1)
12}
13
14let inputPath = CommandLine.arguments[1]
15let outputPath = CommandLine.arguments[2]
16let text = CommandLine.arguments[3]
17
18
19let canvasWidth: CGFloat = 1320
20let canvasHeight: CGFloat = 2868
21
22
23let backgroundColor = CGColor(red: 0xF5 / 255.0, green: 0xF0 / 255.0, blue: 0xE4 / 255.0, alpha: 1)
24let textColor = NSColor(calibratedRed: 0x1B / 255.0, green: 0x1C / 255.0, blue: 0x1F / 255.0, alpha: 1)
25
26guard let dataProvider = CGDataProvider(filename: inputPath),
27 let deviceImage = CGImage(
28 pngDataProviderSource: dataProvider,
29 decode: nil,
30 shouldInterpolate: true,
31 intent: .defaultIntent
32 )
33else {
34 fputs("Failed to load image: \(inputPath)\n", stderr)
35 exit(1)
36}
37
38let deviceWidth = CGFloat(deviceImage.width)
39let deviceHeight = CGFloat(deviceImage.height)
40
41guard
42 let context = CGContext(
43 data: nil,
44 width: Int(canvasWidth),
45 height: Int(canvasHeight),
46 bitsPerComponent: 8,
47 bytesPerRow: 0,
48 space: CGColorSpaceCreateDeviceRGB(),
49 bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
50 )
51else {
52 fputs("Failed to create graphics context\n", stderr)
53 exit(1)
54}
55
56context.setFillColor(backgroundColor)
57context.fill(CGRect(x: 0, y: 0, width: canvasWidth, height: canvasHeight))
58
59let sidePadding: CGFloat = 70
60let bottomPadding: CGFloat = 80
61let topReserved: CGFloat = 330
62let maxDeviceWidth = canvasWidth - sidePadding * 2
63let maxDeviceHeight = canvasHeight - bottomPadding - topReserved
64let scale = min(maxDeviceWidth / deviceWidth, maxDeviceHeight / deviceHeight, 1.0)
65let scaledWidth = deviceWidth * scale
66let scaledHeight = deviceHeight * scale
67let deviceX = (canvasWidth - scaledWidth) / 2
68let deviceY = bottomPadding
69
70context.interpolationQuality = .high
71context.draw(
72 deviceImage,
73 in: CGRect(x: deviceX, y: deviceY, width: scaledWidth, height: scaledHeight)
74)
75
76
77let fontSize = canvasWidth * 0.066
78let baseFont = NSFont.systemFont(ofSize: fontSize, weight: .medium)
79let font =
80 baseFont.fontDescriptor.withDesign(.serif).flatMap { NSFont(descriptor: $0, size: fontSize) }
81 ?? baseFont
82let attributes: [NSAttributedString.Key: Any] = [
83 .font: font,
84 .foregroundColor: textColor,
85]
86let attrString = NSAttributedString(string: text, attributes: attributes)
87let ctLine = CTLineCreateWithAttributedString(attrString)
88let textBounds = CTLineGetBoundsWithOptions(ctLine, .useOpticalBounds)
89
90let textAreaTop = canvasHeight - 40
91let textAreaBottom = deviceY + scaledHeight
92let textX = (canvasWidth - textBounds.width) / 2 - textBounds.origin.x
93let textY = (textAreaTop + textAreaBottom) / 2 - textBounds.height / 2 - textBounds.origin.y
94
95context.textPosition = CGPoint(x: textX, y: textY)
96CTLineDraw(ctLine, context)
97
98guard let resultImage = context.makeImage(),
99 let destination = CGImageDestinationCreateWithURL(
100 URL(fileURLWithPath: outputPath) as CFURL,
101 "public.png" as CFString,
102 1,
103 nil
104 )
105else {
106 fputs("Failed to create output\n", stderr)
107 exit(1)
108}
109
110CGImageDestinationAddImage(destination, resultImage, nil)
111guard CGImageDestinationFinalize(destination) else {
112 fputs("Failed to write output\n", stderr)
113 exit(1)
114}