File size: 6,229 Bytes
3f7c971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
#
# The Python Imaging Library
# $Id$
#
# WCK-style drawing interface operations
#
# History:
# 2003-12-07 fl   created
# 2005-05-15 fl   updated; added to PIL as ImageDraw2
# 2005-05-15 fl   added text support
# 2005-05-20 fl   added arc/chord/pieslice support
#
# Copyright (c) 2003-2005 by Secret Labs AB
# Copyright (c) 2003-2005 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#


"""

(Experimental) WCK-style drawing interface operations



.. seealso:: :py:mod:`PIL.ImageDraw`

"""
from __future__ import annotations

from typing import BinaryIO

from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
from ._typing import StrOrBytesPath


class Pen:
    """Stores an outline color and width."""

    def __init__(self, color: str, width: int = 1, opacity: int = 255) -> None:
        self.color = ImageColor.getrgb(color)
        self.width = width


class Brush:
    """Stores a fill color"""

    def __init__(self, color: str, opacity: int = 255) -> None:
        self.color = ImageColor.getrgb(color)


class Font:
    """Stores a TrueType font and color"""

    def __init__(

        self, color: str, file: StrOrBytesPath | BinaryIO, size: float = 12

    ) -> None:
        # FIXME: add support for bitmap fonts
        self.color = ImageColor.getrgb(color)
        self.font = ImageFont.truetype(file, size)


class Draw:
    """

    (Experimental) WCK-style drawing interface

    """

    def __init__(

        self,

        image: Image.Image | str,

        size: tuple[int, int] | list[int] | None = None,

        color: float | tuple[float, ...] | str | None = None,

    ) -> None:
        if isinstance(image, str):
            if size is None:
                msg = "If image argument is mode string, size must be a list or tuple"
                raise ValueError(msg)
            image = Image.new(image, size, color)
        self.draw = ImageDraw.Draw(image)
        self.image = image
        self.transform = None

    def flush(self) -> Image.Image:
        return self.image

    def render(self, op, xy, pen, brush=None):
        # handle color arguments
        outline = fill = None
        width = 1
        if isinstance(pen, Pen):
            outline = pen.color
            width = pen.width
        elif isinstance(brush, Pen):
            outline = brush.color
            width = brush.width
        if isinstance(brush, Brush):
            fill = brush.color
        elif isinstance(pen, Brush):
            fill = pen.color
        # handle transformation
        if self.transform:
            xy = ImagePath.Path(xy)
            xy.transform(self.transform)
        # render the item
        if op == "line":
            self.draw.line(xy, fill=outline, width=width)
        else:
            getattr(self.draw, op)(xy, fill=fill, outline=outline)

    def settransform(self, offset):
        """Sets a transformation offset."""
        (xoffset, yoffset) = offset
        self.transform = (1, 0, xoffset, 0, 1, yoffset)

    def arc(self, xy, start, end, *options):
        """

        Draws an arc (a portion of a circle outline) between the start and end

        angles, inside the given bounding box.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc`

        """
        self.render("arc", xy, start, end, *options)

    def chord(self, xy, start, end, *options):
        """

        Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points

        with a straight line.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord`

        """
        self.render("chord", xy, start, end, *options)

    def ellipse(self, xy, *options):
        """

        Draws an ellipse inside the given bounding box.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse`

        """
        self.render("ellipse", xy, *options)

    def line(self, xy, *options):
        """

        Draws a line between the coordinates in the ``xy`` list.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line`

        """
        self.render("line", xy, *options)

    def pieslice(self, xy, start, end, *options):
        """

        Same as arc, but also draws straight lines between the end points and the

        center of the bounding box.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice`

        """
        self.render("pieslice", xy, start, end, *options)

    def polygon(self, xy, *options):
        """

        Draws a polygon.



        The polygon outline consists of straight lines between the given

        coordinates, plus a straight line between the last and the first

        coordinate.





        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon`

        """
        self.render("polygon", xy, *options)

    def rectangle(self, xy, *options):
        """

        Draws a rectangle.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle`

        """
        self.render("rectangle", xy, *options)

    def text(self, xy, text, font):
        """

        Draws the string at the given position.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.text`

        """
        if self.transform:
            xy = ImagePath.Path(xy)
            xy.transform(self.transform)
        self.draw.text(xy, text, font=font.font, fill=font.color)

    def textbbox(self, xy, text, font):
        """

        Returns bounding box (in pixels) of given text.



        :return: ``(left, top, right, bottom)`` bounding box



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox`

        """
        if self.transform:
            xy = ImagePath.Path(xy)
            xy.transform(self.transform)
        return self.draw.textbbox(xy, text, font=font.font)

    def textlength(self, text, font):
        """

        Returns length (in pixels) of given text.

        This is the amount by which following text should be offset.



        .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength`

        """
        return self.draw.textlength(text, font=font.font)