mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
pdf-chrome-w2: review fixes (review-2)
This commit is contained in:
@@ -67,9 +67,10 @@ pub struct Picture<'a> {
|
||||
#[prop]
|
||||
revision: Revision,
|
||||
source: Source<'a>,
|
||||
/// The width of the picture's rect as a multiple of its height.
|
||||
/// The width of the picture's rect as a multiple of its height. Unset, an SVG takes its view box's aspect and any
|
||||
/// other source a square.
|
||||
#[prop]
|
||||
aspect: ThumbAspect,
|
||||
aspect: Option<ThumbAspect>,
|
||||
/// The picture's height.
|
||||
#[style_key]
|
||||
#[prop]
|
||||
@@ -96,7 +97,16 @@ impl<'a> Picture<'a> {
|
||||
}
|
||||
|
||||
fn from_source(label: impl Into<Label>, source: Source<'a>) -> Self {
|
||||
Picture { common: Common::new(), revision: Revision::default(), source, aspect: ThumbAspect::SQUARE, extent: PictureSize::Icon }.label(label)
|
||||
Picture { common: Common::new(), revision: Revision::default(), source, aspect: None, extent: PictureSize::Icon }.label(label)
|
||||
}
|
||||
|
||||
/// The width of the picture's rect at `height`: the set aspect, else the SVG's own, else a square.
|
||||
fn width(&self, height: f32) -> f32 {
|
||||
match (self.aspect, &self.source) {
|
||||
(Some(aspect), _) => aspect.of(height),
|
||||
(None, Source::Svg(svg)) => svg_ratio(svg).map_or_else(|| ThumbAspect::SQUARE.of(height), |ratio| ratio * height),
|
||||
(None, _) => ThumbAspect::SQUARE.of(height),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +133,7 @@ impl Component for Picture<'_> {
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let height = cx.style.height;
|
||||
Vec2::new(self.aspect.of(height), height)
|
||||
Vec2::new(self.width(height), height)
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
@@ -190,6 +200,17 @@ const MAX_SVG_SIDE: u32 = 4096;
|
||||
/// Bytes drawing one SVG picture may allocate, its target included.
|
||||
const SVG_DRAW_BUDGET: u64 = 96 << 20;
|
||||
|
||||
/// The widest and narrowest width-to-height ratio a view box gives a picture.
|
||||
const SVG_RATIO_LIMITS: (f32, f32) = (1.0 / 16.0, 16.0);
|
||||
|
||||
/// The width-to-height ratio of `svg`'s view box, from the same bounded parse `rasterise_svg` does; `None` for an SVG
|
||||
/// that is refused or has no usable size.
|
||||
fn svg_ratio(svg: &[u8]) -> Option<f32> {
|
||||
let size = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| svg_tree::parse(svg).ok().map(|tree| tree.size()))).ok().flatten()?;
|
||||
let ratio = size.width() / size.height();
|
||||
ratio.is_finite().then_some(ratio).filter(|ratio| *ratio > 0.0).map(|ratio| ratio.clamp(SVG_RATIO_LIMITS.0, SVG_RATIO_LIMITS.1))
|
||||
}
|
||||
|
||||
/// The store key of `svg` drawn at `width` x `height` pixels: the asset's identity and the size, so a resize or a
|
||||
/// scale change rasterises again and a static picture rasterises once.
|
||||
fn svg_key(svg: &'static [u8], width: u32, height: u32) -> u64 {
|
||||
|
||||
@@ -33,6 +33,9 @@ fn picture_thumbnail(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
/// An original mark: a teal disc with an amber diamond, so the SVG's own colours and its fit show.
|
||||
const MARK: &[u8] = br##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><circle cx="12" cy="12" r="11" fill="#1f8a8a"/><path d="M12 4 L19 12 L12 20 L5 12 Z" fill="#f2b233"/></svg>"##;
|
||||
|
||||
/// An original 4:1 drawing: a wordmark-shaped bar with two dots.
|
||||
const WIDE: &[u8] = br##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 20"><rect x="1" y="1" width="78" height="18" rx="9" fill="#3b5fc0"/><circle cx="12" cy="10" r="5" fill="#ffffff"/><circle cx="68" cy="10" r="5" fill="#f2b233"/></svg>"##;
|
||||
|
||||
/// Bytes that are not an SVG document.
|
||||
const NOT_SVG: &[u8] = b"<svg xmlns=\"http://www.w3.org/2000/svg\"><circle";
|
||||
|
||||
@@ -47,6 +50,12 @@ fn picture_svg(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
.into()
|
||||
}
|
||||
|
||||
/// A 4:1 SVG with no aspect set is shown four times wider than tall.
|
||||
#[story(Picture<'static>, covers = [], drive = Drive::Never)]
|
||||
fn picture_wide_svg(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
Picture::svg("Wide mark", WIDE).extent(PictureSize::Thumb(ThumbSize::Small)).show(ui).into()
|
||||
}
|
||||
|
||||
#[story(Picture<'static>, covers = [], drive = Drive::Never)]
|
||||
fn picture_svg_invalid(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
Picture::svg("Broken", NOT_SVG).extent(PictureSize::Thumb(ThumbSize::Small)).show(ui).into()
|
||||
|
||||
@@ -8,6 +8,24 @@ use egui::{Rect, Shape, Vec2, pos2, vec2};
|
||||
/// A small valid drawing.
|
||||
const MARK: &[u8] = br##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="#2a7"/></svg>"##;
|
||||
|
||||
/// A 4:1 drawing.
|
||||
const WIDE: &[u8] = br##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 20"><rect width="80" height="20" fill="#36c"/></svg>"##;
|
||||
|
||||
/// An SVG picture with no aspect is as wide as its view box says; an aspect set explicitly wins.
|
||||
#[trial]
|
||||
fn an_svg_picture_without_an_aspect_takes_its_view_box_aspect() {
|
||||
use craft_ui::list::ThumbAspect;
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
Picture::svg("Wide", WIDE).show(ui);
|
||||
Picture::svg("Square", WIDE).aspect(ThumbAspect::SQUARE).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(600.0, 300.0)).settle();
|
||||
let wide = fixture.by_label("Wide").expect("the wide picture's node").rect;
|
||||
let square = fixture.by_label("Square").expect("the square picture's node").rect;
|
||||
assert!((wide.width() / wide.height() - 4.0).abs() < 0.05, "a 4:1 drawing is four times wider than tall: {wide:?}");
|
||||
assert!((square.width() - square.height()).abs() < 0.05, "an explicit aspect wins: {square:?}");
|
||||
}
|
||||
|
||||
/// The rects textured shapes cover in one painted frame; a picture with square corners paints a textured mesh.
|
||||
fn pictures(shapes: &[Shape]) -> Vec<Rect> {
|
||||
shapes
|
||||
|
||||
Reference in New Issue
Block a user