def _add_explicit_image_annotation(self, a):
        """Add an image annotation using ContentStream commands instead of the
        Image type's commands. This is testing that the external XObjects API
        works, and that images can be embedded inside other, more complex
        annotations.
        """
        x1, y1, x2, y2 = 10, 310, 50, 350
        location = Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0)

        content_stream = ContentStream([
            StrokeColor(1, 0, 0),
            Rect(x1, y1, x2 - x1, y2 - y1),
            Save(),
            # The image is inside an outer rectangle
            CTM(Image.get_ctm(x1 + 10, y1 + 10, x2 - 10, y2 - 10)),
            XObject('MyXObject'),
            Restore(),
            Stroke(),
        ])
        appearance = Appearance(
            appearance_stream=content_stream,
            xobjects={
                'MyXObject': Image.make_image_xobject(PNG_FILES[0]),
            },
        )

        a.add_annotation(
            'square',
            location=location,
            appearance=appearance,
        )
Ejemplo n.º 2
0
    def make_appearance_stream(self):
        A = self._appearance
        L = self._location

        stream = ContentStream([Save()])
        set_appearance_state(stream, A)
        stream.extend([
            Rect(L.x1, L.y1, L.x2 - L.x1, L.y2 - L.y1),
            CTM(self.get_ctm(L.x1, L.y1, L.x2, L.y2)),
            XObject('Image'),
            Restore(),
        ])
        return stream
Ejemplo n.º 3
0
Archivo: rect.py Proyecto: nbsas/notet
    def make_appearance_stream(self):
        L = self._location
        A = self._appearance
        stream = ContentStream([Save()])

        set_appearance_state(stream, A)
        stream.add(Rect(
            L.x1,
            L.y1,
            L.x2 - L.x1,
            L.y2 - L.y1,
        ))
        stroke_or_fill(stream, A)
        stream.add(Restore())

        # TODO dash array
        return stream
