chiark / gitweb /
Break out TextOptions and TextOptionsSpec
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 19 May 2022 11:29:40 +0000 (12:29 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 19 May 2022 17:40:46 +0000 (18:40 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
dice/overlay-template-extractor
src/dice.rs
src/spec.rs
src/ui.rs

index 4f0cbfcb03089b5d5a867cc3447e1192dd038293..3684b678ce476e788411be25f64ec8b60a0b2008 100755 (executable)
@@ -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 }}');
index cf9df81fd25d7fd3c4a768bf48893323728883a6..7a5eaae040b4bbc9a2b030705bdc3bff0db48d94 100644 (file)
@@ -31,7 +31,7 @@ pub struct Spec {
   // must be >1 faces on image, or >1 texts, and if both, same number
   image: Box<dyn PieceSpec>,
   #[serde(default)] labels: SpecLabels,
-  #[serde(default)] label: SpecLabelSection,
+  #[serde(default)] label: TextOptionsSpec,
   occult: Option<OccultSpec>,
   // 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<ColourSpec>,
-}
-
 #[derive(Debug,Clone,Serialize,Deserialize)]
 #[serde(untagged)]
 pub enum SpecLabels {
@@ -74,7 +69,7 @@ struct Die {
   itemname: String,
   labels: IndexVec<FaceId, String>, // if .len()==1, always use [0]
   image: Arc<dyn InertPieceTrait>, // 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,
 
index 8e00709a36c9f3e7ac90d87414fcef6a3023f1cd..f7c0e4e541a45221fb91e8e18bfd69dae7d6da57 100644 (file)
@@ -241,6 +241,11 @@ impl PieceAngle {
   } }
 }
 
+#[derive(Debug,Default,Clone,Serialize,Deserialize)]
+pub struct TextOptionsSpec {
+  pub colour: Option<ColourSpec>,
+}
+
 #[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;
   }
index 39e9e690ba793a513d9403b9a10527edc931f7b0..35f41503ee4640ce69530b299e5cba2adefc6c66 100644 (file)
--- 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