europeanflag-as-odt.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # This is an example of an OpenDocument Text.
  21. # We are going to draw the European flag. I chose this motive because
  22. # it has just the right complexity for an example. It contains 12 five-edge
  23. # stars in a circle on a blue background. The production specifications can
  24. # be found at http://europa.eu/abc/symbols/emblem/index_en.htm
  25. #
  26. # The stars are drawn with a vector-oriented "turtle" the way Seymour Papert's
  27. # LOGO language does.
  28. import math
  29. from odf.opendocument import OpenDocumentText
  30. from odf.style import Style, GraphicProperties, DrawingPageProperties
  31. from odf.text import P
  32. from odf.draw import G, Polygon, Rect
  33. class VectorSet:
  34. """ A class to simulate LOGO's turtle. The turtle starts a 0,0 pointing
  35. right along the x-axis, since we use the mathematical coordinate system.
  36. """
  37. orientation = 0 # Degrees
  38. x = 0.0
  39. y = 0.0
  40. polygon = []
  41. def forward(self, length):
  42. orirad = math.radians(self.orientation)
  43. self.x = self.x + length * math.cos(orirad)
  44. self.y = self.y + length * math.sin(orirad)
  45. def right(self, turn):
  46. self.orientation = (self.orientation + turn) % 360
  47. def left(self, turn):
  48. self.orientation = (self.orientation - turn) % 360
  49. def mark(self):
  50. self.polygon.append((self.x,self.y))
  51. def firstmark(self):
  52. self.polygon.append(self.polygon[0])
  53. def getpoints(self):
  54. """ Return the polygon points """
  55. strpairs = ["%.0f,%.0f" % item for item in self.polygon]
  56. return ' '.join(strpairs)
  57. def getviewbox(self):
  58. ''' The value of the viewBox attribute is a list of four numbers
  59. <min-x>, <min-y>, <width> and <height>'''
  60. xvals = [ item[0] for item in self.polygon]
  61. maxx = int(reduce(max,xvals)) + 1
  62. minx = int(reduce(min,xvals))
  63. yvals = [ item[1] for item in self.polygon]
  64. maxy = int(reduce(max,yvals)) + 1
  65. miny = int(reduce(min,yvals))
  66. return minx, miny, maxx-minx, maxy-miny
  67. # Create the document
  68. doc = OpenDocumentText()
  69. # The blue background style of the flag
  70. backgroundstyle = Style(family="graphic", name="blueback")
  71. backgroundstyle.addElement(GraphicProperties(fill="solid", fillcolor="#003399", stroke="none"))
  72. doc.automaticstyles.addElement(backgroundstyle)
  73. # The style for the stars
  74. starstyle = Style(family="graphic", name="starstyle")
  75. starstyle.addElement(GraphicProperties(fill="solid", fillcolor="#ffcc00", stroke="none"))
  76. doc.automaticstyles.addElement(starstyle)
  77. # Create a paragraph to contain the drawing
  78. drawpage = P()
  79. doc.text.addElement(drawpage)
  80. group=G()
  81. drawpage.addElement(group)
  82. turtle = VectorSet()
  83. # Draw the edges
  84. turtle.mark()
  85. for edge in [ 0,1,2,3,5 ]:
  86. turtle.forward(100)
  87. turtle.mark()
  88. turtle.right(144)
  89. turtle.forward(100)
  90. turtle.mark()
  91. turtle.left(72)
  92. turtle.firstmark()
  93. # Draw a rectangle containing the blue background
  94. group.addElement(Rect(height="120mm", width="180mm", x="0mm", y="0mm", stylename=backgroundstyle))
  95. viewbox = ' '.join(map(str,turtle.getviewbox()))
  96. points = turtle.getpoints()
  97. # Go around in a circle in twelve steps
  98. for deg in range(0,360,30):
  99. x = 83.3 + math.cos(math.radians(deg)) * 40
  100. y = 53.3 + math.sin(math.radians(deg)) * 40
  101. group.addElement(Polygon(points=points,
  102. stylename=starstyle, viewbox=viewbox, width="13.3mm", height="13.3mm", x="%0.2fmm" % x, y="%0.2fmm" % y))
  103. # Save the work
  104. doc.save("europeanflag", True)