M1.14: app icon and README banner
- assets/app-icon/icon.png (720 px master) and docs/images/banner.png from Daniel's finals; README shows the banner at the top, badges kept. - build.rs downscales the master (area-averaging box filter, premultiplied alpha) to icons/icon.png (256 px) and a multi-size icon.ico (16, 32, 48, 64, 128, 256); a missing or unreadable master prints a cargo warning and falls back to the generated placeholder. No new dependency (png already a build dependency). - ASSETS.md rows for both files; architecture App icon row updated. - .gitignore: /docs/psd/ and /docs/pngs/ so the local sources stay out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f8fef876b
commit
bcc80b99d3
7 changed files with 221 additions and 34 deletions
|
|
@ -1,28 +1,70 @@
|
|||
//! Build script of the desktop app.
|
||||
//!
|
||||
//! Generates the placeholder app icon (an otter-brown rounded square holding a
|
||||
//! pale stone) into the gitignored `icons/` directory, then runs the Tauri
|
||||
//! build step, which needs those files. The real icon is the owner's to
|
||||
//! commission; this one is generated in code and has no asset file.
|
||||
//! Produces the app icons Tauri needs into the gitignored `icons/` directory,
|
||||
//! then runs the Tauri build step, which needs those files. The source is the
|
||||
//! owner's master icon `assets/app-icon/icon.png` (listed in ASSETS.md),
|
||||
//! downscaled to the window icon `icon.png` and to a multi-size `icon.ico`.
|
||||
//! If the master is missing or unreadable, the build prints a warning and
|
||||
//! falls back to a placeholder generated in code (an otter-brown rounded
|
||||
//! square holding a pale stone), so the build never fails over the icon.
|
||||
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::io::Cursor;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Edge length of the generated icon in pixels.
|
||||
/// Edge length of the placeholder and of the window icon `icon.png`.
|
||||
const SIZE: u32 = 256;
|
||||
|
||||
/// Edge lengths of the images in `icon.ico`.
|
||||
const ICO_SIZES: [u32; 6] = [16, 32, 48, 64, 128, 256];
|
||||
|
||||
/// Largest accepted edge length of the master icon, which caps the decoding
|
||||
/// work a broken or oversized file can cause.
|
||||
const MAX_MASTER: u32 = 4096;
|
||||
|
||||
/// Path of the master icon relative to the crate directory.
|
||||
const MASTER: &str = "../../assets/app-icon/icon.png";
|
||||
|
||||
/// A square RGBA image with 8 bits per channel, not premultiplied.
|
||||
struct Image {
|
||||
/// Edge length in pixels.
|
||||
size: u32,
|
||||
/// Pixels row by row, four bytes each.
|
||||
rgba: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Writes the icons and runs `tauri_build`.
|
||||
///
|
||||
/// # Errors
|
||||
/// Fails if the icons cannot be encoded or written.
|
||||
/// Fails if the icons cannot be encoded or written. An unusable master icon
|
||||
/// is not an error; the placeholder replaces it.
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let dir = Path::new(&std::env::var("CARGO_MANIFEST_DIR")?).join("icons");
|
||||
fs::create_dir_all(&dir)?;
|
||||
let png = encode_png(&draw_icon(), SIZE)?;
|
||||
write_if_changed(&dir.join("icon.png"), &png)?;
|
||||
write_if_changed(&dir.join("icon.ico"), &ico_from_png(&png))?;
|
||||
let manifest = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?);
|
||||
let master_path = manifest.join(MASTER);
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed={}", master_path.display());
|
||||
let master = match load_master(&master_path) {
|
||||
Ok(image) => image,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"cargo:warning=app icon {}: {e}; using the generated placeholder",
|
||||
master_path.display()
|
||||
);
|
||||
Image {
|
||||
size: SIZE,
|
||||
rgba: draw_icon(),
|
||||
}
|
||||
}
|
||||
};
|
||||
let dir = manifest.join("icons");
|
||||
fs::create_dir_all(&dir)?;
|
||||
write_if_changed(&dir.join("icon.png"), &encode_png(&resize(&master, SIZE))?)?;
|
||||
let mut entries = Vec::with_capacity(ICO_SIZES.len());
|
||||
for size in ICO_SIZES {
|
||||
entries.push((size, encode_png(&resize(&master, size))?));
|
||||
}
|
||||
write_if_changed(&dir.join("icon.ico"), &ico(&entries))?;
|
||||
tauri_build::build();
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -39,8 +81,137 @@ fn write_if_changed(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
|||
fs::write(path, bytes)
|
||||
}
|
||||
|
||||
/// Draws the icon as RGBA pixels, row by row, with 4×4 supersampling for
|
||||
/// smooth edges.
|
||||
/// Reads the master icon and converts it to 8-bit RGBA.
|
||||
///
|
||||
/// # Errors
|
||||
/// Fails if the file cannot be read or decoded, is not square, or its edge
|
||||
/// length is 0 or above [`MAX_MASTER`].
|
||||
fn load_master(path: &Path) -> Result<Image, Box<dyn Error>> {
|
||||
let bytes = fs::read(path)?;
|
||||
let mut decoder = png::Decoder::new(Cursor::new(bytes));
|
||||
decoder.set_transformations(png::Transformations::normalize_to_color8());
|
||||
let mut reader = decoder.read_info()?;
|
||||
let (width, height) = (reader.info().width, reader.info().height);
|
||||
if width != height {
|
||||
return Err(format!("not square ({width}x{height})").into());
|
||||
}
|
||||
if width == 0 || width > MAX_MASTER {
|
||||
return Err(format!("edge length {width} outside 1..={MAX_MASTER}").into());
|
||||
}
|
||||
let mut buf = vec![0; reader.output_buffer_size().ok_or("image too large")?];
|
||||
let frame = reader.next_frame(&mut buf)?;
|
||||
if frame.bit_depth != png::BitDepth::Eight {
|
||||
return Err("unsupported bit depth".into());
|
||||
}
|
||||
let data = buf.get(..frame.buffer_size()).ok_or("short frame")?;
|
||||
let rgba: Vec<u8> = match frame.color_type {
|
||||
png::ColorType::Rgba => data.to_vec(),
|
||||
png::ColorType::Rgb => data
|
||||
.as_chunks::<3>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|&[r, g, b]| [r, g, b, 255])
|
||||
.collect(),
|
||||
png::ColorType::GrayscaleAlpha => data
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|&[v, a]| [v, v, v, a])
|
||||
.collect(),
|
||||
png::ColorType::Grayscale => data.iter().flat_map(|&v| [v, v, v, 255]).collect(),
|
||||
png::ColorType::Indexed => return Err("palette not expanded".into()),
|
||||
};
|
||||
let expected = (width as usize) * (width as usize) * 4;
|
||||
if rgba.len() != expected {
|
||||
return Err(format!("pixel data has {} bytes, expected {expected}", rgba.len()).into());
|
||||
}
|
||||
Ok(Image { size: width, rgba })
|
||||
}
|
||||
|
||||
/// Resamples an image to `size`×`size` with an area-averaging box filter on
|
||||
/// premultiplied alpha, which keeps edges clean when shrinking a lot (720 px
|
||||
/// to 16 px). Returns a copy when the size already matches.
|
||||
fn resize(src: &Image, size: u32) -> Image {
|
||||
if src.size == size {
|
||||
return Image {
|
||||
size,
|
||||
rgba: src.rgba.clone(),
|
||||
};
|
||||
}
|
||||
let taps = box_taps(src.size, size);
|
||||
let (src_n, dst_n) = (src.size as usize, size as usize);
|
||||
let mut rows = vec![[0.0f64; 4]; dst_n * src_n];
|
||||
for y in 0..src_n {
|
||||
for (x, row_taps) in taps.iter().enumerate() {
|
||||
let mut acc = [0.0f64; 4];
|
||||
for &(i, w) in row_taps {
|
||||
let at = (y * src_n + i) * 4;
|
||||
if let Some(&[r, g, b, a]) = src.rgba.get(at..at + 4) {
|
||||
let a = f64::from(a) / 255.0;
|
||||
acc[0] += f64::from(r) * a * w;
|
||||
acc[1] += f64::from(g) * a * w;
|
||||
acc[2] += f64::from(b) * a * w;
|
||||
acc[3] += a * w;
|
||||
}
|
||||
}
|
||||
if let Some(cell) = rows.get_mut(y * dst_n + x) {
|
||||
*cell = acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut rgba = Vec::with_capacity(dst_n * dst_n * 4);
|
||||
for col_taps in &taps {
|
||||
for x in 0..dst_n {
|
||||
let mut acc = [0.0f64; 4];
|
||||
for &(j, w) in col_taps {
|
||||
if let Some(cell) = rows.get(j * dst_n + x) {
|
||||
for (sum, value) in acc.iter_mut().zip(cell) {
|
||||
*sum += value * w;
|
||||
}
|
||||
}
|
||||
}
|
||||
let [r, g, b, a] = acc;
|
||||
let channel = |c: f64| {
|
||||
if a > 0.0 {
|
||||
(c / a).round().clamp(0.0, 255.0) as u8
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
rgba.extend_from_slice(&[
|
||||
channel(r),
|
||||
channel(g),
|
||||
channel(b),
|
||||
(a * 255.0).round().clamp(0.0, 255.0) as u8,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Image { size, rgba }
|
||||
}
|
||||
|
||||
/// Box-filter taps for scaling `src` samples to `dst`: for each output
|
||||
/// sample, the source indices it covers and their weights, which sum to 1.
|
||||
fn box_taps(src: u32, dst: u32) -> Vec<Vec<(usize, f64)>> {
|
||||
let scale = f64::from(src) / f64::from(dst);
|
||||
(0..dst)
|
||||
.map(|o| {
|
||||
let start = f64::from(o) * scale;
|
||||
let end = start + scale;
|
||||
let first = start.floor() as u32;
|
||||
let last = (end.ceil() as u32).min(src);
|
||||
(first..last)
|
||||
.filter_map(|i| {
|
||||
let lo = start.max(f64::from(i));
|
||||
let hi = end.min(f64::from(i) + 1.0);
|
||||
(hi > lo).then(|| (i as usize, (hi - lo) / scale))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Draws the placeholder icon as RGBA pixels, row by row, with 4×4
|
||||
/// supersampling for smooth edges.
|
||||
fn draw_icon() -> Vec<u8> {
|
||||
let body = [0x6b, 0x4a, 0x32];
|
||||
let stone = [0xd9, 0xd2, 0xc5];
|
||||
|
|
@ -100,31 +271,42 @@ fn in_stone(x: f64, y: f64) -> bool {
|
|||
((x - 0.5) / 0.26).powi(2) + ((y - 0.6) / 0.18).powi(2) <= 1.0
|
||||
}
|
||||
|
||||
/// Encodes RGBA pixels as PNG.
|
||||
/// Encodes an image as RGBA PNG.
|
||||
///
|
||||
/// # Errors
|
||||
/// Fails if the encoder rejects the data.
|
||||
fn encode_png(pixels: &[u8], size: u32) -> Result<Vec<u8>, png::EncodingError> {
|
||||
fn encode_png(image: &Image) -> Result<Vec<u8>, png::EncodingError> {
|
||||
let mut out = Vec::new();
|
||||
{
|
||||
let mut encoder = png::Encoder::new(&mut out, size, size);
|
||||
let mut encoder = png::Encoder::new(&mut out, image.size, image.size);
|
||||
encoder.set_color(png::ColorType::Rgba);
|
||||
encoder.set_depth(png::BitDepth::Eight);
|
||||
let mut writer = encoder.write_header()?;
|
||||
writer.write_image_data(pixels)?;
|
||||
writer.write_image_data(&image.rgba)?;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Wraps a 256×256 PNG in an ICO container with a single entry, which every
|
||||
/// Windows version since Vista reads.
|
||||
fn ico_from_png(png: &[u8]) -> Vec<u8> {
|
||||
let len = u32::try_from(png.len()).unwrap_or(u32::MAX);
|
||||
let mut ico = Vec::with_capacity(22 + png.len());
|
||||
ico.extend_from_slice(&[0, 0, 1, 0, 1, 0]);
|
||||
ico.extend_from_slice(&[0, 0, 0, 0, 1, 0, 32, 0]);
|
||||
ico.extend_from_slice(&len.to_le_bytes());
|
||||
ico.extend_from_slice(&22u32.to_le_bytes());
|
||||
ico.extend_from_slice(png);
|
||||
/// Builds an ICO container from PNG-encoded entries of the given edge
|
||||
/// lengths, which every Windows version since Vista reads. An edge length of
|
||||
/// 256 is stored as 0, as the format requires.
|
||||
fn ico(entries: &[(u32, Vec<u8>)]) -> Vec<u8> {
|
||||
let count = u16::try_from(entries.len()).unwrap_or(u16::MAX);
|
||||
let mut offset = 6 + 16 * entries.len();
|
||||
let mut ico = Vec::with_capacity(offset + entries.iter().map(|(_, p)| p.len()).sum::<usize>());
|
||||
ico.extend_from_slice(&[0, 0, 1, 0]);
|
||||
ico.extend_from_slice(&count.to_le_bytes());
|
||||
for (size, png) in entries {
|
||||
let edge = u8::try_from(*size).unwrap_or(0);
|
||||
let len = u32::try_from(png.len()).unwrap_or(u32::MAX);
|
||||
let at = u32::try_from(offset).unwrap_or(u32::MAX);
|
||||
ico.extend_from_slice(&[edge, edge, 0, 0, 1, 0, 32, 0]);
|
||||
ico.extend_from_slice(&len.to_le_bytes());
|
||||
ico.extend_from_slice(&at.to_le_bytes());
|
||||
offset += png.len();
|
||||
}
|
||||
for (_, png) in entries {
|
||||
ico.extend_from_slice(png);
|
||||
}
|
||||
ico
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue