chiark / gitweb /
dice: Arrange to use text_options.start_element
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 19 May 2022 18:57:48 +0000 (19:57 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 19 May 2022 18:58:22 +0000 (19:58 +0100)
What a palaver!

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
dice/overlay-template-extractor
src/dice.rs

index 62c5e509590e79c78ed1ede0e34b15f1fa9456de..817a881db886cc047c140a45738632aa98b36d2e 100755 (executable)
@@ -67,17 +67,23 @@ sub filter_text () {
   my $exp_y = $adj * $font_size;
   die "$got_y != $exp_y" unless $got_y == $exp_y;
 
-  filter_element('text', qw(x y font-family text-align text-anchor));
-  $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');
+  # the attributes will be put back by Rust code,
+  # in TextOptions::start_element
+  filter_element('text', qw(font-family));
   $node->removeChildNodes();
   $node->appendText('{{ label_text }}');
+  $node->setNodeName('svg:text_tm3vljl79wqouvre');
 }
-# This <text> node also acts as the sentinel for when the JS
-# wants to remove the cooldown timer.
-p process_node('textlabel', \&filter_text);
+sub process_text_node() {
+  $_ = process_node('textlabel', \&filter_text);
+  # This <text> node also acts as the sentinel for when the JS
+  # wants to remove the cooldown timer.
+  s/text_tm3vljl79wqouvre/{{ label_start_element }}/;
+  s/text_tm3vljl79wqouvre/text/;
+  die "$_ ?" if m/text_tm3vljl79wqouvre/;
+  $_;
+}
+p process_text_node();
 
 p "{% if cooldown_active %}\n";
 p process_node('timeblack', sub {
index 22682941b764e8a317a6ef1ef8bff39b0fcf3c26..e77a7fdd23d47b98264071cf70f9874e14f366df 100644 (file)
@@ -88,8 +88,7 @@ impl PieceXData for State {
 #[derive(Serialize, Debug)]
 struct OverlayTemplateContext<'c> {
   label_text: &'c str,
-  label_y_adjust: f64,
-  label_options: &'c TextOptions,
+  label_start_element: Html,
   cooldown_active: bool,
   radius: f64,
   remprop: f64,
@@ -506,13 +505,11 @@ impl InertPieceTrait for Die {
       default()
     };
 
-    // todo: Ideally we would use text_options.start_element
-    // but I don't see how to readily  make a template suitable for that
+    let label_start_element = self.text_options.start_element();
 
     let tc = OverlayTemplateContext {
       label_text: label,
-      label_options: &self.text_options,
-      label_y_adjust: self.text_options.y_adjust(),
+      label_start_element,
 
       cooldown_active,
       radius: self.cooldown_radius,