improved hpgl parser : plot strings

Fri, 11 Jan 2008 18:49:00 +0100

author
David Douard <david.douard@logilab.fr>
date
Fri, 11 Jan 2008 18:49:00 +0100
changeset 20
de60b63b2275
parent 19
42af82fdb8bb
child 21
66b7cd43bac1

improved hpgl parser : plot strings

hpgl_parser.py file | annotate | diff | comparison | revisions
--- a/hpgl_parser.py	Thu Jan 03 19:06:48 2008 +0100
+++ b/hpgl_parser.py	Fri Jan 11 18:49:00 2008 +0100
@@ -69,6 +69,9 @@
             self.plot_lines(values)
         self.pos = values[-1]
 
+    def plot_string(self, s):
+        pass
+    
     def get_window_for_paper(self):
         return 0,100,0,100
     def get_scaling_points_for_paper(self):
@@ -82,7 +85,7 @@
         
     # HPGL-related methods
     def ESC(self):
-        print "ESC"
+        #print "ESC"
         self.idx += 1
 
     def OE(self):
@@ -109,6 +112,7 @@
     def DF(self):
         """ Default """
         self.pen = 0
+        self.pen_width = 1 # 1/300 inch
         self.pos = 0,0
         self.char_set = "std"
         self.plot_mode = 'absolute'
@@ -345,8 +349,34 @@
     def LB(self):
         """ Character Plot """
         values = self.extract_string()
-        # TODO
+        self.plot_string(values)
+        x, y = self.pos
+        values = values.split('\n')
+        if self.char_size == "absolute":
+            x += len(values[-1]) * self.char_width *1016/2.54
+            y += (len(values)-1) * self.char_height *1016/2.54
+        else:
+            x0, x1, y0, y1 = self.scale
+            dx = x1-x0
+            dy = y1-y0
+            x += len(values[-1]) * self.char_width / 100.0 * dx
+            y += (len(values)-1) * self.char_height / 100.0 * dy
+            
+        #print "LB pos[%s] %s -> %s"%(repr(values), self.pos, (x,y))
+        self.pos = [x, y]
 
+    def get_char_size(self):
+        if self.char_size == "absolute":
+            x = self.char_width *1016/2.54
+            y = self.char_height *1016/2.54
+        else:
+            x0, x1, y0, y1 = self.scale
+            dx = x1-x0
+            dy = y1-y0
+            x = self.char_width / 100.0 * dx
+            y = self.char_height / 100.0 * dy
+        return x, y
+    
     def DI(self):
         """ Absolute Direction """
         values = self.extract_value()
@@ -369,17 +399,27 @@
         values = self.extract_value()
         # TODO
         if len(values) == 0:
-            values = 0, -1
-        #if len(values) == 2:
-        #    self.pos = 
+            values = 0, 1
+        x, y = self.pos
+        if self.char_size == "absolute":
+            x += values[0] * self.char_width *1016/2.54
+            y += values[1] * self.char_height *1016/2.54
+        else:
+            x0, x1, y0, y1 = self.scale
+            dx = x1-x0
+            dy = y1-y0
+            x += values[0] * self.char_width / 100.0 * dx
+            y += values[1] * self.char_height / 100.0 * dy
+        #print "CB pos[%s] %s -> %s"%(repr(values), self.pos, (x,y))
+        self.pos = [x, y]
 
     def SI(self):
         """ Set Absolute Character Size """
         values = self.extract_value()
         self.char_size = "absolute"
         if len(values) == 0:
-            self.char_width = 0.1879
-            self.char_height = 0.2690
+            self.char_width = 0.1879 # cm
+            self.char_height = 0.2690 # cm
         elif len(values) == 2:
             self.char_width, self.char_height = values
 
@@ -388,8 +428,8 @@
         values = self.extract_value()
         self.char_size = "relative"
         if len(values) == 0:
-            self.char_width = 0.75
-            self.char_height = 1.5
+            self.char_width = 0.75 # percentage
+            self.char_height = 1.5 # id
         elif len(values) == 2:
             self.char_width, self.char_height = values
         

mercurial