Skip to content

Commit ba17da2

Browse files
authored
fix(cli): correct Android hdpi icon size and background color format (#16101)
1 parent e49d1b4 commit ba17da2

2 files changed

Lines changed: 64 additions & 20 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
`tauri icon` now generates 72x72 Android `hdpi` launcher icons (previously 49x49) and writes the Android launcher background color in `#RRGGBB`/`#AARRGGBB` notation instead of the raw CSS color string, which Android rejected or misread. Invalid SVG sources and `--png 0` now return an error instead of panicking.

‎crates/tauri-cli/src/icon.rs‎

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ enum Source {
129129
}
130130

131131
impl Source {
132-
fn resize_exact(&self, size: u32) -> DynamicImage {
132+
fn resize_exact(&self, size: u32) -> Result<DynamicImage> {
133133
match self {
134134
Self::Svg { tree, fit } => rasterize_svg(tree, *fit, size),
135135
Self::DynamicImage(image) => {
136136
// image.resize_exact(size, size, FilterType::Lanczos3)
137-
resize_image(image, size, size)
137+
Ok(resize_image(image, size, size))
138138
}
139139
}
140140
}
@@ -184,7 +184,10 @@ fn read_source(path: PathBuf) -> Result<Source> {
184184
};
185185

186186
let svg_data = std::fs::read(&path).fs_context("Failed to read source icon", &path)?;
187-
usvg::Tree::from_data(&svg_data, &opt).unwrap()
187+
usvg::Tree::from_data(&svg_data, &opt).context(format!(
188+
"failed to parse SVG source icon {}",
189+
path.display()
190+
))?
188191
};
189192

190193
Ok(Source::Svg {
@@ -210,10 +213,10 @@ fn parse_bg_color(bg_color_string: &String) -> Result<Rgba<u8>> {
210213
let bg_color = css_color::Srgb::from_str(bg_color_string)
211214
.map(|color| {
212215
Rgba([
213-
(color.red * 255.) as u8,
214-
(color.green * 255.) as u8,
215-
(color.blue * 255.) as u8,
216-
(color.alpha * 255.) as u8,
216+
(color.red * 255.).round() as u8,
217+
(color.green * 255.).round() as u8,
218+
(color.blue * 255.).round() as u8,
219+
(color.alpha * 255.).round() as u8,
217220
])
218221
})
219222
.map_err(|_e| {
@@ -231,7 +234,7 @@ fn parse_bg_color(bg_color_string: &String) -> Result<Rgba<u8>> {
231234
// final resolution, there is no intermediate bitmap and no quality loss — a
232235
// source declaring a tiny intrinsic size (e.g. `width="16"`) is just as crisp
233236
// as a large one.
234-
fn rasterize_svg(tree: &usvg::Tree, fit: Option<Fit>, size: u32) -> DynamicImage {
237+
fn rasterize_svg(tree: &usvg::Tree, fit: Option<Fit>, size: u32) -> Result<DynamicImage> {
235238
let native = tree.size();
236239
let (nw, nh) = (native.width(), native.height());
237240

@@ -256,7 +259,9 @@ fn rasterize_svg(tree: &usvg::Tree, fit: Option<Fit>, size: u32) -> DynamicImage
256259
let transform =
257260
tiny_skia::Transform::from_scale(scale, scale).post_translate(-off_x * scale, -off_y * scale);
258261

259-
let mut pixmap = tiny_skia::Pixmap::new(size, size).unwrap();
262+
let Some(mut pixmap) = tiny_skia::Pixmap::new(size, size) else {
263+
crate::error::bail!("invalid icon size {size}x{size}");
264+
};
260265
resvg::render(tree, transform, &mut pixmap.as_mut());
261266

262267
// Switch to use `Pixmap::take_demultiplied` in the future when it's published
@@ -265,7 +270,7 @@ fn rasterize_svg(tree: &usvg::Tree, fit: Option<Fit>, size: u32) -> DynamicImage
265270
let pixel = pixmap.pixel(x, y).unwrap().demultiply();
266271
Rgba([pixel.red(), pixel.green(), pixel.blue(), pixel.alpha()])
267272
});
268-
DynamicImage::ImageRgba8(img_buffer)
273+
Ok(DynamicImage::ImageRgba8(img_buffer))
269274
}
270275

271276
// Convert a non-square source into a square one according to the `fit` mode.
@@ -311,6 +316,9 @@ pub fn command(options: Options) -> Result<()> {
311316
dirs.tauri.join("icons")
312317
});
313318
let png_icon_sizes = options.png.unwrap_or_default();
319+
if png_icon_sizes.contains(&0) {
320+
crate::error::bail!("PNG icon sizes must be greater than 0");
321+
}
314322

315323
create_dir_all(&out_dir).fs_context("Can't create output directory", &out_dir)?;
316324

@@ -358,7 +366,7 @@ pub fn command(options: Options) -> Result<()> {
358366
ico(&source, &out_dir).context("Failed to generate .ico file")?;
359367

360368
png(&source, &out_dir, bg_color).context("Failed to generate png icons")?;
361-
android(&source, &input, manifest, &bg_color_string, &out_dir)
369+
android(&source, &input, manifest, bg_color, &out_dir)
362370
.context("Failed to generate android icons")?;
363371
} else {
364372
for target in png_icon_sizes.into_iter().map(|size| {
@@ -417,7 +425,7 @@ fn icns(source: &Source, out_dir: &Path) -> Result<()> {
417425
let size = entry.size;
418426
let mut buf = Vec::new();
419427

420-
let image = source.resize_exact(size);
428+
let image = source.resize_exact(size)?;
421429

422430
write_png(image.as_bytes(), &mut buf, size).context("failed to write output file")?;
423431

@@ -452,7 +460,7 @@ fn ico(source: &Source, out_dir: &Path) -> Result<()> {
452460
let mut frames = Vec::new();
453461

454462
for size in [16, 24, 32, 48, 64, 256] {
455-
let image = source.resize_exact(size);
463+
let image = source.resize_exact(size)?;
456464

457465
// Only the 256px layer can be compressed according to the ico specs.
458466
if size == 256 {
@@ -490,7 +498,7 @@ fn android(
490498
source: &Source,
491499
input: &Path,
492500
manifest: Option<Manifest>,
493-
bg_color: &String,
501+
bg_color: Rgba<u8>,
494502
out_dir: &Path,
495503
) -> Result<()> {
496504
fn android_entries(out_dir: &Path) -> Result<AndroidEntries> {
@@ -503,7 +511,7 @@ fn android(
503511
let targets = vec![
504512
AndroidEntry {
505513
name: "hdpi",
506-
size: 49,
514+
size: 72,
507515
foreground_size: 162,
508516
},
509517
AndroidEntry {
@@ -583,7 +591,8 @@ fn android(
583591
monochrome: monochrome_entries,
584592
})
585593
}
586-
fn create_color_file(out_dir: &Path, color: &String) -> Result<()> {
594+
fn create_color_file(out_dir: &Path, color: Rgba<u8>) -> Result<()> {
595+
let color = android_color(color);
587596
let values_folder = out_dir.join("values");
588597
create_dir_all(&values_folder).fs_context(
589598
"Can't create Android values output directory",
@@ -741,6 +750,17 @@ fn android(
741750
Ok(())
742751
}
743752

753+
// Format a color as an Android color resource value, which only accepts
754+
// `#RGB`, `#ARGB`, `#RRGGBB` and `#AARRGGBB`.
755+
fn android_color(color: Rgba<u8>) -> String {
756+
let [r, g, b, a] = color.0;
757+
if a == u8::MAX {
758+
format!("#{r:02X}{g:02X}{b:02X}")
759+
} else {
760+
format!("#{a:02X}{r:02X}{g:02X}{b:02X}")
761+
}
762+
}
763+
744764
// Generate .png files in 32x32, 64x64, 128x128, 256x256, 512x512 (icon.png)
745765
// Main target: Linux
746766
fn png(source: &Source, out_dir: &Path, ios_color: Rgba<u8>) -> Result<()> {
@@ -883,7 +903,7 @@ fn resize_png(
883903
bg: Option<Background>,
884904
scale_percent: Option<f32>,
885905
) -> Result<DynamicImage> {
886-
let mut image = source.resize_exact(size);
906+
let mut image = source.resize_exact(size)?;
887907

888908
match bg {
889909
Some(Background::Color(bg_color)) => {
@@ -897,7 +917,7 @@ fn resize_png(
897917
image = bg_img.into();
898918
}
899919
Some(Background::Image(bg_source)) => {
900-
let mut bg = bg_source.resize_exact(size);
920+
let mut bg = bg_source.resize_exact(size)?;
901921

902922
let fg = scale_percent
903923
.map(|scale| resize_asset(&image, size, scale))
@@ -1115,7 +1135,7 @@ mod tests {
11151135
fit: Some(Fit::Cover),
11161136
};
11171137
// rendered directly from the vector tree at the requested size
1118-
let image = source.resize_exact(64);
1138+
let image = source.resize_exact(64).unwrap();
11191139
assert_eq!((image.width(), image.height()), (64, 64));
11201140
// center of the cropped region stays opaque
11211141
assert_eq!(image.get_pixel(32, 32)[3], 255);
@@ -1127,13 +1147,31 @@ mod tests {
11271147
tree: svg_landscape(),
11281148
fit: Some(Fit::Contain),
11291149
};
1130-
let image = source.resize_exact(64);
1150+
let image = source.resize_exact(64).unwrap();
11311151
assert_eq!((image.width(), image.height()), (64, 64));
11321152
// top band is transparent padding, the centered content is opaque
11331153
assert_eq!(image.get_pixel(32, 2)[3], 0);
11341154
assert_eq!(image.get_pixel(32, 32)[3], 255);
11351155
}
11361156

1157+
#[test]
1158+
fn svg_zero_size_is_an_error() {
1159+
let source = Source::Svg {
1160+
tree: svg_landscape(),
1161+
fit: Some(Fit::Cover),
1162+
};
1163+
assert!(source.resize_exact(0).is_err());
1164+
}
1165+
1166+
#[test]
1167+
fn android_color_uses_argb_notation() {
1168+
let color = |s: &str| android_color(parse_bg_color(&s.to_string()).unwrap());
1169+
assert_eq!(color("white"), "#FFFFFF");
1170+
assert_eq!(color("rgb(0 0 0)"), "#000000");
1171+
assert_eq!(color("#11223380"), "#80112233");
1172+
assert_eq!(color("#fff"), "#FFFFFF");
1173+
}
1174+
11371175
#[test]
11381176
fn ico_stores_entries_smallest_first() {
11391177
let out_dir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)