File: test_renderPS.py

package info (click to toggle)
python-reportlab 4.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,708 kB
  • sloc: python: 99,020; xml: 1,494; makefile: 143; sh: 12
file content (204 lines) | stat: -rw-r--r-- 7,690 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
from xml.dom import minidom
import unittest
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPS


class RenderPSSimpleTestCase(unittest.TestCase):
    "Testing renderPS module."

    def test0(self):
        "Test two strings in drawing."

        path = outputfile("test_renderPS_simple_test0.ps")

        d = Drawing(200, 100)
        d.add(String(0, 0, "foo"))
        d.add(String(100, 0, "bar"))
        renderPS.drawToFile(d, path)

    def test1(self):
        "Test two strings in group in drawing."

        path = outputfile("test_renderPS_simple_test1.ps")

        d = Drawing(200, 100)
        g = Group()
        g.add(String(0, 0, "foo"))
        g.add(String(100, 0, "bar"))
        d.add(g)
        renderPS.drawToFile(d, path)

    def test2(self):
        "Test two strings in transformed group in drawing."

        path = outputfile("test_renderPS_simple_test2.ps")

        d = Drawing(200, 100)
        g = Group()
        g.add(String(0, 0, "foo"))
        g.add(String(100, 0, "bar"))
        g.scale(1.5, 1.2)
        g.translate(50, 0)
        d.add(g)
        renderPS.drawToFile(d, path)

    def test3(self):
        from reportlab.lib.units import cm
        from reportlab.lib import colors

        width=300
        height=60

        #Create fairly simple drawing object,
        drawing=Drawing(width, height)

        p=ArcPath(strokeColor=colors.darkgreen,
                          fillColor=colors.green,
                          hrefURL="http://en.wikipedia.org/wiki/Vector_path",
                          hrefTitle="This big letter C is actually a closed vector path.",
                          strokewidth=0)
        p.addArc(1*cm, 1*cm, 0.8*cm, 20, 340, moveTo=True)
        p.addArc(1*cm, 1*cm, 0.9*cm, 20, 340, reverse=True)
        p.closePath()
        drawing.add(p)

        drawing.add(Rect(2.25*cm, 0.1*cm, 1.5*cm, 0.8*cm, rx=0.25*cm, ry=0.25*cm,

        hrefURL="http://en.wikipedia.org/wiki/Rounded_rectangle",
                               hrefTitle="Rounded Rectangle",
                               strokeColor=colors.red,
                               fillColor=colors.yellow))

        drawing.add(String(1*cm, 1*cm, "Hello World!",
                                 hrefURL="http://en.wikipedia.org/wiki/Hello_world",
                                 hrefTitle="Why 'Hello World'?",
                                 fillColor=colors.darkgreen))
        drawing.add(Rect(4.5*cm, 0.5*cm, 5*cm, 1*cm,
                                hrefURL="http://en.wikipedia.org/wiki/Rectangle",
                                hrefTitle="Wikipedia page on rectangles",
                                strokeColor=colors.blue,
                                fillColor=colors.red))
        drawing.add(Ellipse(7*cm, 1*cm, 2*cm, 0.95*cm,
                                  hrefURL="http://en.wikipedia.org/wiki/Ellipse",
                                  strokeColor=colors.black,
                                  fillColor=colors.yellow))
        drawing.add(Circle(7*cm, 1*cm, 0.9*cm,
                                  hrefURL="http://en.wikipedia.org/wiki/Circle",
                                 strokeColor=colors.black,
                                 fillColor=colors.brown))
        drawing.add(Ellipse(7*cm, 1*cm, 0.5*cm, 0.9*cm,
                                  hrefTitle="Tooltip with no link?",
                                  strokeColor=colors.black,
                                  fillColor=colors.black))
        drawing.add(Polygon([4.5*cm, 1.25*cm, 5*cm, 0.1*cm, 4*cm, 0.1*cm],
                                  hrefURL="http://en.wikipedia.org/wiki/Polygon",
                                  hrefTitle="This triangle is a simple polygon.",
                                  strokeColor=colors.darkgreen,
                                  fillColor=colors.green))

        renderPS.drawToFile(drawing, outputfile("test_renderPS_simple_test3.ps"))

    def test4(self):
        "Test character encoding."

        path = outputfile("test_renderPS_simple_test4.ps")
        specialChar = u'\u2019'

        d = Drawing(200, 100)
        d.add(String(0, 0, "foo"+specialChar))
        d.add(String(100, 0, "bar"))
        renderPS.drawToFile(d, path)

    def test5(self):
        '''tests drawToString inspired by https://bitbucket.org/egillet/ 
        & https://bitbucket.org/johanndt/'''
        d = Drawing(1,1)
        self.assertTrue(isStr(renderPS.drawToString(d)),msg='renderPS.draweToString should return bytes')

    def tearDown(self):
        "When finished, make a little index page to view them in situ"
        
        body = """<html>
    <head><title>renderPS test output</title></head>
    <body>
        <h1>renderPS test output in a web page</h1>
        <p>We have four SVG diagrams embedded in this page.  Each is within a cyan-coloured div.
        The first 3 have a native size of 400x200, thus consume a height of 200 pixels on
        the page.  The last is 300x60.</p>

        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test0.ps" type="image/svg+xml" />
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test1.ps" type="image/svg+xml" />
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test2.ps" type="image/svg+xml" />
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" />
        </div>

        <hr>
        <p>Test of resizing:  the ones below are sized 50%, 100%, 150%. We did this by explicitly setting
        the width and height in the <code>embed</code> tag.</p>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" width="150" height="45"/>
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" width="300" height="60"/>
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" width="450" height="90"/>
        </div>
        <hr/>

        <p>Test of resizing again:  the ones below are sized 50%, 100%, 150% by setting width only.</p>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" width="150"/>
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" width="300"/>
        </div>
        <hr/>
        <div style="background-color:cyan">
            <embed src="test_renderPS_simple_test3.ps" type="image/svg+xml" width="450"/>
        </div>
        <hr/>

        
    </body>
<html>
"""
        with open('test_renderPS_output.html', 'w') as f:
            f.write(body)

class RenderPSAxesTestCase(unittest.TestCase):
    "Testing renderPS module on Axes widgets."

    def test0(self):
        "Test two strings in drawing."

        path = outputfile("axestest0.ps")
        from reportlab.graphics.charts.axes import XCategoryAxis

        d = XCategoryAxis().demo()
        renderPS.drawToFile(d, path)

def makeSuite():
    return makeSuiteForClasses(RenderPSSimpleTestCase, RenderPSAxesTestCase)

#noruntests
if __name__ == "__main__":
    unittest.TextTestRunner().run(makeSuite())
    printLocation()