Commits

Hiroki MIZUNO committed a1ca53a

excel format: support arc and ellipse

Comments (0)

Files changed (1)

src/blockdiag/imagedraw/excel.py

-# -*- coding: utf-8 -*- #  Copyright 2011 Takeshi KOMIYA # #  Licensed under the Apache License, Version 2.0 (the "License"); #  you may not use this file except in compliance with the License.  #  You may obtain a copy of the License at # #      http://www.apache.org/licenses/LICENSE-2.0 # #  Unless required by applicable law or agreed to in writing, software #  distributed under the License is distributed on an "AS IS" BASIS, #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  #  See the License for the specific language governing permissions and #  limitations under the License.
+# -*- coding: utf-8 -*- #  Copyright 2011 Takeshi KOMIYA 
+# 
+#  Licensed under the Apache License, Version 2.0 (the "License"); 
+#  you may not use this file except in compliance with the License.  
+#  You may obtain a copy of the License at 
+# 
+#      http://www.apache.org/licenses/LICENSE-2.0 
+# 
+#  Unless required by applicable law or agreed to in writing, software 
+#  distributed under the License is distributed on an "AS IS" BASIS, 
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
+#  See the License for the specific language governing permissions and 
+#  limitations under the License.  
 
 import re
 import base64
     def __init__(self, filename, size, **kwargs):
         self.excel = win32com.client.Dispatch("Excel.Application") 
         self.excel.Visible=True
-#        self.DisplayAlerts=False #�x�����b�Z�[�W��OFF
+        self.DisplayAlerts=False #�x�����b�Z�[�W��OFF
         self.book = self.excel.Workbooks.Add()
 
     def path(self, pd, **kwargs):
         pass
 
     def rgb(self, rgb):
-        r,g,b = rgb
-        return (r << 16) + (g << 8) + b
+        if rgb == "white":
+            return 0xFFFFFF
+        else:
+            r,g,b = rgb
+            return (r << 16) + (g << 8) + b
+
+    def set_style(self, shape, kwargs):
+        fill = kwargs.get('fill')
+        outline = kwargs.get('outline')
+        tick = kwargs.get('tick')
+
+        if tick != None:
+            shape.Line.Weight = 1
+
+        if outline != None:
+            shape.Line.ForeColor.RGB = self.rgb(outline)
+        if fill != None:
+            shape.Fill.ForeColor.RGB = self.rgb(fill)
 
     def rectangle(self, box, **kwargs):
         left = box[0]
         width = box[2] - box[0]
         height = box[3]  - box[1]
 
-        fill = kwargs.get('fill', 'none')
-        outline = kwargs.get('outline')
-
-        shape = self.book.ActiveSheet.Shapes.AddShape(1, left, top, width, height)
-        shape.Line.ForeColor.RGB = self.rgb(outline)
-
-        if fill != "none":
-            shape.Fill.ForeColor.RGB = self.rgb(fill)
+        shape = self.book.ActiveSheet.Shapes.AddShape(1, left, top, width, height);
+        self.set_style(shape, kwargs)
 
     def text(self, xy, string, font, **kwargs):
         print "text"
         shape.Line.Visible = 0
 
         shape.Fill.ForeColor.RGB = 0xFFFFFF
+        shape.Fill.Visible = False
         chars = shape.TextFrame.Characters()
         chars.Text = string
         chars.Font.Color = self.rgb(fill)
         self.book.ActiveSheet.Shapes.AddLine(x1, y1, x2, y2)
 
     def arc(self, xy, start, end, **kwargs):
-        print "arc"
+        w = (xy[2] - xy[0])
+        h = (xy[3] - xy[1])
+        pt = XY(xy[0], xy[1])
+        shape = self.book.ActiveSheet.Shapes.AddShape(25, pt.x, pt.y, w, h)
+        shape.Adjustments.SetItem(1, start - end)
+        shape.Rotation = start + 90
+        shape.Left = xy[0]
+        shape.Top = xy[1]
 
     def ellipse(self, xy, **kwargs):
-        print "eclipse"
+        w = (xy[2] - xy[0])
+        h = (xy[3] - xy[1])
+        pt = XY(xy[0], xy[1])
+
+        shape = self.book.ActiveSheet.Shapes.AddShape(9, pt.x, pt.y, w, h)
+        self.set_style(shape, kwargs)
 
     def polygon(self, xy, **kwargs):
-        fill = kwargs.get('fill', 'none')
-        outline = kwargs.get('outline')
-
         form = self.book.ActiveSheet.Shapes.BuildFreeform(0, xy[0][0], xy[0][1])
 
         for (x,y) in xy[1:]:
             form.AddNodes(0, 0, x, y) 
         shape = form.ConvertToShape()
-        shape.Line.ForeColor.RGB = self.rgb(outline)
-
-        if fill != "none":
-            shape.Fill.ForeColor.RGB = self.rgb(fill)
-
+        self.set_style(shape, kwargs)
 
     def loadImage(self, filename, box):
         pass