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
use crate::wmf::imports::*;
/// The Scan Object specifies a collection of scanlines.
#[derive(Clone, Debug)]
#[allow(clippy::struct_field_names)]
pub struct Scan {
/// Count (2 bytes): A 16-bit unsigned integer that specifies the number of
/// horizontal (x-axis) coordinates in the ScanLines array. This value MUST
/// be a multiple of 2, since left and right endpoints are required to
/// specify each scanline.
pub count: u16,
/// Top (2 bytes): A 16-bit unsigned integer that defines the vertical
/// (y-axis) coordinate, in logical units, of the top scanline.
pub top: u16,
/// Bottom (2 bytes): A 16-bit unsigned integer that defines the vertical
/// (y-axis) coordinate, in logical units, of the bottom scanline.
pub bottom: u16,
/// ScanLines (variable): An array of scanlines, each specified by left and
/// right horizontal (x-axis) coordinates of its endpoints.
pub scan_lines: Vec<ScanLine>,
/// Count2 (2 bytes): A 16-bit unsigned integer that MUST be the same as
/// the value of the Count field; it is present to allow upward travel in
/// the structure.
pub count2: u16,
}
impl Scan {
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip_all,
err(level = tracing::Level::ERROR, Display),
))]
pub fn parse<R: crate::wmf::Read>(
buf: &mut R,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
let ((count, count_bytes), (top, top_bytes), (bottom, bottom_bytes)) = (
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
);
let mut consumed_bytes = count_bytes + top_bytes + bottom_bytes;
if count % 2 != 0 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: format!("The count field `{count}` must be even value"),
});
}
let line_count = count as usize / 2;
let mut scan_lines = Vec::with_capacity(line_count);
for _ in 0..line_count {
let (v, c) = ScanLine::parse(buf)?;
consumed_bytes += c;
scan_lines.push(v);
}
let (count2, c) = crate::wmf::parser::read_u16_from_le_bytes(buf)?;
consumed_bytes += c;
if count != count2 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: format!(
"The count field `{count}` and count2 field `{count2}` \
must have same value"
),
});
}
Ok((
Self {
count,
top,
bottom,
scan_lines,
count2,
},
consumed_bytes,
))
}
}
#[derive(Clone, Debug)]
pub struct ScanLine {
/// Left (2 bytes): A 16-bit unsigned integer that defines the horizontal
/// (x-axis) coordinate, in logical units, of the left endpoint of the
/// scanline.
pub left: u16,
/// Right (2 bytes): A 16-bit unsigned integer that defines the horizontal
/// (x-axis) coordinate, in logical units, of the right endpoint of the
/// scanline.
pub right: u16,
}
impl ScanLine {
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip_all,
err(level = tracing::Level::ERROR, Display),
))]
pub fn parse<R: crate::wmf::Read>(
buf: &mut R,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
let ((left, left_bytes), (right, right_bytes)) = (
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
);
Ok((Self { left, right }, left_bytes + right_bytes))
}
}