1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (C) 2007 Søren Roug, European Environment Agency 4# 5# This is free software. You may redistribute it under the terms 6# of the Apache license and the GNU General Public License Version 7# 2 or at your option any later version. 8# 9# This program is distributed in the hope that it will be useful, 10# but WITHOUT ANY WARRANTY; without even the implied warranty of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12# GNU General Public License for more details. 13# 14# You should have received a copy of the GNU General Public 15# License along with this program; if not, write to the Free Software 16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17# 18# Contributor(s): 19# 20 21# This is an example of an OpenDocument Drawing. 22# We are going to draw the European flag. I chose this motive because 23# it has just the right complexity for an example. It contains 12 five-edge 24# stars in a circle on a blue background. The production specifications can 25# be found at http://europa.eu/abc/symbols/emblem/index_en.htm 26# 27# The stars are drawn with a vector-oriented "turtle" the way Seymour Papert's 28# LOGO language does. 29import math 30from odf.opendocument import OpenDocumentDrawing 31from odf.style import Style, MasterPage, PageLayout, PageLayoutProperties, \ 32 GraphicProperties, DrawingPageProperties 33from odf.draw import Page, G, Polygon, Rect 34 35class VectorSet: 36 """ A class to simulate LOGO's turtle. The turtle starts a 0,0 pointing 37 right along the x-axis, since we use the mathematical coordinate system. 38 """ 39 orientation = 0 # Degrees 40 x = 0.0 41 y = 0.0 42 polygon = [] 43 44 def forward(self, length): 45 orirad = math.radians(self.orientation) 46 self.x = self.x + length * math.cos(orirad) 47 self.y = self.y + length * math.sin(orirad) 48 49 def right(self, turn): 50 self.orientation = (self.orientation + turn) % 360 51 52 def left(self, turn): 53 self.orientation = (self.orientation - turn) % 360 54 55 def mark(self): 56 self.polygon.append((self.x,self.y)) 57 58 def firstmark(self): 59 self.polygon.append(self.polygon[0]) 60 61 def getpoints(self): 62 """ Return the polygon points """ 63 strpairs = ["%.0f,%.0f" % item for item in self.polygon] 64 return ' '.join(strpairs) 65 66 def getviewbox(self): 67 ''' The value of the viewBox attribute is a list of four numbers 68 <min-x>, <min-y>, <width> and <height>''' 69 xvals = [ item[0] for item in self.polygon] 70 maxx = int(reduce(max,xvals)) + 1 71 minx = int(reduce(min,xvals)) 72 yvals = [ item[1] for item in self.polygon] 73 maxy = int(reduce(max,yvals)) + 1 74 miny = int(reduce(min,yvals)) 75 return minx, miny, maxx-minx, maxy-miny 76 77 78# Create the document 79doc = OpenDocumentDrawing() 80 81# Create the drawing page 82dpstyle = Style(family="drawing-page",name="DP1") 83dpstyle.addElement(DrawingPageProperties(backgroundsize="border", fill="none")) 84doc.automaticstyles.addElement(dpstyle) 85 86# The blue background style of the flag 87backgroundstyle = Style(family="graphic", name="blueback") 88backgroundstyle.addElement(GraphicProperties(fill="solid", fillcolor="#003399", stroke="none")) 89doc.automaticstyles.addElement(backgroundstyle) 90 91# The style for the stars 92starstyle = Style(family="graphic", name="starstyle") 93starstyle.addElement(GraphicProperties(fill="solid", fillcolor="#ffcc00", stroke="none")) 94doc.automaticstyles.addElement(starstyle) 95 96# Create page layout specifying dimensions 97plstyle = PageLayout(name="PM1") 98plstyle.addElement(PageLayoutProperties(margin="0cm", pageheight="120mm", pagewidth="180mm", printorientation="portrait")) 99doc.automaticstyles.addElement(plstyle) 100 101# Create a master page 102masterpage = MasterPage(stylename=dpstyle, name="Default", pagelayoutname=plstyle) 103doc.masterstyles.addElement(masterpage) 104 105# Create a page to contain the drawing 106drawpage = Page(masterpagename=masterpage, name="page1", stylename=dpstyle) 107doc.drawing.addElement(drawpage) 108 109group=G() 110drawpage.addElement(group) 111 112turtle = VectorSet() 113# Draw the edges 114turtle.mark() 115for edge in [ 0,1,2,3,5 ]: 116 turtle.forward(100) 117 turtle.mark() 118 turtle.right(144) 119 turtle.forward(100) 120 turtle.mark() 121 turtle.left(72) 122turtle.firstmark() 123 124# Draw a rectangle containing the blue background 125group.addElement(Rect(height="120mm", width="180mm", x="0mm", y="0mm", stylename=backgroundstyle)) 126 127viewbox = ' '.join(map(str,turtle.getviewbox())) 128points = turtle.getpoints() 129 130# Go around in a circle in twelve steps 131for deg in range(0,360,30): 132 x = 83.3 + math.cos(math.radians(deg)) * 40 133 y = 53.3 + math.sin(math.radians(deg)) * 40 134 group.addElement(Polygon(points=points, 135 stylename=starstyle, viewbox=viewbox, width="13.3mm", height="13.3mm", x="%0.2fmm" % x, y="%0.2fmm" % y)) 136 137# Save the work 138doc.save("europeanflag", True) 139