Files
doubao-clone/scripts/rasterize-icons.mjs
l f056bcf45a 豆包复刻:原生安卓(Compose + 内嵌 Termux)+ Web / Windows
一个界面照着豆包抠的 AI 助手。主力是安卓端,完全离线自主:不依赖电脑、
不需要另外装 Termux。

安卓端
- 原生 Jetpack Compose,7 个页面,配色/圆角/间距对着真机截图量出来的
- 完整动效:抽屉滑入、页面切换、气泡淡入上移、面板底部升起、typing 三点、按钮回弹
- 内置 Termux:bash/apt/pkg 388 个命令、真 pty 终端;
  pty 不可用的 ROM 上自动降级成命令控制台
- APK 内嵌 CPython 3.13(Chaquopy),AI 内核跑在手机上
- 设备能力:定位 / 陀螺仪 / 加速度计 / 光线 / 电量 / 剪贴板
- 语音:朗读 + 语音输入,火山豆包音色 / OpenAI 兼容 / 设备自带三级引擎

AI 能力
- 10 家 OpenAI 兼容提供商:方舟 / OpenAI / DeepSeek / Kimi / 智谱 /
  通义 / 硅基流动 / OpenRouter / 本地 Ollama / 自定义
- 思考强度按各家支持情况发不同字段(thinking / reasoning_effort / 不发),
  认不出来的一律不发,避免给不支持的厂商发字段直接 400
- MCP 三种传输(http / stdio / inproc)、工具热加载、文件上传、权限模式

跨端
- 安卓走原生 Compose,Web / Windows 走 Lynx
- Node 与 Python 两份内核接口逐字段一致,关键路径都有对拍脚本

自检脚本(都不需要真 Key)
- verify-phone.mjs          设备端 24 项验收
- verify-speech.mjs         语音链路 31 项,两份内核各跑一遍
- parity-thinking.mjs       思考强度映射对拍
- probe-settings-wipe.mjs   设置防清空攻击测试
- mock-llm.mjs              假的 OpenAI + 火山语音服务

许可 MIT;第三方组件(Termux Apache-2.0、Chaquopy、Compose、LynxJS、Electron)
的授权见 LICENSE。
2026-10-09 14:09:36 +08:00

132 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 把 assets 里的 SVG 图标/头像栅格化成 PNG。
*
* 为什么需要:Lynx 在 Android 上走 Fresco 加载图片,而 Fresco 不认 SVG,
* logcat 里会看到 `LynxImageManager: onFailed ... reason: unknown image format`,
* 结果是所有 SVG 图标在真机/模拟器上都不显示(Web 端正常)。
*
* 做法:用无头 Edge 把这些 SVG 渲染成一张透明底的雪碧图,再按格子裁成单张 PNG。
*
* 用法: node scripts/rasterize-icons.mjs
*/
import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const here = path.dirname(fileURLToPath(import.meta.url));
const repo = path.resolve(here, '..');
const ASSETS = path.join(repo, 'app', 'src', 'assets');
const CELL = 96; // 每个图标的栅格尺寸(原 viewBox 是 24,等于 4x)
const AVATAR_CELL = 192;
const COLS = 8;
/** 收集要处理的 svg */
function collect(dir, prefix) {
const full = path.join(ASSETS, dir);
if (!fs.existsSync(full)) return [];
return fs
.readdirSync(full)
.filter((f) => f.endsWith('.svg'))
.map((f) => ({
name: f.replace(/\.svg$/, ''),
outDir: `${prefix}`,
file: path.join(full, f),
content: fs.readFileSync(path.join(full, f), 'utf8'),
}));
}
const icons = collect('icons', 'icons-png');
const avatars = collect('avatars', 'avatars-png');
const groups = [
{ items: icons, cell: CELL, out: path.join(ASSETS, 'icons-png') },
{ items: avatars, cell: AVATAR_CELL, out: path.join(ASSETS, 'avatars-png') },
];
async function shoot(items, cell, outDir, tag) {
const rows = Math.ceil(items.length / COLS);
const width = COLS * cell;
const height = rows * cell;
const cells = items
.map(
(item) => `<div class="cell">${item.content
.replace(/width="\d+"/, `width="${cell}"`)
.replace(/height="\d+"/, `height="${cell}"`)}</div>`,
)
.join('\n');
const html = `<!doctype html><html><head><meta charset="utf-8"><style>
html,body{margin:0;padding:0;background:transparent;}
.grid{display:grid;grid-template-columns:repeat(${COLS}, ${cell}px);width:${width}px;}
.cell{width:${cell}px;height:${cell}px;display:flex;align-items:center;justify-content:center;}
.cell svg{display:block;}
</style></head><body><div class="grid">${cells}</div></body></html>`;
const htmlPath = path.join(repo, `.rasterize-${tag}.html`);
fs.writeFileSync(htmlPath, html, 'utf8');
const pngPath = path.join(repo, `.rasterize-${tag}.png`);
fs.rmSync(pngPath, { force: true });
const res = spawnSync(
process.execPath,
[path.join(here, 'screenshot.mjs'), `file:///${htmlPath.replace(/\\/g, '/')}`, pngPath, '2500'],
{
stdio: 'inherit',
env: {
...process.env,
OMIT_BG: '1',
// 关键:视口必须正好是网格尺寸,否则网格会折行、裁剪就全错位
VIEWPORT: `${width}x${height}`,
SCALE: '1',
},
},
);
if (res.status !== 0) throw new Error('截图失败');
return { pngPath, htmlPath, width, height, rows };
}
const { execFileSync } = await import('node:child_process');
const PY = process.env.PYTHON || 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python312\\python.exe';
for (const group of groups) {
if (group.items.length === 0) continue;
console.log(`\n=== ${group.out} :${group.items.length} 个 ===`);
const { pngPath, htmlPath } = await shoot(group.items, group.cell, group.out, path.basename(group.out));
fs.mkdirSync(group.out, { recursive: true });
// 用 Pillow 按格子裁
const cropScript = path.join(repo, `.rasterize-crop.py`);
fs.writeFileSync(
cropScript,
[
'import sys, json, os',
'from PIL import Image',
'src, out_dir, cell, cols, names = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]), json.loads(sys.argv[5])',
'img = Image.open(src).convert("RGBA")',
'os.makedirs(out_dir, exist_ok=True)',
'for i, name in enumerate(names):',
' r, c = divmod(i, cols)',
' tile = img.crop((c * cell, r * cell, (c + 1) * cell, (r + 1) * cell))',
' tile.save(os.path.join(out_dir, name + ".png"))',
'print("cropped", len(names))',
].join('\n'),
'utf8',
);
const out = execFileSync(
PY,
[cropScript, pngPath, group.out, String(group.cell), String(COLS), JSON.stringify(group.items.map((i) => i.name))],
{ encoding: 'utf8' },
);
console.log(out.trim());
if (process.env.KEEP !== '1') {
fs.rmSync(htmlPath, { force: true });
fs.rmSync(pngPath, { force: true });
fs.rmSync(cropScript, { force: true });
} else {
console.log('保留了雪碧图:', pngPath);
}
}
console.log('\n完成,PNG 在 app/src/assets/icons-png 与 avatars-png');