mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-20 01:07:23 +00:00
213 lines
8.0 KiB
JavaScript
213 lines
8.0 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
// Android masks a 72dp safe area inside 108dp; full-bleed backgrounds and a separate wordmark avoid double silhouettes/cropping.
|
|
|
|
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
|
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import * as Console from "effect/Console";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Path from "effect/Path";
|
|
import * as Schema from "effect/Schema";
|
|
import sharp from "sharp";
|
|
|
|
type IconVariant = "dev" | "nightly" | "prod";
|
|
|
|
const ADAPTIVE_CANVAS = 432;
|
|
const SPLASH_CANVAS = 1152;
|
|
const TEXT = { x: 15.53, y: 37, width: 94.5, height: 57 };
|
|
const WORDMARK_FRACTION = 0.48;
|
|
const COMPOSER_CANVAS_PT = 1024;
|
|
const SVG_DENSITY = 300;
|
|
const OUTPUT_DIRECTORY = "apps/mobile/assets";
|
|
const PRODUCTION_BACKGROUND_COLOR = "#000000";
|
|
|
|
export class AndroidIconRenderError extends Schema.TaggedError<AndroidIconRenderError>()(
|
|
"AndroidIconRenderError",
|
|
{ layer: Schema.String, cause: Schema.Defect() },
|
|
) {}
|
|
|
|
const wordmarkTransform = (size: number) => {
|
|
const scale = (size * WORDMARK_FRACTION) / TEXT.width;
|
|
const tx = (size - TEXT.width * scale) / 2 - TEXT.x * scale;
|
|
const ty = (size - TEXT.height * scale) / 2 - TEXT.y * scale;
|
|
return `translate(${tx.toFixed(3)} ${ty.toFixed(3)}) scale(${scale.toFixed(4)})`;
|
|
};
|
|
|
|
const canvasSvg = (size: number, inner: string) =>
|
|
`<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" fill="none">${inner}</svg>`;
|
|
|
|
// Remove the iOS 10pt corner clip; Android's own mask needs full bleed.
|
|
const fullBleed = (svg: string) =>
|
|
svg.replace(/<rect width="128" height="128" rx="10"\/>/, '<rect width="128" height="128"/>');
|
|
|
|
const rasterize = (layer: string, svg: string, width: number, height = width) =>
|
|
Effect.tryPromise({
|
|
try: () =>
|
|
sharp(Buffer.from(svg), { density: SVG_DENSITY }).resize(width, height).png().toBuffer(),
|
|
catch: (cause) => new AndroidIconRenderError({ layer, cause }),
|
|
});
|
|
|
|
const composite = (
|
|
layer: string,
|
|
base: Buffer,
|
|
overlays: ReadonlyArray<{ input: Buffer; left?: number; top?: number }>,
|
|
) =>
|
|
Effect.tryPromise({
|
|
try: () =>
|
|
sharp(base)
|
|
.composite([...overlays])
|
|
.png()
|
|
.toBuffer(),
|
|
catch: (cause) => new AndroidIconRenderError({ layer, cause }),
|
|
});
|
|
|
|
const solidCanvas = (layer: string, size: number, background: string) =>
|
|
Effect.tryPromise({
|
|
try: () =>
|
|
sharp({ create: { width: size, height: size, channels: 4, background } })
|
|
.png()
|
|
.toBuffer(),
|
|
catch: (cause) => new AndroidIconRenderError({ layer, cause }),
|
|
});
|
|
|
|
const readLayerSource = Effect.fn("androidIcons.readLayerSource")(function* (
|
|
repositoryRoot: string,
|
|
variant: IconVariant,
|
|
file: string,
|
|
) {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
return yield* fs.readFileString(
|
|
path.join(repositoryRoot, "assets", variant, "app-icon.icon", "Assets", file),
|
|
);
|
|
});
|
|
|
|
const renderForeground = Effect.fn("androidIcons.renderForeground")(function* (
|
|
repositoryRoot: string,
|
|
size: number,
|
|
) {
|
|
const text = yield* readLayerSource(repositoryRoot, "prod", "text.svg");
|
|
const paths = text.match(/<path[^>]*\/>/g) ?? [];
|
|
return yield* rasterize(
|
|
"foreground",
|
|
canvasSvg(size, `<g transform="${wordmarkTransform(size)}">${paths.join("")}</g>`),
|
|
size,
|
|
);
|
|
});
|
|
|
|
const renderDevelopmentBackground = Effect.fn("androidIcons.renderDevelopmentBackground")(
|
|
function* (repositoryRoot: string, size: number) {
|
|
const annotations = yield* readLayerSource(repositoryRoot, "dev", "annotations.svg");
|
|
const defs = annotations.match(/<defs>[\s\S]*?<\/defs>/)?.[0] ?? "";
|
|
const body = annotations.replace(/^[\s\S]*?<\/defs>/, "").replace(/<\/svg>\s*$/, "");
|
|
const paper = yield* readLayerSource(repositoryRoot, "dev", "background.svg");
|
|
const background = yield* rasterize("dev-background", fullBleed(paper), size);
|
|
const overlay = yield* rasterize(
|
|
"dev-annotations",
|
|
canvasSvg(size, `${defs}<g transform="${wordmarkTransform(size)}">${body}</g>`),
|
|
size,
|
|
);
|
|
return yield* composite("dev-background", background, [{ input: overlay }]);
|
|
},
|
|
);
|
|
|
|
const renderNightlyBackground = Effect.fn("androidIcons.renderNightlyBackground")(function* (
|
|
repositoryRoot: string,
|
|
size: number,
|
|
) {
|
|
// Match assets/nightly/app-icon.icon/icon.json; omit blur, which Icon Composer ignores and rasterization smears.
|
|
const clouds = [
|
|
{ file: "cloud-lower-left.svg", scale: 25, translation: [-309.6375, 268.66077693836917] },
|
|
{
|
|
file: "cloud-upper-right.svg",
|
|
scale: 15,
|
|
translation: [387.9605131881942, -134.30064713259117],
|
|
},
|
|
] as const;
|
|
const k = size / COMPOSER_CANVAS_PT;
|
|
const overlays = yield* Effect.forEach(clouds, (cloud) =>
|
|
Effect.gen(function* () {
|
|
const width = Math.round(64 * cloud.scale * k);
|
|
const height = Math.round(32 * cloud.scale * k);
|
|
const left = Math.round((COMPOSER_CANVAS_PT / 2 + cloud.translation[0]) * k - width / 2);
|
|
const top = Math.round((COMPOSER_CANVAS_PT / 2 + cloud.translation[1]) * k - height / 2);
|
|
const source = yield* readLayerSource(repositoryRoot, "nightly", cloud.file);
|
|
const png = yield* rasterize(
|
|
cloud.file,
|
|
source.replace(/ filter="url\(#soft\)"/, ""),
|
|
width,
|
|
height,
|
|
);
|
|
const x0 = Math.max(0, left);
|
|
const y0 = Math.max(0, top);
|
|
const x1 = Math.min(size, left + width);
|
|
const y1 = Math.min(size, top + height);
|
|
const clipped = yield* Effect.tryPromise({
|
|
try: () =>
|
|
sharp(png)
|
|
.extract({ left: x0 - left, top: y0 - top, width: x1 - x0, height: y1 - y0 })
|
|
.png()
|
|
.toBuffer(),
|
|
catch: (cause) => new AndroidIconRenderError({ layer: cloud.file, cause }),
|
|
});
|
|
return { input: clipped, left: x0, top: y0 };
|
|
}),
|
|
);
|
|
const sky = yield* readLayerSource(repositoryRoot, "nightly", "background.svg");
|
|
const background = yield* rasterize("nightly-background", fullBleed(sky), size);
|
|
return yield* composite("nightly-background", background, overlays);
|
|
});
|
|
|
|
const renderBackground = Effect.fn("androidIcons.renderBackground")(function* (
|
|
repositoryRoot: string,
|
|
variant: IconVariant,
|
|
size: number,
|
|
) {
|
|
switch (variant) {
|
|
case "dev":
|
|
return yield* renderDevelopmentBackground(repositoryRoot, size);
|
|
case "nightly":
|
|
return yield* renderNightlyBackground(repositoryRoot, size);
|
|
case "prod":
|
|
return yield* solidCanvas("prod-background", size, PRODUCTION_BACKGROUND_COLOR);
|
|
}
|
|
});
|
|
|
|
const renderSplashIcon = Effect.fn("androidIcons.renderSplashIcon")(function* (
|
|
repositoryRoot: string,
|
|
variant: IconVariant,
|
|
) {
|
|
const background = yield* renderBackground(repositoryRoot, variant, SPLASH_CANVAS);
|
|
const foreground = yield* renderForeground(repositoryRoot, SPLASH_CANVAS);
|
|
return yield* composite(`${variant}-splash`, background, [{ input: foreground }]);
|
|
});
|
|
|
|
const exportAndroidIcons = Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const repositoryRoot = path.resolve(import.meta.dirname, "..");
|
|
const outputs = [
|
|
["android-icon-foreground.png", yield* renderForeground(repositoryRoot, ADAPTIVE_CANVAS)],
|
|
[
|
|
"android-icon-background-dev.png",
|
|
yield* renderDevelopmentBackground(repositoryRoot, ADAPTIVE_CANVAS),
|
|
],
|
|
[
|
|
"android-icon-background-nightly.png",
|
|
yield* renderNightlyBackground(repositoryRoot, ADAPTIVE_CANVAS),
|
|
],
|
|
["android-splash-icon-dev.png", yield* renderSplashIcon(repositoryRoot, "dev")],
|
|
["android-splash-icon-nightly.png", yield* renderSplashIcon(repositoryRoot, "nightly")],
|
|
["android-splash-icon-prod.png", yield* renderSplashIcon(repositoryRoot, "prod")],
|
|
] as const;
|
|
for (const [name, contents] of outputs) {
|
|
yield* fs.writeFile(path.join(repositoryRoot, OUTPUT_DIRECTORY, name), contents);
|
|
yield* Console.log(`wrote ${OUTPUT_DIRECTORY}/${name}`);
|
|
}
|
|
});
|
|
|
|
if (import.meta.main) {
|
|
exportAndroidIcons.pipe(Effect.provide(NodeServices.layer), NodeRuntime.runMain);
|
|
}
|