def _add_rounded_rectangles(self, a):
     """Add a few rounded rectangles with different border radii."""
     y1, y2 = 360, 410
     xs = [10, 60, 110]
     rxs = [5, 10, 15]
     rys = [5, 5, 15]
     for x1, rx, ry in zip(xs, rxs, rys):
         x2 = x1 + 40
         location = Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0)
         content_stream = ContentStream([
             Save(),
             StrokeColor(1, 0, 0),
             FillColor(0, 1, 0),
         ])
         add_rounded_rectangle(
             stream=content_stream,
             x=x1,
             y=y1,
             width=(x2 - x1),
             height=(y2 - y1),
             rx=rx,
             ry=ry,
         )
         content_stream.extend([
             StrokeAndFill(),
             Restore(),
         ])
         appearance = Appearance(appearance_stream=content_stream, )
         a.add_annotation('square', location, appearance)
Esempio n. 2
0
    def make_appearance_stream(self):
        A = self._appearance
        L = self._location

        stream = ContentStream([
            Save(),
            BeginText(),
            FillColor(*A.fill[:3]),
            Font(PDF_ANNOTATOR_FONT, A.font_size),
        ])

        graphics_state = A.get_graphics_state()
        if graphics_state.has_content():
            stream.add(CSGraphicsState(GRAPHICS_STATE_NAME))

        # Actually draw the text inside the rectangle
        stream.extend(
            get_text_commands(
                L.x1,
                L.y1,
                L.x2,
                L.y2,
                text=A.content,
                font_size=A.font_size,
                wrap_text=A.wrap_text,
                align=A.text_align,
                baseline=A.text_baseline,
                line_spacing=A.line_spacing,
            ))
        stream.extend([
            EndText(),
            Restore(),
        ])

        return stream
Esempio n. 3
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
Esempio n. 4
0
    def make_appearance_stream(self):
        A = self._appearance
        points = self._location.points

        stream = ContentStream([Save()])
        set_appearance_state(stream, A)
        stream.add(Move(points[0][0], points[0][1]))
        for x, y in points[1:]:
            stream.add(CSLine(x, y))
        # TODO add a 'close' attribute?
        stream.extend([Stroke(), Restore()])

        return stream
Esempio n. 5
0
    def make_appearance_stream(self):
        A = self._appearance
        points = self._location.points

        stream = ContentStream([Save()])
        set_appearance_state(stream, A)
        stream.add(Move(points[0][0], points[0][1]))
        # TODO "real" PDF editors do smart smoothing of ink points using
        # interpolated Bezier curves.
        for x, y in points[1:]:
            stream.add(CSLine(x, y))
        stream.extend([Stroke(), Restore()])

        return stream
    def _add_explicit_text_annotation(self, a):
        x1, y1, x2, y2 = 110, 310, 200, 350
        font_size = 4

        content_stream = ContentStream([
            Save(),
            BeginText(),
            FillColor(0, 0, 0),
            Font('MyFontyFont', font_size),
        ])
        content_stream.extend(
            get_text_commands(
                x1,
                y1,
                x2,
                y2,
                text=(r'Twas brilling and the slithy toves \n'
                      r'Did gyre and gimbel in the wabe \n'
                      r'All mimsy were the borogroves \n'
                      r'And the mome raths outgrabe \n'),
                font_size=font_size,
                wrap_text=True,
                align=constants.TEXT_ALIGN_LEFT,
                baseline=constants.TEXT_BASELINE_TOP,
                line_spacing=1.2,
            ))
        content_stream.extend([
            EndText(),
            Restore(),
        ])

        appearance = Appearance(
            appearance_stream=content_stream,
            fonts={'MyFontyFont': FreeText.make_font_object()},
        )
        a.add_annotation(
            'square',
            location=Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0),
            appearance=appearance,
        )
Esempio n. 7
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')