svglab 0.1.0__tar.gz

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.
Files changed (39) hide show
  1. svglab-0.1.0/LICENSE +7 -0
  2. svglab-0.1.0/PKG-INFO +302 -0
  3. svglab-0.1.0/README.md +267 -0
  4. svglab-0.1.0/pyproject.toml +157 -0
  5. svglab-0.1.0/svglab/__init__.py +201 -0
  6. svglab-0.1.0/svglab/__main__.py +102 -0
  7. svglab-0.1.0/svglab/attrparse/__init__.py +65 -0
  8. svglab-0.1.0/svglab/attrparse/angle.py +45 -0
  9. svglab-0.1.0/svglab/attrparse/color.py +45 -0
  10. svglab-0.1.0/svglab/attrparse/d.py +487 -0
  11. svglab-0.1.0/svglab/attrparse/grammars/angle.lark +12 -0
  12. svglab-0.1.0/svglab/attrparse/grammars/length.lark +12 -0
  13. svglab-0.1.0/svglab/attrparse/grammars/point.lark +10 -0
  14. svglab-0.1.0/svglab/attrparse/grammars/points.lark +11 -0
  15. svglab-0.1.0/svglab/attrparse/grammars/transform.lark +25 -0
  16. svglab-0.1.0/svglab/attrparse/length.py +53 -0
  17. svglab-0.1.0/svglab/attrparse/names.py +335 -0
  18. svglab-0.1.0/svglab/attrparse/point.py +95 -0
  19. svglab-0.1.0/svglab/attrparse/points.py +24 -0
  20. svglab-0.1.0/svglab/attrparse/transform.py +163 -0
  21. svglab-0.1.0/svglab/attrparse/utils.py +65 -0
  22. svglab-0.1.0/svglab/attrs/__init__.py +0 -0
  23. svglab-0.1.0/svglab/attrs/common.py +76 -0
  24. svglab-0.1.0/svglab/attrs/groups.py +174 -0
  25. svglab-0.1.0/svglab/attrs/presentation.py +388 -0
  26. svglab-0.1.0/svglab/attrs/regular.py +1012 -0
  27. svglab-0.1.0/svglab/attrs/types.py +111 -0
  28. svglab-0.1.0/svglab/elements/__init__.py +194 -0
  29. svglab-0.1.0/svglab/elements/common.py +496 -0
  30. svglab-0.1.0/svglab/elements/names.py +125 -0
  31. svglab-0.1.0/svglab/elements/tags.py +1148 -0
  32. svglab-0.1.0/svglab/elements/text_elements.py +68 -0
  33. svglab-0.1.0/svglab/elements/traits.py +70 -0
  34. svglab-0.1.0/svglab/errors.py +6 -0
  35. svglab-0.1.0/svglab/models.py +160 -0
  36. svglab-0.1.0/svglab/parse.py +177 -0
  37. svglab-0.1.0/svglab/py.typed +0 -0
  38. svglab-0.1.0/svglab/serialize.py +416 -0
  39. svglab-0.1.0/svglab/utils.py +216 -0
