1
1
use float_cmp::approx_eq;
2
use rsvg::{CairoRenderer, Loader};
3

            
4
use rsvg::test_utils::setup_font_map;
5
use rsvg::{test_compare_render_output, test_svg_reference};
6

            
7
// From https://www.w3.org/Style/CSS/Test/Fonts/Ahem/
8
//
9
//   > The Ahem font was developed by Todd Fahrner and Myles C. Maxfield to
10
//   > help test writers develop predictable tests. The units per em is 1000,
11
//   > the ascent is 800, and the descent is 200, thereby making the em
12
//   > square exactly square. The glyphs for most characters is simply a box
13
//   > which fills this square. The codepoints mapped to this full square
14
//   > with a full advance are the following ranges:
15
//
16
// So, ascent is 4/5 of the font-size, descent is 1/5.  Mind the positions below.
17
test_compare_render_output!(
18
    ahem_font,
19
    500,
20
    500,
21
    br##"<?xml version="1.0" encoding="UTF-8"?>
22
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
23
  <text style="font: 50px Ahem;" x="50" y="50" fill="black">abcde</text>
24
</svg>"##,
25
    br##"<?xml version="1.0" encoding="UTF-8"?>
26
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
27
  <rect x="50" y="10" width="250" height="50" fill="black"/>
28
</svg>"##,
29
);
30

            
31
test_svg_reference!(
32
    text_anchor_chunk_806,
33
    "tests/fixtures/text/bug806-text-anchor-chunk.svg",
34
    "tests/fixtures/text/bug806-text-anchor-chunk-ref.svg"
35
);
36

            
37
test_svg_reference!(
38
    span_bounds_when_offset_by_dx,
39
    "tests/fixtures/text/span-bounds-when-offset-by-dx.svg",
40
    "tests/fixtures/text/span-bounds-when-offset-by-dx-ref.svg"
41
);
42

            
43
// FIXME: Ignored because with the change to render all text as paths, the rendering
44
// of these is different.
45
//
46
// test_svg_reference!(
47
//     tspan_direction_change_804,
48
//     "tests/fixtures/text/bug804-tspan-direction-change.svg",
49
//     "tests/fixtures/text/bug804-tspan-direction-change-ref.svg"
50
// );
51

            
52
test_svg_reference!(
53
    unicode_bidi_override,
54
    "tests/fixtures/text/unicode-bidi-override.svg",
55
    "tests/fixtures/text/unicode-bidi-override-ref.svg"
56
);
57

            
58
test_svg_reference!(
59
    display_none,
60
    "tests/fixtures/text/display-none.svg",
61
    "tests/fixtures/text/display-none-ref.svg"
62
);
63

            
64
test_svg_reference!(
65
    visibility_hidden,
66
    "tests/fixtures/text/visibility-hidden.svg",
67
    "tests/fixtures/text/visibility-hidden-ref.svg"
68
);
69

            
70
test_svg_reference!(
71
    visibility_hidden_x_attr,
72
    "tests/fixtures/text/visibility-hidden-x-attr.svg",
73
    "tests/fixtures/text/visibility-hidden-ref.svg"
74
);
75

            
76
test_svg_reference!(
77
    bounds,
78
    "tests/fixtures/text/bounds.svg",
79
    "tests/fixtures/text/bounds-ref.svg"
80
);
81

            
82
5
fn rect(x: f64, y: f64, width: f64, height: f64) -> cairo::Rectangle {
83
5
    cairo::Rectangle::new(x, y, width, height)
84
5
}
85

            
86
4
fn rectangle_approx_eq(a: &cairo::Rectangle, b: &cairo::Rectangle) -> bool {
87
    // FIXME: this is super fishy; shouldn't we be using 2x the epsilon against the width/height
88
    // instead of the raw coordinates?
89
4
    approx_eq!(f64, a.x(), b.x())
90
4
        && approx_eq!(f64, a.y(), b.y())
91
4
        && approx_eq!(f64, a.width(), b.width())
92
4
        && approx_eq!(f64, a.height(), b.height())
93
4
}
94

            
95
// Test that the computed geometry of text layers is as expected.
96
#[test]
97
2
fn test_text_layer_geometry() {
98
1
    setup_font_map();
99

            
100
1
    let handle = Loader::new()
101
        .read_path("tests/fixtures/text/bounds.svg")
102
        .unwrap_or_else(|e| panic!("could not load: {}", e));
103

            
104
1
    let renderer = CairoRenderer::new(&handle).test_mode(true);
105

            
106
1
    let viewport = rect(0.0, 0.0, 600.0, 600.0);
107

            
108
    // tuples of (element_id, ink_rect)
109
2
    let cases = vec![
110
1
        ("#a", rect(50.0, 60.0, 100.0, 50.0)),
111
1
        ("#b", rect(200.0, 60.0, 50.0, 100.0)),
112
1
        ("#c", rect(300.0, 60.0, 50.0, 100.0)),
113
1
        ("#d", rect(400.0, 60.0, 100.0, 50.0)),
114
    ];
115

            
116
5
    for (id, expected_ink_rect) in cases {
117
4
        let (ink_rect, _) = renderer.geometry_for_layer(Some(id), &viewport).unwrap();
118
        assert!(
119
4
            rectangle_approx_eq(&ink_rect, &expected_ink_rect),
120
            "ink_rect: {:?}, expected: {:?}",
121
            ink_rect,
122
            expected_ink_rect
123
        );
124
    }
125
2
}