summaryrefslogtreecommitdiff
path: root/src/layout/mod.rs
blob: 1cc16a2685be74490fd07806995d40f655a30530 (plain) (blame)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! The core layouting engine.

use std::io::{self, Write};
use smallvec::SmallVec;
use toddle::query::{SharedFontLoader, FontIndex};

use crate::error::Error;
use crate::syntax::{SyntaxModel, SpanVec};
use crate::size::{Size, Size2D, SizeBox};
use crate::style::LayoutStyle;

mod actions;
mod model;
mod line;
mod stack;
mod text;

/// Common types for layouting.
pub mod prelude {
    pub use super::*;
    pub use GenericAxis::*;
    pub use SpecificAxis::*;
    pub use Direction::*;
    pub use Alignment::*;
}

/// Different kinds of layouters (fully re-exported).
pub mod layouters {
    pub use super::model::ModelLayouter;
    pub use super::line::{LineLayouter, LineContext};
    pub use super::stack::{StackLayouter, StackContext};
    pub use super::text::{layout_text, TextContext};
}

pub use self::actions::{LayoutAction, LayoutActions};
pub use self::layouters::*;
pub use self::prelude::*;


/// A collection of layouts.
pub type MultiLayout = Vec<Layout>;

/// A sequence of layouting actions inside a box.
#[derive(Debug, Clone)]
pub struct Layout {
    /// The size of the box.
    pub dimensions: Size2D,
    /// How to align this layout in a parent container.
    pub alignment: LayoutAlignment,
    /// The actions composing this layout.
    pub actions: Vec<LayoutAction>,
}

impl Layout {
    /// Returns a vector with all used font indices.
    pub fn find_used_fonts(&self) -> Vec<FontIndex> {
        let mut fonts = Vec::new();
        for action in &self.actions {
            if let LayoutAction::SetFont(index, _) = action {
                if !fonts.contains(index) {
                    fonts.push(*index);
                }
            }
        }
        fonts
    }
}

/// The general context for layouting.
#[derive(Debug, Clone)]
pub struct LayoutContext<'a, 'p> {
    /// The font loader to retrieve fonts from when typesetting text
    /// using [`layout_text`].
    pub loader: &'a SharedFontLoader<'p>,
    /// The style for pages and text.
    pub style: &'a LayoutStyle,
    /// The base unpadded dimensions of this container (for relative sizing).
    pub base: Size2D,
    /// The spaces to layout in.
    pub spaces: LayoutSpaces,
    /// Whether to have repeated spaces or to use only the first and only once.
    pub repeat: bool,
    /// The initial axes along which content is laid out.
    pub axes: LayoutAxes,
    /// The alignment of the finished layout.
    pub alignment: LayoutAlignment,
    /// Whether the layout that is to be created will be nested in a parent
    /// container.
    pub nested: bool,
    /// Whether to debug render a box around the layout.
    pub debug: bool,
}

pub struct Layouted<T> {
    pub output: T,
    pub errors: SpanVec<Error>,
}

impl<T> Layouted<T> {
    pub fn map<F, U>(self, f: F) -> Layouted<U> where F: FnOnce(T) -> U {
        Layouted {
            output: f(self.output),
            errors: self.errors,
        }
    }
}

pub async fn layout(model: &SyntaxModel, ctx: LayoutContext<'_, '_>) -> Layouted<MultiLayout> {
    let mut layouter = ModelLayouter::new(ctx);
    layouter.layout_syntax_model(model).await;
    layouter.finish()
}

/// A possibly stack-allocated vector of layout spaces.
pub type LayoutSpaces = SmallVec<[LayoutSpace; 2]>;

/// Spacial layouting constraints.
#[derive(Debug, Copy, Clone)]
pub struct LayoutSpace {
    /// The maximum size of the box to layout in.
    pub dimensions: Size2D,
    /// Padding that should be respected on each side.
    pub padding: SizeBox,
    /// Whether to expand the dimensions of the resulting layout to the full
    /// dimensions of this space or to shrink them to fit the content for the
    /// horizontal and vertical axis.
    pub expansion: LayoutExpansion,
}

impl LayoutSpace {
    /// The offset from the origin to the start of content, that is,
    /// `(padding.left, padding.top)`.
    pub fn start(&self) -> Size2D {
        Size2D::new(self.padding.left, self.padding.top)
    }

    /// The actually usable area (dimensions minus padding).
    pub fn usable(&self) -> Size2D {
        self.dimensions.unpadded(self.padding)
    }

    /// A layout space without padding and dimensions reduced by the padding.
    pub fn usable_space(&self) -> LayoutSpace {
        LayoutSpace {
            dimensions: self.usable(),
            padding: SizeBox::ZERO,
            expansion: LayoutExpansion::new(false, false),
        }
    }
}

/// Whether to fit to content or expand to the space's size.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LayoutExpansion {
    pub horizontal: bool,
    pub vertical: bool,
}

impl LayoutExpansion {
    pub fn new(horizontal: bool, vertical: bool) -> LayoutExpansion {
        LayoutExpansion { horizontal, vertical }
    }

    /// Borrow the specified component mutably.
    pub fn get_mut(&mut self, axis: SpecificAxis) -> &mut bool {
        match axis {
            Horizontal => &mut self.horizontal,
            Vertical => &mut self.vertical,
        }
    }
}

/// The axes along which the content is laid out.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LayoutAxes {
    pub primary: Direction,
    pub secondary: Direction,
}

