testdrawings.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/bin/env python
  2. #Copyright ReportLab Europe Ltd. 2000-2017
  3. #see license.txt for license details
  4. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/graphics/testdrawings.py
  5. __version__='3.3.0'
  6. __doc__="""Defines some standard drawings to use as test cases
  7. This contains a number of routines to generate test drawings
  8. for reportlab/graphics. For now they are contrived, but we will expand them
  9. to try and trip up any parser. Feel free to add more.
  10. """
  11. from reportlab.graphics.shapes import *
  12. from reportlab.lib import colors
  13. def getDrawing1():
  14. """Hello World, on a rectangular background"""
  15. D = Drawing(400, 200)
  16. D.add(Rect(50, 50, 300, 100, fillColor=colors.yellow)) #round corners
  17. D.add(String(180,100, 'Hello World', fillColor=colors.red))
  18. return D
  19. def getDrawing2():
  20. """This demonstrates the basic shapes. There are
  21. no groups or references. Each solid shape should have
  22. a purple fill."""
  23. D = Drawing(400, 200) #, fillColor=colors.purple)
  24. D.add(Line(10,10,390,190))
  25. D.add(Circle(100,100,20, fillColor=colors.purple))
  26. D.add(Circle(200,100,20, fillColor=colors.purple))
  27. D.add(Circle(300,100,20, fillColor=colors.purple))
  28. D.add(Wedge(330,100,40, -10,40, fillColor=colors.purple))
  29. D.add(PolyLine([120,10,130,20,140,10,150,20,160,10,
  30. 170,20,180,10,190,20,200,10]))
  31. D.add(Polygon([300,20,350,20,390,80,300,75, 330, 40]))
  32. D.add(Ellipse(50, 150, 40, 20))
  33. D.add(Rect(120, 150, 60, 30,
  34. strokeWidth=10,
  35. strokeColor=colors.red,
  36. fillColor=colors.yellow)) #square corners
  37. D.add(Rect(220, 150, 60, 30, 10, 10)) #round corners
  38. D.add(String(10,50, 'Basic Shapes', fillColor=colors.black))
  39. return D
  40. ##def getDrawing2():
  41. ## """This drawing uses groups. Each group has two circles and a comment.
  42. ## The line style is set at group level and should be red for the left,
  43. ## bvlue for the right."""
  44. ## D = Drawing(400, 200)
  45. ##
  46. ## Group1 = Group()
  47. ##
  48. ## Group1.add(String(50, 50, 'Group 1', fillColor=colors.black))
  49. ## Group1.add(Circle(75,100,25))
  50. ## Group1.add(Circle(125,100,25))
  51. ## D.add(Group1)
  52. ##
  53. ## Group2 = Group(
  54. ## String(250, 50, 'Group 2', fillColor=colors.black),
  55. ## Circle(275,100,25),
  56. ## Circle(325,100,25)#,
  57. ##def getDrawing2():
  58. ## """This drawing uses groups. Each group has two circles and a comment.
  59. ## The line style is set at group level and should be red for the left,
  60. ## bvlue for the right."""
  61. ## D = Drawing(400, 200)
  62. ##
  63. ## Group1 = Group()
  64. ##
  65. ## Group1.add(String(50, 50, 'Group 1', fillColor=colors.black))
  66. ## Group1.add(Circle(75,100,25))
  67. ## Group1.add(Circle(125,100,25))
  68. ## D.add(Group1)
  69. ##
  70. ## Group2 = Group(
  71. ## String(250, 50, 'Group 2', fillColor=colors.black),
  72. ## Circle(275,100,25),
  73. ## Circle(325,100,25)#,
  74. ##
  75. ## #group attributes
  76. ## #strokeColor=colors.blue
  77. ## )
  78. ## D.add(Group2)
  79. ## return D
  80. ##
  81. ##
  82. ##def getDrawing3():
  83. ## """This uses a named reference object. The house is a 'subroutine'
  84. ## the basic brick colored walls are defined, but the roof and window
  85. ## color are undefined and may be set by the container."""
  86. ##
  87. ## D = Drawing(400, 200, fill=colors.bisque)
  88. ##
  89. ##
  90. ## House = Group(
  91. ## Rect(2,20,36,30, fill=colors.bisque), #walls
  92. ## Polygon([0,20,40,20,20,5]), #roof
  93. ## Rect(8, 38, 8, 12), #door
  94. ## Rect(25, 38, 8, 7), #window
  95. ## Rect(8, 25, 8, 7), #window
  96. ## Rect(25, 25, 8, 7) #window
  97. ##
  98. ## )
  99. ## D.addDef('MyHouse', House)
  100. ##
  101. ## # one row all the same color
  102. ## D.add(String(20, 40, 'British Street...',fill=colors.black))
  103. ## for i in range(6):
  104. ## x = i * 50
  105. ## D.add(NamedReference('MyHouse',
  106. ## House,
  107. ## transform=translate(x, 40),
  108. ## fill = colors.brown
  109. ## )
  110. ## )
  111. ##
  112. ## # now do a row all different
  113. ## D.add(String(20, 120, 'Mediterranean Street...',fill=colors.black))
  114. ## x = 0
  115. ## for color in (colors.blue, colors.yellow, colors.orange,
  116. ## colors.red, colors.green, colors.chartreuse):
  117. ## D.add(NamedReference('MyHouse',
  118. ## House,
  119. ## transform=translate(x,120),
  120. ## fill = color,
  121. ## )
  122. ## )
  123. ## x = x + 50
  124. ## #..by popular demand, the mayor gets a big one at the end
  125. ## D.add(NamedReference('MyHouse',
  126. ## House,
  127. ## transform=mmult(translate(x,110), scale(1.2,1.2)),
  128. ## fill = color,
  129. ## )
  130. ## )
  131. ##
  132. ##
  133. ## return D
  134. ##
  135. ##def getDrawing4():
  136. ## """This tests that attributes are 'unset' correctly when
  137. ## one steps back out of a drawing node. All the circles are part of a
  138. ## group setting the line color to blue; the second circle explicitly
  139. ## sets it to red. Ideally, the third circle should go back to blue."""
  140. ## D = Drawing(400, 200)
  141. ##
  142. ##
  143. ## G = Group(
  144. ## Circle(100,100,20),
  145. ## Circle(200,100,20, stroke=colors.blue),
  146. ## Circle(300,100,20),
  147. ## stroke=colors.red,
  148. ## stroke_width=3,
  149. ## fill=colors.aqua
  150. ## )
  151. ## D.add(G)
  152. ##
  153. ##
  154. ## D.add(String(10,50, 'Stack Unwinding - should be red, blue, red'))
  155. ##
  156. ## return D
  157. ##
  158. ##
  159. ##def getDrawing5():
  160. ## """This Rotates Coordinate Axes"""
  161. ## D = Drawing(400, 200)
  162. ##
  163. ##
  164. ##
  165. ## Axis = Group(
  166. ## Line(0,0,100,0), #x axis
  167. ## Line(0,0,0,50), # y axis
  168. ## Line(0,10,10,10), #ticks on y axis
  169. ## Line(0,20,10,20),
  170. ## Line(0,30,10,30),
  171. ## Line(0,40,10,40),
  172. ## Line(10,0,10,10), #ticks on x axis
  173. ## Line(20,0,20,10),
  174. ## Line(30,0,30,10),
  175. ## Line(40,0,40,10),
  176. ## Line(50,0,50,10),
  177. ## Line(60,0,60,10),
  178. ## Line(70,0,70,10),
  179. ## Line(80,0,80,10),
  180. ## Line(90,0,90,10),
  181. ## String(20, 35, 'Axes', fill=colors.black)
  182. ## )
  183. ##
  184. ## D.addDef('Axes', Axis)
  185. ##
  186. ## D.add(NamedReference('Axis', Axis,
  187. ## transform=translate(10,10)))
  188. ## D.add(NamedReference('Axis', Axis,
  189. ## transform=mmult(translate(150,10),rotate(15)))
  190. ## )
  191. ## return D
  192. ##
  193. ##def getDrawing6():
  194. ## """This Rotates Text"""
  195. ## D = Drawing(400, 300, fill=colors.black)
  196. ##
  197. ## xform = translate(200,150)
  198. ## C = (colors.black,colors.red,colors.green,colors.blue,colors.brown,colors.gray, colors.pink,
  199. ## colors.lavender,colors.lime, colors.mediumblue, colors.magenta, colors.limegreen)
  200. ##
  201. ## for i in range(12):
  202. ## D.add(String(0, 0, ' - - Rotated Text', fill=C[i%len(C)], transform=mmult(xform, rotate(30*i))))
  203. ##
  204. ## return D
  205. ##
  206. ##def getDrawing7():
  207. ## """This defines and tests a simple UserNode0 (the trailing zero denotes
  208. ## an experimental method which is not part of the supported API yet).
  209. ## Each of the four charts is a subclass of UserNode which generates a random
  210. ## series when rendered."""
  211. ##
  212. ## class MyUserNode(UserNode0):
  213. ## import whrandom, math
  214. ##
  215. ##
  216. ## def provideNode(self, sender):
  217. ## """draw a simple chart that changes everytime it's drawn"""
  218. ## # print "here's a random number %s" % self.whrandom.random()
  219. ## #print "MyUserNode.provideNode being called by %s" % sender
  220. ## g = Group()
  221. ## #g._state = self._state # this is naughty
  222. ## PingoNode.__init__(g, self._state) # is this less naughty ?
  223. ## w = 80.0
  224. ## h = 50.0
  225. ## g.add(Rect(0,0, w, h, stroke=colors.black))
  226. ## N = 10.0
  227. ## x,y = (0,h)
  228. ## dx = w/N
  229. ## for ii in range(N):
  230. ## dy = (h/N) * self.whrandom.random()
  231. ## g.add(Line(x,y,x+dx, y-dy))
  232. ## x = x + dx
  233. ## y = y - dy
  234. ## return g
  235. ##
  236. ## D = Drawing(400,200, fill=colors.white) # AR - same size as others
  237. ##
  238. ## D.add(MyUserNode())
  239. ##
  240. ## graphcolor= [colors.green, colors.red, colors.brown, colors.purple]
  241. ## for ii in range(4):
  242. ## D.add(Group( MyUserNode(stroke=graphcolor[ii], stroke_width=2),
  243. ## transform=translate(ii*90,0) ))
  244. ##
  245. ## #un = MyUserNode()
  246. ## #print un.provideNode()
  247. ## return D
  248. ##
  249. ##def getDrawing8():
  250. ## """Test Path operations--lineto, curveTo, etc."""
  251. ## D = Drawing(400, 200, fill=None, stroke=colors.purple, stroke_width=2)
  252. ##
  253. ## xform = translate(200,100)
  254. ## C = (colors.black,colors.red,colors.green,colors.blue,colors.brown,colors.gray, colors.pink,
  255. ## colors.lavender,colors.lime, colors.mediumblue, colors.magenta, colors.limegreen)
  256. ## p = Path(50,50)
  257. ## p.lineTo(100,100)
  258. ## p.moveBy(-25,25)
  259. ## p.curveTo(150,125, 125,125, 200,50)
  260. ## p.curveTo(175, 75, 175, 98, 62, 87)
  261. ##
  262. ##
  263. ## D.add(p)
  264. ## D.add(String(10,30, 'Tests of path elements-lines and bezier curves-and text formating'))
  265. ## D.add(Line(220,150, 220,200, stroke=colors.red))
  266. ## D.add(String(220,180, "Text should be centered", text_anchor="middle") )
  267. ##
  268. ##
  269. ## return D
  270. if __name__=='__main__':
  271. print(__doc__)