From: Ian Jackson Date: Sun, 17 Apr 2022 22:24:57 +0000 (+0100) Subject: Move 0.35 magic value for font y adjustment into a Rust constant X-Git-Tag: otter-1.1.0~501 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8c9e1a034eb834b90c8f394740fed9709ef67d2d;p=otter.git Move 0.35 magic value for font y adjustment into a Rust constant Signed-off-by: Ian Jackson --- diff --git a/dice/overlay-template-extractor b/dice/overlay-template-extractor index cdc13c18..9cd6b5b1 100755 --- a/dice/overlay-template-extractor +++ b/dice/overlay-template-extractor @@ -58,7 +58,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('y', "{{ label_font_size * 0.35 }}"); + $node->setAttribute('y', "{{ label_y_adjust }}"); $node->removeChildNodes(); $node->appendText('{{ label_text }}'); } diff --git a/src/dice.rs b/src/dice.rs index 1e05dea1..a43c3e63 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -85,6 +85,7 @@ impl PieceXData for State { struct OverlayTemplateContext<'c> { label_text: &'c str, label_font_size: f64, + label_y_adjust: f64, cooldown_active: bool, radius: f64, @@ -437,9 +438,13 @@ impl InertPieceTrait for Die { default() }; + let label_font_size = DEFAULT_LABEL_FONT_SIZE; + + let tc = OverlayTemplateContext { label_text: &label, - label_font_size: DEFAULT_LABEL_FONT_SIZE, + label_font_size, + label_y_adjust: label_font_size * SVG_FONT_Y_ADJUST_OF_FONT_SIZE, cooldown_active, radius: self.cooldown_radius, diff --git a/src/pieces.rs b/src/pieces.rs index 0d1a8df0..f41fadc7 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -31,6 +31,13 @@ pub const SELECT_STROKE_WIDTH: f64 = 2.0; pub const DEFAULT_EDGE_WIDTH: f64 = 0.2; pub const INVISIBLE_EDGE_SENSITIVE: f64 = 2.; +// When trying to centre text, we use text-align and/or text-anchor +// to do the horizontal positioning, but vertical positioning is +// troublesome. We bodge it. Multiple the font size (in pixels) +// by this, and add it to the SVG y coordinate (ie, shufting the text +// down). +pub const SVG_FONT_Y_ADJUST_OF_FONT_SIZE: f64 = 0.35; + fn default_edge_width() -> f64 { DEFAULT_EDGE_WIDTH } #[derive(Copy,Clone,Debug,Error,Serialize,Deserialize)]