GenerateStoreAssets.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import Foundation
  2. import CoreGraphics
  3. import CoreText
  4. import ImageIO
  5. import UniformTypeIdentifiers
  6. // Native vector layout around unmodified simulator screenshots. No fabricated app UI.
  7. let root = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
  8. let outputRoot = root.appendingPathComponent("AppStoreAssets")
  9. let english = CommandLine.arguments.contains("--english")
  10. let locale = english ? "en" : "zh-Hans"
  11. let rawPrefix = english ? "Screenshots/appstore-raw/en" : "Screenshots/appstore-raw"
  12. let space = CGColorSpace(name: CGColorSpace.sRGB)!
  13. struct Slide {
  14. let name: String, screen: String, tag: String, title: [String], subtitle: String, footer: String
  15. }
  16. let slides: [Slide] = english ? [
  17. .init(name: "01-classic", screen: "game", tag: "CLASSIC SUDOKU", title: ["One square.", "New possibilities."], subtitle: "Find a moment of focus, one number at a time.", footer: "Notes · Highlights · Hints & Undo"),
  18. .init(name: "02-difficulty", screen: "difficulty", tag: "FIVE DIFFICULTIES", title: ["Your level.", "Your challenge."], subtitle: "From Easy to Master, find your next challenge.", footer: "Easy · Medium · Hard · Expert · Master"),
  19. .init(name: "03-daily", screen: "daily", tag: "DAILY CHALLENGE", title: ["One puzzle a day.", "A star to earn."], subtitle: "Finish a daily puzzle. Light up your calendar.", footer: "Daily puzzles · Past dates to revisit"),
  20. .init(name: "04-continue", screen: "continue", tag: "SAVE YOUR PROGRESS", title: ["Take a break.", "Pick up your game."], subtitle: "Keep your unfinished puzzle ready for later.", footer: "Automatic saves · Offline play"),
  21. .init(name: "05-settings", screen: "settings", tag: "MAKE IT YOURS", title: ["Your settings.", "Your way to play."], subtitle: "Fine-tune the little things that help you focus.", footer: "English & Chinese · Highlights · Haptics")
  22. ] : [
  23. .init(name: "01-classic", screen: "game", tag: "经典数独", title: ["每一格", "都有新可能"], subtitle: "在数字之间,享受专注思考的片刻", footer: "候选笔记 · 关联高亮 · 提示与撤销"),
  24. .init(name: "02-difficulty", screen: "difficulty", tag: "五档难度", title: ["从第一题", "到新高度"], subtitle: "简单到大师,找到适合自己的挑战", footer: "简单 · 中等 · 困难 · 专家 · 大师"),
  25. .init(name: "03-daily", screen: "daily", tag: "每日挑战", title: ["每天一题", "点亮你的日历"], subtitle: "完成每日挑战,让坚持留下星标", footer: "每日固定题目 · 历史日期回顾"),
  26. .init(name: "04-continue", screen: "continue", tag: "进度接续", title: ["暂停一下", "随时继续"], subtitle: "保留未完成对局,接着上次的思考", footer: "自动保存进度 · 离线也能玩"),
  27. .init(name: "05-settings", screen: "settings", tag: "随心设置", title: ["顺手的设置", "专注的体验"], subtitle: "按自己的习惯,调整每一次操作", footer: "中英文切换 · 高亮设置 · 触感反馈")
  28. ]
  29. func color(_ hex: UInt32, alpha: CGFloat = 1) -> CGColor {
  30. CGColor(colorSpace: space, components: [CGFloat(hex >> 16 & 255) / 255, CGFloat(hex >> 8 & 255) / 255, CGFloat(hex & 255) / 255, alpha])!
  31. }
  32. func load(_ path: String) -> CGImage {
  33. let source = CGImageSourceCreateWithURL(root.appendingPathComponent(path) as CFURL, nil)!
  34. return CGImageSourceCreateImageAtIndex(source, 0, nil)!
  35. }
  36. func context(_ width: Int, _ height: Int) -> CGContext {
  37. let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!
  38. context.translateBy(x: 0, y: CGFloat(height)); context.scaleBy(x: 1, y: -1)
  39. context.interpolationQuality = .high
  40. return context
  41. }
  42. func write(_ image: CGImage, to url: URL) {
  43. let destination = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)!
  44. CGImageDestinationAddImage(destination, image, nil)
  45. precondition(CGImageDestinationFinalize(destination))
  46. }
  47. func drawImage(_ image: CGImage, in rect: CGRect, context c: CGContext) {
  48. c.saveGState(); c.translateBy(x: rect.minX, y: rect.maxY); c.scaleBy(x: 1, y: -1)
  49. c.draw(image, in: CGRect(origin: .zero, size: rect.size)); c.restoreGState()
  50. }
  51. func line(_ text: String, top: CGFloat, size: CGFloat, ink: UInt32, center: CGFloat = 603, maxWidth: CGFloat = 1100, semibold: Bool = false, tracking: CGFloat = 0, context c: CGContext) {
  52. var fontSize = size
  53. var result: CTLine!
  54. repeat {
  55. let fontName = english ? (semibold ? "AvenirNext-DemiBold" : "AvenirNext-Regular") : (semibold ? "PingFangSC-Semibold" : "PingFangSC-Regular")
  56. let font = CTFontCreateWithName(fontName as CFString, fontSize, nil)
  57. let string = NSAttributedString(string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font, NSAttributedString.Key(kCTForegroundColorAttributeName as String): color(ink), NSAttributedString.Key(kCTKernAttributeName as String): tracking])
  58. result = CTLineCreateWithAttributedString(string)
  59. if CTLineGetTypographicBounds(result, nil, nil, nil) <= maxWidth { break }
  60. fontSize -= 1
  61. } while fontSize > 10
  62. var ascent: CGFloat = 0
  63. let width = CGFloat(CTLineGetTypographicBounds(result, &ascent, nil, nil))
  64. c.saveGState(); c.translateBy(x: center - width / 2, y: top + ascent); c.scaleBy(x: 1, y: -1)
  65. c.textMatrix = .identity; c.textPosition = .zero; CTLineDraw(result, c); c.restoreGState()
  66. }
  67. let crown = load("JokerSudoku/Assets.xcassets/JokerCrown.imageset/JokerCrown.png")
  68. for family in ["iphone", "ipad"] {
  69. let isPad = family == "ipad"
  70. let width = isPad ? 2048 : 1206, height = isPad ? 2732 : 2622
  71. let scale = CGFloat(width) / 1206
  72. let h = CGFloat(height) / scale
  73. for (index, slide) in slides.enumerated() {
  74. let c = context(width, height)
  75. c.scaleBy(x: scale, y: scale)
  76. let gradient = CGGradient(colorsSpace: space, colors: [color(0xFBFCFE), color(0xE4EFFC)] as CFArray, locations: [0, 1])!
  77. c.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: 1206, y: h), options: [])
  78. c.setFillColor(color(0x338CF2, alpha: 0.07))
  79. c.fillEllipse(in: CGRect(x: 700, y: isPad ? 140 : 400, width: 1000, height: 1000))
  80. c.setFillColor(color(0xF2C45C, alpha: 0.10))
  81. c.fillEllipse(in: CGRect(x: -440, y: h - 800, width: 1000, height: 1000))
  82. // Quiet nine-box motif, behind the screenshot rather than over the app.
  83. c.saveGState(); c.translateBy(x: 50, y: h - 350); c.rotate(by: -.pi / 18)
  84. for number in 0...3 {
  85. let position = CGFloat(number) * 120
  86. c.move(to: CGPoint(x: position, y: 0)); c.addLine(to: CGPoint(x: position, y: 360))
  87. c.move(to: CGPoint(x: 0, y: position)); c.addLine(to: CGPoint(x: 360, y: position))
  88. }
  89. c.setStrokeColor(color(0x3979B9, alpha: 0.05)); c.setLineWidth(2); c.strokePath(); c.restoreGState()
  90. let brandY: CGFloat = isPad ? 35 : 66
  91. drawImage(crown, in: CGRect(x: 350, y: brandY, width: 64, height: 53), context: c)
  92. line("JOKER SUDOKU", top: brandY + 10, size: 27, ink: 0x29435E, center: 640, maxWidth: 460, semibold: true, tracking: 5, context: c)
  93. let kickerTop: CGFloat = isPad ? 104 : 174
  94. line(String(format: "%02d", index + 1) + " / " + slide.tag, top: kickerTop, size: isPad ? 18 : 22, ink: 0x327DD2, tracking: 2, context: c)
  95. let titleTop: CGFloat = isPad ? 146 : 242
  96. let titleSize: CGFloat = isPad ? 65 : 96
  97. for (row, title) in slide.title.enumerated() {
  98. line(title, top: titleTop + CGFloat(row) * (isPad ? 79 : 121), size: titleSize, ink: row == 1 ? 0x277DDE : 0x243F5B, semibold: true, tracking: 1, context: c)
  99. }
  100. line(slide.subtitle, top: isPad ? 320 : 516, size: isPad ? 24 : 29, ink: 0x60768D, maxWidth: 1050, context: c)
  101. let screenshot = load("\(rawPrefix)/\(family)/\(slide.screen).png")
  102. let imageWidth: CGFloat = isPad ? 862 : 852
  103. let imageHeight = imageWidth * CGFloat(screenshot.height) / CGFloat(screenshot.width)
  104. let rect = CGRect(x: (1206 - imageWidth) / 2, y: isPad ? 380 : 650, width: imageWidth, height: imageHeight)
  105. precondition(rect.maxY < h - 45, "Screenshot must fit without cropping")
  106. let outer = rect.insetBy(dx: -10, dy: -10)
  107. let rounding: CGFloat = isPad ? 24 : 58
  108. let bezel = CGPath(roundedRect: outer, cornerWidth: rounding + 9, cornerHeight: rounding + 9, transform: nil)
  109. c.saveGState(); c.setShadow(offset: CGSize(width: 0, height: 18), blur: 40, color: color(0x163D68, alpha: 0.18))
  110. c.addPath(bezel); c.setFillColor(color(0x1A304A)); c.fillPath(); c.restoreGState()
  111. c.saveGState()
  112. c.addPath(CGPath(roundedRect: rect, cornerWidth: rounding, cornerHeight: rounding, transform: nil)); c.clip()
  113. drawImage(screenshot, in: rect, context: c); c.restoreGState()
  114. line(slide.footer, top: h - (isPad ? 52 : 70), size: isPad ? 18 : 22, ink: 0x627A92, tracking: 1, context: c)
  115. let url = outputRoot.appendingPathComponent("\(locale)/\(family)/\(slide.name).png")
  116. write(c.makeImage()!, to: url)
  117. print("\(family)/\(slide.name).png \(width)x\(height)")
  118. }
  119. }
  120. // One contact sheet for reviewing the two complete five-image sets.
  121. let preview = context(1600, 1340)
  122. preview.setFillColor(color(0xFFFFFF)); preview.fill(CGRect(x: 0, y: 0, width: 1600, height: 1340))
  123. for (row, family) in ["iphone", "ipad"].enumerated() {
  124. let top: CGFloat = row == 0 ? 65 : 805
  125. line(family == "iphone" ? "iPhone · 1206 × 2622" : "iPad · 2048 × 2732", top: top - 48, size: 26, ink: 0x243F5B, center: 800, semibold: true, context: preview)
  126. for (column, slide) in slides.enumerated() {
  127. let image = load("AppStoreAssets/\(locale)/\(family)/\(slide.name).png")
  128. let w: CGFloat = 292
  129. drawImage(image, in: CGRect(x: 22 + CGFloat(column) * 316, y: top, width: w, height: w * CGFloat(image.height) / CGFloat(image.width)), context: preview)
  130. }
  131. }
  132. write(preview.makeImage()!, to: outputRoot.appendingPathComponent(english ? "preview-en.png" : "preview.png"))