From 5fc93959f9461a16c161bd61672313ff6319b620 Mon Sep 17 00:00:00 2001
From: Eyal Perry <eyal.perry88@gmail.com>
Date: Wed, 8 Sep 2021 16:48:13 -0400
Subject: [PATCH] added DPI

---
 modules/read/hpgl.js | 64 +++++++++++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 19 deletions(-)

diff --git a/modules/read/hpgl.js b/modules/read/hpgl.js
index c7953c3..8205936 100644
--- a/modules/read/hpgl.js
+++ b/modules/read/hpgl.js
@@ -39,14 +39,16 @@ var inputs = {
 // outputs
 //
 var outputs = {
-   path:{type:'array',
-      event:function(){
+  toolpath:{type:'object',
+     event:function(){
         cmd = {}
-        cmd.path = mod.path;
-        cmd.maxX = mod.maxX;
-        cmd.maxY = mod.maxY;
-        mods.output(mod,'path',cmd)
-    }}
+        cmd.path = mod.path
+        cmd.name = mod.name
+        cmd.dpi = parseFloat(mod.dpitext.value)
+        cmd.width = mod.width
+        cmd.height = mod.height
+        mods.output(mod,'toolpath',cmd)
+        }}
 }
 //
 // interface
@@ -104,17 +106,36 @@ var interface = function(div){
    //
    var info = document.createElement('div')
       info.setAttribute('id',div.id+'info')
-      var text = document.createTextNode('file:')
+      info.appendChild(document.createTextNode('dpi: '))
+      var input = document.createElement('input')
+         input.type = 'text'
+         input.size = 6
+         input.addEventListener('input',function(){
+            mod.dpi = parseFloat(mod.dpitext.value)
+            mod.mmtext.nodeValue = (25.4*mod.width/mod.dpi).toFixed(3)
+               +' x '+(25.4*mod.height/mod.dpi).toFixed(3)+' mm'
+            mod.intext.nodeValue = (mod.width/mod.dpi).toFixed(3)
+               +' x '+(mod.height/mod.dpi).toFixed(3)+' in'
+            outputs.toolpath.event()
+            })
+         info.appendChild(input)
+         mod.dpitext = input
+      info.appendChild(document.createElement('br'))
+      var text = document.createTextNode('px: ')
          info.appendChild(text)
-         mod.name = text
+         mod.pxtext = text
       info.appendChild(document.createElement('br'))
-      var text = document.createTextNode('width:')
+      var text = document.createTextNode('mm: ')
          info.appendChild(text)
-         mod.width = text
+         mod.mmtext = text
       info.appendChild(document.createElement('br'))
-      var text = document.createTextNode('height:')
+      var text = document.createTextNode('in: ')
          info.appendChild(text)
-         mod.height = text
+         mod.intext = text
+      info.appendChild(document.createElement('br'))
+      var text = document.createTextNode('')
+         info.appendChild(text)
+         mod.name = text
       div.appendChild(info)
    }
 //
@@ -130,7 +151,7 @@ function hpgl_read_handler(event) {
    file_reader.onload = hpgl_load_handler
    var input_file = mod.file.files[0]
    var file_name = input_file.name
-   mod.name.nodeValue = "file: "+file_name
+   mod.name.nodeValue = file_name
    file_reader.readAsText(input_file)
    }
 //
@@ -178,14 +199,19 @@ function hpgl_load_handler(event) {
          if (path[segment][i][1] > maxY) maxY = path[segment][i][1];
        }
      }
-     mod.width.nodeValue = "width: "+minX.toString() + " - " + maxX.toString();
-     mod.height.nodeValue = "height: "+minY.toString() + " - " + maxY.toString();
-     mod.maxX = maxX;
-     mod.maxY = maxY;
      var x0 = minX;
      var width = maxX-minX;
      var y0 = minY;
      var height = maxY-minY;
+     mod.width = width;
+     mod.height = height;
+     var dpi = Math.max(width, height) / 10
+     mod.dpitext.value = dpi.toFixed(3)
+     mod.pxtext.nodeValue = width+' x '+height+' px'
+     mod.mmtext.nodeValue = (25.4*width/dpi).toFixed(3)
+        +' x '+(25.4*height/dpi).toFixed(3)+' mm'
+     mod.intext.nodeValue = (width/dpi).toFixed(3)
+        +' x '+(height/dpi).toFixed(3)+' in'
      //
      // display
      //
@@ -207,7 +233,7 @@ function hpgl_load_handler(event) {
      }
 
      mod.path = path;
-     outputs.path.event();
+     outputs.toolpath.event();
    }
 
 
-- 
GitLab