summaryrefslogtreecommitdiff
path: root/src/style.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-03 15:07:57 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-03 15:07:57 +0200
commit95bae5725cf6495644e2593f8492f1cd0e5bd3c1 (patch)
tree919dd90cac7623bcbbc09d9c92399eaa65e537f2 /src/style.rs
parent0fc25d732d7cbc37cf801645849d1060f2cec4a3 (diff)
Int, Float, Relative and Linear values 🍉
Diffstat (limited to 'src/style.rs')
-rw-r--r--src/style.rs78
1 files changed, 51 insertions, 27 deletions
diff --git a/src/style.rs b/src/style.rs
index b6c1a278..74deeeb9 100644
--- a/src/style.rs
+++ b/src/style.rs
@@ -2,8 +2,8 @@
use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight};
-use crate::geom::{Insets, Sides, Size};
-use crate::length::{Length, ScaleLength};
+use crate::geom::{Insets, Linear, Sides, Size};
+use crate::length::Length;
use crate::paper::{Paper, PaperClass, PAPER_A4};
/// Defines properties of pages and text.
@@ -28,37 +28,35 @@ pub struct TextStyle {
/// Whether the emphasis toggle is active or inactive. This determines
/// whether the next `_` makes italic or non-italic.
pub emph: bool,
- /// The base font size.
- pub base_font_size: f64,
- /// The font scale to apply on the base font size.
- pub font_scale: f64,
- /// The word spacing (as a multiple of the font size).
- pub word_spacing_scale: f64,
- /// The line spacing (as a multiple of the font size).
- pub line_spacing_scale: f64,
- /// The paragraphs spacing (as a multiple of the font size).
- pub paragraph_spacing_scale: f64,
+ /// The font size.
+ pub font_size: FontSize,
+ /// The word spacing (relative to the the font size).
+ pub word_spacing: Linear,
+ /// The line spacing (relative to the the font size).
+ pub line_spacing: Linear,
+ /// The paragraphs spacing (relative to the the font size).
+ pub par_spacing: Linear,
}
impl TextStyle {
- /// The scaled font size.
+ /// The absolute font size.
pub fn font_size(&self) -> f64 {
- self.base_font_size * self.font_scale
+ self.font_size.eval()
}
/// The absolute word spacing.
pub fn word_spacing(&self) -> f64 {
- self.word_spacing_scale * self.font_size()
+ self.word_spacing.eval(self.font_size())
}
/// The absolute line spacing.
pub fn line_spacing(&self) -> f64 {
- (self.line_spacing_scale - 1.0) * self.font_size()
+ self.line_spacing.eval(self.font_size())
}
/// The absolute paragraph spacing.
pub fn paragraph_spacing(&self) -> f64 {
- (self.paragraph_spacing_scale - 1.0) * self.font_size()
+ self.par_spacing.eval(self.font_size())
}
}
@@ -85,15 +83,41 @@ impl Default for TextStyle {
},
strong: false,
emph: false,
- base_font_size: Length::pt(11.0).as_raw(),
- font_scale: 1.0,
- word_spacing_scale: 0.25,
- line_spacing_scale: 1.2,
- paragraph_spacing_scale: 1.5,
+ font_size: FontSize::abs(Length::pt(11.0).as_raw()),
+ word_spacing: Linear::rel(0.25),
+ line_spacing: Linear::rel(0.2),
+ par_spacing: Linear::rel(0.5),
}
}
}
+/// The font size, defined by base and scale.
+#[derive(Debug, Clone, PartialEq)]
+pub struct FontSize {
+ /// The base font size, updated whenever the font size is set absolutely.
+ pub base: f64,
+ /// The scale to apply on the base font size, updated when the font size
+ /// is set relatively.
+ pub scale: Linear,
+}
+
+impl FontSize {
+ /// Create a new font size.
+ pub fn new(base: f64, scale: Linear) -> Self {
+ Self { base, scale }
+ }
+
+ /// Create a new font size with the given `base` and a scale of `1.0`.
+ pub fn abs(base: f64) -> Self {
+ Self::new(base, Linear::rel(1.0))
+ }
+
+ /// Compute the absolute font size.
+ pub fn eval(&self) -> f64 {
+ self.scale.eval(self.base)
+ }
+}
+
/// Defines the size and margins of a page.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct PageStyle {
@@ -103,7 +127,7 @@ pub struct PageStyle {
pub size: Size,
/// The amount of white space in the order [left, top, right, bottom]. If a
/// side is set to `None`, the default for the paper class is used.
- pub margins: Sides<Option<ScaleLength>>,
+ pub margins: Sides<Option<Linear>>,
}
impl PageStyle {
@@ -121,10 +145,10 @@ impl PageStyle {
let Size { width, height } = self.size;
let default = self.class.default_margins();
Insets {
- x0: -self.margins.left.unwrap_or(default.left).raw_scaled(width),
- y0: -self.margins.top.unwrap_or(default.top).raw_scaled(height),
- x1: -self.margins.right.unwrap_or(default.right).raw_scaled(width),
- y1: -self.margins.bottom.unwrap_or(default.bottom).raw_scaled(height),
+ x0: -self.margins.left.unwrap_or(default.left).eval(width),
+ y0: -self.margins.top.unwrap_or(default.top).eval(height),
+ x1: -self.margins.right.unwrap_or(default.right).eval(width),
+ y1: -self.margins.bottom.unwrap_or(default.bottom).eval(height),
}
}
}