Skip to content

Commit

Permalink
Merge pull request #4 from wechat-miniprogram/feat-display
Browse files Browse the repository at this point in the history
feat: dump stylesheet
  • Loading branch information
TtTRz authored Dec 26, 2024
2 parents 97d159f + 1339540 commit b3ff80a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
14 changes: 12 additions & 2 deletions float-pigment-css/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,24 @@ impl StyleSheetGroup {
path: &str,
scope: Option<NonZeroUsize>,
) -> u16 {
self.append_from_resource_with_warnings(res, path, scope).0
}

/// Append a style sheet from the resource, returning its index and warnings like @import not found.
pub fn append_from_resource_with_warnings(
&mut self,
res: &StyleSheetResource,
path: &str,
scope: Option<NonZeroUsize>,
) -> (u16, Vec<Warning>) {
let path = drop_css_extension(path);
let (ss, _warnings) = res.link(path, scope);
let (ss, warnings) = res.link(path, scope);
let ret = self.sheets.len();
if Self::is_invalid_index(ret) {
panic!("The number of stylesheets has reached the maximum limit.")
}
self.sheets.push(ss);
ret as u16
(ret as u16, warnings)
}

/// Replace a style sheet from the resource by its index.
Expand Down
51 changes: 51 additions & 0 deletions float-pigment-css/src/sheet/font_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,52 @@ pub struct FontFace {
pub font_display: Option<FontDisplay>,
}

impl core::fmt::Display for FontFace {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "@font-face {{")?;
write!(f, " font-family: {};", self.font_family)?;
write!(f, " src: ")?;
for src in &self.src {
write!(f, "{}, ", src)?;
}
write!(f, ";")?;
if let Some(fs) = &self.font_style {
write!(f, " font-style: {};", fs)?;
}
if let Some(fw) = &self.font_weight {
write!(f, " font-weight: {};", fw)?;
}
if let Some(fd) = &self.font_display {
write!(f, " font-display: {};", fd)?;
}
write!(f, "}}")
}
}

#[allow(missing_docs)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub enum FontSrc {
Local(FontFamilyName),
Url(FontUrl),
}

impl core::fmt::Display for FontSrc {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Local(ff) => write!(f, r#"local("{}")"#, ff),
Self::Url(url) => {
write!(f, r#"url("{}")"#, url.url)?;
if let Some(formats) = &url.format {
for format in formats {
write!(f, r#" format("{}")"#, format)?;
}
}
Ok(())
}
}
}
}

#[allow(missing_docs)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct FontUrl {
Expand All @@ -41,6 +80,18 @@ pub enum FontDisplay {
Optional,
}

impl core::fmt::Display for FontDisplay {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Auto => write!(f, "auto"),
Self::Block => write!(f, "block"),
Self::Swap => write!(f, "swap"),
Self::Fallback => write!(f, "fallback"),
Self::Optional => write!(f, "optional"),
}
}
}

impl Default for FontFace {
fn default() -> Self {
Self {
Expand Down
39 changes: 39 additions & 0 deletions float-pigment-css/src/sheet/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ impl KeyFrames {
}
}

impl core::fmt::Display for KeyFrames {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "@keyframes {} {{ ", self.ident)?;
for keyframe in &self.keyframes {
write!(f, "{} ", keyframe)?;
}
write!(f, "}}")
}
}

/// The percentage field in a keyframe item.
#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -61,3 +71,32 @@ impl KeyFrameRule {
}
}
}

impl core::fmt::Display for KeyFrameRule {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{} {{ ",
self.keyframe
.iter()
.map(|x| {
match x {
KeyFrame::From => "from".to_owned(),
KeyFrame::To => "to".to_owned(),
KeyFrame::Ratio(ratio) => format!("{:.2}%", ratio * 100.),
}
})
.collect::<Vec<_>>()
.join(", ")
)?;
for prop in &self.properties {
write!(
f,
"{}: {}; ",
prop.get_property_name(),
prop.get_property_value_string()
)?;
}
write!(f, "}}")
}
}
15 changes: 15 additions & 0 deletions float-pigment-css/src/sheet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,21 @@ impl core::fmt::Debug for StyleSheet {
}
}

impl core::fmt::Display for StyleSheet {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for rule in self.rules.iter() {
write!(f, " {}", rule)?;
}
for font_face in self.font_face.iter() {
write!(f, " {}", font_face)?;
}
for keyframes in self.keyframes.iter() {
write!(f, " {}", keyframes)?;
}
Ok(())
}
}

impl StyleSheet {
#[doc(hidden)]
#[allow(clippy::should_implement_trait)]
Expand Down
2 changes: 1 addition & 1 deletion float-pigment-css/src/sheet/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;
use crate::property::Property;

/// A CSS property with some metadata.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyMeta {
/// A single normal property, e.g. `font-size: 16px`.
Normal {
Expand Down

0 comments on commit b3ff80a

Please sign in to comment.