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
test_svg_reference!(
83
    bug1109_scaled_text_anchor_middle,
84
    "tests/fixtures/text/bug1109-scaled-text-anchor-middle.svg",
85
    "tests/fixtures/text/bug1109-scaled-text-anchor-middle-ref.svg"
86
);
87

            
88
5
fn rect(x: f64, y: f64, width: f64, height: f64) -> cairo::Rectangle {
89
5
    cairo::Rectangle::new(x, y, width, height)
90
5
}
91

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

            
101
// Test that the computed geometry of text layers is as expected.
102
#[test]
103
2
fn test_text_layer_geometry() {
104
1
    setup_font_map();
105

            
106
1
    let handle = Loader::new()
107
        .read_path("tests/fixtures/text/bounds.svg")
108
        .unwrap_or_else(|e| panic!("could not load: {}", e));
109

            
110
1
    let renderer = CairoRenderer::new(&handle).test_mode(true);
111

            
112
1
    let viewport = rect(0.0, 0.0, 600.0, 600.0);
113

            
114
    // tuples of (element_id, ink_rect)
115
2
    let cases = vec![
116
1
        ("#a", rect(50.0, 60.0, 100.0, 50.0)),
117
1
        ("#b", rect(200.0, 60.0, 50.0, 100.0)),
118
1
        ("#c", rect(300.0, 60.0, 50.0, 100.0)),
119
1
        ("#d", rect(400.0, 60.0, 100.0, 50.0)),
120
    ];
121

            
122
5
    for (id, expected_ink_rect) in cases {
123
4
        let (ink_rect, _) = renderer.geometry_for_layer(Some(id), &viewport).unwrap();
124
        assert!(
125
4
            rectangle_approx_eq(&ink_rect, &expected_ink_rect),
126
            "ink_rect: {:?}, expected: {:?}",
127
            ink_rect,
128
            expected_ink_rect
129
        );
130
1
    }
131
2
}