From 1165774e395198d20381b0156017941a18d1ab14 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 19 May 2022 12:29:40 +0100 Subject: [PATCH] Break out TextOptions and TextOptionsSpec Signed-off-by: Ian Jackson --- dice/overlay-template-extractor | 2 +- src/dice.rs | 20 +++++++------------- src/spec.rs | 15 +++++++++++++++ src/ui.rs | 5 +++++ 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/dice/overlay-template-extractor b/dice/overlay-template-extractor index 4f0cbfcb..3684b678 100755 --- a/dice/overlay-template-extractor +++ b/dice/overlay-template-extractor @@ -59,7 +59,7 @@ sub filter_text () { filter_element('text', qw(x y font-family text-align text-anchor)); $node->setAttribute('font-size', "{{ label_font_size }}px"); $node->setAttribute('y', "{{ label_y_adjust }}"); - $node->setAttribute('fill', "{{ label_colour }}"); + $node->setAttribute('fill', "{{ label_options.colour }}"); $node->setAttribute('pointer-events', 'none'); $node->removeChildNodes(); $node->appendText('{{ label_text }}'); diff --git a/src/dice.rs b/src/dice.rs index cf9df81f..7a5eaae0 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -31,7 +31,7 @@ pub struct Spec { // must be >1 faces on image, or >1 texts, and if both, same number image: Box, #[serde(default)] labels: SpecLabels, - #[serde(default)] label: SpecLabelSection, + #[serde(default)] label: TextOptionsSpec, occult: Option, // 1.0 means base outline circle size on most distant corner of bounding box // minimum is 0.5; maximum is 1.5 @@ -47,11 +47,6 @@ pub struct OccultSpec { #[serde(default)] label: String, } -#[derive(Debug,Default,Clone,Serialize,Deserialize)] -pub struct SpecLabelSection { - pub colour: Option, -} - #[derive(Debug,Clone,Serialize,Deserialize)] #[serde(untagged)] pub enum SpecLabels { @@ -74,7 +69,7 @@ struct Die { itemname: String, labels: IndexVec, // if .len()==1, always use [0] image: Arc, // if image.nfaces()==1, always use face 0 - label_colour: Colour, + text_options: TextOptions, surround_outline: CircleOutline, cooldown_radius: f64, cooldown_time: Duration, @@ -95,8 +90,7 @@ struct OverlayTemplateContext<'c> { label_text: &'c str, label_font_size: f64, label_y_adjust: f64, - label_colour: &'c Html, - + label_options: &'c TextOptions, cooldown_active: bool, radius: f64, remprop: f64, @@ -157,7 +151,7 @@ impl PieceSpec for Spec { index_vec!["".into()] }; - let label_colour = self.label.colour.resolve()?; + let text_options = self.label.resolve()?; if_let!{ Some((nfaces,_)) = nfaces; else throw!(SpecError::MultipleFacesRequired) }; @@ -228,7 +222,7 @@ impl PieceSpec for Spec { let occ_ilk = LOI::Distinct; let our_occ_image = Arc::new(Die { nfaces, cooldown_time, cooldown_radius, surround_outline, - label_colour: label_colour.clone(), + text_options: text_options.clone(), itemname: itemname.clone(), desc: desc.clone(), image: occ_image, @@ -240,7 +234,7 @@ impl PieceSpec for Spec { let die = Die { nfaces, cooldown_time, cooldown_radius, surround_outline, - itemname, labels, desc, label_colour, + itemname, labels, desc, text_options, image: image.into() }; @@ -518,7 +512,7 @@ impl InertPieceTrait for Die { let tc = OverlayTemplateContext { label_text: label, - label_colour: &self.label_colour, + label_options: &self.text_options, label_font_size, label_y_adjust: label_font_size * SVG_FONT_Y_ADJUST_OF_FONT_SIZE, diff --git a/src/spec.rs b/src/spec.rs index 8e00709a..f7c0e4e5 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -241,6 +241,11 @@ impl PieceAngle { } } } +#[derive(Debug,Default,Clone,Serialize,Deserialize)] +pub struct TextOptionsSpec { + pub colour: Option, +} + #[derive(Debug,Copy,Clone,Eq,PartialEq)] #[derive(Default,Serialize,Deserialize)] #[serde(try_from="u8")] @@ -636,6 +641,16 @@ pub mod imp { } } + impl TextOptionsSpec { + #[throws(SpecError)] + /// Default colour is always black + pub fn resolve(&self) -> TextOptions { + let TextOptionsSpec { colour } = self; + let colour = colour.resolve()?; + TextOptions { colour } + } + } + impl UrlSpec { const MAX_LEN: usize = 200; } diff --git a/src/ui.rs b/src/ui.rs index 39e9e690..35f41503 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -26,6 +26,11 @@ pub const LABEL_FONT_SIZE: f64 = 4.0; pub const HTML_TEXT_LABEL_ELEM_START: HtmlLit = Html::lit(r##"text pointer-events="none""##); +#[derive(Debug,Serialize,Deserialize,Clone)] +pub struct TextOptions { + pub colour: Colour, +} + /// Fudge factor /// /// When trying to centre text, we use text-align and/or text-anchor -- 2.30.2