forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

titles.rs120 lines, 4.4 KiBHistoryRawDownload
1//! Where a title card sits over the preview's picture, how much of it shows at
2//! a frame, and which titles are up. Pure geometry, so the preview
3//! ([`crate::ui::preview`]) draws what this says and the tests pin it without a
4//! window. The numbers are the engine's ([`engine::text`]): the same margins,
5//! the same size as a share of the picture height, the same box padding and
6//! outline weights, so a title reads where the export burns it.
7
8use engine::Title;
9use engine::text::{Outline, TextStyle};
10
11/// The title-safe margin, as a share of the picture height, on all four sides.
12pub(crate) const MARGIN: f32 = 0.05;
13
14/// The engine's line pitch in em: Archivo's ascent plus descent and line gap
15/// (878 + 210 + 0 over 1000 units), times the engine's 1.1 leading.
16pub(crate) const LINE_EM: f32 = (0.878 + 0.210) * 1.1;
17
18/// The box behind the words, in em: the horizontal and vertical padding.
19pub(crate) const BOX_PAD_X_EM: f32 = 0.3;
20pub(crate) const BOX_PAD_Y_EM: f32 = 0.15;
21
22/// The smallest em the engine draws at, in px.
23const MIN_EM_PX: f32 = 6.0;
24
25/// A rectangle in window pixels, or in the region's own local pixels: the
26/// caller says which.
27#[derive(Clone, Copy, Debug, PartialEq)]
28pub(crate) struct Rect {
29 pub(crate) x: f32,
30 pub(crate) y: f32,
31 pub(crate) w: f32,
32 pub(crate) h: f32,
33}
34
35/// The rectangle a `frame_w` x `frame_h` picture takes when it is fitted inside
36/// `region` and centred, the way the picture itself is drawn
37/// ([`crate::render::letterboxed_image`], `ObjectFit::Contain`). An empty
38/// picture or region has no picture to fit: the region comes back as it is.
39pub(crate) fn contain(region: Rect, frame_w: f32, frame_h: f32) -> Rect {
40 if frame_w <= 0. || frame_h <= 0. || region.w <= 0. || region.h <= 0. {
41 return region;
42 }
43 let scale = (region.w / frame_w).min(region.h / frame_h);
44 let (w, h) = (frame_w * scale, frame_h * scale);
45 Rect {
46 x: region.x + (region.w - w) / 2.,
47 y: region.y + (region.h - h) / 2.,
48 w,
49 h,
50 }
51}
52
53/// The em size in px for a style: its size in permille of the picture's
54/// displayed height, as the engine reads it (`size / 1000 * frame height`),
55/// with the engine's six-pixel floor.
56pub(crate) fn em_px(style: &TextStyle, picture_h: f32) -> f32 {
57 (style.clamped().size as f32 / 1000. * picture_h).max(MIN_EM_PX)
58}
59
60/// The pitch of one line of a title at `em`.
61pub(crate) fn line_px(em: f32) -> f32 {
62 em * LINE_EM
63}
64
65/// The area a title's words are set in over `picture`: the picture inset by
66/// the title-safe margin on all four sides. Words wrap to its width, and sit
67/// at the top, the middle or the bottom of its height as the style's place
68/// says: the drawn block's own height depends on how the words wrap, so the
69/// vertical placement is the caller's to make inside this rectangle.
70pub(crate) fn title_area(picture: Rect) -> Rect {
71 let margin = MARGIN * picture.h;
72 Rect {
73 x: picture.x + margin,
74 y: picture.y + margin,
75 w: (picture.w - 2. * margin).max(0.),
76 h: (picture.h - 2. * margin).max(0.),
77 }
78}
79
80/// The copies that draw the outline, in px offsets off the text, the engine's
81/// stroke radius for the style's em: a radius of em/24 (thin) or em/12 (thick),
82/// at least one and two px. Four diagonals for thin, eight around for thick.
83pub(crate) fn outline_offsets(outline: Outline, em: f32) -> Vec<(f32, f32)> {
84 let r = match outline {
85 Outline::None => return Vec::new(),
86 Outline::Thin => (em / 24.).round().max(1.),
87 Outline::Thick => (em / 12.).round().max(2.),
88 };
89 match outline {
90 Outline::Thick => vec![
91 (-r, 0.),
92 (r, 0.),
93 (0., -r),
94 (0., r),
95 (-r, -r),
96 (r, -r),
97 (-r, r),
98 (r, r),
99 ],
100 _ => vec![(-r, -r), (r, -r), (-r, r), (r, r)],
101 }
102}
103
104/// The opacity of a title at its frame: its 0 to 255 alpha as a fraction.
105pub(crate) fn opacity(alpha: u8) -> f32 {
106 f32::from(alpha) / 255.
107}
108
109/// The titles up at timeline `frame`, in list order (a later one draws over an
110/// earlier one), each with its alpha. A title whose fade has it at nothing is
111/// not up.
112pub(crate) fn active_titles(titles: &[Title], frame: u32) -> Vec<(&Title, u8)> {
113 titles
114 .iter()
115 .filter_map(|t| match t.alpha_at(frame) {
116 0 => None,
117 a => Some((t, a)),
118 })
119 .collect()
120}