encre_css/plugins/typography/line_height/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias = "typography")]
3use crate::prelude::build_plugin::*;
4
5use std::borrow::Cow;
6
7#[derive(Debug)]
8pub(crate) struct PluginDefinition;
9
10impl Plugin for PluginDefinition {
11    fn can_handle(&self, context: ContextCanHandle) -> bool {
12        match context.modifier {
13            Modifier::Builtin { value, .. } => {
14                ["none", "tight", "snug", "normal", "relaxed", "loose"].contains(&&**value)
15                    || spacing::is_matching_builtin_spacing(value)
16            }
17            Modifier::Arbitrary { value, .. } => {
18                *value == "normal"
19                    || is_matching_number(value)
20                    || is_matching_length(value)
21                    || is_matching_percentage(value)
22            }
23        }
24    }
25
26    fn handle(&self, context: &mut ContextHandle) {
27        match context.modifier {
28            Modifier::Builtin { is_negative, value } => context.buffer.line(format_args!(
29                "line-height: {};",
30                match *value {
31                    "none" => Cow::Borrowed("1"),
32                    "tight" => Cow::Borrowed("1.25"),
33                    "snug" => Cow::Borrowed("1.375"),
34                    "normal" => Cow::Borrowed("1.5"),
35                    "relaxed" => Cow::Borrowed("1.625"),
36                    "loose" => Cow::Borrowed("2"),
37                    _ => spacing::get(value, *is_negative).unwrap(),
38                }
39            )),
40            Modifier::Arbitrary { value, .. } => {
41                context.buffer.line(format_args!("line-height: {value};"));
42            }
43        }
44    }
45}