Keep tall card portraits cropped to the card size (#77)

Add ChampionSource::card_portrait (Data Dragon loading art) and
Tracker::refresh_card_portraits, which downloads the missing ones, crops them
to the card's 0.68 aspect at the mockup's 24 % focus, shrinks them to
308 x 453 px and saves them as JPEG in portraits/cards via a .part file.
ChampionRow gains card_portrait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-20 21:08:57 +01:00
1 parent 34e8fb8a50
commit 50c0427958
8 files changed
+365 -2

No files matched your search

+43
View File
@@ -355,6 +355,21 @@ pub struct FakeChampions {
failing: bool,
no_portrait: Vec<String>,
catalog_calls: Arc<AtomicUsize>,
/// What every card portrait download delivers.
card_art: Vec<u8>,
/// The card portrait downloads fail while this is set.
card_failure: Arc<AtomicBool>,
card_calls: Arc<AtomicUsize>,
}
/// A small valid JPEG, tall like the loading art (40 x 60 px).
pub fn card_art() -> Vec<u8> {
let img = image::RgbImage::from_fn(40, 60, |x, y| image::Rgb([x as u8 * 6, y as u8 * 4, 90]));
let mut out = Vec::new();
image::codecs::jpeg::JpegEncoder::new(&mut out)
.encode(img.as_raw(), 40, 60, image::ExtendedColorType::Rgb8)
.unwrap();
out
}
impl FakeChampions {
@@ -373,9 +388,28 @@ impl FakeChampions {
failing: false,
no_portrait: Vec::new(),
catalog_calls: Arc::default(),
card_art: card_art(),
card_failure: Arc::default(),
card_calls: Arc::default(),
}
}
/// Every card portrait download delivers these bytes.
pub fn with_card_art(mut self, bytes: Vec<u8>) -> FakeChampions {
self.card_art = bytes;
self
}
/// Set it to make the card portrait downloads fail (and clear it to let them succeed).
pub fn card_failure(&self) -> Arc<AtomicBool> {
self.card_failure.clone()
}
/// Card portrait downloads attempted.
pub fn card_counter(&self) -> Arc<AtomicUsize> {
self.card_calls.clone()
}
/// Every call fails with a network error.
pub fn failing(mut self) -> FakeChampions {
self.failing = true;
@@ -423,6 +457,15 @@ impl ChampionSource for FakeChampions {
}
Ok(format!("png of {key}").into_bytes())
}
fn card_portrait(&self, key: &str) -> Result<Vec<u8>, SourceError> {
self.card_calls.fetch_add(1, Ordering::SeqCst);
self.check()?;
if self.card_failure.load(Ordering::SeqCst) || self.no_portrait.iter().any(|k| k == key) {
return Err(SourceError::Network("coupé".into()));
}
Ok(self.card_art.clone())
}
}
// Statistics scenarios: a small catalog, two Seasons and Matches described by champion and Placement.