summaryrefslogtreecommitdiff
path: root/src/style.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/style.rs')
-rw-r--r--src/style.rs73
1 files changed, 70 insertions, 3 deletions
diff --git a/src/style.rs b/src/style.rs
index 616619e1..1dcae0da 100644
--- a/src/style.rs
+++ b/src/style.rs
@@ -4,6 +4,7 @@ use toddle::query::FontClass;
use FontClass::*;
use crate::size::{Size, Size2D, SizeBox};
+use crate::syntax::ParseResult;
/// Defines properties of pages and text.
#[derive(Debug, Default, Clone)]
@@ -20,8 +21,10 @@ pub struct TextStyle {
/// The fallback classes from which the font needs to match the
/// leftmost possible one.
pub fallback: Vec<FontClass>,
- /// The font size.
- pub font_size: Size,
+ /// The base font size.
+ pub base_font_size: Size,
+ /// The font scale to apply on the base font size.
+ pub font_scale: f32,
/// The word spacing (as a multiple of the font size).
pub word_spacing: f32,
/// The line spacing (as a multiple of the font size).
@@ -31,6 +34,11 @@ pub struct TextStyle {
}
impl TextStyle {
+ /// The scaled font size.
+ pub fn font_size(&self) -> Size {
+ self.base_font_size * self.font_scale
+ }
+
/// Toggle a class.
///
/// If the class was one of _italic_ or _bold_, then:
@@ -73,7 +81,8 @@ impl Default for TextStyle {
TextStyle {
classes: vec![Regular],
fallback: vec![Serif],
- font_size: Size::pt(11.0),
+ base_font_size: Size::pt(11.0),
+ font_scale: 1.0,
word_spacing: 0.25,
line_spacing: 1.2,
paragraph_spacing: 1.5,
@@ -109,3 +118,61 @@ impl Default for PageStyle {
}
}
}
+
+/// The size of a page with the given name.
+pub fn parse_paper_name(paper: &str) -> ParseResult<Size2D> {
+ Ok(match paper {
+ "A0" | "a0" => PAPER_A0,
+ "A1" | "a1" => PAPER_A1,
+ "A2" | "a2" => PAPER_A2,
+ "A3" | "a3" => PAPER_A3,
+ "A4" | "a4" => PAPER_A4,
+ "A5" | "a5" => PAPER_A5,
+ "A6" | "a6" => PAPER_A6,
+ "A7" | "a7" => PAPER_A7,
+ "A8" | "a8" => PAPER_A8,
+ "A9" | "a9" => PAPER_A9,
+ "A10" | "a10" => PAPER_A10,
+ "A11" | "a11" => PAPER_A11,
+ "Letter" | "letter" => PAPER_LETTER,
+ "Legal" | "legal" => PAPER_LEGAL,
+ "Tabloid" | "tabloid" => PAPER_TABLOID,
+ "Ledger" | "ledger" => PAPER_LEDGER,
+ "Junior-Legal" | "junior-legal" => PAPER_JUNIOR_LEGAL,
+ "Half-Letter" | "half-letter" => PAPER_HALF_LETTER,
+ "Government-Letter" | "government-letter" => PAPER_GOVERNMENT_LETTER,
+
+ _ => error!("unknown paper size: `{}`", paper),
+ })
+}
+
+macro_rules! paper {
+ ($var:ident: $width:expr, $height: expr) => {
+ /// The size of the paper that's in the name.
+ pub const $var: Size2D = Size2D {
+ x: Size { points: 2.83465 * $width },
+ y: Size { points: 2.83465 * $height },
+ };
+ };
+}
+
+// Common paper sizes in mm.
+paper!(PAPER_A0: 841.0, 1189.0);
+paper!(PAPER_A1: 594.0, 841.0);
+paper!(PAPER_A2: 420.0, 594.0);
+paper!(PAPER_A3: 297.0, 420.0);
+paper!(PAPER_A4: 210.0, 297.0);
+paper!(PAPER_A5: 148.0, 210.0);
+paper!(PAPER_A6: 105.0, 148.0);
+paper!(PAPER_A7: 74.0, 105.0);
+paper!(PAPER_A8: 52.0, 74.0);
+paper!(PAPER_A9: 37.0, 52.0);
+paper!(PAPER_A10: 26.0, 37.0);
+paper!(PAPER_A11: 18.0, 26.0);
+paper!(PAPER_LETTER: 216.0, 279.0);
+paper!(PAPER_LEGAL: 216.0, 356.0);
+paper!(PAPER_TABLOID: 279.0, 432.0);
+paper!(PAPER_LEDGER: 432.0, 279.0);
+paper!(PAPER_JUNIOR_LEGAL: 127.0, 203.0);
+paper!(PAPER_HALF_LETTER: 140.0, 216.0);
+paper!(PAPER_GOVERNMENT_LETTER: 203.0, 267.0);