zng_ext_font/unit.rs
1use zng_layout::unit::*;
2use zng_var::impl_from_and_into_var;
3
4/// Text font size.
5///
6/// The [`Default`] value is the *root* font size, usually the one set in the window widget.
7///
8/// [`Default`]: Length::Default
9pub type FontSize = Length;
10
11/// Text line height.
12///
13/// The [`Default`] value is computed from the font metrics, `ascent - descent + line_gap`, this is
14/// usually similar to `1.2.em()`. Relative values are computed from the default value, so `200.pct()` is double
15/// the default line height.
16///
17/// [`Default`]: Length::Default
18pub type LineHeight = Length;
19
20/// Extra spacing added in between text letters.
21///
22/// Letter spacing is computed using the font data, this unit represents
23/// extra space added to the computed spacing.
24///
25/// A "letter" is a character glyph cluster, e.g.: `a`, `â`, `1`, `-`, `漢`.
26///
27/// The [`Default`] value signals that letter spacing can be tweaked when text *justification* is enabled, all other
28/// values disable automatic adjustments for justification. Relative values are computed from the length of the space `' '` character.
29///
30/// [`Default`]: Length::Default
31pub type LetterSpacing = Length;
32
33/// Extra spacing added to the Unicode `U+0020 SPACE` character.
34///
35/// Word spacing is done using the space character "advance" as defined in the font,
36/// this unit represents extra spacing added to that default spacing.
37///
38/// A "word" is the sequence of characters in-between space characters. This extra
39/// spacing is applied per space character not per word, if there are three spaces between words
40/// the extra spacing is applied thrice. Usually the number of spaces between words is collapsed to one,
41/// see [`WhiteSpace`](crate::WhiteSpace).
42///
43/// The [`Default`] value signals that word spacing can be tweaked when text *justification* is enabled, all other
44/// values disable automatic adjustments for justification. Relative values are computed from the length of the space `' '` character,
45/// so a word spacing of `100.pct()` visually adds *another* space in between words.
46///
47/// [`Default`]: Length::Default
48pub type WordSpacing = Length;
49
50/// Extra spacing in-between text lines.
51///
52/// The [`Default`] value is zero. Relative values are calculated from the [`LineHeight`], so `50.pct()` is half
53/// the computed line height.
54///
55/// [`Default`]: Length::Default
56pub type LineSpacing = Length;
57
58/// Extra spacing in-between paragraphs.
59///
60/// The initial paragraph space is `line_height + line_spacing * 2`, this extra spacing is added to that.
61///
62/// A "paragraph" is a sequence of lines in-between wgt lines (empty or spaces only). This extra space is applied per wgt line
63/// not per paragraph, if there are three wgt lines between paragraphs the extra spacing is applied trice.
64///
65/// The [`Default`] value is zero.
66///
67/// [`Default`]: Length::Default
68pub type ParagraphSpacing = Length;
69
70/// Length of a `TAB` space.
71///
72/// Relative lengths are computed from the normal space character "advance" plus the [`WordSpacing`].
73/// So a `200%` length is 2 spaces.
74///
75/// The [`Default`] value is `400.pct()`, 4 spaces.
76///
77/// [`Default`]: Length::Default
78pub type TabLength = Length;
79
80/// Height of the text underline decoration.
81///
82/// Relative lengths are computed from `1.em()`, with a minimum of one pixel.
83///
84/// The [`Default`] value is defined by the font.
85///
86/// [`Default`]: Length::Default
87pub type UnderlineThickness = Length;
88
89/// Height of the text overline or strikethrough decoration.
90///
91/// Relative lengths are computed from `1.em()`, with a minimum of one pixel.
92///
93/// The [`Default`] value is `10.pct()`.
94pub type TextLineThickness = Length;
95
96/// Extra spacing at the start of lines.
97#[derive(Clone, PartialEq, Eq, Debug, Default)]
98pub struct Indentation {
99 /// The ident space width.
100 pub spacing: Length,
101 /// If `false` indents only the first lines after a line break.
102 ///
103 /// If `true` indent all lines except the first lines (hang).
104 pub invert: bool,
105}
106impl_from_and_into_var! {
107 fn from(percent: FactorPercent) -> Indentation {
108 Length::from(percent).into()
109 }
110 fn from(norm: Factor) -> Indentation {
111 Length::from(norm).into()
112 }
113 fn from(f: f32) -> Indentation {
114 Length::from(f).into()
115 }
116 fn from(i: i32) -> Indentation {
117 Length::from(i).into()
118 }
119 fn from(l: Px) -> Indentation {
120 Length::from(l).into()
121 }
122 fn from(l: Dip) -> Indentation {
123 Length::from(l).into()
124 }
125 fn from(expr: LengthExpr) -> Indentation {
126 Length::from(expr).into()
127 }
128 fn from(spacing: Length) -> Indentation {
129 Indentation { spacing, invert: false }
130 }
131
132 fn from<S: Into<Length>>(spacing_invert: (S, bool)) -> Indentation {
133 Indentation {
134 spacing: spacing_invert.0.into(),
135 invert: spacing_invert.1,
136 }
137 }
138}