Skip to main content

lightweight_pdf_core/
style.rs

1//! Flat style properties shared across elements (no cascading, see
2//! `plan/03-builder-api-design.md`).
3
4/// Opaque handle for a font. `lightweight-pdf-core` never sees font bytes, only this
5/// key (see `plan/00a-contracts-and-artifacts.md`, point 3).
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7pub struct FontKey(pub &'static str);
8
9impl FontKey {
10    pub const SANS_REGULAR: FontKey = FontKey("sans-regular");
11    pub const SANS_BOLD: FontKey = FontKey("sans-bold");
12}
13
14#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
15pub enum Align {
16    #[default]
17    Start,
18    Center,
19    End,
20}
21
22/// Overflow policy for explicitly, fixed-size elements. See
23/// `plan/05-overflow-and-robustness.md`, Grundprinzip 3. `Visible` is
24/// intentionally not part of V1 (ADR-011).
25#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
26pub enum Overflow {
27    /// Default: clip hard at the element's box.
28    #[default]
29    Clip,
30    /// Single-line text only: truncate with a trailing "…".
31    Ellipsis,
32}
33
34#[derive(Clone, Copy, PartialEq, Eq, Debug)]
35pub struct Color(pub u8, pub u8, pub u8);
36
37impl Color {
38    pub const BLACK: Color = Color(0, 0, 0);
39    pub const WHITE: Color = Color(255, 255, 255);
40
41    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
42        Color(r, g, b)
43    }
44}
45
46impl Default for Color {
47    fn default() -> Self {
48        Color::BLACK
49    }
50}
51
52#[derive(Clone, Copy, PartialEq, Debug)]
53pub struct Border {
54    pub width: f32,
55    pub color: Color,
56}
57
58#[derive(Clone, Copy, PartialEq, Debug)]
59pub struct TextStyle {
60    pub font: FontKey,
61    pub size: f32,
62    pub color: Color,
63    pub align: Align,
64    /// Multiple of `size`, e.g. 1.2 for 20% leading.
65    pub line_height: f32,
66}
67
68impl Default for TextStyle {
69    fn default() -> Self {
70        TextStyle {
71            font: FontKey::SANS_REGULAR,
72            size: 12.0,
73            color: Color::BLACK,
74            align: Align::Start,
75            line_height: 1.2,
76        }
77    }
78}
79
80/// Properties shared by every container/block element: `padding, width,
81/// height, overflow, background, border`, plus
82/// `flex` (taffy-vocabulary, ADR-004) and `keep_with_next` (ADR-007 /
83/// Grundprinzip 9). Deliberately no `margin` on elements (ADR/03: only
84/// `padding` + `Row`/`Column` `gap`, margin is a `Document`-level property).
85#[derive(Clone, Copy, PartialEq, Debug, Default)]
86pub struct Common {
87    pub width: Option<f32>,
88    pub height: Option<f32>,
89    /// Growth factor along the parent's main axis; `None` means "auto",
90    /// i.e. sized from measured content. Only has an effect when the
91    /// parent's main-axis size is bounded (see lightweight-pdf-layout Row/Column).
92    pub flex: Option<f32>,
93    pub padding: f32,
94    pub overflow: Overflow,
95    pub background: Option<Color>,
96    pub border: Option<Border>,
97    /// See `plan/05-overflow-and-robustness.md` Grundprinzip 9 / ADR-007:
98    /// only placed if the immediately following sibling also still fits.
99    pub keep_with_next: bool,
100}