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