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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
use text::{
Color, RasterOptions, StrokeStyle, TextAlign, TextBackground, TextEngine, TextLayoutParams,
rasterize, text_visual_rect,
};
const FONT_REGULAR: &[u8] = include_bytes!("fixtures/DejaVuSans.ttf");
const FONT_BOLD: &[u8] = include_bytes!("fixtures/DejaVuSans-Bold.ttf");
fn engine() -> TextEngine {
TextEngine::with_font_bytes(&[FONT_REGULAR, FONT_BOLD])
}
fn params(content: &str) -> TextLayoutParams {
TextLayoutParams {
content: content.to_string(),
font_family: "DejaVu Sans".to_string(),
..TextLayoutParams::default()
}
}
fn opaque_pixels(buffer: &text::RgbaBuffer) -> usize {
buffer.pixels.chunks(4).filter(|px| px[3] > 0).count()
}
#[test]
fn raster_produces_non_empty_output_with_visual_rect_dimensions() {
let mut engine = engine();
let measured = engine.measure(¶ms("Hello"), 90.0);
let visual = text_visual_rect(
TextAlign::Center,
&measured.block,
&TextBackground::default(),
1.0,
);
let buffer = rasterize(
&mut engine,
&measured,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: None,
stroke: None,
},
);
assert_eq!(buffer.width, visual.width.ceil() as u32);
assert_eq!(buffer.height, visual.height.ceil() as u32);
assert_eq!(buffer.pixels.len(), buffer.width as usize * buffer.height as usize * 4);
assert!(opaque_pixels(&buffer) > 0);
assert_eq!(buffer.pixel(0, 0).a, 0);
}
#[test]
fn raster_fills_background_inside_padded_box() {
let mut engine = engine();
let measured = engine.measure(¶ms("Hello"), 90.0);
let background = TextBackground {
enabled: true,
color: "#ff0000".to_string(),
..TextBackground::default()
};
let red = Color::rgb(255, 0, 0);
let buffer = rasterize(
&mut engine,
&measured,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: Some((&background, red)),
stroke: None,
},
);
assert_eq!(buffer.height, 102);
let corner = buffer.pixel(2, 2);
assert!(corner.a > 200);
assert!(corner.r > 200);
assert!(corner.g < 60);
assert!(corner.b < 60);
}
#[test]
fn raster_dimensions_grow_with_background_padding() {
let mut engine = engine();
let measured = engine.measure(¶ms("Hello"), 90.0);
let plain = rasterize(
&mut engine,
&measured,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: None,
stroke: None,
},
);
let background = TextBackground {
enabled: true,
color: "#ff0000".to_string(),
..TextBackground::default()
};
let padded = rasterize(
&mut engine,
&measured,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: Some((&background, Color::rgb(255, 0, 0))),
stroke: None,
},
);
assert_eq!(padded.width, plain.width + 60);
assert_eq!(padded.height, plain.height + 84);
}
#[test]
fn raster_underline_adds_pixels_below_text() {
let mut engine = engine();
let plain_params = params("Hello");
let plain = engine.measure(&plain_params, 90.0);
let plain_buffer = rasterize(
&mut engine,
&plain,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: None,
stroke: None,
},
);
let underlined_params = TextLayoutParams {
text_decoration: text::TextDecoration::Underline,
..params("Hello")
};
let underlined = engine.measure(&underlined_params, 90.0);
let underlined_buffer = rasterize(
&mut engine,
&underlined,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: None,
stroke: None,
},
);
assert!(opaque_pixels(&underlined_buffer) > opaque_pixels(&plain_buffer));
}
#[test]
fn raster_stroke_widens_glyph_coverage() {
let mut engine = engine();
let measured = engine.measure(¶ms("Hello"), 180.0);
let fill_only = rasterize(
&mut engine,
&measured,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: None,
stroke: None,
},
);
let stroked = rasterize(
&mut engine,
&measured,
&RasterOptions {
text_color: Color::rgb(255, 255, 255),
background: None,
stroke: Some(StrokeStyle {
color: Color::rgb(0, 0, 0),
width: 4.0,
}),
},
);
assert!(opaque_pixels(&stroked) > opaque_pixels(&fill_only));
}
|