mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-02 06:57:04 -06:00
Text: preserve signed insets and scale only when both are non-negative
Address review finding: keep signed insets unchanged when their sum fits, scaling opposing insets proportionally only when both in the pair are non-negative and their sum exceeds the box dimension.
This commit is contained in:
@@ -94,18 +94,18 @@ impl BoxGeom {
|
||||
text_rect
|
||||
};
|
||||
// Insets wider than the shape scale down proportionally to fit inside it (#1632).
|
||||
let (mut ins_l, mut ins_r) = (bp.inset_l.unwrap_or(7.2).max(0.0), bp.inset_r.unwrap_or(7.2).max(0.0));
|
||||
let (mut ins_t, mut ins_b) = (bp.inset_t.unwrap_or(3.6).max(0.0), bp.inset_b.unwrap_or(3.6).max(0.0));
|
||||
let (mut ins_l, mut ins_r) = (bp.inset_l.unwrap_or(7.2), bp.inset_r.unwrap_or(7.2));
|
||||
let (mut ins_t, mut ins_b) = (bp.inset_t.unwrap_or(3.6), bp.inset_b.unwrap_or(3.6));
|
||||
let w = r.width().max(0.0);
|
||||
let total_x = ins_l + ins_r;
|
||||
if total_x > w && total_x > 0.0 {
|
||||
if ins_l >= 0.0 && ins_r >= 0.0 && total_x > w && total_x > 0.0 {
|
||||
let s = w / total_x;
|
||||
ins_l *= s;
|
||||
ins_r *= s;
|
||||
}
|
||||
let h = r.height().max(0.0);
|
||||
let total_y = ins_t + ins_b;
|
||||
if total_y > h && total_y > 0.0 {
|
||||
if ins_t >= 0.0 && ins_b >= 0.0 && total_y > h && total_y > 0.0 {
|
||||
let s = h / total_y;
|
||||
ins_t *= s;
|
||||
ins_b *= s;
|
||||
|
||||
@@ -1949,3 +1949,18 @@ fn insets_wider_than_the_shape_scale_down_within_the_shape_bounds() {
|
||||
// The text center must remain inside the 14.17 pt wide shape, not pushed ~127.5 pt to the right.
|
||||
assert!((0.0..=14.17).contains(&mid), "line center {mid} should be within [0, 14.17]");
|
||||
}
|
||||
|
||||
/// #1632: signed insets (e.g. negative left inset with positive right inset) are preserved when
|
||||
/// their sum fits within the shape bounds.
|
||||
#[test]
|
||||
fn signed_insets_are_preserved_when_they_fit() {
|
||||
let (p, mut sh) = setup("Text", 100.0, 50.0);
|
||||
sh.ph = None;
|
||||
let bp = &mut sh.text.as_mut().unwrap().body;
|
||||
bp.inset_l = Some(-10.0);
|
||||
bp.inset_r = Some(10.0);
|
||||
sh.text.as_mut().unwrap().paragraphs[0].props = deckcraft_model::text::ParaProps { align: Some(Align::Left), ..Default::default() };
|
||||
let l = lay(&p, &sh, 100.0, 50.0);
|
||||
assert_eq!(l.lines.len(), 1);
|
||||
assert_eq!(l.inner.x0, -10.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user