| ... | ... | @@ -0,0 +1,182 @@ |
| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | // Renders the Android adaptive-icon layers from the Icon Composer SVG sources. |
| 4 | // |
| 5 | // Icon Composer exports already contain a rounded-square silhouette, and Android masks |
| 6 | // the central 72dp of a 108dp adaptive canvas, so exporting them as a foreground produces |
| 7 | // a double-framed icon with the letters cropped by the mask. Instead, each variant gets a |
| 8 | // full-bleed background layer (the artwork behind the wordmark) and a shared transparent |
| 9 | // foreground that keeps the wordmark inside the safe zone. |
| 10 | |
| 11 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 12 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 13 | import * as Console from "effect/Console"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import * as FileSystem from "effect/FileSystem"; |
| 16 | import * as Path from "effect/Path"; |
| 17 | import * as Schema from "effect/Schema"; |
| 18 | import sharp from "sharp"; |
| 19 | |
| 20 | type IconVariant = "dev" | "nightly" | "prod"; |
| 21 | |
| 22 | // 108dp at xxxhdpi. Expo's prebuild derives every density bucket from this. |
| 23 | const CANVAS = 432; |
| 24 | // Icon Composer's layer sources use a 128pt viewBox; the wordmark path spans this box. |
| 25 | const TEXT = { x: 15.53, y: 37, width: 94.5, height: 57 }; |
| 26 | // Wordmark width as a fraction of the 108dp canvas. The visible area is 72dp (66dp |
| 27 | // guaranteed), so 0.48 leaves the letters at ~72% of the mask with room for the |
| 28 | // launcher's own zoom effects. |
| 29 | const WORDMARK_FRACTION = 0.48; |
| 30 | // Icon Composer positions layers on a 1024pt canvas, with translation relative to center. |
| 31 | const COMPOSER_CANVAS_PT = 1024; |
| 32 | const SVG_DENSITY = 300; |
| 33 | const OUTPUT_DIRECTORY = "apps/mobile/assets"; |
| 34 | |
| 35 | export class AndroidIconRenderError extends Schema.TaggedErrorClass<AndroidIconRenderError>()( |
| 36 | "AndroidIconRenderError", |
| 37 | { layer: Schema.String, cause: Schema.Defect() }, |
| 38 | ) {} |
| 39 | |
| 40 | const wordmarkTransform = () => { |
| 41 | const scale = (CANVAS * WORDMARK_FRACTION) / TEXT.width; |
| 42 | const tx = (CANVAS - TEXT.width * scale) / 2 - TEXT.x * scale; |
| 43 | const ty = (CANVAS - TEXT.height * scale) / 2 - TEXT.y * scale; |
| 44 | return `translate(${tx.toFixed(3)} ${ty.toFixed(3)}) scale(${scale.toFixed(4)})`; |
| 45 | }; |
| 46 | |
| 47 | const canvasSvg = (inner: string) => |
| 48 | `<svg xmlns="http://www.w3.org/2000/svg" width="${CANVAS}" height="${CANVAS}" viewBox="0 0 ${CANVAS} ${CANVAS}" fill="none">${inner}</svg>`; |
| 49 | |
| 50 | // The layer sources clip to a 10pt rounded rectangle for the iOS silhouette. Android |
| 51 | // applies its own mask, so the layer must bleed to the canvas edge. |
| 52 | const fullBleed = (svg: string) => |
| 53 | svg.replace(/<rect width="128" height="128" rx="10"\/>/, '<rect width="128" height="128"/>'); |
| 54 | |
| 55 | const rasterize = (layer: string, svg: string, width: number, height = width) => |
| 56 | Effect.tryPromise({ |
| 57 | try: () => |
| 58 | sharp(Buffer.from(svg), { density: SVG_DENSITY }).resize(width, height).png().toBuffer(), |
| 59 | catch: (cause) => new AndroidIconRenderError({ layer, cause }), |
| 60 | }); |
| 61 | |
| 62 | const composite = ( |
| 63 | layer: string, |
| 64 | base: Buffer, |
| 65 | overlays: ReadonlyArray<{ input: Buffer; left?: number; top?: number }>, |
| 66 | ) => |
| 67 | Effect.tryPromise({ |
| 68 | try: () => |
| 69 | sharp(base) |
| 70 | .composite([...overlays]) |
| 71 | .png() |
| 72 | .toBuffer(), |
| 73 | catch: (cause) => new AndroidIconRenderError({ layer, cause }), |
| 74 | }); |
| 75 | |
| 76 | const readLayerSource = Effect.fn("androidIcons.readLayerSource")(function* ( |
| 77 | repositoryRoot: string, |
| 78 | variant: IconVariant, |
| 79 | file: string, |
| 80 | ) { |
| 81 | const fs = yield* FileSystem.FileSystem; |
| 82 | const path = yield* Path.Path; |
| 83 | return yield* fs.readFileString( |
| 84 | path.join(repositoryRoot, "assets", variant, "app-icon.icon", "Assets", file), |
| 85 | ); |
| 86 | }); |
| 87 | |
| 88 | const renderForeground = Effect.fn("androidIcons.renderForeground")(function* ( |
| 89 | repositoryRoot: string, |
| 90 | ) { |
| 91 | const text = yield* readLayerSource(repositoryRoot, "prod", "text.svg"); |
| 92 | const paths = text.match(/<path[^>]*\/>/g) ?? []; |
| 93 | return yield* rasterize( |
| 94 | "foreground", |
| 95 | canvasSvg(`<g transform="${wordmarkTransform()}">${paths.join("")}</g>`), |
| 96 | CANVAS, |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | const renderDevelopmentBackground = Effect.fn("androidIcons.renderDevelopmentBackground")( |
| 101 | function* (repositoryRoot: string) { |
| 102 | // The annotation layer shares the wordmark's coordinate space, so it is scaled and |
| 103 | // centered the same way to keep the dimension lines around the letters. |
| 104 | const annotations = yield* readLayerSource(repositoryRoot, "dev", "annotations.svg"); |
| 105 | const defs = annotations.match(/<defs>[\s\S]*?<\/defs>/)?.[0] ?? ""; |
| 106 | const body = annotations.replace(/^[\s\S]*?<\/defs>/, "").replace(/<\/svg>\s*$/, ""); |
| 107 | const paper = yield* readLayerSource(repositoryRoot, "dev", "background.svg"); |
| 108 | const background = yield* rasterize("dev-background", fullBleed(paper), CANVAS); |
| 109 | const overlay = yield* rasterize( |
| 110 | "dev-annotations", |
| 111 | canvasSvg(`${defs}<g transform="${wordmarkTransform()}">${body}</g>`), |
| 112 | CANVAS, |
| 113 | ); |
| 114 | return yield* composite("dev-background", background, [{ input: overlay }]); |
| 115 | }, |
| 116 | ); |
| 117 | |
| 118 | const renderNightlyBackground = Effect.fn("androidIcons.renderNightlyBackground")(function* ( |
| 119 | repositoryRoot: string, |
| 120 | ) { |
| 121 | // Positions mirror assets/nightly/app-icon.icon/icon.json. The SVG blur filter is |
| 122 | // dropped because Icon Composer ignores it and it smears at this raster size. |
| 123 | const clouds = [ |
| 124 | { file: "cloud-lower-left.svg", scale: 25, translation: [-309.6375, 268.66077693836917] }, |
| 125 | { |
| 126 | file: "cloud-upper-right.svg", |
| 127 | scale: 15, |
| 128 | translation: [387.9605131881942, -134.30064713259117], |
| 129 | }, |
| 130 | ] as const; |
| 131 | const k = CANVAS / COMPOSER_CANVAS_PT; |
| 132 | const overlays = yield* Effect.forEach(clouds, (cloud) => |
| 133 | Effect.gen(function* () { |
| 134 | const width = Math.round(64 * cloud.scale * k); |
| 135 | const height = Math.round(32 * cloud.scale * k); |
| 136 | const left = Math.round((COMPOSER_CANVAS_PT / 2 + cloud.translation[0]) * k - width / 2); |
| 137 | const top = Math.round((COMPOSER_CANVAS_PT / 2 + cloud.translation[1]) * k - height / 2); |
| 138 | const source = yield* readLayerSource(repositoryRoot, "nightly", cloud.file); |
| 139 | const png = yield* rasterize( |
| 140 | cloud.file, |
| 141 | source.replace(/ filter="url\(#soft\)"/, ""), |
| 142 | width, |
| 143 | height, |
| 144 | ); |
| 145 | const x0 = Math.max(0, left); |
| 146 | const y0 = Math.max(0, top); |
| 147 | const x1 = Math.min(CANVAS, left + width); |
| 148 | const y1 = Math.min(CANVAS, top + height); |
| 149 | const clipped = yield* Effect.tryPromise({ |
| 150 | try: () => |
| 151 | sharp(png) |
| 152 | .extract({ left: x0 - left, top: y0 - top, width: x1 - x0, height: y1 - y0 }) |
| 153 | .png() |
| 154 | .toBuffer(), |
| 155 | catch: (cause) => new AndroidIconRenderError({ layer: cloud.file, cause }), |
| 156 | }); |
| 157 | return { input: clipped, left: x0, top: y0 }; |
| 158 | }), |
| 159 | ); |
| 160 | const sky = yield* readLayerSource(repositoryRoot, "nightly", "background.svg"); |
| 161 | const background = yield* rasterize("nightly-background", fullBleed(sky), CANVAS); |
| 162 | return yield* composite("nightly-background", background, overlays); |
| 163 | }); |
| 164 | |
| 165 | const exportAndroidIcons = Effect.gen(function* () { |
| 166 | const fs = yield* FileSystem.FileSystem; |
| 167 | const path = yield* Path.Path; |
| 168 | const repositoryRoot = path.resolve(import.meta.dirname, ".."); |
| 169 | const outputs = [ |
| 170 | ["android-icon-foreground.png", yield* renderForeground(repositoryRoot)], |
| 171 | ["android-icon-background-dev.png", yield* renderDevelopmentBackground(repositoryRoot)], |
| 172 | ["android-icon-background-nightly.png", yield* renderNightlyBackground(repositoryRoot)], |
| 173 | ] as const; |
| 174 | for (const [name, contents] of outputs) { |
| 175 | yield* fs.writeFile(path.join(repositoryRoot, OUTPUT_DIRECTORY, name), contents); |
| 176 | yield* Console.log(`wrote ${OUTPUT_DIRECTORY}/${name}`); |
| 177 | } |
| 178 | }); |
| 179 | |
| 180 | if (import.meta.main) { |
| 181 | exportAndroidIcons.pipe(Effect.provide(NodeServices.layer), NodeRuntime.runMain); |
| 182 | } |