1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Fit paths into rectangles.

use crate::aabb::bounding_rect;
use crate::math::*;
use crate::path::builder::*;
use crate::path::iterator::*;
use crate::path::Path;

/// The strategy to use when fitting (stretching, overflow, etc.)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum FitStyle {
    /// Stretch vertically and horizontally to fit the destination rectangle exactly.
    Stretch,
    /// Uniformly scale without overflow.
    Min,
    /// Uniformly scale with overflow.
    Max,
    /// Uniformly scale to fit horizontally.
    Horizontal,
    /// Uniformly scale to fit vertically.
    Vertical,
}

/// Computes a transform that fits a rectangle into another one.
pub fn fit_rectangle(src_rect: &Rect, dst_rect: &Rect, style: FitStyle) -> Transform {
    let scale: Vector = vector(
        dst_rect.size.width / src_rect.size.width,
        dst_rect.size.height / src_rect.size.height,
    );

    let scale = match style {
        FitStyle::Stretch => scale,
        FitStyle::Min => {
            let s = f32::min(scale.x, scale.y);
            vector(s, s)
        }
        FitStyle::Max => {
            let s = f32::max(scale.x, scale.y);
            vector(s, s)
        }
        FitStyle::Horizontal => vector(scale.x, scale.x),
        FitStyle::Vertical => vector(scale.y, scale.y),
    };

    let src_center = src_rect.origin.lerp(src_rect.max(), 0.5);
    let dst_center = dst_rect.origin.lerp(dst_rect.max(), 0.5);

    Transform::create_translation(-src_center.x, -src_center.y)
        .post_scale(scale.x, scale.y)
        .post_translate(dst_center.to_vector())
}

/// Fits a path into a rectangle.
pub fn fit_path(path: &Path, output_rect: &Rect, style: FitStyle) -> Path {
    let aabb = bounding_rect(path.iter());
    let transform = fit_rectangle(&aabb, output_rect, style);

    let mut builder = Path::builder();
    for evt in path.iter().transformed(&transform) {
        builder.path_event(evt)
    }

    builder.build()
}

#[test]
fn simple_fit() {
    fn approx_eq(a: &Rect, b: &Rect) -> bool {
        use crate::geom::euclid::approxeq::ApproxEq;
        let result = a.origin.approx_eq(&b.origin) && a.max().approx_eq(&b.max());
        if !result {
            println!("{:?} == {:?}", a, b);
        }
        result
    }

    let t = fit_rectangle(
        &rect(0.0, 0.0, 1.0, 1.0),
        &rect(0.0, 0.0, 2.0, 2.0),
        FitStyle::Stretch,
    );

    assert!(approx_eq(
        &t.transform_rect(&rect(0.0, 0.0, 1.0, 1.0)),
        &rect(0.0, 0.0, 2.0, 2.0)
    ));

    let t = fit_rectangle(
        &rect(1.0, 2.0, 4.0, 4.0),
        &rect(0.0, 0.0, 2.0, 8.0),
        FitStyle::Stretch,
    );

    assert!(approx_eq(
        &t.transform_rect(&rect(1.0, 2.0, 4.0, 4.0)),
        &rect(0.0, 0.0, 2.0, 8.0)
    ));

    let t = fit_rectangle(
        &rect(1.0, 2.0, 2.0, 4.0),
        &rect(0.0, 0.0, 2.0, 2.0),
        FitStyle::Horizontal,
    );

    assert!(approx_eq(
        &t.transform_rect(&rect(1.0, 2.0, 2.0, 4.0)),
        &rect(0.0, -1.0, 2.0, 4.0)
    ));

    let t = fit_rectangle(
        &rect(1.0, 2.0, 2.0, 2.0),
        &rect(0.0, 0.0, 4.0, 2.0),
        FitStyle::Horizontal,
    );

    assert!(approx_eq(
        &t.transform_rect(&rect(1.0, 2.0, 2.0, 2.0)),
        &rect(0.0, -1.0, 4.0, 4.0)
    ));
}