svglab-0.1.0/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 Tomáš Režňák
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
svglab-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,302 @@
1
+ Metadata-Version: 2.1
2
+ Name: svglab
3
+ Version: 0.1.0
4
+ Summary: A manipulation and optimization library for Scalable Vector Graphics
5
+ License: MIT
6
+ Author: Tomáš Režňák
7
+ Author-email: tomas.reznak@volny.cz
8
+ Requires-Python: >=3.10,<4.0
9
+ Classifier: Framework :: Pydantic :: 2
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Multimedia :: Graphics
19
+ Classifier: Topic :: Multimedia :: Graphics :: Editors :: Vector-Based
20
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
21
+ Classifier: Topic :: Text Processing :: Markup :: XML
22
+ Classifier: Typing :: Typed
23
+ Requires-Dist: beautifulsoup4 (>=4.12.3,<5.0.0)
24
+ Requires-Dist: bidict (>=0.23.1,<0.24.0)
25
+ Requires-Dist: html5lib (>=1.1,<2.0)
26
+ Requires-Dist: lark (>=1.2.2,<2.0.0)
27
+ Requires-Dist: lxml (>=5.3.0,<6.0.0)
28
+ Requires-Dist: pydantic (>=2.10.4,<3.0.0)
29
+ Requires-Dist: pydantic-extra-types (>=2.10.1,<3.0.0)
30
+ Requires-Dist: pyright (>=1.1.391,<2.0.0)
31
+ Requires-Dist: readable-number (>=0.1.3,<0.2.0)
32
+ Requires-Dist: svgpathtools (>=1.6.1,<2.0.0)
33
+ Description-Content-Type: text/markdown
34
+
35
+ <a id="readme-top"></a>
36
+
37
+ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/reznakt/svglab/check.yml)
38
+ ![GitHub License](https://img.shields.io/github/license/reznakt/svglab)
39
+ ![Python version](https://img.shields.io/badge/python-3.10%20|%203.11%20|%203.12%20|%203.13-blue)
40
+ ![Maintenance](https://img.shields.io/maintenance/yes/2025)
41
+ ![GitHub repo size](https://img.shields.io/github/repo-size/reznakt/svglab)
42
+ ![GitHub Repo stars](https://img.shields.io/github/stars/reznakt/svglab?style=flat)
43
+
44
+ <br />
45
+ <div align="center">
46
+ <img width="150" height="150" src="https://www.reshot.com/download/icons/HSMU75PJ3C/reshot-icon-flask-HSMU75PJ3C.svg" />
47
+
48
+ <h3 align="center">svglab</h3>
49
+
50
+ <p align="center">
51
+ <em>A manipulation and optimization library for Scalable Vector Graphics</em>
52
+ </p>
53
+ </div>
54
+ <hr />
55
+
56
+ ### Table of Contents
57
+
58
+ <ul>
59
+ <li>
60
+ <a href="#about-the-project">About The Project</a>
61
+ </li>
62
+ <li>
63
+ <a href="#getting-started">Getting Started</a>
64
+ <ul>
65
+ <li><a href="#prerequisites">Prerequisites</a></li>
66
+ <li><a href="#installation">Installation</a></li>
67
+ </ul>
68
+ </li>
69
+ <li><a href="#usage">Usage</a></li>
70
+ <li>
71
+ <a href="#development">Development</a>
72
+ <ul>
73
+ <li><a href="#setup">Setup</a></li>
74
+ <li><a href="#common-tasks">Common tasks</a></li>
75
+ </ul>
76
+ </li>
77
+ <li><a href="#license">License</a></li>
78
+ </ul>
79
+
80
+ ## About The Project
81
+
82
+ ### Features
83
+
84
+ - SVG parsing, manipulation, and writing
85
+ - Support for all SVG 1.1 elements and attributes
86
+ - Support for special XML elements (`CDATA`, comments, text)
87
+ - Attributes are parsed into native Python types for easy manipulation
88
+ - Highly configurable formatting options:
89
+ - indentation level
90
+ - maximum precision for floating-point numbers
91
+ - color mode (`rgb`, `rgba`, `hsl`, `hex`, `named`)
92
+ - relative/absolute path commands
93
+ - scientific notation for small/large numbers
94
+ - and many more...
95
+ - Strong type safety:
96
+ - one class per distinct SVG element
97
+ - typed attributes
98
+ - runtime validation thanks to [pydantic](https://pypi.org/project/pydantic/)
99
+ - Support for all [beautifulsoup4](https://pypi.org/project/beautifulsoup4/) parsers (e.g., `html.parser`, `lxml`, `html5lib`)
100
+
101
+ ```mermaid
102
+ ---
103
+ title: Element hierarchy
104
+ ---
105
+
106
+ graph TD
107
+ Element:::abc --> TextElement
108
+ Element --> Tag
109
+
110
+ Tag:::abc --> PairedTag
111
+
112
+ PairedTag:::abc --> G
113
+ PairedTag --> Svg
114
+ PairedTag --> etc1[...]
115
+
116
+ Tag --> Rect
117
+ Tag --> Circle
118
+ Tag --> etc2[...]
119
+
120
+ TextElement:::abc --> Text
121
+ TextElement --> Comment
122
+ TextElement --> CData
123
+
124
+ etc1:::etc
125
+ etc2:::etc
126
+
127
+ classDef abc stroke:white,stroke-width:2px;
128
+ classDef etc stroke:gray,stroke-width:2px;
129
+ classDef default stroke:orange,stroke-width:2px;
130
+ ```
131
+
132
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
133
+
134
+ ## Getting Started
135
+
136
+ ### Prerequisites
137
+
138
+ - [CPython](https://www.python.org/) $\geq$ 3.10
139
+ - [Poetry](https://python-poetry.org/) (development only)
140
+
141
+ ### Installation
142
+
143
+ **From PyPi**:
144
+ ```sh
145
+ pip install svglab
146
+ ```
147
+ **From source**:
148
+ ```sh
149
+ # Via HTTPS
150
+ pip install git+https://github.com/reznakt/svglab.git
151
+
152
+ # Via SSH
153
+ pip install git+ssh://git@github.com/reznakt/svglab.git
154
+ ```
155
+
156
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
157
+
158
+ ## Usage
159
+
160
+ ```python
161
+ from svglab import (
162
+ CData,
163
+ Comment,
164
+ G,
165
+ Path,
166
+ Polyline,
167
+ RawText,
168
+ Rect,
169
+ parse_svg,
170
+ )
171
+ from svglab.attrparse import Color, D, Length, Point, SkewX, Translate
172
+ from svglab.serialize import Formatter, set_formatter
173
+
174
+
175
+ # Configure custom formatting options
176
+ set_formatter(Formatter(indent=4, max_precision=2, color_mode="rgb"))
177
+
178
+
179
+ # Parse an existing SVG file
180
+ svg = parse_svg(
181
+ """
182
+ <svg xmlns="http://www.w3.org/2000/svg">
183
+ <g>
184
+ <rect
185
+ id="background"
186
+ width="100cm"
187
+ height="100%"
188
+ transform="rotate(45)"
189
+ stroke="red"
190
+ />
191
+ <rect color="hsl(0, 100%, 100%)"/>
192
+ <!-- This is a comment -->
193
+ <![CDATA[.background { fill: blue; }]]>
194
+ Hello SVG!
195
+ <path d="M 10,10 L 100,100 Q 100,100 50,50 Z"/>
196
+ <polygon points="0,0 100,0 100,100 0,100"/>
197
+ </g>
198
+ </svg>
199
+ """
200
+ )
201
+
202
+ print(svg)
203
+
204
+ # Create an element programmatically
205
+ group = G().add_children(
206
+ Rect(
207
+ width=Length(15, "px"),
208
+ height=Length(20),
209
+ transform=[SkewX(45.123), Translate(10, 20)],
210
+ color=Color("#ff0000"),
211
+ ),
212
+ Comment("This is a comment"),
213
+ CData(".background { fill: blue; }"),
214
+ RawText("Hello SVG!"),
215
+ Path(
216
+ d=D()
217
+ .move_to(Point(10, 10))
218
+ .line_to(Point(100, 100))
219
+ .quadratic_bezier_to(Point(100, 100), Point(50, 50))
220
+ .move_to(Point(50, 50))
221
+ .cubic_bezier_to(
222
+ Point(100, 100), Point(100, 100), Point(10, 10)
223
+ )
224
+ .arc_to(
225
+ Point(50, 50), 90, Point(100, 100), large=True, sweep=False
226
+ )
227
+ .close()
228
+ ),
229
+ Polyline(
230
+ points=[
231
+ Point(0, 0),
232
+ Point(100, 0),
233
+ Point(100, 100),
234
+ Point(0, 100),
235
+ ],
236
+ stroke_linecap="square",
237
+ opacity=0.5,
238
+ ),
239
+ )
240
+
241
+ # Add the element to the SVG
242
+ svg.add_child(group)
243
+
244
+ # Manipulate attributes
245
+ print(svg.xmlns) # http://www.w3.org/2000/svg
246
+ svg.xmlns = "http://example.com"
247
+
248
+ # Save to a file
249
+ svg.save("output.svg")
250
+
251
+ # Search the element tree
252
+ print(svg.find(G).find(Rect).width)
253
+ print(*svg.find_all(Rect), sep="\n")
254
+ ```
255
+
256
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
257
+
258
+ ## Development
259
+
260
+ ### Setup
261
+
262
+ ```sh
263
+ # Install dependencies
264
+ poetry install
265
+
266
+ # Activate the virtual environment
267
+ poetry shell
268
+
269
+ # Optional: Install pre-commit hooks
270
+ pre-commit install
271
+ ```
272
+
273
+ ### Common tasks
274
+
275
+ ```sh
276
+ # Run tests
277
+ poe test
278
+
279
+ # Run type checker
280
+ poe typecheck
281
+
282
+ # Run linter
283
+ poe lint
284
+
285
+ # Fix linting errors
286
+ poe lint-fix
287
+
288
+ # Run formatter
289
+ poe format
290
+
291
+ # Fix formatting errors
292
+ poe format-fix
293
+ ```
294
+
295
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
296
+
297
+ ## License
298
+
299
+ This software is distributed under the MIT License. See [`LICENSE`](LICENSE) for more information.
300
+
301
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
302
+
svglab-0.1.0/README.md ADDED
@@ -0,0 +1,267 @@
1
+ <a id="readme-top"></a>
2
+
3
+ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/reznakt/svglab/check.yml)
4
+ ![GitHub License](https://img.shields.io/github/license/reznakt/svglab)
5
+ ![Python version](https://img.shields.io/badge/python-3.10%20|%203.11%20|%203.12%20|%203.13-blue)
6
+ ![Maintenance](https://img.shields.io/maintenance/yes/2025)
7
+ ![GitHub repo size](https://img.shields.io/github/repo-size/reznakt/svglab)
8
+ ![GitHub Repo stars](https://img.shields.io/github/stars/reznakt/svglab?style=flat)
9
+
10
+ <br />
11
+ <div align="center">
12
+ <img width="150" height="150" src="https://www.reshot.com/download/icons/HSMU75PJ3C/reshot-icon-flask-HSMU75PJ3C.svg" />
13
+
14
+ <h3 align="center">svglab</h3>
15
+
16
+ <p align="center">
17
+ <em>A manipulation and optimization library for Scalable Vector Graphics</em>
18
+ </p>
19
+ </div>
20
+ <hr />
21
+
22
+ ### Table of Contents
23
+
24
+ <ul>
25
+ <li>
26
+ <a href="#about-the-project">About The Project</a>
27
+ </li>
28
+ <li>
29
+ <a href="#getting-started">Getting Started</a>
30
+ <ul>
31
+ <li><a href="#prerequisites">Prerequisites</a></li>
32
+ <li><a href="#installation">Installation</a></li>
33
+ </ul>
34
+ </li>
35
+ <li><a href="#usage">Usage</a></li>
36
+ <li>
37
+ <a href="#development">Development</a>
38
+ <ul>
39
+ <li><a href="#setup">Setup</a></li>
40
+ <li><a href="#common-tasks">Common tasks</a></li>
41
+ </ul>
42
+ </li>
43
+ <li><a href="#license">License</a></li>
44
+ </ul>
45
+
46
+ ## About The Project
47
+
48
+ ### Features
49
+
50
+ - SVG parsing, manipulation, and writing
51
+ - Support for all SVG 1.1 elements and attributes
52
+ - Support for special XML elements (`CDATA`, comments, text)
53
+ - Attributes are parsed into native Python types for easy manipulation
54
+ - Highly configurable formatting options:
55
+ - indentation level
56
+ - maximum precision for floating-point numbers
57
+ - color mode (`rgb`, `rgba`, `hsl`, `hex`, `named`)
58
+ - relative/absolute path commands
59
+ - scientific notation for small/large numbers
60
+ - and many more...
61
+ - Strong type safety:
62
+ - one class per distinct SVG element
63
+ - typed attributes
64
+ - runtime validation thanks to [pydantic](https://pypi.org/project/pydantic/)
65
+ - Support for all [beautifulsoup4](https://pypi.org/project/beautifulsoup4/) parsers (e.g., `html.parser`, `lxml`, `html5lib`)
66
+
67
+ ```mermaid
68
+ ---
69
+ title: Element hierarchy
70
+ ---
71
+
72
+ graph TD
73
+ Element:::abc --> TextElement
74
+ Element --> Tag
75
+
76
+ Tag:::abc --> PairedTag
77
+
78
+ PairedTag:::abc --> G
79
+ PairedTag --> Svg
80
+ PairedTag --> etc1[...]
81
+
82
+ Tag --> Rect
83
+ Tag --> Circle
84
+ Tag --> etc2[...]
85
+
86
+ TextElement:::abc --> Text
87
+ TextElement --> Comment
88
+ TextElement --> CData
89
+
90
+ etc1:::etc
91
+ etc2:::etc
92
+
93
+ classDef abc stroke:white,stroke-width:2px;
94
+ classDef etc stroke:gray,stroke-width:2px;
95
+ classDef default stroke:orange,stroke-width:2px;
96
+ ```
97
+
98
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
99
+
100
+ ## Getting Started
101
+
102
+ ### Prerequisites
103
+
104
+ - [CPython](https://www.python.org/) $\geq$ 3.10
105
+ - [Poetry](https://python-poetry.org/) (development only)
106
+
107
+ ### Installation
108
+
109
+ **From PyPi**:
110
+ ```sh
111
+ pip install svglab
112
+ ```
113
+ **From source**:
114
+ ```sh
115
+ # Via HTTPS
116
+ pip install git+https://github.com/reznakt/svglab.git
117
+
118
+ # Via SSH
119
+ pip install git+ssh://git@github.com/reznakt/svglab.git
120
+ ```
121
+
122
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
123
+
124
+ ## Usage
125
+
126
+ ```python
127
+ from svglab import (
128
+ CData,
129
+ Comment,
130
+ G,
131
+ Path,
132
+ Polyline,
133
+ RawText,
134
+ Rect,
135
+ parse_svg,
136
+ )
137
+ from svglab.attrparse import Color, D, Length, Point, SkewX, Translate
138
+ from svglab.serialize import Formatter, set_formatter
139
+
140
+
141
+ # Configure custom formatting options
142
+ set_formatter(Formatter(indent=4, max_precision=2, color_mode="rgb"))
143
+
144
+
145
+ # Parse an existing SVG file
146
+ svg = parse_svg(
147
+ """
148
+ <svg xmlns="http://www.w3.org/2000/svg">
149
+ <g>
150
+ <rect
151
+ id="background"
152
+ width="100cm"
153
+ height="100%"
154
+ transform="rotate(45)"
155
+ stroke="red"
156
+ />
157
+ <rect color="hsl(0, 100%, 100%)"/>
158
+ <!-- This is a comment -->
159
+ <![CDATA[.background { fill: blue; }]]>
160
+ Hello SVG!
161
+ <path d="M 10,10 L 100,100 Q 100,100 50,50 Z"/>
162
+ <polygon points="0,0 100,0 100,100 0,100"/>
163
+ </g>
164
+ </svg>
165
+ """
166
+ )
167
+
168
+ print(svg)
169
+
170
+ # Create an element programmatically
171
+ group = G().add_children(
172
+ Rect(
173
+ width=Length(15, "px"),
174
+ height=Length(20),
175
+ transform=[SkewX(45.123), Translate(10, 20)],
176
+ color=Color("#ff0000"),
177
+ ),
178
+ Comment("This is a comment"),
179
+ CData(".background { fill: blue; }"),
180
+ RawText("Hello SVG!"),
181
+ Path(
182
+ d=D()
183
+ .move_to(Point(10, 10))
184
+ .line_to(Point(100, 100))
185
+ .quadratic_bezier_to(Point(100, 100), Point(50, 50))
186
+ .move_to(Point(50, 50))
187
+ .cubic_bezier_to(
188
+ Point(100, 100), Point(100, 100), Point(10, 10)
189
+ )
190
+ .arc_to(
191
+ Point(50, 50), 90, Point(100, 100), large=True, sweep=False
192
+ )
193
+ .close()
194
+ ),
195
+ Polyline(
196
+ points=[
197
+ Point(0, 0),
198
+ Point(100, 0),
199
+ Point(100, 100),
200
+ Point(0, 100),
201
+ ],
202
+ stroke_linecap="square",
203
+ opacity=0.5,
204
+ ),
205
+ )
206
+
207
+ # Add the element to the SVG
208
+ svg.add_child(group)
209
+
210
+ # Manipulate attributes
211
+ print(svg.xmlns) # http://www.w3.org/2000/svg
212
+ svg.xmlns = "http://example.com"
213
+
214
+ # Save to a file
215
+ svg.save("output.svg")
216
+
217
+ # Search the element tree
218
+ print(svg.find(G).find(Rect).width)
219
+ print(*svg.find_all(Rect), sep="\n")
220
+ ```
221
+
222
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
223
+
224
+ ## Development
225
+
226
+ ### Setup
227
+
228
+ ```sh
229
+ # Install dependencies
230
+ poetry install
231
+
232
+ # Activate the virtual environment
233
+ poetry shell
234
+
235
+ # Optional: Install pre-commit hooks
236
+ pre-commit install
237
+ ```
238
+
239
+ ### Common tasks
240
+
241
+ ```sh
242
+ # Run tests
243
+ poe test
244
+
245
+ # Run type checker
246
+ poe typecheck
247
+
248
+ # Run linter
249
+ poe lint
250
+
251
+ # Fix linting errors
252
+ poe lint-fix
253
+
254
+ # Run formatter
255
+ poe format
256
+
257
+ # Fix formatting errors
258
+ poe format-fix
259
+ ```
260
+
261
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
262
+
263
+ ## License
264
+
265
+ This software is distributed under the MIT License. See [`LICENSE`](LICENSE) for more information.
266
+
267
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>