summaryrefslogtreecommitdiff
path: root/tests/src/text.rs
blob: 6f7c7d83b22d09852e8ecacc159ee924dc5e0b0b (plain)
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
121
122
123
124
125
126
127
use cairo;
use float_cmp::approx_eq;
use librsvg::{CairoRenderer, Loader};

use crate::reference_utils::{Compare, Evaluate, Reference};
use crate::utils::{load_svg, render_document, setup_font_map, SurfaceSize};
use crate::{test_compare_render_output, test_svg_reference};

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

test_svg_reference!(
    text_anchor_chunk_806,
    "tests/fixtures/text/bug806-text-anchor-chunk.svg",
    "tests/fixtures/text/bug806-text-anchor-chunk-ref.svg"
);

test_svg_reference!(
    span_bounds_when_offset_by_dx,
    "tests/fixtures/text/span-bounds-when-offset-by-dx.svg",
    "tests/fixtures/text/span-bounds-when-offset-by-dx-ref.svg"
);

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

test_svg_reference!(
    unicode_bidi_override,
    "tests/fixtures/text/unicode-bidi-override.svg",
    "tests/fixtures/text/unicode-bidi-override-ref.svg"
);

test_svg_reference!(
    display_none,
    "tests/fixtures/text/display-none.svg",
    "tests/fixtures/text/display-none-ref.svg"
);

test_svg_reference!(
    visibility_hidden,
    "tests/fixtures/text/visibility-hidden.svg",
    "tests/fixtures/text/visibility-hidden-ref.svg"
);

test_svg_reference!(
    visibility_hidden_x_attr,
    "tests/fixtures/text/visibility-hidden-x-attr.svg",
    "tests/fixtures/text/visibility-hidden-ref.svg"
);

test_svg_reference!(
    bounds,
    "tests/fixtures/text/bounds.svg",
    "tests/fixtures/text/bounds-ref.svg"
);

fn rect(x: f64, y: f64, width: f64, height: f64) -> cairo::Rectangle {
    cairo::Rectangle::new(x, y, width, height)
}

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

// Test that the computed geometry of text layers is as expected.
#[test]
fn test_text_layer_geometry() {
    setup_font_map();

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

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

    let viewport = rect(0.0, 0.0, 600.0, 600.0);

    // tuples of (element_id, ink_rect)
    let cases = vec![
        ("#a", rect(50.0, 60.0, 100.0, 50.0)),
        ("#b", rect(200.0, 60.0, 50.0, 100.0)),
        ("#c", rect(300.0, 60.0, 50.0, 100.0)),
        ("#d", rect(400.0, 60.0, 100.0, 50.0)),
    ];

    for (id, expected_ink_rect) in cases {
        let (ink_rect, _) = renderer.geometry_for_layer(Some(id), &viewport).unwrap();
        assert!(
            rectangle_approx_eq(&ink_rect, &expected_ink_rect),
            "ink_rect: {:?}, expected: {:?}",
            ink_rect,
            expected_ink_rect
        );
    }
}