thorvg-python 1.0.1__py3-none-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/env python3
2
+ import ctypes
3
+ from typing import Optional, Sequence, Tuple, cast
4
+
5
+ from ..base import (
6
+ ColorStop,
7
+ GradientStruct,
8
+ Identifier,
9
+ Matrix,
10
+ Result,
11
+ StrokeFill,
12
+ TvgType,
13
+ )
14
+ from ..engine import Engine
15
+
16
+
17
+ class Gradient:
18
+ """
19
+ Common Gradient API
20
+
21
+ A module managing the gradient fill of objects.
22
+
23
+ The module enables to set and to get the gradient colors and their arrangement inside the gradient bounds,
24
+ to specify the gradient bounds and the gradient behavior in case the area defined by the gradient bounds
25
+ is smaller than the area to be filled.
26
+
27
+ This is base Gradient class. Please use LinearGradient or RadialGradient for creating
28
+ """
29
+
30
+ def __init__(self, engine: Engine, grad: GradientStruct):
31
+ self.engine = engine
32
+ self.thorvg_lib = engine.thorvg_lib
33
+ self._grad = grad
34
+
35
+ def set_color_stops(
36
+ self,
37
+ color_stop: Sequence[ColorStop],
38
+ ) -> Result:
39
+ """Sets the parameters of the colors of the gradient and their position.
40
+
41
+ :param Sequence[ColorStop] color_stop: An array of ColorStop data structure.
42
+
43
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer.
44
+ :rtype: Result
45
+ """
46
+ color_stop_arr_type = ColorStop * len(color_stop)
47
+ self.thorvg_lib.tvg_gradient_set_color_stops.argtypes = [
48
+ ctypes.POINTER(GradientStruct),
49
+ ctypes.POINTER(color_stop_arr_type),
50
+ ctypes.c_uint32,
51
+ ]
52
+ self.thorvg_lib.tvg_gradient_set_color_stops.restype = Result
53
+ return self.thorvg_lib.tvg_gradient_set_color_stops(
54
+ ctypes.pointer(self._grad),
55
+ ctypes.pointer(color_stop_arr_type(*color_stop)),
56
+ ctypes.c_uint32(len(color_stop)),
57
+ )
58
+
59
+ def get_color_stops(
60
+ self,
61
+ ) -> Tuple[Result, Sequence[ColorStop]]:
62
+ """Gets the parameters of the colors of the gradient, their position and number
63
+
64
+ The function does not allocate any memory.
65
+
66
+ :return: INVALID_ARGUMENT A ``nullptr`` passed as the argument.
67
+ :rtype: Result
68
+ :return: An array of ColorStop data structure.
69
+ :rtype: Sequence[ColorStop]
70
+ """
71
+ color_stop_ptr = ctypes.POINTER(ColorStop)()
72
+ cnt = ctypes.c_uint32()
73
+ self.thorvg_lib.tvg_gradient_get_color_stops.argtypes = [
74
+ ctypes.POINTER(GradientStruct),
75
+ ctypes.POINTER(ctypes.POINTER(ColorStop)),
76
+ ctypes.POINTER(ctypes.c_uint32),
77
+ ]
78
+ self.thorvg_lib.tvg_gradient_get_color_stops.restype = Result
79
+ result = self.thorvg_lib.tvg_gradient_get_color_stops(
80
+ ctypes.pointer(self._grad),
81
+ ctypes.pointer(color_stop_ptr),
82
+ ctypes.pointer(cnt),
83
+ )
84
+ color_stop_type = ColorStop * cnt.value
85
+ color_stop_arr = color_stop_type.from_address(
86
+ ctypes.addressof(color_stop_ptr.contents)
87
+ )
88
+ return result, [color_stop_arr[i] for i in range(cnt.value)]
89
+
90
+ def set_spread(
91
+ self,
92
+ spread: StrokeFill,
93
+ ) -> Result:
94
+ """Sets the StrokeFill value, which specifies how to fill the area outside the gradient bounds.
95
+
96
+ :param StrokeFill spread: The FillSpread value.
97
+
98
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer.
99
+ :rtype: Result
100
+ """
101
+ self.thorvg_lib.tvg_gradient_set_spread.argtypes = [
102
+ ctypes.POINTER(GradientStruct),
103
+ ctypes.c_int,
104
+ ]
105
+ self.thorvg_lib.tvg_gradient_set_spread.restype = Result
106
+ return self.thorvg_lib.tvg_gradient_set_spread(
107
+ ctypes.pointer(self._grad),
108
+ spread,
109
+ )
110
+
111
+ def get_spread(self) -> Tuple[Result, StrokeFill]:
112
+ """Gets the FillSpread value of the gradient object.
113
+
114
+ :return: INVALID_ARGUMENT A ``nullptr`` passed as the argument.
115
+ :rtype: Result
116
+ :return: The FillSpread value.
117
+ :rtype: StrokeFill
118
+ """
119
+ spread = ctypes.c_int()
120
+ self.thorvg_lib.tvg_gradient_get_spread.argtypes = [
121
+ ctypes.POINTER(GradientStruct),
122
+ ctypes.POINTER(ctypes.c_int),
123
+ ]
124
+ self.thorvg_lib.tvg_gradient_get_spread.restype = Result
125
+ result = self.thorvg_lib.tvg_gradient_get_spread(
126
+ ctypes.pointer(self._grad),
127
+ ctypes.pointer(spread),
128
+ )
129
+ return result, StrokeFill(spread.value)
130
+
131
+ def set_transform(self, m: Matrix) -> Result:
132
+ """Sets the matrix of the affine transformation for the gradient object.
133
+
134
+ The augmented matrix of the transformation is expected to be given.
135
+
136
+ :param Matrix m The 3x3 augmented matrix.
137
+
138
+ :return: INVALID_ARGUMENT A ``nullptr`` is passed as the argument.
139
+ :rtype: Result
140
+ """
141
+ self.thorvg_lib.tvg_gradient_set_transform.argtypes = [
142
+ ctypes.POINTER(GradientStruct),
143
+ ctypes.POINTER(Matrix),
144
+ ]
145
+ self.thorvg_lib.tvg_gradient_set_transform.restype = Result
146
+ return self.thorvg_lib.tvg_gradient_set_transform(
147
+ ctypes.pointer(self._grad),
148
+ ctypes.pointer(m),
149
+ )
150
+
151
+ def get_transform(self) -> Tuple[Result, Matrix]:
152
+ """Gets the matrix of the affine transformation of the gradient object.
153
+
154
+ In case no transformation was applied, the identity matrix is set.
155
+
156
+ :return: INVALID_ARGUMENT A ``nullptr`` is passed as the argument.
157
+ :rtype: Result
158
+ :return: The 3x3 augmented matrix.
159
+ :rtype: Matrix
160
+ """
161
+ m = Matrix()
162
+ self.thorvg_lib.tvg_gradient_get_transform.argtypes = [
163
+ ctypes.POINTER(GradientStruct),
164
+ ctypes.POINTER(Matrix),
165
+ ]
166
+ self.thorvg_lib.tvg_gradient_get_transform.restype = Result
167
+ result = self.thorvg_lib.tvg_gradient_get_transform(
168
+ ctypes.pointer(self._grad),
169
+ ctypes.pointer(m),
170
+ )
171
+ return result, m
172
+
173
+ def get_type(self) -> Tuple[Result, TvgType]:
174
+ """Gets the unique value of the gradient instance indicating the instance type.
175
+
176
+ :return: INVALID_ARGUMENT In case a ``nullptr`` is passed as the argument.
177
+ :rtype: Result
178
+ :return: The unique type of the gradient instance type.
179
+ :rtype: TvgType
180
+
181
+ .. note::
182
+ Experimental API
183
+ """
184
+ _type = ctypes.c_int()
185
+ self.thorvg_lib.tvg_gradient_get_type.argtypes = [
186
+ ctypes.POINTER(GradientStruct),
187
+ ctypes.POINTER(ctypes.c_int),
188
+ ]
189
+ self.thorvg_lib.tvg_gradient_get_type.restype = Result
190
+ result = self.thorvg_lib.tvg_gradient_get_type(
191
+ ctypes.pointer(self._grad),
192
+ ctypes.pointer(_type),
193
+ )
194
+ return result, TvgType(_type.value)
195
+
196
+ def get_identifier(self) -> Tuple[Result, Identifier]:
197
+ """
198
+ .. deprecated:: 0.15
199
+ .. seealso:: Gradient.get_type()
200
+ """
201
+ identifier = ctypes.c_int()
202
+ self.thorvg_lib.tvg_gradient_get_identifier.argtypes = [
203
+ ctypes.POINTER(GradientStruct),
204
+ ctypes.POINTER(ctypes.c_int),
205
+ ]
206
+ self.thorvg_lib.tvg_gradient_get_identifier.restype = Result
207
+ result = self.thorvg_lib.tvg_gradient_get_identifier(
208
+ ctypes.pointer(self._grad),
209
+ ctypes.pointer(identifier),
210
+ )
211
+ return result, Identifier(identifier.value)
212
+
213
+ def duplicate(self) -> Optional["Gradient"]:
214
+ """Duplicates the given GradientStruct object.
215
+
216
+ Creates a new object and sets its all properties as in the original object.
217
+
218
+ :return: A copied GradientStruct object if succeed, ``None`` otherwise.
219
+ :rtype: GradientStruct
220
+ """
221
+ self.thorvg_lib.tvg_gradient_duplicate.argtypes = [
222
+ ctypes.POINTER(GradientStruct)
223
+ ]
224
+ self.thorvg_lib.tvg_gradient_duplicate.restype = ctypes.POINTER(GradientStruct)
225
+ tvg_gradient = cast(
226
+ GradientStruct,
227
+ self.thorvg_lib.tvg_gradient_duplicate(ctypes.pointer(self._grad)).contents,
228
+ )
229
+ if type(self).__name__ == "LinearGradient":
230
+ from .linear import LinearGradient
231
+
232
+ return LinearGradient(self.engine, tvg_gradient)
233
+ elif type(self).__name__ == "RadialGradient":
234
+ from .radial import RadialGradient
235
+
236
+ return RadialGradient(self.engine, tvg_gradient)
237
+ else:
238
+ raise TypeError(f"Unknown Gradient class {type(self).__name__}")
239
+
240
+ def _del(self) -> Result:
241
+ """Deletes the given gradient object.
242
+
243
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer.
244
+ :rtype: Result
245
+ """
246
+ self.thorvg_lib.tvg_gradient_del.argtypes = [ctypes.POINTER(GradientStruct)]
247
+ self.thorvg_lib.tvg_gradient_del.restype = Result
248
+ return self.thorvg_lib.tvg_gradient_del(ctypes.pointer(self._grad))
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env python3
2
+ import ctypes
3
+ from typing import Optional, Tuple
4
+
5
+ from ..base import GradientStruct, Result
6
+ from ..engine import Engine
7
+ from . import Gradient
8
+
9
+
10
+ class LinearGradient(Gradient):
11
+ """
12
+ Linear Gradient API
13
+ """
14
+
15
+ def __init__(self, engine: Engine, grad: Optional[GradientStruct] = None):
16
+ self.engine = engine
17
+ self.thorvg_lib = engine.thorvg_lib
18
+ if grad is None:
19
+ self._grad = self._new()
20
+ else:
21
+ self._grad = grad
22
+
23
+ def _new(self) -> GradientStruct:
24
+ """Creates a new linear gradient object.
25
+
26
+ Note that you need not call this method as it is auto called when initializing ``LinearGradient()``.
27
+
28
+ .. code-block:: python
29
+
30
+ from thorvg_python import Engine, Shape, LinearGradient, ColorStop
31
+
32
+ engine = Engine()
33
+ shape = Shape(engine)
34
+ shape.append_rect(700, 700, 100, 100, 20, 20)
35
+ grad = LinearGradient(engine)
36
+ grad.set(700, 700, 800, 800)
37
+ color_stops = [
38
+ ColorStop(0.0, 0, 0, 0, 255),
39
+ ColorStop(1.0, 0, 255, 0, 255),
40
+ ]
41
+ grad.set_color_stops(color_stops, 2)
42
+ shape.set_linear_gradient(grad)
43
+
44
+ :return: A new linear gradient object.
45
+ :rtype: GradientStruct
46
+ """
47
+ self.thorvg_lib.tvg_linear_gradient_new.restype = ctypes.POINTER(GradientStruct)
48
+ return self.thorvg_lib.tvg_linear_gradient_new().contents
49
+
50
+ def set(
51
+ self,
52
+ x1: float,
53
+ y1: float,
54
+ x2: float,
55
+ y2: float,
56
+ ) -> Result:
57
+ """Sets the linear gradient bounds.
58
+
59
+ The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
60
+ the given points (``x1``, ``y1``) and (``x2``, ``y2``), respectively. Both lines are perpendicular to the line linking
61
+ (``x1``, ``y1``) and (``x2``, ``y2``).
62
+
63
+ :param float x1: The horizontal coordinate of the first point used to determine the gradient bounds.
64
+ :param float y1: The vertical coordinate of the first point used to determine the gradient bounds.
65
+ :param float x2: The horizontal coordinate of the second point used to determine the gradient bounds.
66
+ :param float y2: The vertical coordinate of the second point used to determine the gradient bounds.
67
+
68
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer.
69
+ :rtype: Result
70
+
71
+ .. note::
72
+ In case the first and the second points are equal, an object is filled with a single color using the last color specified in the tvg_gradient_set_color_stops().
73
+ .. seealso:: Gradient.set_color_stops()
74
+ """
75
+ self.thorvg_lib.tvg_linear_gradient_set.argtypes = [
76
+ ctypes.POINTER(GradientStruct),
77
+ ctypes.c_float,
78
+ ctypes.c_float,
79
+ ctypes.c_float,
80
+ ctypes.c_float,
81
+ ]
82
+ self.thorvg_lib.tvg_linear_gradient_set.restype = Result
83
+ return self.thorvg_lib.tvg_linear_gradient_set(
84
+ ctypes.pointer(self._grad),
85
+ ctypes.c_float(x1),
86
+ ctypes.c_float(y1),
87
+ ctypes.c_float(x2),
88
+ ctypes.c_float(y2),
89
+ )
90
+
91
+ def get(
92
+ self,
93
+ ) -> Tuple[Result, float, float, float, float]:
94
+ """Gets the linear gradient bounds.
95
+
96
+ The bounds of the linear gradient are defined as a surface constrained by two parallel lines crossing
97
+ the given points (``x1``, ``y1``) and (``x2``, ``y2``), respectively. Both lines are perpendicular to the line linking
98
+ (``x1``, ``y1``) and (``x2``, ``y2``).
99
+
100
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer.
101
+ :rtype: Result
102
+ :return: The horizontal coordinate of the first point used to determine the gradient bounds.
103
+ :rtype: float
104
+ :return: The vertical coordinate of the first point used to determine the gradient bounds.
105
+ :rtype: float
106
+ :return: The horizontal coordinate of the second point used to determine the gradient bounds.
107
+ :rtype: float
108
+ :return: The vertical coordinate of the second point used to determine the gradient bounds.
109
+ :rtype: float
110
+ """
111
+ x1 = ctypes.c_float()
112
+ y1 = ctypes.c_float()
113
+ x2 = ctypes.c_float()
114
+ y2 = ctypes.c_float()
115
+ self.thorvg_lib.tvg_linear_gradient_get.argtypes = [
116
+ ctypes.POINTER(GradientStruct),
117
+ ctypes.POINTER(ctypes.c_float),
118
+ ctypes.POINTER(ctypes.c_float),
119
+ ctypes.POINTER(ctypes.c_float),
120
+ ctypes.POINTER(ctypes.c_float),
121
+ ]
122
+ self.thorvg_lib.tvg_linear_gradient_get.restype = Result
123
+ result = self.thorvg_lib.tvg_linear_gradient_get(
124
+ ctypes.pointer(self._grad),
125
+ ctypes.pointer(x1),
126
+ ctypes.pointer(y1),
127
+ ctypes.pointer(x2),
128
+ ctypes.pointer(y2),
129
+ )
130
+ return result, x1.value, y1.value, x2.value, y2.value
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env python3
2
+ import ctypes
3
+ from typing import Optional, Tuple
4
+
5
+ from ..base import GradientStruct, Result
6
+ from ..engine import Engine
7
+ from . import Gradient
8
+
9
+
10
+ class RadialGradient(Gradient):
11
+ """
12
+ Radial Gradient API
13
+ """
14
+
15
+ def __init__(self, engine: Engine, grad: Optional[GradientStruct] = None):
16
+ self.engine = engine
17
+ self.thorvg_lib = engine.thorvg_lib
18
+ if grad is None:
19
+ self._grad = self._new()
20
+ else:
21
+ self._grad = grad
22
+
23
+ def _new(self) -> GradientStruct:
24
+ """Creates a new radial gradient object.
25
+
26
+ Note that you need not call this method as it is auto called when initializing ``RadialGradient()``.
27
+
28
+ .. code-block:: python
29
+
30
+ from thorvg_python import Engine, Shape, RadialGradient, Tvg_ColorStop
31
+
32
+ engine = Engine()
33
+ shape = Shape(engine)
34
+ shape.append_rect(700, 700, 100, 100, 20, 20)
35
+ grad = RadialGradient(engine)
36
+ grad.set(550, 550, 50)
37
+ color_stops =
38
+ [
39
+ ColorStop(0.0, 0, 0, 0, 255),
40
+ ColorStop(1.0, 0, 255, 0, 255),
41
+ ]
42
+ grad.set_color_stops(color_stops, 2)
43
+ shape.set_radial_gradient(grad)
44
+
45
+ :return: A new radial gradient object.
46
+ :rtype: GradientStruct
47
+ """
48
+ self.thorvg_lib.tvg_radial_gradient_new.restype = ctypes.POINTER(GradientStruct)
49
+ return self.thorvg_lib.tvg_radial_gradient_new().contents
50
+
51
+ def set(
52
+ self,
53
+ cx: float,
54
+ cy: float,
55
+ radius: float,
56
+ ) -> Result:
57
+ """Sets the radial gradient bounds.
58
+
59
+ The radial gradient bounds are defined as a circle centered in a given point (``cx``, ``cy``) of a given radius.
60
+
61
+ :param float cx: The horizontal coordinate of the center of the bounding circle.
62
+ :param float cy: The vertical coordinate of the center of the bounding circle.
63
+ :param float radius: The radius of the bounding circle.
64
+
65
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer or the ``radius`` value less than zero.
66
+ :rtype: Result
67
+
68
+ .. note::
69
+ In case the ``radius`` is zero, an object is filled with a single color using the last color specified in the specified in the tvg_gradient_set_color_stops().
70
+ .. seealso:: Gradient.set_color_stops()
71
+ """
72
+ self.thorvg_lib.tvg_radial_gradient_set.argtypes = [
73
+ ctypes.POINTER(GradientStruct),
74
+ ctypes.c_float,
75
+ ctypes.c_float,
76
+ ctypes.c_float,
77
+ ]
78
+ self.thorvg_lib.tvg_radial_gradient_set.restype = Result
79
+ return self.thorvg_lib.tvg_radial_gradient_set(
80
+ ctypes.pointer(self._grad),
81
+ ctypes.c_float(cx),
82
+ ctypes.c_float(cy),
83
+ ctypes.c_float(radius),
84
+ )
85
+
86
+ def get(self) -> Tuple[Result, float, float, float]:
87
+ """The function gets radial gradient center point ant radius
88
+
89
+ :return: INVALID_ARGUMENT An invalid GradientStruct pointer.
90
+ :rtype: Result
91
+ :return: The horizontal coordinate of the center of the bounding circle.
92
+ :rtype: float
93
+ :return: The vertical coordinate of the center of the bounding circle.
94
+ :rtype: float
95
+ :return: The radius of the bounding circle.
96
+ :rtype: float
97
+ """
98
+ cx = ctypes.c_float()
99
+ cy = ctypes.c_float()
100
+ radius = ctypes.c_float()
101
+ self.thorvg_lib.tvg_radial_gradient_get.argtypes = [
102
+ ctypes.POINTER(GradientStruct),
103
+ ctypes.POINTER(ctypes.c_float),
104
+ ctypes.POINTER(ctypes.c_float),
105
+ ctypes.POINTER(ctypes.c_float),
106
+ ]
107
+ self.thorvg_lib.tvg_radial_gradient_get.restype = Result
108
+ result = self.thorvg_lib.tvg_radial_gradient_get(
109
+ ctypes.pointer(self._grad),
110
+ ctypes.pointer(cx),
111
+ ctypes.pointer(cy),
112
+ ctypes.pointer(radius),
113
+ )
114
+ return result, cx.value, cy.value, radius.value