From 6dc8755270eff32d2885123b8e2ecebf3f2cae6c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 19 May 2022 12:42:22 +0100 Subject: [PATCH] Move text size into TextOptions, so dice now support setting text size Signed-off-by: Ian Jackson --- dice/overlay-template-extractor | 2 +- docs/gamespec.rst | 3 +++ src/dice.rs | 9 ++------- src/spec.rs | 8 +++++--- src/ui.rs | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/dice/overlay-template-extractor b/dice/overlay-template-extractor index 3684b678..9ab896f7 100755 --- a/dice/overlay-template-extractor +++ b/dice/overlay-template-extractor @@ -57,7 +57,7 @@ sub filter_circle () { 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('font-size', "{{ label_options.size }}px"); $node->setAttribute('y', "{{ label_y_adjust }}"); $node->setAttribute('fill', "{{ label_options.colour }}"); $node->setAttribute('pointer-events', 'none'); diff --git a/docs/gamespec.rst b/docs/gamespec.rst index 7380282c..20587d83 100644 --- a/docs/gamespec.rst +++ b/docs/gamespec.rst @@ -347,6 +347,9 @@ Parameters: * ``label.colour`` [string, defaults to black]: Colour to write the ``labels`` text strings. + * ``label.size`` [number, default 8]: + Font size for the ``labels`` text strings (in pixeels, svg ``px``). + * ``cooldown``: Duration of the cooldown time. [duration - number(s) with units; default "4s"] diff --git a/src/dice.rs b/src/dice.rs index 7a5eaae0..524d8d13 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -88,7 +88,6 @@ impl PieceXData for State { #[derive(Serialize, Debug)] struct OverlayTemplateContext<'c> { label_text: &'c str, - label_font_size: f64, label_y_adjust: f64, label_options: &'c TextOptions, cooldown_active: bool, @@ -151,7 +150,7 @@ impl PieceSpec for Spec { index_vec!["".into()] }; - let text_options = self.label.resolve()?; + let text_options = self.label.resolve(DEFAULT_LABEL_FONT_SIZE)?; if_let!{ Some((nfaces,_)) = nfaces; else throw!(SpecError::MultipleFacesRequired) }; @@ -507,14 +506,10 @@ impl InertPieceTrait for Die { default() }; - let label_font_size = DEFAULT_LABEL_FONT_SIZE; - - let tc = OverlayTemplateContext { label_text: label, label_options: &self.text_options, - label_font_size, - label_y_adjust: label_font_size * SVG_FONT_Y_ADJUST_OF_FONT_SIZE, + label_y_adjust: self.text_options.size * SVG_FONT_Y_ADJUST_OF_FONT_SIZE, cooldown_active, radius: self.cooldown_radius, diff --git a/src/spec.rs b/src/spec.rs index f7c0e4e5..7634c5cd 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -244,6 +244,7 @@ impl PieceAngle { #[derive(Debug,Default,Clone,Serialize,Deserialize)] pub struct TextOptionsSpec { pub colour: Option, + pub size: Option, } #[derive(Debug,Copy,Clone,Eq,PartialEq)] @@ -644,10 +645,11 @@ pub mod imp { impl TextOptionsSpec { #[throws(SpecError)] /// Default colour is always black - pub fn resolve(&self) -> TextOptions { - let TextOptionsSpec { colour } = self; + pub fn resolve(&self, default_size: f64) -> TextOptions { + let TextOptionsSpec { colour, size } = self; let colour = colour.resolve()?; - TextOptions { colour } + let size = size.unwrap_or(default_size); + TextOptions { colour, size } } } diff --git a/src/ui.rs b/src/ui.rs index 35f41503..fb35c410 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -29,6 +29,7 @@ pub const HTML_TEXT_LABEL_ELEM_START: HtmlLit = #[derive(Debug,Serialize,Deserialize,Clone)] pub struct TextOptions { pub colour: Colour, + pub size: f64, // px } /// Fudge factor -- 2.30.2