From 2ce11e2c3a12d82dae614f7642b81d998f2561e8 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Mon, 13 Jul 2015 22:20:54 -0400
Subject: [PATCH] first attempt at porting drawnumber

---
 pd/nw/pdgui.js      | 33 +++++++++++++++++++++++-
 pd/src/g_template.c | 62 +++++++++++++++++++++++++++++++--------------
 2 files changed, 75 insertions(+), 20 deletions(-)

diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 38599c7d0..02f767c2c 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -2167,7 +2167,7 @@ function gui_text_new(canvasname, myname, type, isselected, x, y, text, font) {
     if (g !== null) {
         g.appendChild(svg_text);
     } else {
-        gui_post("gui_text_new: can't find parent group");
+        gui_post("gui_text_new: can't find parent group " + myname);
     }
 
     if (isselected) {
@@ -3052,6 +3052,37 @@ function gui_plot_vis(cid, basex, basey, data_array, attr_array, tag_array) {
     }
 }
 
+function gui_drawnumber_vis(cid, parent_tag, tag, x, y, scale_x, scale_y,
+    font, fontsize, text) {
+    gui_post("font is " + font);
+    var lines, i, len, tspan;
+    var g = get_item(cid, parent_tag);
+    var svg_text = create_item(cid, 'text', {
+        // x and y are fudge factors. Text on the tk canvas used an anchor
+        // at the top-right corner of the text's bbox.  SVG uses the baseline.
+        // There's probably a programmatic way to do this, but for now-- fudge factors
+        // based on the DejaVu Sans Mono font. :)
+        transform: 'scale(' + scale_x + ',' + scale_y + ') ' +
+                   'translate(' + x + ')',
+        y: y,
+        // Turns out we can't do 'hanging' baseline
+        // because it's borked when scaled. Bummer...
+        // 'dominant-baseline': 'hanging',
+        'shape-rendering': 'optimizeSpeed',
+        'font-size': font + 'px',
+        id: tag
+    });
+
+    // fill svg_text with tspan content by splitting on '\n'
+    text_to_tspans(cid, svg_text, text);
+
+    if (g !== null) {
+        g.appendChild(svg_text);
+    } else {
+        gui_post("gui_drawnumber: can't find parent group" + parent_tag);
+    }
+}
+
 function add_popup(cid, popup) {
     popup_menu[cid] = popup;
 }
diff --git a/pd/src/g_template.c b/pd/src/g_template.c
index 30a12276c..bf4e8055d 100644
--- a/pd/src/g_template.c
+++ b/pd/src/g_template.c
@@ -6122,30 +6122,54 @@ static void drawnumber_vis(t_gobj *z, t_glist *glist, t_glist *parentglist,
             SETSYMBOL(&at, fielddesc_getsymbol(&x->x_value, template, data, 0));
         else SETFLOAT(&at, fielddesc_getfloat(&x->x_value, template, data, 0));
         drawnumber_sprintf(x, buf, &at);
-        /*sys_vgui(".x%lx.c create text %d %d -anchor nw -fill %s -text {%s}",
-                glist_getcanvas(glist), xloc, yloc, colorstring, buf);
-        sys_vgui(" -font {{%s} -%d %s}", sys_font,
-            sys_hostfontsize(fontsize), sys_fontweight);*/
-        sys_vgui(".x%lx.c create ptext %d "
-                 "[expr {[font metrics {{%s} %d} -ascent] + %d}] "
-                 "-textanchor start -fill %s -text {%s}\\\n",
-               glist_getcanvas(glist), xloc, sys_font,
-               sys_hostfontsize(fontsize), yloc, colorstring, buf);
+        //sys_vgui(".x%lx.c create ptext %d "
+        //         "[expr {[font metrics {{%s} %d} -ascent] + %d}] "
+        //         "-textanchor start -fill %s -text {%s}\\\n",
+        //       glist_getcanvas(glist), xloc, sys_font,
+        //       sys_hostfontsize(fontsize), yloc, colorstring, buf);
         /* have to remove fontweight for the time being... */
-        sys_vgui(" -fontfamily {%s} -fontsize %d", sys_font, fontsize);
-        sys_vgui(" -matrix { {%g 0} {0 %g} {0 0} }", xscale, yscale);
+        //sys_vgui(" -fontfamily {%s} -fontsize %d", sys_font, fontsize);
+        //sys_vgui(" -matrix { {%g 0} {0 %g} {0 0} }", xscale, yscale);
 
+
+        char parent_tagbuf[MAXPDSTRING];
         if (in_array)
-            sys_vgui(" -parent .scelem%lx.%lx \\\n", parentglist, data);
+        {
+            //sys_vgui(" -parent .scelem%lx.%lx \\\n", parentglist, data);
+            sprintf(parent_tagbuf,"scelem%lx.%lx", (long unsigned int)parentglist, (long unsigned int)data);
+        }
         else
-            sys_vgui(" -parent .dgroup%lx.%lx \\\n",
-                x->x_canvas, data);
-
-        sys_vgui(" -tags {.x%lx.x%lx.template%lx scalar%lx}\n", 
-            glist_getcanvas(glist), glist, data, sc);
+        {
+            //sys_vgui(" -parent .dgroup%lx.%lx \\\n",
+            //    x->x_canvas, data);
+            sprintf(parent_tagbuf, "dgroup%lx.%lx", (long unsigned int)x->x_canvas, (long unsigned int)data);
+        }
+        char tagbuf[MAXPDSTRING];
+        //sys_vgui(" -tags {.x%lx.x%lx.template%lx scalar%lx}\n", 
+        //    glist_getcanvas(glist), glist, data, sc);
+        sprintf(tagbuf, "drawnumber%lx.%lx", (long unsigned int)x, (long unsigned int)data);
+
+        gui_vmess("gui_drawnumber_vis", "xssiiffsis",
+            glist_getcanvas(glist),
+            parent_tagbuf,
+            tagbuf,
+            xloc,
+            yloc, // Wrong-- we need to take font height into account
+            xscale,
+            yscale,
+            sys_font,
+            fontsize,
+            buf);
+    }
+    else
+    {
+        //sys_vgui(".x%lx.c delete .x%lx.x%lx.template%lx\n",
+        //    glist_getcanvas(glist), glist_getcanvas(glist), glist, data);
+        char tagbuf[MAXPDSTRING];
+        sprintf(tagbuf, "drawnumber%lx.%lx", (long unsigned int)x, (long unsigned int)data);
+        gui_vmess("gui_draw_erase_item", "xs", glist_getcanvas(glist),
+            tagbuf);
     }
-    else sys_vgui(".x%lx.c delete .x%lx.x%lx.template%lx\n",
-        glist_getcanvas(glist), glist_getcanvas(glist), glist, data);
 }
 
 static t_float drawnumber_motion_ycumulative;
-- 
GitLab