//! CSS. SVG export's style attributes and sheets, and the CSS web pages style objects with //! ([`css_rules`]: the CSS Properties panel and its exports), write each property they share in //! the same way: the declarations of transparency, fonts and type spacing come from the functions //! here, colours are `#rrggbb` (or `rgba()` where CSS has no separate opacity), gradients take the //! stops SVG export writes, and numbers go through [`fmt_num`]. use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; use vectorcraft_color::{BlendMode, Color, GradientGeom, GradientKind, GradientPaint, Paint}; use vectorcraft_doc::{AppearanceItem, CharStyle, Document, Justify, LineCap, LiveShape, Node, NodeId, NodeKind, StrokeAlign, StrokeLayer, TextKind}; use vectorcraft_effects::RasterFx; use vectorcraft_geom::shapes::{self, CornerKind}; use vectorcraft_geom::{Affine, PathData, Point, Rect}; use crate::export::sanitize_id; use crate::fmt_num; /// CSS declarations, in order: (property, value). pub(crate) type Props = Vec<(&'static str, String)>; /// A declaration block: `property:value;property:value`. pub(crate) fn declarations<'a>(props: impl IntoIterator) -> String { props.into_iter().map(|(k, v)| format!("{k}:{v}")).collect::>().join(";") } /// A CSS string. pub(crate) fn css_string(s: &str) -> String { quoted(s, '"') } /// `s` as a CSS string in `quote`s. A backslash and the quote are escaped, and so are line breaks /// and other control characters (a newline would end the string and let the rest be rules) and /// `<` and `>` (which could end a `"), r#""w\d \"\3c /style\3e ""#); assert_eq!(css_string("wght"), "\"wght\""); } /// A monospaced width form sets the characters on a fixed width: Proportional Metrics' `palt` /// is not written with it and the font's pair kerning is turned off, and with the other forms /// `palt` is written and kerning stays. #[test] fn width_forms_leave_out_proportional_metrics() { use vectorcraft_doc::GlyphForm; let prop = |form, key: &str| { let st = CharStyle { proportional_metrics: true, glyph_form: form, ..CharStyle::default() }; type_props(&st, &|v| format!("{v}px")).into_iter().find(|(k, _)| *k == key).map(|(_, v)| v) }; assert_eq!(prop(GlyphForm::Default, "font-feature-settings").as_deref(), Some("\"palt\" 1")); assert_eq!(prop(GlyphForm::Jis78, "font-feature-settings").as_deref(), Some("\"palt\" 1, \"jp78\" 1")); assert_eq!(prop(GlyphForm::Jis78, "font-kerning"), None); for form in [GlyphForm::FullWidth, GlyphForm::HalfWidth, GlyphForm::ThirdWidth, GlyphForm::QuarterWidth] { let tag = std::str::from_utf8(form.feature().unwrap()).unwrap(); assert_eq!(prop(form, "font-feature-settings"), Some(format!("\"{tag}\" 1")), "{form:?}"); assert_eq!(prop(form, "font-kerning").as_deref(), Some("none"), "{form:?}"); } } }