impl LayoutAxes {
    pub fn new(primary: Direction, secondary: Direction) -> LayoutAxes {
        if primary.axis() == secondary.axis() {
            panic!("LayoutAxes::new: invalid aligned axes {:?} and {:?}",
                primary, secondary);
        }

        LayoutAxes { primary, secondary }
    }

    /// Return the direction of the specified generic axis.
    pub fn get(self, axis: GenericAxis) -> Direction {
        match axis {
            Primary => self.primary,
            Secondary => self.secondary,
        }
    }

    /// Borrow the direction of the specified generic axis mutably.
    pub fn get_mut(&mut self, axis: GenericAxis) -> &mut Direction {
        match axis {
            Primary => &mut self.primary,
            Secondary => &mut self.secondary,
        }
    }

    /// Return the direction of the specified specific axis.
    pub fn get_specific(self, axis: SpecificAxis) -> Direction {
        self.get(axis.to_generic(self))
    }
}

/// The two generic layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum GenericAxis {
    Primary,
    Secondary,
}

impl GenericAxis {
    /// The specific version of this axis in the given system of axes.
    pub fn to_specific(self, axes: LayoutAxes) -> SpecificAxis {
        axes.get(self).axis()
    }

    /// The other axis.
    pub fn inv(self) -> GenericAxis {
        match self {
            Primary => Secondary,
            Secondary => Primary,
        }
    }
}

/// The two specific layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum SpecificAxis {
    Horizontal,
    Vertical,
}

impl SpecificAxis {
    /// The generic version of this axis in the given system of axes.
    pub fn to_generic(self, axes: LayoutAxes) -> GenericAxis {
        if self == axes.primary.axis() { Primary } else { Secondary }
    }

    /// The other axis.
    pub fn inv(self) -> SpecificAxis {
        match self {
            Horizontal => Vertical,
            Vertical => Horizontal,
        }
    }
}

/// Directions along which content is laid out.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Direction {
    LeftToRight,
    RightToLeft,
    TopToBottom,
    BottomToTop,
}

impl Direction {
    /// The specific axis this direction belongs to.
    pub fn axis(self) -> SpecificAxis {
        match self {
            LeftToRight | RightToLeft => Horizontal,
            TopToBottom | BottomToTop => Vertical,
        }
    }

    /// Whether this axis points into the positive coordinate direction.
    pub fn is_positive(self) -> bool {
        match self {
            LeftToRight | TopToBottom => true,
            RightToLeft | BottomToTop => false,
        }
    }

    /// The inverse axis.
    pub fn inv(self) -> Direction {
        match self {
            LeftToRight => RightToLeft,
            RightToLeft => LeftToRight,
            TopToBottom => BottomToTop,
            BottomToTop => TopToBottom,
        }
    }

    /// The factor for this direction.
    ///
    /// - `1` if the direction is positive.
    /// - `-1` if the direction is negative.
    pub fn factor(self) -> i32 {
        if self.is_positive() { 1 } else { -1 }
    }
}

/// Where to align a layout in a container.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LayoutAlignment {
    pub primary: Alignment,
    pub secondary: Alignment,
}

impl LayoutAlignment {
    pub fn new(primary: Alignment, secondary: Alignment) -> LayoutAlignment {
        LayoutAlignment { primary, secondary }
    }

    /// Borrow the alignment of the specified generic axis mutably.
    pub fn get_mut(&mut self, axis: GenericAxis) -> &mut Alignment {
        match axis {
            Primary => &mut self.primary,
            Secondary => &mut self.secondary,
        }
    }
}

/// Where to align content.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Alignment {
    Origin,
    Center,
    End,
}

impl Alignment {
    /// The inverse alignment.
    pub fn inv(self) -> Alignment {
        match self {
            Origin => End,
            Center => Center,
            End => Origin,
        }
    }
}

/// Whitespace between boxes with different interaction properties.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpacingKind {
    /// A hard space consumes surrounding soft spaces and is always layouted.
    Hard,
    /// A soft space consumes surrounding soft spaces with higher value.
    Soft(u32),
}

impl SpacingKind {
    /// The standard spacing kind used for paragraph spacing.
    pub const PARAGRAPH: SpacingKind = SpacingKind::Soft(1);

    /// The standard spacing kind used for line spacing.
    pub const LINE: SpacingKind = SpacingKind::Soft(2);

    /// The standard spacing kind used for word spacing.
    pub const WORD: SpacingKind = SpacingKind::Soft(1);
}

/// The last appeared spacing.
#[derive(Debug, Copy, Clone, PartialEq)]
enum LastSpacing {
    Hard,
    Soft(Size, u32),
    None,
}

impl LastSpacing {
    /// The size of the soft space if this is a soft space or zero otherwise.
    fn soft_or_zero(self) -> Size {
        match self {
            LastSpacing::Soft(space, _) => space,
            _ => Size::ZERO,
        }
    }
}

/// Layout components that can be serialized.
pub trait Serialize {
    /// Serialize the data structure into an output writable.
    fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()>;
}

impl Serialize for Layout {
    fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
        writeln!(f, "{:.4} {:.4}", self.dimensions.x.to_pt(), self.dimensions.y.to_pt())?;
        writeln!(f, "{}", self.actions.len())?;
        for action in &self.actions {
            action.serialize(f)?;
            writeln!(f)?;
        }
        Ok(())
    }
}

impl Serialize for MultiLayout {
    fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
        writeln!(f, "{}", self.len())?;
        for layout in self {
            layout.serialize(f)?;
        }
        Ok(())
    }
}