From 82b28dee5cedf92a522b7078e10cdf2cdccafb85 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 21 Sep 2023 16:22:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20HTML=20unit=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/html.rs | 2 ++ pagetop/src/html/unit.rs | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 pagetop/src/html/unit.rs diff --git a/pagetop/src/html.rs b/pagetop/src/html.rs index 18e16fea..34cb7cdb 100644 --- a/pagetop/src/html.rs +++ b/pagetop/src/html.rs @@ -25,6 +25,8 @@ pub use attribute::AttributeValue; mod classes; pub use classes::{Classes, ClassesOp}; +pub mod unit; + pub enum PrepareMarkup { None, Text(&'static str), diff --git a/pagetop/src/html/unit.rs b/pagetop/src/html/unit.rs new file mode 100644 index 00000000..44371fa3 --- /dev/null +++ b/pagetop/src/html/unit.rs @@ -0,0 +1,54 @@ +use crate::concat_string; + +// About pixels: Pixels (px) are relative to the viewing device. For low-dpi devices, 1px is one +// device pixel (dot) of the display. For printers and high resolution screens 1px implies multiple +// device pixels. + +// About em: 2em means 2 times the size of the current font. The em and rem units are practical in +// creating perfectly scalable layout! + +// About viewport: If the browser window size is 50cm wide, 1vw = 0.5cm. + +#[rustfmt::skip] +#[derive(Default)] +pub enum Value { + #[default] + None, + Auto, + + Cm(isize), // Centimeters. + In(isize), // Inches (1in = 96px = 2.54cm). + Mm(isize), // Millimeters. + Pc(isize), // Picas (1pc = 12pt). + Pt(isize), // Points (1pt = 1/72 of 1in). + Px(isize), // Pixels (1px = 1/96th of 1in). + + RelEm(f32), // Relative to the font-size of the element. + RelPct(f32), // Percentage relative to the parent element. + RelRem(f32), // Relative to font-size of the root element. + RelVh(f32), // Relative to 1% of the height of the viewport. + RelVw(f32), // Relative to 1% of the value of the viewport. +} + +#[rustfmt::skip] +impl ToString for Value { + fn to_string(&self) -> String { + match self { + Value::None => "".to_owned(), + Value::Auto => "auto".to_owned(), + // Absolute value. + Value::Cm(av) => concat_string!(av.to_string(), "cm"), + Value::In(av) => concat_string!(av.to_string(), "in"), + Value::Mm(av) => concat_string!(av.to_string(), "mm"), + Value::Pc(av) => concat_string!(av.to_string(), "pc"), + Value::Pt(av) => concat_string!(av.to_string(), "pt"), + Value::Px(av) => concat_string!(av.to_string(), "px"), + // Relative value. + Value::RelEm(rv) => concat_string!(rv.to_string(), "em"), + Value::RelPct(rv) => concat_string!(rv.to_string(), "%"), + Value::RelRem(rv) => concat_string!(rv.to_string(), "rem"), + Value::RelVh(rv) => concat_string!(rv.to_string(), "vh"), + Value::RelVw(rv) => concat_string!(rv.to_string(), "vw"), + } + } +}