Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

3.7 KB
Raw Blame
// 生成网站的分享图(Open Graph,1200×630):左侧图标与标题,右侧露出一部分真实截图。
//
//   swift tools/site/og.swift
//
// 输出 docs/assets/og.png(简体中文)、og-zh-hant.png(繁体中文,台湾与香港共用)与 og-en.png(英文)。

import AppKit

let W: CGFloat = 1200, H: CGFloat = 630
let paper = NSColor(srgbRed: 0xF5 / 255.0, green: 0xF5 / 255.0, blue: 0xF4 / 255.0, alpha: 1)
let ink = NSColor(srgbRed: 0x1D / 255.0, green: 0x1D / 255.0, blue: 0x1F / 255.0, alpha: 1)
let dim = NSColor(srgbRed: 0x6E / 255.0, green: 0x6E / 255.0, blue: 0x73 / 255.0, alpha: 1)

// lang 交给 Core Text 选中文字体:繁体用苹方的繁体版,标点的位置也随之不同
func render(name: String, title: String, note: String, lang: String = "",
            shot: String = "docs/assets/screenshots/history.png", to path: String) throws {
    let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(W), pixelsHigh: Int(H),
                               bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
                               colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
    defer { NSGraphicsContext.restoreGraphicsState() }

    paper.setFill()
    NSRect(x: 0, y: 0, width: W, height: H).fill()

    // 右侧截图:带窗口外框的那张,向右下出血
    if let shot = NSImage(contentsOfFile: shot) {
        let h: CGFloat = 560
        let w = h * shot.size.width / shot.size.height
        shot.draw(in: NSRect(x: 600, y: H - 70 - h, width: w, height: h))
    }

    // 左侧:图标、产品名、一句话、注脚(坐标按左上原点换算)
    func y(_ top: CGFloat, _ height: CGFloat) -> CGFloat { H - top - height }
    NSImage(contentsOfFile: "assets/brand/icon-1024.png")?
        .draw(in: NSRect(x: 64, y: y(72, 112), width: 112, height: 112))

    let brand = NSAttributedString(string: name, attributes: [
        .font: NSFont.systemFont(ofSize: 34, weight: .semibold), .foregroundColor: ink,
    ])
    brand.draw(at: NSPoint(x: 80, y: y(200, 42)))

    let para = NSMutableParagraphStyle()
    para.lineSpacing = 6
    let headline = NSAttributedString(string: title, attributes: [
        .font: NSFont.systemFont(ofSize: 54, weight: .bold), .foregroundColor: ink,
        .paragraphStyle: para, .kern: -1, NSAttributedString.Key("NSLanguage"): lang,
    ])
    headline.draw(in: NSRect(x: 80, y: y(262, 150), width: 540, height: 150))

    let foot = NSAttributedString(string: note, attributes: [
        .font: NSFont.systemFont(ofSize: 22, weight: .regular), .foregroundColor: dim,
        NSAttributedString.Key("NSLanguage"): lang,
    ])
    foot.draw(at: NSPoint(x: 80, y: y(470, 28)))

    try rep.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: path))
}

try render(name: "隔空贴 PasteFerry", title: "电脑与手机,\n内容随手互通。",
           note: "剪贴板 · 历史 · 文件传送 · 免费开源",
           to: "docs/assets/og.png")
try render(name: "隔空貼 PasteFerry", title: "電腦與手機,\n內容隨手互通。",
           note: "剪貼簿 · 歷史 · 檔案傳送 · 免費開源", lang: "zh-Hant",
           shot: "docs/assets/screenshots/zh-tw/history.png",
           to: "docs/assets/og-zh-hant.png")
try render(name: "PasteFerry", title: "Computers. Phones.\nWithin reach.",
           note: "Clipboard · History · File transfer · Free",
           shot: "docs/assets/screenshots/en/history.png",
           to: "docs/assets/og-en.png")
print("✓ docs/assets/og.png, og-zh-hant.png, og-en.png")