Ejemplo n.º 4
0
 def test_commands_get_number_formatting(self):
     # Regression test to make sure that all commands that output number
     # have sane formatting.
     stream = ContentStream([
         StrokeWidth(0.0),
         StrokeColor(0.0, 0.0, 0.0),
         FillColor(0.0, 0.0, 0.0),
         Rect(0.0, 0.0, 0.0, 0.0),
         Move(0.0, 0.0),
         Line(0.0, 0.0),
         Bezier(0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
         CTM([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
         TextMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
     ]).resolve()
     assert stream == ('0 w '
                       '0 0 0 RG '
                       '0 0 0 rg '
                       '0 0 0 0 re '
                       '0 0 m '
                       '0 0 l '
                       '0 0 0 0 0 0 c '
                       '0 0 0 0 0 0 cm '
                       '0 0 0 0 0 0 Tm')
Ejemplo n.º 5
0
 def test_content_stream(self):
     # Basically a smoke test for all the simple functions of ContentStream
     cs = ContentStream([Save(), StrokeWidth(2)])
     cs.add(StrokeColor(0, 0, 0))
     cs.add(FillColor(1, 0, 0))
     cs.extend([
         Move(10, 10),
         Line(20, 20),
         Bezier(30, 30, 40, 40, 50, 50),
         Rect(50, 50, 10, 10),
     ])
     cs = ContentStream.join(
         cs,
         ContentStream([
             Close(),
             StrokeAndFill(),
             Stroke(),
             Fill(),
             Restore(),
         ]))
     assert cs.resolve() == ('q 2 w 0 0 0 RG 1 0 0 rg 10 10 m 20 20 l '
                             '30 30 40 40 50 50 c 50 50 10 10 re '
                             'h B S f Q')
Ejemplo n.º 6
0
 def test_transform_rect(self):
     transformed = Rect(1, 1, 2, 2).transform([2, 0, 0, 2, 5, 10]).resolve()
     assert transformed == '7 12 4 4 re'
Ejemplo n.º 7
0
class TestContentStream(TestCase):
    # a list of (ContentStream, stream_string) pairs for testing parse/resolve
    FIXTURES = [
        (ContentStream([
            CTM([1, 0, 0, 1, 0, 0]),
            Font('Helvetica', 12),
            TextMatrix([1, 0, 0, 1, 20, 50]),
            BeginText(),
            Text('Sure, why not?'),
            EndText(),
        ]), ('1 0 0 1 0 0 cm /Helvetica 12 Tf '
             '1 0 0 1 20 50 Tm BT '
             '(Sure, why not?) Tj ET')),
        (ContentStream([
            Save(),
            StrokeWidth(2),
            StrokeColor(0, 0, 0),
            FillColor(1, 0, 0),
            Move(10, 10),
            Line(20, 20),
            Bezier(30, 30, 40, 40, 50, 50),
            Rect(50, 50, 10, 10),
            Close(),
            StrokeAndFill(),
            Stroke(),
            Fill(),
            Restore(),
        ]), ('q 2 w 0 0 0 RG 1 0 0 rg 10 10 m 20 20 l '
             '30 30 40 40 50 50 c 50 50 10 10 re '
             'h B S f Q')),
    ]

    def test_equality(self):
        assert ContentStream() == ContentStream()

        cs1 = ContentStream([Save(), FillColor(1, 0, 0)])
        cs2 = ContentStream([Save(), FillColor(1, 0, 0)])
        assert cs1 == cs2

    def test_content_stream_not_equal_to_string(self):
        assert ContentStream() != ''
        assert ContentStream([Save()]) != 'q'

    def test_resolve(self):
        for cs, stream_string in self.FIXTURES:
            assert cs.resolve() == stream_string

    def test_content_stream(self):
        # Basically a smoke test for all the simple functions of ContentStream
        cs = ContentStream([Save(), StrokeWidth(2)])
        cs.add(StrokeColor(0, 0, 0))
        cs.add(FillColor(1, 0, 0))
        cs.extend([
            Move(10, 10),
            Line(20, 20),
            Bezier(30, 30, 40, 40, 50, 50),
            Rect(50, 50, 10, 10),
        ])
        cs = ContentStream.join(
            cs,
            ContentStream([
                Close(),
                StrokeAndFill(),
                Stroke(),
                Fill(),
                Restore(),
            ]))
        assert cs.resolve() == ('q 2 w 0 0 0 RG 1 0 0 rg 10 10 m 20 20 l '
                                '30 30 40 40 50 50 c 50 50 10 10 re '
                                'h B S f Q')

    def test_text_content_stream(self):
        cs = ContentStream([
            CTM([1, 0, 0, 1, 0, 0]),
            Font('Helvetica', 12),
            TextMatrix([1, 0, 0, 1, 20, 50]),
            BeginText(),
            Text('Sure, why not?'),
            EndText(),
        ])
        assert cs.resolve() == ('1 0 0 1 0 0 cm /Helvetica 12 Tf '
                                '1 0 0 1 20 50 Tm BT '
                                '(Sure, why not?) Tj ET')

    def test_transform_move(self):
        transformed = Move(1, 1).transform([2, 0, 0, 2, 5, 10]).resolve()
        assert transformed == '7 12 m'

    def test_transform_line(self):
        transformed = Line(1, 1).transform([2, 0, 0, 2, 5, 10]).resolve()
        assert transformed == '7 12 l'

    def test_transform_rect(self):
        transformed = Rect(1, 1, 2, 2).transform([2, 0, 0, 2, 5, 10]).resolve()
        assert transformed == '7 12 4 4 re'

    def test_transform_bezier(self):
        transformed = Bezier(1, 1, 2, 2, 3,
                             3).transform([2, 0, 0, 2, 5, 10], ).resolve()
        assert transformed == '7 12 9 14 11 16 c'

    def test_transform_content_stream(self):
        cs = ContentStream([
            Save(),
            Move(1, 1),
            Restore(),
        ])
        transformed = cs.transform([2, 0, 0, 2, 5, 10]).resolve()
        assert transformed == 'q 7 12 m Q'