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,35 @@
1
+ #!/usr/bin/env python3
2
+ """thorvg-python"""
3
+
4
+ __version__ = "1.0.1"
5
+
6
+ from .animation import Animation # type: ignore # noqa: F401
7
+ from .animation.lottie import LottieAnimation # type: ignore # noqa: F401
8
+ from .base import BlendMethod # type: ignore # noqa: F401
9
+ from .base import Colorspace # type: ignore # noqa: F401
10
+ from .base import ColorStop # type: ignore # noqa: F401
11
+ from .base import CompositeMethod # type: ignore # noqa: F401
12
+ from .base import EngineBackend # type: ignore # noqa: F401
13
+ from .base import FillRule # type: ignore # noqa: F401
14
+ from .base import Identifier # type: ignore # noqa: F401
15
+ from .base import Matrix # type: ignore # noqa: F401
16
+ from .base import MempoolPolicy # type: ignore # noqa: F401
17
+ from .base import PathCommand # type: ignore # noqa: F401
18
+ from .base import PointStruct # type: ignore # noqa: F401
19
+ from .base import Result # type: ignore # noqa: F401
20
+ from .base import StrokeCap # type: ignore # noqa: F401
21
+ from .base import StrokeFill # type: ignore # noqa: F401
22
+ from .base import StrokeJoin # type: ignore # noqa: F401
23
+ from .base import TvgType # type: ignore # noqa: F401
24
+ from .canvas import Canvas # type: ignore # noqa: F401
25
+ from .canvas.sw import SwCanvas # type: ignore # noqa: F401
26
+ from .engine import Engine # type: ignore # noqa: F401
27
+ from .gradient import Gradient # type: ignore # noqa: F401
28
+ from .gradient.linear import LinearGradient # type: ignore # noqa: F401
29
+ from .gradient.radial import RadialGradient # type: ignore # noqa: F401
30
+ from .paint import Paint # type: ignore # noqa: F401
31
+ from .paint.picture import Picture # type: ignore # noqa: F401
32
+ from .paint.scene import Scene # type: ignore # noqa: F401
33
+ from .paint.shape import Shape # type: ignore # noqa: F401
34
+ from .paint.text import Text # type: ignore # noqa: F401
35
+ from .saver import Saver # type: ignore # noqa: F401
@@ -0,0 +1,251 @@
1
+ #!/usr/bin/env python3
2
+ import ctypes
3
+ from typing import Optional, Tuple
4
+
5
+ from ..base import AnimationStruct, PaintStruct, Result
6
+ from ..engine import Engine
7
+ from ..paint.picture import Picture
8
+
9
+
10
+ class Animation:
11
+ """
12
+ Animation API
13
+
14
+ A module for manipulation of animatable images.
15
+
16
+ The module supports the display and control of animation frames.
17
+ """
18
+
19
+ def __init__(self, engine: Engine, animation: Optional[AnimationStruct] = None):
20
+ self.engine = engine
21
+ self.thorvg_lib = engine.thorvg_lib
22
+ if animation is None:
23
+ self._animation = self._new()
24
+ else:
25
+ self._animation = animation
26
+
27
+ def _new(self) -> AnimationStruct:
28
+ """Creates a new Animation object.
29
+
30
+ Note that you need not call this method as it is auto called when initializing ``Animation()``.
31
+
32
+ :return: AnimationStruct A new AnimationStruct object.
33
+ :rtype: AnimationStruct
34
+
35
+ .. versionadded:: 0.13
36
+ """
37
+ self.thorvg_lib.tvg_animation_new.restype = ctypes.POINTER(AnimationStruct)
38
+ return self.thorvg_lib.tvg_animation_new().contents
39
+
40
+ def set_frame(
41
+ self,
42
+ no: float,
43
+ ) -> Result:
44
+ """Specifies the current frame in the animation.
45
+
46
+ :param float no: The index of the animation frame to be displayed. The index should be less than the Animation.get_total_frame().
47
+
48
+ :return:
49
+ - INVALID_ARGUMENT An invalid AnimationStruct pointer.
50
+ - INSUFFICIENT_CONDITION if the given ``no`` is the same as the current frame value.
51
+ - NOT_SUPPORTED The picture data does not support animations.
52
+ :rtype: Result
53
+
54
+ .. note::
55
+ For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value
56
+ is less than 0.001. In such cases, it returns ``Result::InsufficientCondition``.
57
+ Values less than 0.001 may be disregarded and may not be accurately retained by the Animation.
58
+
59
+ .. seealso:: Animation.get_total_frame()
60
+
61
+ .. versionadded:: 0.13
62
+ """
63
+ self.thorvg_lib.tvg_animation_set_frame.argtypes = [
64
+ ctypes.POINTER(AnimationStruct),
65
+ ctypes.c_float,
66
+ ]
67
+ self.thorvg_lib.tvg_animation_set_frame.restype = Result
68
+ return self.thorvg_lib.tvg_animation_set_frame(
69
+ ctypes.pointer(self._animation),
70
+ ctypes.c_float(no),
71
+ )
72
+
73
+ def get_picture(self) -> Picture:
74
+ """Retrieves a picture instance associated with this animation instance.
75
+
76
+ This function provides access to the picture instance that can be used to load animation formats, such as Lottie(json).
77
+ After setting up the picture, it can be pushed to the designated canvas, enabling control over animation frames
78
+ with this Animation instance.
79
+
80
+ :return: A picture instance that is tied to this animation.
81
+ :rtype: Picture
82
+
83
+ .. warning::
84
+ The picture instance is owned by Animation. It should not be deleted manually.
85
+
86
+ .. versionadded:: 0.13
87
+ """
88
+ self.thorvg_lib.tvg_animation_get_picture.argtypes = [
89
+ ctypes.POINTER(AnimationStruct),
90
+ ]
91
+ self.thorvg_lib.tvg_animation_get_picture.restype = ctypes.POINTER(PaintStruct)
92
+ return Picture(
93
+ self.engine,
94
+ self.thorvg_lib.tvg_animation_get_picture(
95
+ ctypes.pointer(self._animation),
96
+ ).contents,
97
+ )
98
+
99
+ def get_frame(self) -> Tuple[Result, float]:
100
+ """Retrieves the current frame number of the animation.
101
+
102
+ :return: INVALID_ARGUMENT An invalid AnimationStruct pointer or ``no``
103
+ :rtype: Result
104
+ :return: The current frame number of the animation, between 0 and totalFrame() - 1.
105
+ :rtype: float
106
+
107
+ .. seealso:: Animation.get_total_frame()
108
+ .. seealso:: Animation.set_frame()
109
+
110
+ .. versionadded:: 0.13
111
+ """
112
+ no = ctypes.c_float()
113
+ self.thorvg_lib.tvg_animation_get_frame.argtypes = [
114
+ ctypes.POINTER(AnimationStruct),
115
+ ctypes.POINTER(ctypes.c_float),
116
+ ]
117
+ self.thorvg_lib.tvg_animation_get_frame.restype = Result
118
+ result = self.thorvg_lib.tvg_animation_get_frame(
119
+ ctypes.pointer(self._animation),
120
+ ctypes.pointer(no),
121
+ )
122
+ return result, no.value
123
+
124
+ def get_total_frame(self) -> Tuple[Result, float]:
125
+ """Retrieves the total number of frames in the animation.
126
+
127
+ :return: INVALID_ARGUMENT An invalid AnimationStruct pointer or ``cnt``.
128
+ :rtype: Result
129
+ :return: The total number of frames in the animation.
130
+ :rtype: float
131
+
132
+ .. note::
133
+ Frame numbering starts from 0.
134
+ .. note::
135
+ If the Picture is not properly configured, this function will return 0.
136
+
137
+ .. versionadded:: 0.13
138
+ """
139
+ cnt = ctypes.c_float()
140
+ self.thorvg_lib.tvg_animation_get_total_frame.argtypes = [
141
+ ctypes.POINTER(AnimationStruct),
142
+ ctypes.POINTER(ctypes.c_float),
143
+ ]
144
+ self.thorvg_lib.tvg_animation_get_total_frame.restype = Result
145
+ result = self.thorvg_lib.tvg_animation_get_total_frame(
146
+ ctypes.pointer(self._animation),
147
+ ctypes.pointer(cnt),
148
+ )
149
+ return result, cnt.value
150
+
151
+ def get_duration(self) -> Tuple[Result, float]:
152
+ """Retrieves the duration of the animation in seconds.
153
+
154
+ :return: INVALID_ARGUMENT An invalid AnimationStruct pointer or ``duration``.
155
+ :rtype: Result
156
+ :return: The duration of the animation in seconds.
157
+ :rtype: float
158
+
159
+ .. note::
160
+ If the Picture is not properly configured, this function will return 0.
161
+
162
+ .. versionadded:: 0.13
163
+ """
164
+ duration = ctypes.c_float()
165
+ self.thorvg_lib.tvg_animation_get_duration.argtypes = [
166
+ ctypes.POINTER(AnimationStruct),
167
+ ctypes.POINTER(ctypes.c_float),
168
+ ]
169
+ self.thorvg_lib.tvg_animation_get_duration.restype = Result
170
+ result = self.thorvg_lib.tvg_animation_get_duration(
171
+ ctypes.pointer(self._animation),
172
+ ctypes.pointer(duration),
173
+ )
174
+ return result, duration.value
175
+
176
+ def set_segment(
177
+ self,
178
+ begin: float,
179
+ end: float,
180
+ ) -> Result:
181
+ """Specifies the playback segment of the animation.
182
+
183
+ :param float begin: segment begin.
184
+ :param float end: segment end.
185
+
186
+ :return:
187
+ - INSUFFICIENT_CONDITION In case the animation is not loaded.
188
+ - INVALID_ARGUMENT When the given parameters are out of range.
189
+ :rtype: Result
190
+
191
+ .. note::
192
+ Experimental API
193
+ """
194
+ self.thorvg_lib.tvg_animation_set_segment.argtypes = [
195
+ ctypes.POINTER(AnimationStruct),
196
+ ctypes.c_float,
197
+ ctypes.c_float,
198
+ ]
199
+ self.thorvg_lib.tvg_animation_set_segment.restype = Result
200
+ result = self.thorvg_lib.tvg_animation_set_segment(
201
+ ctypes.pointer(self._animation),
202
+ ctypes.c_float(begin),
203
+ ctypes.c_float(end),
204
+ )
205
+ return result
206
+
207
+ def get_segment(self) -> Tuple[Result, float, float]:
208
+ """Gets the current segment.
209
+
210
+ :return:
211
+ - INSUFFICIENT_CONDITION In case the animation is not loaded.
212
+ - INVALID_ARGUMENT When the given parameters are ``nullptr``.
213
+ :rtype: Result
214
+ :return: segment begin.
215
+ :rtype: float
216
+ :return: segment end.
217
+ :rtype: float
218
+
219
+ .. note::
220
+ Experimental API
221
+ """
222
+ begin = ctypes.c_float()
223
+ end = ctypes.c_float()
224
+ self.thorvg_lib.tvg_animation_get_segment.argtypes = [
225
+ ctypes.POINTER(AnimationStruct),
226
+ ctypes.POINTER(ctypes.c_float),
227
+ ctypes.POINTER(ctypes.c_float),
228
+ ]
229
+ self.thorvg_lib.tvg_animation_get_segment.restype = Result
230
+ result = self.thorvg_lib.tvg_animation_get_segment(
231
+ ctypes.pointer(self._animation),
232
+ ctypes.pointer(begin),
233
+ ctypes.pointer(end),
234
+ )
235
+ return result, begin.value, end.value
236
+
237
+ def _del(self) -> Result:
238
+ """Deletes the given AnimationStruct object.
239
+
240
+ :return: INVALID_ARGUMENT An invalid AnimationStruct pointer.
241
+ :rtype: Result
242
+
243
+ .. versionadded:: 0.13
244
+ """
245
+ self.thorvg_lib.tvg_animation_del.argtypes = [
246
+ ctypes.POINTER(AnimationStruct),
247
+ ]
248
+ self.thorvg_lib.tvg_animation_del.restype = Result
249
+ return self.thorvg_lib.tvg_animation_del(
250
+ ctypes.pointer(self._animation),
251
+ )
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env python3
2
+ import ctypes
3
+ from typing import Optional, Tuple
4
+
5
+ from ..base import AnimationStruct, Result
6
+ from ..engine import Engine
7
+ from . import Animation
8
+
9
+
10
+ class LottieAnimation(Animation):
11
+ """
12
+ LottieAnimation Extension API
13
+
14
+ A module for manipulation of the scene tree
15
+
16
+ This module helps to control the scene tree.
17
+ """
18
+
19
+ def __init__(self, engine: Engine, animation: Optional[AnimationStruct] = None):
20
+ self.engine = engine
21
+ self.thorvg_lib = engine.thorvg_lib
22
+ if animation is None:
23
+ self._animation = self._new()
24
+ else:
25
+ self._animation = animation
26
+
27
+ def new(self) -> AnimationStruct:
28
+ """Creates a new LottieAnimation object.
29
+
30
+ :return: AnimationStruct A new Tvg_LottieAnimation object.
31
+ :rtype: AnimationStruct
32
+
33
+ .. versionadded:: 0.15
34
+ """
35
+ self.thorvg_lib.tvg_lottie_animation_new.restype = ctypes.POINTER(
36
+ AnimationStruct
37
+ )
38
+ return self.thorvg_lib.tvg_lottie_animation_new().contents
39
+
40
+ def override(
41
+ self,
42
+ slot: Optional[str],
43
+ ) -> Result:
44
+ """Override the lottie properties through the slot data.
45
+
46
+ :param str slot: The Lottie slot data in json, or ``None`` to reset.
47
+
48
+ :return:
49
+ - INSUFFICIENT_CONDITION In case the animation is not loaded.
50
+ - INVALID_ARGUMENT When the given ``slot`` is invalid
51
+ - NOT_SUPPORTED The Lottie Animation is not supported.
52
+ :rtype: Result
53
+
54
+ .. note::
55
+ Experimental API
56
+ """
57
+ if slot is not None and slot != "":
58
+ slot_bytes = slot.encode() + b"\x00"
59
+ slot_arr_type = ctypes.c_char * len(slot_bytes)
60
+ slot_arr_type_ptr = ctypes.POINTER(slot_arr_type)
61
+ slot_arr_ptr = ctypes.pointer(slot_arr_type.from_buffer_copy(slot_bytes))
62
+ else:
63
+ slot_arr_type_ptr = ctypes.c_void_p # type: ignore
64
+ slot_arr_ptr = ctypes.c_void_p() # type: ignore
65
+ self.thorvg_lib.tvg_lottie_animation_override.argtypes = [
66
+ ctypes.POINTER(AnimationStruct),
67
+ slot_arr_type_ptr,
68
+ ]
69
+ self.thorvg_lib.tvg_lottie_animation_override.restype = Result
70
+ return self.thorvg_lib.tvg_lottie_animation_override(
71
+ ctypes.pointer(self._animation),
72
+ slot_arr_ptr,
73
+ )
74
+
75
+ def set_marker(
76
+ self,
77
+ marker: str,
78
+ ) -> Result:
79
+ """Specifies a segment by marker.
80
+
81
+ :param str marker: The name of the segment marker.
82
+
83
+ :return:
84
+ - INSUFFICIENT_CONDITION In case the animation is not loaded.
85
+ - INVALID_ARGUMENT When the given ``marker`` is invalid.
86
+ - NOT_SUPPORTED The Lottie Animation is not supported.
87
+ :rtype: Result
88
+
89
+ .. note::
90
+ Experimental API
91
+ """
92
+ marker_bytes = marker.encode() + b"\x00"
93
+ marker_arr_type = ctypes.c_char * len(marker_bytes)
94
+ marker_arr = marker_arr_type.from_buffer_copy(marker_bytes)
95
+ self.thorvg_lib.tvg_lottie_animation_set_marker.argtypes = [
96
+ ctypes.POINTER(AnimationStruct),
97
+ ctypes.POINTER(marker_arr_type),
98
+ ]
99
+ self.thorvg_lib.tvg_lottie_animation_set_marker.restype = Result
100
+ return self.thorvg_lib.tvg_lottie_animation_set_marker(
101
+ ctypes.pointer(self._animation),
102
+ ctypes.pointer(marker_arr),
103
+ )
104
+
105
+ def get_markers_cnt(
106
+ self,
107
+ ) -> Tuple[Result, int]:
108
+ """Gets the marker count of the animation.
109
+
110
+ :return: INVALID_ARGUMENT In case a ``nullptr`` is passed as the argument.
111
+ :rtype: Result
112
+ :return: The count value of the markers.
113
+ :rtype: int
114
+
115
+ .. note::
116
+ Experimental API
117
+ """
118
+ cnt = ctypes.c_uint32()
119
+ self.thorvg_lib.tvg_lottie_animation_get_markers_cnt.argtypes = [
120
+ ctypes.POINTER(AnimationStruct),
121
+ ctypes.POINTER(ctypes.c_uint32),
122
+ ]
123
+ self.thorvg_lib.tvg_lottie_animation_get_markers_cnt.restype = Result
124
+ result = self.thorvg_lib.tvg_lottie_animation_get_markers_cnt(
125
+ ctypes.pointer(self._animation),
126
+ ctypes.pointer(cnt),
127
+ )
128
+ return result, cnt.value
129
+
130
+ def get_marker(
131
+ self,
132
+ idx: int,
133
+ ) -> Tuple[Result, Optional[str]]:
134
+ """Gets the marker name by a given index.
135
+
136
+ :param int idx: The index of the animation marker, starts from 0.
137
+
138
+ :return: INVALID_ARGUMENT In case ``nullptr`` is passed as the argument or ``idx`` is out of range.
139
+ :rtyle: Result
140
+ :return: The name of marker when succeed.
141
+ :rtype: Optional[str]
142
+
143
+ .. note::
144
+ Experimental API
145
+ """
146
+ name = ctypes.c_char_p()
147
+ self.thorvg_lib.tvg_lottie_animation_get_marker.argtypes = [
148
+ ctypes.POINTER(AnimationStruct),
149
+ ctypes.POINTER(ctypes.c_uint32),
150
+ ctypes.POINTER(ctypes.c_char_p),
151
+ ]
152
+ self.thorvg_lib.tvg_lottie_animation_get_marker.restype = Result
153
+ result = self.thorvg_lib.tvg_lottie_animation_get_marker(
154
+ ctypes.pointer(self._animation),
155
+ ctypes.c_uint32(idx),
156
+ ctypes.pointer(name),
157
+ )
158
+ if name.value is not None:
159
+ _name = name.value.decode("utf-8")
160
+ else:
161
+ _name = None
162
+ return result, _name