chiark / gitweb /
dice: Actually display the labels
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 23:12:42 +0000 (00:12 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 23:12:42 +0000 (00:12 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
dice/overlay-template-extractor
src/dice.rs

index 168cf324ff0e72790682cf5127252cb37a496d1f..0f2a9910cd134ed4135c2132bcf2294d3fbf4c97 100755 (executable)
@@ -55,6 +55,17 @@ sub filter_circle () {
   $node->setAttribute('r', "{{radius}}");
 }
 
+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->removeChildNodes();
+  $node->appendText('{{ label_text }}');
+}
+p "{% if label_text != \"\" %}\n";
+process_node('textlabel', \&filter_text);
+p "{% endif %}\n";
+
 p "{% if cooldown_active %}\n";
 process_node('timeblack', sub {
   $nom_radius = $node->getAttribute('r');
index c48353d35167ae938caaa278d84f8e621f214740..349b6945b93b6824fb47c019c1d3aff4b95a356c 100644 (file)
@@ -24,6 +24,8 @@ const COOLDOWN_EXTRA_RADIUS: f64 =
   0.5 * (SELECT_STROKE_WIDTH + COOLDOWN_STROKE_WIDTH) +
   DEFAULT_EDGE_WIDTH;
 
+const DEFAULT_LABEL_FONT_SIZE: f64 = 8.;
+
 #[derive(Debug,Serialize,Deserialize)]
 pub struct Spec {
   // must be >1 faces on image, or >1 texts, and if both, same number
@@ -76,6 +78,9 @@ impl PieceXData for State {
 
 #[derive(Serialize, Debug)]
 struct OverlayTemplateContext<'c> {
+  label_text: &'c str,
+  label_font_size: f64,
+
   cooldown_active: bool,
   radius: f64,
   remprop: f64,
@@ -342,6 +347,9 @@ impl InertPieceTrait for Die {
     let iface = if self.image.nfaces() == 1 { default() } else { face };
     self.image.svg(f, vpid, iface, xdata)?;
 
+    let label = self.labels.get(face).map(|s| &**s).unwrap_or_else(
+        || self.labels.get(0).map(|s| &**s) .unwrap_or_default());
+
     let remprop = self.cooldown_remprop(state)?;
 
     let cooldown_active = remprop != 0.;
@@ -355,6 +363,9 @@ impl InertPieceTrait for Die {
     };
 
     let tc = OverlayTemplateContext {
+      label_text: &label,
+      label_font_size: DEFAULT_LABEL_FONT_SIZE,
+
       cooldown_active,
       radius: self.cooldown_radius,
       path_d: &path_d,