Compare commits
189 Commits
revert-60-
...
master
Author | SHA1 | Date |
---|---|---|
Andrew Port | fcb648b9bb | |
Andrew Port | ec546a71d4 | |
Andrew Port | bc930005c2 | |
Andrew Port | ae9b79e77a | |
Andrew Port | 289ee6ecb4 | |
Andrew Port | 592fe3a525 | |
Andrew Port | 81870e1f85 | |
Andrew Port | 6015a97090 | |
Andrew Port | 788b2b43a2 | |
Andrew Port | b282094b53 | |
Andrew Port | 229773ff9d | |
Kaspar Emanuel | a16a060c27 | |
Sebastian Kuzminsky | e94483510e | |
Sebastian Kuzminsky | 6abda09d1c | |
Andrew Port | 5c73056420 | |
Andrew Port | 4f5d8f3bf2 | |
Andrew Port | c4d98afc68 | |
Andrew Port | 9c69e45d6e | |
Andrew Port | dc2f6e90cc | |
Andrew Port | 96676b7697 | |
Andrew Port | 2a1cb735e9 | |
Andrew Port | 3eb21161cf | |
Andrew Port | 31b6f3dd90 | |
Andrew Port | d9515ea399 | |
Andrew Port | 944ccf5e89 | |
Tatarize | b6e5a623ea | |
Tatarize | 4c6abc5820 | |
Andrew Port | b8dfb6770a | |
Andrew Port | 0e17702d04 | |
Andrew Port | 8df19f1c12 | |
Andrew Port | 9ac7f62515 | |
Andrew Port | d8a6e5e509 | |
Andrew Port | 73c887a8a3 | |
Andrew Port | f7e074339d | |
Andrew Port | d9f5a2a781 | |
Andrew Port | 740e2bf991 | |
Andrew Port | 8cbe6f0f81 | |
Andrew Port | b82530aaac | |
Andrew Port | e8792f4d2d | |
Andrew Port | d3a66f0bbd | |
Andrew Port | 356d86df78 | |
Andrew Port | a989c9831d | |
FlyingSamson | 07f46d41f8 | |
FlyingSamson | 2fc016d48f | |
FlyingSamson | aacd5fa96d | |
FlyingSamson | db5200f460 | |
FlyingSamson | a473ee3f4c | |
FlyingSamson | 02a223c220 | |
FlyingSamson | 68e0d1f30d | |
FlyingSamson | a743e0293c | |
FlyingSamson | 1771fbfb06 | |
FlyingSamson | 33f4639bbf | |
FlyingSamson | 50b335f3da | |
FlyingSamson | ccdd10212c | |
FlyingSamson | ce43c75cd8 | |
chanicpanic | 19df25b99b | |
Andrew Port | c84c897bf2 | |
Andrew Port | ac138b8e5d | |
Andrew Port | 2dc06df20f | |
Andrew Port | 72fa3dcf17 | |
Catherine Holloway | 6c655ad220 | |
Andrew Port | 5037fac574 | |
Andrew Port | abd99f0846 | |
Andrew Port | d1421d6286 | |
Andrew Port | 8ad7458b31 | |
Andrew Port | 1b8caeec71 | |
Andrew Port | 002e691686 | |
Andrew Port | 3576591e08 | |
Andrew Port | ca094feea9 | |
Andrew Port | 3a6711a5e7 | |
Andrew Port | bbf75d0b5a | |
Andrew Port | 5b9ee30544 | |
Andrew Port | b35488efb0 | |
Andrew Port | d97519ffa3 | |
Andrew Port | 5a3bb8fca8 | |
Andrew Port | d998587a32 | |
Andrew Port | b8579b2c12 | |
Andrew Port | 05a2d271b7 | |
Andrew Port | a69898f83b | |
Andrew Port | 09ce497a4f | |
Julian Rüth | 39d3ba713f | |
Andrew Port | e4c7b53f62 | |
Andrew Port | 8f4b1fee00 | |
Andrew Port | a78ecf4290 | |
Andrew Port | 73e0ae2b21 | |
Andrew Port | da5286f79e | |
Andrew Port | 8b8ac6c9fe | |
Andrew Port | 44d08b6737 | |
Andrew Port | 60984969a7 | |
Andrew Port | 3b33445c25 | |
Andrew Port | c4b77697f2 | |
Andrew Port | 56bbba0bd1 | |
Andrew Port | 4f685e732a | |
Andrew Port | b8f4e71f5b | |
Andrew Port | e993ff95c5 | |
Andrew Port | 1b503a7b2f | |
Andrew Port | e0f212a334 | |
Andrew Port | be33f182fb | |
Andrew Port | 81ff41f881 | |
Andrew Port | 5be6b258e1 | |
Andrew Port | add170c926 | |
Andrew Port | 7d22c76e10 | |
Andrew Port | f19c473c69 | |
Andrew Port | bd55d303de | |
Andrew Port | 43c0d2d807 | |
Andrew Port | ecd39743ab | |
Andrew Port | cfc97e0ce2 | |
Andrew Port | 426ce7d56c | |
Andrew Port | 9c2e403036 | |
Andrew Port | 767413c896 | |
Andrew Port | ed207f2241 | |
Andrew Port | c5f49de5fe | |
Andrew Port | dac4600e6f | |
Andrew Port | 3d22a7cf52 | |
Andrew Port | a67de75137 | |
Andrew Port | 5ea0fb226d | |
Andrew Port | 8dc12d4efc | |
Vrroom | baba1d18b2 | |
Wes Bouaziz | 1e5bfb4252 | |
Nathan Hurst | a0fc28849c | |
tatarize | 3a1fe8695d | |
Andrew Port | 091394b5e3 | |
Andrew Port | 561c89ad47 | |
Andrew Port | ee58270f66 | |
Andrew Port | 30f517e735 | |
Andrew Port | 44e88d54e5 | |
Andrew Port | f9febbd85e | |
Andrew Port | 1c9363d426 | |
Andrew Port | 69e2e27efb | |
Andrew Port | ceffdc4a5b | |
Andrew Port | 2fb96b0906 | |
Andrew Port | 07771be9bf | |
Andrew Port | 5aeb6e3bf7 | |
Andrew Port | 0f4c9c598a | |
tatarize | b3d9544624 | |
Andrew Port | 45dc873f82 | |
Andrew Port | cae729bd48 | |
Andrew Port | 110acc9e00 | |
Andrew Port | 7a183c4e3c | |
Andrew Port | 9fa559a070 | |
Andrew Port | 1b4ed34ac9 | |
Andrew Port | 772d6698bf | |
Andrew Port | bdbd976e0a | |
Andy Port | d354b8ffe4 | |
Andy Port | 8f92e43f58 | |
Andy Port | 4342501591 | |
tatarize | 0c9dd318aa | |
Andy Port | 1a4807e929 | |
Andy Port | 12c8d07bad | |
Andy Port | 22f3dafe87 | |
Andy Port | 945ae49967 | |
tatarize | 90dfeb7b13 | |
Andy Port | c89c68f421 | |
Andy Port | ab44fcd564 | |
Andy Port | 4b7f17c7bd | |
Andy Port | 70534a6b6c | |
Andy Port | 1f7503aabd | |
Andy Port | 445899b2eb | |
Andy Port | d673176347 | |
NataliaTs | b503b9b3a5 | |
Andy Port | b714ff872d | |
Sebastian Kuzminsky | 685f9a6eaf | |
Matthew Carruth | 5ae88df6d5 | |
David Romero | 90f8f76185 | |
Grey | f99f9d6bb3 | |
Antoine Beyeler | b117f85811 | |
ugultopu | 538b8777e1 | |
Antoine Beyeler | b767536e38 | |
Sebastian Kuzminsky | 2eb8fb62ed | |
Sebastian Kuzminsky | 929202aa62 | |
taoari | c7b6c030a6 | |
Sebastian Kuzminsky | 8457dc01ee | |
skef | fd7348a1df | |
Andy | ae42197d10 | |
Sumeet P | b4e211fd79 | |
Andy Port | 58d48029ac | |
Sebastian Kuzminsky | b37e74f5f3 | |
tatarize | e91a35c3da | |
Sebastian Kuzminsky | 2feb3c92b5 | |
Andy Port | d810653b63 | |
Andy Port | 9e218b2b3b | |
Andy Port | 74a881a181 | |
Andy Port | a86be9d306 | |
Andy Port | ee8eda5aae | |
Andy Port | aa03a4aecb | |
Andy Port | 95179a6bfa | |
Andy Port | 7fa103e533 | |
Andy | f77f94db5b | |
Andy Port | 165372562e |
|
@ -0,0 +1,14 @@
|
||||||
|
name: Codacy
|
||||||
|
|
||||||
|
on: ["push"]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
codacy-analysis-cli:
|
||||||
|
name: Codacy Analysis CLI
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@main
|
||||||
|
|
||||||
|
- name: Run Codacy Analysis CLI
|
||||||
|
uses: codacy/codacy-analysis-cli-action@master
|
|
@ -0,0 +1,65 @@
|
||||||
|
# For most projects, this workflow file will not need changing; you simply need
|
||||||
|
# to commit it to your repository.
|
||||||
|
#
|
||||||
|
# You may wish to alter this file to override the set of languages analyzed,
|
||||||
|
# or to provide custom queries or build logic.
|
||||||
|
#
|
||||||
|
# ******** NOTE ********
|
||||||
|
# We have attempted to detect the languages in your repository. Please check
|
||||||
|
# the `language` matrix defined below to confirm you have the correct set of
|
||||||
|
# supported CodeQL languages.
|
||||||
|
#
|
||||||
|
name: "CodeQL"
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
schedule:
|
||||||
|
- cron: '30 2 * * 3'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
analyze:
|
||||||
|
name: Analyze
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
actions: read
|
||||||
|
contents: read
|
||||||
|
security-events: write
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
language: [ 'python' ]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# Initializes the CodeQL tools for scanning.
|
||||||
|
- name: Initialize CodeQL
|
||||||
|
uses: github/codeql-action/init@v1
|
||||||
|
with:
|
||||||
|
languages: ${{ matrix.language }}
|
||||||
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
|
# By default, queries listed here will override any specified in a config file.
|
||||||
|
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||||
|
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||||
|
|
||||||
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
|
- name: Autobuild
|
||||||
|
uses: github/codeql-action/autobuild@v1
|
||||||
|
|
||||||
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
|
# 📚 https://git.io/JvXDl
|
||||||
|
|
||||||
|
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||||
|
# and modify them (or add more) to build your code if your project
|
||||||
|
# uses a compiled language
|
||||||
|
|
||||||
|
#- run: |
|
||||||
|
# make bootstrap
|
||||||
|
# make release
|
||||||
|
|
||||||
|
- name: Perform CodeQL Analysis
|
||||||
|
uses: github/codeql-action/analyze@v1
|
|
@ -0,0 +1,34 @@
|
||||||
|
name: Github CI Unit Testing for Legacy Environments
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
continue-on-error: true
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-18.04, macos-10.15, windows-2019]
|
||||||
|
python-version: [2.7, 3.5, 3.6]
|
||||||
|
steps:
|
||||||
|
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# configure python
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
# install deps
|
||||||
|
- name: Install dependencies for ${{ matrix.os }} Python ${{ matrix.python-version }}
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements.txt
|
||||||
|
pip install scipy
|
||||||
|
|
||||||
|
# find and run all unit tests
|
||||||
|
- name: Run unit tests
|
||||||
|
run: python -m unittest discover test
|
|
@ -0,0 +1,34 @@
|
||||||
|
name: Github CI Unit Testing
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
continue-on-error: true
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
|
python-version: [3.7, 3.8, 3.9, "3.10", "3.11"]
|
||||||
|
steps:
|
||||||
|
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# configure python
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
# install deps
|
||||||
|
- name: Install dependencies for ${{ matrix.os }} Python ${{ matrix.python-version }}
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements.txt
|
||||||
|
pip install scipy
|
||||||
|
|
||||||
|
# find and run all unit tests
|
||||||
|
- name: Run unit tests
|
||||||
|
run: python -m unittest discover test
|
|
@ -0,0 +1,37 @@
|
||||||
|
name: Publish to TestPyPI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-n-publish:
|
||||||
|
name: Build and publish to TestPyPI
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@master
|
||||||
|
- name: Set up Python 3
|
||||||
|
uses: actions/setup-python@v1
|
||||||
|
with:
|
||||||
|
python-version: 3
|
||||||
|
- name: Install pypa/build
|
||||||
|
run: >-
|
||||||
|
python -m
|
||||||
|
pip install
|
||||||
|
build
|
||||||
|
--user
|
||||||
|
- name: Build a binary wheel and a source tarball
|
||||||
|
run: >-
|
||||||
|
python -m
|
||||||
|
build
|
||||||
|
--sdist
|
||||||
|
--wheel
|
||||||
|
--outdir dist/
|
||||||
|
.
|
||||||
|
- name: Publish to Test PyPI
|
||||||
|
uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
|
with:
|
||||||
|
skip_existing: true
|
||||||
|
password: ${{ secrets.TESTPYPI_API_TOKEN }}
|
||||||
|
repository_url: https://test.pypi.org/legacy/
|
|
@ -0,0 +1,42 @@
|
||||||
|
name: Publish to PyPI if new version
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-n-publish:
|
||||||
|
name: Build and publish to TestPyPI and PyPI
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@master
|
||||||
|
- name: Set up Python 3
|
||||||
|
uses: actions/setup-python@v1
|
||||||
|
with:
|
||||||
|
python-version: 3
|
||||||
|
- name: Install pypa/build
|
||||||
|
run: >-
|
||||||
|
python -m
|
||||||
|
pip install
|
||||||
|
build
|
||||||
|
--user
|
||||||
|
- name: Build a binary wheel and a source tarball
|
||||||
|
run: >-
|
||||||
|
python -m
|
||||||
|
build
|
||||||
|
--sdist
|
||||||
|
--wheel
|
||||||
|
--outdir dist/
|
||||||
|
.
|
||||||
|
- name: Publish to Test PyPI
|
||||||
|
uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
|
with:
|
||||||
|
skip_existing: true
|
||||||
|
password: ${{ secrets.TESTPYPI_API_TOKEN }}
|
||||||
|
repository_url: https://test.pypi.org/legacy/
|
||||||
|
- name: Publish to PyPI
|
||||||
|
if: startsWith(github.ref, 'refs/tags')
|
||||||
|
uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
|
with:
|
||||||
|
password: ${{ secrets.PYPI_API_TOKEN }}
|
|
@ -5,3 +5,4 @@ build
|
||||||
svgpathtools.egg-info
|
svgpathtools.egg-info
|
||||||
!.travis.yml
|
!.travis.yml
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
|
!/.github
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
language: python
|
|
||||||
python:
|
|
||||||
- "2.7"
|
|
||||||
- "3.6"
|
|
||||||
install:
|
|
||||||
- pip install numpy svgwrite
|
|
||||||
script:
|
|
||||||
- python -m unittest discover test
|
|
|
@ -18,7 +18,7 @@ example**. Feel free to make a pull-request too (see relevant section below).
|
||||||
|
|
||||||
## Submitting Pull-Requests
|
## Submitting Pull-Requests
|
||||||
|
|
||||||
#### New features should come with unittests and docstrings.
|
#### New features come with unittests and docstrings.
|
||||||
If you want to add a cool/useful feature to svgpathtools, that's great! Just
|
If you want to add a cool/useful feature to svgpathtools, that's great! Just
|
||||||
make sure your pull-request includes both thorough unittests and well-written
|
make sure your pull-request includes both thorough unittests and well-written
|
||||||
docstrings. See relevant sections below on "Testing Style" and
|
docstrings. See relevant sections below on "Testing Style" and
|
||||||
|
@ -42,8 +42,9 @@ you want your code's variable names to match some official documentation, or
|
||||||
PEP8 guidelines contradict those present in this document).
|
PEP8 guidelines contradict those present in this document).
|
||||||
* Include docstrings and in-line comments where appropriate. See
|
* Include docstrings and in-line comments where appropriate. See
|
||||||
"Docstring Style" section below for more info.
|
"Docstring Style" section below for more info.
|
||||||
* Use explicit, uncontracted names (e.g. "parse_transform" instead of
|
* Use explicit, uncontracted names (e.g. `parse_transform` instead of
|
||||||
"parse_trafo"). The ideal names should be something a user can guess
|
`parse_trafo`). Maybe the most important feature for a name is how easy it is
|
||||||
|
for a user to guess (after having seen other names used in `svgpathtools`).
|
||||||
* Use a capital 'T' denote a Path object's parameter, use a lower case 't' to
|
* Use a capital 'T' denote a Path object's parameter, use a lower case 't' to
|
||||||
denote a Path segment's parameter. See the methods `Path.t2T` and `Path.T2t`
|
denote a Path segment's parameter. See the methods `Path.t2T` and `Path.T2t`
|
||||||
if you're unsure what I mean. In the ambiguous case, use either 't' or another
|
if you're unsure what I mean. In the ambiguous case, use either 't' or another
|
||||||
|
|
199
README.ipynb
|
@ -2,11 +2,12 @@
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
|
"[![Donate](https://img.shields.io/badge/donate-paypal-brightgreen)](https://www.paypal.com/donate?business=4SKJ27AM4EYYA&no_recurring=0&item_name=Support+the+creator+of+svgpathtools?++He%27s+a+student+and+would+appreciate+it.&currency_code=USD)\n",
|
||||||
|
"![Python](https://img.shields.io/pypi/pyversions/svgpathtools.svg)\n",
|
||||||
|
"[![PyPI](https://img.shields.io/pypi/v/svgpathtools)](https://pypi.org/project/svgpathtools/)\n",
|
||||||
|
"[![PyPI - Downloads](https://img.shields.io/pypi/dm/svgpathtools?color=yellow)](https://pypistats.org/packages/svgpathtools)\n",
|
||||||
"# svgpathtools\n",
|
"# svgpathtools\n",
|
||||||
"\n",
|
"\n",
|
||||||
"svgpathtools is a collection of tools for manipulating and analyzing SVG Path objects and Bézier curves.\n",
|
"svgpathtools is a collection of tools for manipulating and analyzing SVG Path objects and Bézier curves.\n",
|
||||||
|
@ -39,20 +40,10 @@
|
||||||
"## Prerequisites\n",
|
"## Prerequisites\n",
|
||||||
"- **numpy**\n",
|
"- **numpy**\n",
|
||||||
"- **svgwrite**\n",
|
"- **svgwrite**\n",
|
||||||
|
"- **scipy** (optional but recommended for performance)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Setup\n",
|
"## Setup\n",
|
||||||
"\n",
|
"\n",
|
||||||
"If not already installed, you can **install the prerequisites** using pip.\n",
|
|
||||||
"\n",
|
|
||||||
"```bash\n",
|
|
||||||
"$ pip install numpy\n",
|
|
||||||
"```\n",
|
|
||||||
"\n",
|
|
||||||
"```bash\n",
|
|
||||||
"$ pip install svgwrite\n",
|
|
||||||
"```\n",
|
|
||||||
"\n",
|
|
||||||
"Then **install svgpathtools**:\n",
|
|
||||||
"```bash\n",
|
"```bash\n",
|
||||||
"$ pip install svgpathtools\n",
|
"$ pip install svgpathtools\n",
|
||||||
"``` \n",
|
"``` \n",
|
||||||
|
@ -91,9 +82,7 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true,
|
"collapsed": true
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -103,11 +92,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
|
@ -146,10 +131,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"The ``Path`` class is a mutable sequence, so it behaves much like a list.\n",
|
"The ``Path`` class is a mutable sequence, so it behaves much like a list.\n",
|
||||||
"So segments can **append**ed, **insert**ed, set by index, **del**eted, **enumerate**d, **slice**d out, etc."
|
"So segments can **append**ed, **insert**ed, set by index, **del**eted, **enumerate**d, **slice**d out, etc."
|
||||||
|
@ -158,11 +140,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
|
@ -226,10 +204,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Reading SVGSs\n",
|
"### Reading SVGSs\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -240,11 +215,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
|
@ -277,10 +248,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Writing SVGSs (and some geometric functions and methods)\n",
|
"### Writing SVGSs (and some geometric functions and methods)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -291,11 +259,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's make a new SVG that's identical to the first\n",
|
"# Let's make a new SVG that's identical to the first\n",
|
||||||
|
@ -304,20 +268,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"![output1.svg](output1.svg)"
|
"![output1.svg](output1.svg)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"There will be many more examples of writing and displaying path data below.\n",
|
"There will be many more examples of writing and displaying path data below.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -334,11 +292,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 6,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
|
@ -374,10 +328,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Bezier curves as NumPy polynomial objects\n",
|
"### Bezier curves as NumPy polynomial objects\n",
|
||||||
"Another great way to work with the parameterizations for `Line`, `QuadraticBezier`, and `CubicBezier` objects is to convert them to ``numpy.poly1d`` objects. This is done easily using the ``Line.poly()``, ``QuadraticBezier.poly()`` and ``CubicBezier.poly()`` methods. \n",
|
"Another great way to work with the parameterizations for `Line`, `QuadraticBezier`, and `CubicBezier` objects is to convert them to ``numpy.poly1d`` objects. This is done easily using the ``Line.poly()``, ``QuadraticBezier.poly()`` and ``CubicBezier.poly()`` methods. \n",
|
||||||
|
@ -402,11 +353,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 7,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
|
@ -442,10 +389,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"The ability to convert between Bezier objects to NumPy polynomial objects is very useful. For starters, we can take turn a list of Bézier segments into a NumPy array \n",
|
"The ability to convert between Bezier objects to NumPy polynomial objects is very useful. For starters, we can take turn a list of Bézier segments into a NumPy array \n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -461,11 +405,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 8,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
|
@ -510,10 +450,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Translations (shifts), reversing orientation, and normal vectors"
|
"### Translations (shifts), reversing orientation, and normal vectors"
|
||||||
]
|
]
|
||||||
|
@ -521,11 +458,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 9,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Speaking of tangents, let's add a normal vector to the picture\n",
|
"# Speaking of tangents, let's add a normal vector to the picture\n",
|
||||||
|
@ -551,20 +484,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"![vectorframes.svg](vectorframes.svg)"
|
"![vectorframes.svg](vectorframes.svg)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Rotations and Translations"
|
"### Rotations and Translations"
|
||||||
]
|
]
|
||||||
|
@ -572,11 +499,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": 10,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's take a Line and an Arc and make some pictures\n",
|
"# Let's take a Line and an Arc and make some pictures\n",
|
||||||
|
@ -599,20 +522,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"![decorated_ellipse.svg](decorated_ellipse.svg)"
|
"![decorated_ellipse.svg](decorated_ellipse.svg)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### arc length and inverse arc length\n",
|
"### arc length and inverse arc length\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -622,11 +539,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": 11,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# First we'll load the path data from the file test.svg\n",
|
"# First we'll load the path data from the file test.svg\n",
|
||||||
|
@ -664,20 +577,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"![output2.svg](output2.svg)"
|
"![output2.svg](output2.svg)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Intersections between Bezier curves"
|
"### Intersections between Bezier curves"
|
||||||
]
|
]
|
||||||
|
@ -685,11 +592,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 12,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's find all intersections between redpath and the other \n",
|
"# Let's find all intersections between redpath and the other \n",
|
||||||
|
@ -706,20 +609,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"![output_intersections.svg](output_intersections.svg)"
|
"![output_intersections.svg](output_intersections.svg)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### An Advanced Application: Offsetting Paths\n",
|
"### An Advanced Application: Offsetting Paths\n",
|
||||||
"Here we'll find the [offset curve](https://en.wikipedia.org/wiki/Parallel_curve) for a few paths."
|
"Here we'll find the [offset curve](https://en.wikipedia.org/wiki/Parallel_curve) for a few paths."
|
||||||
|
@ -728,11 +625,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 13,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false,
|
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from svgpathtools import parse_path, Line, Path, wsvg\n",
|
"from svgpathtools import parse_path, Line, Path, wsvg\n",
|
||||||
|
@ -772,20 +665,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"![offset_curves.svg](offset_curves.svg)"
|
"![offset_curves.svg](offset_curves.svg)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Compatibility Notes for users of svg.path (v2.0)\n",
|
"## Compatibility Notes for users of svg.path (v2.0)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -806,9 +693,7 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true,
|
"collapsed": true
|
||||||
"deletable": true,
|
|
||||||
"editable": true
|
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": []
|
||||||
|
@ -823,16 +708,16 @@
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.12"
|
"version": "3.7.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,515 @@
|
||||||
|
[![Donate](https://img.shields.io/badge/donate-paypal-brightgreen)](https://www.paypal.com/donate?business=4SKJ27AM4EYYA&no_recurring=0&item_name=Support+the+creator+of+svgpathtools?++He%27s+a+student+and+would+appreciate+it.&currency_code=USD)
|
||||||
|
![Python](https://img.shields.io/pypi/pyversions/svgpathtools.svg)
|
||||||
|
[![PyPI](https://img.shields.io/pypi/v/svgpathtools)](https://pypi.org/project/svgpathtools/)
|
||||||
|
[![PyPI - Downloads](https://img.shields.io/pypi/dm/svgpathtools?color=yellow)](https://pypistats.org/packages/svgpathtools)
|
||||||
|
# svgpathtools
|
||||||
|
|
||||||
|
|
||||||
|
svgpathtools is a collection of tools for manipulating and analyzing SVG Path objects and Bézier curves.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
svgpathtools contains functions designed to **easily read, write and display SVG files** as well as *a large selection of geometrically\-oriented tools* to **transform and analyze path elements**.
|
||||||
|
|
||||||
|
Additionally, the submodule *bezier.py* contains tools for for working with general **nth order Bezier curves stored as n-tuples**.
|
||||||
|
|
||||||
|
Some included tools:
|
||||||
|
|
||||||
|
- **read**, **write**, and **display** SVG files containing Path (and other) SVG elements
|
||||||
|
- convert Bézier path segments to **numpy.poly1d** (polynomial) objects
|
||||||
|
- convert polynomials (in standard form) to their Bézier form
|
||||||
|
- compute **tangent vectors** and (right-hand rule) **normal vectors**
|
||||||
|
- compute **curvature**
|
||||||
|
- break discontinuous paths into their **continuous subpaths**.
|
||||||
|
- efficiently compute **intersections** between paths and/or segments
|
||||||
|
- find a **bounding box** for a path or segment
|
||||||
|
- **reverse** segment/path orientation
|
||||||
|
- **crop** and **split** paths and segments
|
||||||
|
- **smooth** paths (i.e. smooth away kinks to make paths differentiable)
|
||||||
|
- **transition maps** from path domain to segment domain and back (T2t and t2T)
|
||||||
|
- compute **area** enclosed by a closed path
|
||||||
|
- compute **arc length**
|
||||||
|
- compute **inverse arc length**
|
||||||
|
- convert RGB color tuples to hexadecimal color strings and back
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
- **numpy**
|
||||||
|
- **svgwrite**
|
||||||
|
- **scipy** (optional, but recommended for performance)
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ pip install svgpathtools
|
||||||
|
```
|
||||||
|
|
||||||
|
### Alternative Setup
|
||||||
|
You can download the source from Github and install by using the command (from inside the folder containing setup.py):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ python setup.py install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Credit where credit's due
|
||||||
|
Much of the core of this module was taken from [the svg.path (v2.0) module](https://github.com/regebro/svg.path). Interested svg.path users should see the compatibility notes at bottom of this readme.
|
||||||
|
|
||||||
|
## Basic Usage
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
The svgpathtools module is primarily structured around four path segment classes: ``Line``, ``QuadraticBezier``, ``CubicBezier``, and ``Arc``. There is also a fifth class, ``Path``, whose objects are sequences of (connected or disconnected<sup id="a1">[1](#f1)</sup>) path segment objects.
|
||||||
|
|
||||||
|
* ``Line(start, end)``
|
||||||
|
|
||||||
|
* ``Arc(start, radius, rotation, large_arc, sweep, end)`` Note: See docstring for a detailed explanation of these parameters
|
||||||
|
|
||||||
|
* ``QuadraticBezier(start, control, end)``
|
||||||
|
|
||||||
|
* ``CubicBezier(start, control1, control2, end)``
|
||||||
|
|
||||||
|
* ``Path(*segments)``
|
||||||
|
|
||||||
|
See the relevant docstrings in *path.py* or the [official SVG specifications](<http://www.w3.org/TR/SVG/paths.html>) for more information on what each parameter means.
|
||||||
|
|
||||||
|
<u id="f1">1</u> Warning: Some of the functionality in this library has not been tested on discontinuous Path objects. A simple workaround is provided, however, by the ``Path.continuous_subpaths()`` method. [↩](#a1)
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
from __future__ import division, print_function
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Coordinates are given as points in the complex plane
|
||||||
|
from svgpathtools import Path, Line, QuadraticBezier, CubicBezier, Arc
|
||||||
|
seg1 = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j) # A cubic beginning at (300, 100) and ending at (200, 300)
|
||||||
|
seg2 = Line(200+300j, 250+350j) # A line beginning at (200, 300) and ending at (250, 350)
|
||||||
|
path = Path(seg1, seg2) # A path traversing the cubic and then the line
|
||||||
|
|
||||||
|
# We could alternatively created this Path object using a d-string
|
||||||
|
from svgpathtools import parse_path
|
||||||
|
path_alt = parse_path('M 300 100 C 100 100 200 200 200 300 L 250 350')
|
||||||
|
|
||||||
|
# Let's check that these two methods are equivalent
|
||||||
|
print(path)
|
||||||
|
print(path_alt)
|
||||||
|
print(path == path_alt)
|
||||||
|
|
||||||
|
# On a related note, the Path.d() method returns a Path object's d-string
|
||||||
|
print(path.d())
|
||||||
|
print(parse_path(path.d()) == path)
|
||||||
|
```
|
||||||
|
|
||||||
|
Path(CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j)),
|
||||||
|
Line(start=(200+300j), end=(250+350j)))
|
||||||
|
Path(CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j)),
|
||||||
|
Line(start=(200+300j), end=(250+350j)))
|
||||||
|
True
|
||||||
|
M 300.0,100.0 C 100.0,100.0 200.0,200.0 200.0,300.0 L 250.0,350.0
|
||||||
|
True
|
||||||
|
|
||||||
|
|
||||||
|
The ``Path`` class is a mutable sequence, so it behaves much like a list.
|
||||||
|
So segments can **append**ed, **insert**ed, set by index, **del**eted, **enumerate**d, **slice**d out, etc.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Let's append another to the end of it
|
||||||
|
path.append(CubicBezier(250+350j, 275+350j, 250+225j, 200+100j))
|
||||||
|
print(path)
|
||||||
|
|
||||||
|
# Let's replace the first segment with a Line object
|
||||||
|
path[0] = Line(200+100j, 200+300j)
|
||||||
|
print(path)
|
||||||
|
|
||||||
|
# You may have noticed that this path is connected and now is also closed (i.e. path.start == path.end)
|
||||||
|
print("path is continuous? ", path.iscontinuous())
|
||||||
|
print("path is closed? ", path.isclosed())
|
||||||
|
|
||||||
|
# The curve the path follows is not, however, smooth (differentiable)
|
||||||
|
from svgpathtools import kinks, smoothed_path
|
||||||
|
print("path contains non-differentiable points? ", len(kinks(path)) > 0)
|
||||||
|
|
||||||
|
# If we want, we can smooth these out (Experimental and only for line/cubic paths)
|
||||||
|
# Note: smoothing will always works (except on 180 degree turns), but you may want
|
||||||
|
# to play with the maxjointsize and tightness parameters to get pleasing results
|
||||||
|
# Note also: smoothing will increase the number of segments in a path
|
||||||
|
spath = smoothed_path(path)
|
||||||
|
print("spath contains non-differentiable points? ", len(kinks(spath)) > 0)
|
||||||
|
print(spath)
|
||||||
|
|
||||||
|
# Let's take a quick look at the path and its smoothed relative
|
||||||
|
# The following commands will open two browser windows to display path and spaths
|
||||||
|
from svgpathtools import disvg
|
||||||
|
from time import sleep
|
||||||
|
disvg(path)
|
||||||
|
sleep(1) # needed when not giving the SVGs unique names (or not using timestamp)
|
||||||
|
disvg(spath)
|
||||||
|
print("Notice that path contains {} segments and spath contains {} segments."
|
||||||
|
"".format(len(path), len(spath)))
|
||||||
|
```
|
||||||
|
|
||||||
|
Path(CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j)),
|
||||||
|
Line(start=(200+300j), end=(250+350j)),
|
||||||
|
CubicBezier(start=(250+350j), control1=(275+350j), control2=(250+225j), end=(200+100j)))
|
||||||
|
Path(Line(start=(200+100j), end=(200+300j)),
|
||||||
|
Line(start=(200+300j), end=(250+350j)),
|
||||||
|
CubicBezier(start=(250+350j), control1=(275+350j), control2=(250+225j), end=(200+100j)))
|
||||||
|
path is continuous? True
|
||||||
|
path is closed? True
|
||||||
|
path contains non-differentiable points? True
|
||||||
|
spath contains non-differentiable points? False
|
||||||
|
Path(Line(start=(200+101.5j), end=(200+298.5j)),
|
||||||
|
CubicBezier(start=(200+298.5j), control1=(200+298.505j), control2=(201.057124638+301.057124638j), end=(201.060660172+301.060660172j)),
|
||||||
|
Line(start=(201.060660172+301.060660172j), end=(248.939339828+348.939339828j)),
|
||||||
|
CubicBezier(start=(248.939339828+348.939339828j), control1=(249.649982143+349.649982143j), control2=(248.995+350j), end=(250+350j)),
|
||||||
|
CubicBezier(start=(250+350j), control1=(275+350j), control2=(250+225j), end=(200+100j)),
|
||||||
|
CubicBezier(start=(200+100j), control1=(199.62675237+99.0668809257j), control2=(200+100.495j), end=(200+101.5j)))
|
||||||
|
Notice that path contains 3 segments and spath contains 6 segments.
|
||||||
|
|
||||||
|
|
||||||
|
### Reading SVGSs
|
||||||
|
|
||||||
|
The **svg2paths()** function converts an svgfile to a list of Path objects and a separate list of dictionaries containing the attributes of each said path.
|
||||||
|
Note: Line, Polyline, Polygon, and Path SVG elements can all be converted to Path objects using this function.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Read SVG into a list of path objects and list of dictionaries of attributes
|
||||||
|
from svgpathtools import svg2paths, wsvg
|
||||||
|
paths, attributes = svg2paths('test.svg')
|
||||||
|
|
||||||
|
# Update: You can now also extract the svg-attributes by setting
|
||||||
|
# return_svg_attributes=True, or with the convenience function svg2paths2
|
||||||
|
from svgpathtools import svg2paths2
|
||||||
|
paths, attributes, svg_attributes = svg2paths2('test.svg')
|
||||||
|
|
||||||
|
# Let's print out the first path object and the color it was in the SVG
|
||||||
|
# We'll see it is composed of two CubicBezier objects and, in the SVG file it
|
||||||
|
# came from, it was red
|
||||||
|
redpath = paths[0]
|
||||||
|
redpath_attribs = attributes[0]
|
||||||
|
print(redpath)
|
||||||
|
print(redpath_attribs['stroke'])
|
||||||
|
```
|
||||||
|
|
||||||
|
Path(CubicBezier(start=(10.5+80j), control1=(40+10j), control2=(65+10j), end=(95+80j)),
|
||||||
|
CubicBezier(start=(95+80j), control1=(125+150j), control2=(150+150j), end=(180+80j)))
|
||||||
|
red
|
||||||
|
|
||||||
|
|
||||||
|
### Writing SVGSs (and some geometric functions and methods)
|
||||||
|
|
||||||
|
The **wsvg()** function creates an SVG file from a list of path. This function can do many things (see docstring in *paths2svg.py* for more information) and is meant to be quick and easy to use.
|
||||||
|
Note: Use the convenience function **disvg()** (or set 'openinbrowser=True') to automatically attempt to open the created svg file in your default SVG viewer.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Let's make a new SVG that's identical to the first
|
||||||
|
wsvg(paths, attributes=attributes, svg_attributes=svg_attributes, filename='output1.svg')
|
||||||
|
```
|
||||||
|
|
||||||
|
![output1.svg](output1.svg)
|
||||||
|
|
||||||
|
There will be many more examples of writing and displaying path data below.
|
||||||
|
|
||||||
|
### The .point() method and transitioning between path and path segment parameterizations
|
||||||
|
SVG Path elements and their segments have official parameterizations.
|
||||||
|
These parameterizations can be accessed using the ``Path.point()``, ``Line.point()``, ``QuadraticBezier.point()``, ``CubicBezier.point()``, and ``Arc.point()`` methods.
|
||||||
|
All these parameterizations are defined over the domain 0 <= t <= 1.
|
||||||
|
|
||||||
|
**Note:** In this document and in inline documentation and doctrings, I use a capital ``T`` when referring to the parameterization of a Path object and a lower case ``t`` when referring speaking about path segment objects (i.e. Line, QaudraticBezier, CubicBezier, and Arc objects).
|
||||||
|
Given a ``T`` value, the ``Path.T2t()`` method can be used to find the corresponding segment index, ``k``, and segment parameter, ``t``, such that ``path.point(T)=path[k].point(t)``.
|
||||||
|
There is also a ``Path.t2T()`` method to solve the inverse problem.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Example:
|
||||||
|
|
||||||
|
# Let's check that the first segment of redpath starts
|
||||||
|
# at the same point as redpath
|
||||||
|
firstseg = redpath[0]
|
||||||
|
print(redpath.point(0) == firstseg.point(0) == redpath.start == firstseg.start)
|
||||||
|
|
||||||
|
# Let's check that the last segment of redpath ends on the same point as redpath
|
||||||
|
lastseg = redpath[-1]
|
||||||
|
print(redpath.point(1) == lastseg.point(1) == redpath.end == lastseg.end)
|
||||||
|
|
||||||
|
# This next boolean should return False as redpath is composed multiple segments
|
||||||
|
print(redpath.point(0.5) == firstseg.point(0.5))
|
||||||
|
|
||||||
|
# If we want to figure out which segment of redpoint the
|
||||||
|
# point redpath.point(0.5) lands on, we can use the path.T2t() method
|
||||||
|
k, t = redpath.T2t(0.5)
|
||||||
|
print(redpath[k].point(t) == redpath.point(0.5))
|
||||||
|
```
|
||||||
|
|
||||||
|
True
|
||||||
|
True
|
||||||
|
False
|
||||||
|
True
|
||||||
|
|
||||||
|
|
||||||
|
### Bezier curves as NumPy polynomial objects
|
||||||
|
Another great way to work with the parameterizations for `Line`, `QuadraticBezier`, and `CubicBezier` objects is to convert them to ``numpy.poly1d`` objects. This is done easily using the ``Line.poly()``, ``QuadraticBezier.poly()`` and ``CubicBezier.poly()`` methods.
|
||||||
|
There's also a ``polynomial2bezier()`` function in the pathtools.py submodule to convert polynomials back to Bezier curves.
|
||||||
|
|
||||||
|
**Note:** cubic Bezier curves are parameterized as $$\mathcal{B}(t) = P_0(1-t)^3 + 3P_1(1-t)^2t + 3P_2(1-t)t^2 + P_3t^3$$
|
||||||
|
where $P_0$, $P_1$, $P_2$, and $P_3$ are the control points ``start``, ``control1``, ``control2``, and ``end``, respectively, that svgpathtools uses to define a CubicBezier object. The ``CubicBezier.poly()`` method expands this polynomial to its standard form
|
||||||
|
$$\mathcal{B}(t) = c_0t^3 + c_1t^2 +c_2t+c3$$
|
||||||
|
where
|
||||||
|
$$\begin{bmatrix}c_0\\c_1\\c_2\\c_3\end{bmatrix} =
|
||||||
|
\begin{bmatrix}
|
||||||
|
-1 & 3 & -3 & 1\\
|
||||||
|
3 & -6 & -3 & 0\\
|
||||||
|
-3 & 3 & 0 & 0\\
|
||||||
|
1 & 0 & 0 & 0\\
|
||||||
|
\end{bmatrix}
|
||||||
|
\begin{bmatrix}P_0\\P_1\\P_2\\P_3\end{bmatrix}$$
|
||||||
|
|
||||||
|
`QuadraticBezier.poly()` and `Line.poly()` are [defined similarly](https://en.wikipedia.org/wiki/B%C3%A9zier_curve#General_definition).
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Example:
|
||||||
|
b = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j)
|
||||||
|
p = b.poly()
|
||||||
|
|
||||||
|
# p(t) == b.point(t)
|
||||||
|
print(p(0.235) == b.point(0.235))
|
||||||
|
|
||||||
|
# What is p(t)? It's just the cubic b written in standard form.
|
||||||
|
bpretty = "{}*(1-t)^3 + 3*{}*(1-t)^2*t + 3*{}*(1-t)*t^2 + {}*t^3".format(*b.bpoints())
|
||||||
|
print("The CubicBezier, b.point(x) = \n\n" +
|
||||||
|
bpretty + "\n\n" +
|
||||||
|
"can be rewritten in standard form as \n\n" +
|
||||||
|
str(p).replace('x','t'))
|
||||||
|
```
|
||||||
|
|
||||||
|
True
|
||||||
|
The CubicBezier, b.point(x) =
|
||||||
|
|
||||||
|
(300+100j)*(1-t)^3 + 3*(100+100j)*(1-t)^2*t + 3*(200+200j)*(1-t)*t^2 + (200+300j)*t^3
|
||||||
|
|
||||||
|
can be rewritten in standard form as
|
||||||
|
|
||||||
|
3 2
|
||||||
|
(-400 + -100j) t + (900 + 300j) t - 600 t + (300 + 100j)
|
||||||
|
|
||||||
|
|
||||||
|
The ability to convert between Bezier objects to NumPy polynomial objects is very useful. For starters, we can take turn a list of Bézier segments into a NumPy array
|
||||||
|
|
||||||
|
### Numpy Array operations on Bézier path segments
|
||||||
|
|
||||||
|
[Example available here](https://github.com/mathandy/svgpathtools/blob/master/examples/compute-many-points-quickly-using-numpy-arrays.py)
|
||||||
|
|
||||||
|
To further illustrate the power of being able to convert our Bezier curve objects to numpy.poly1d objects and back, lets compute the unit tangent vector of the above CubicBezier object, b, at t=0.5 in four different ways.
|
||||||
|
|
||||||
|
### Tangent vectors (and more on NumPy polynomials)
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
t = 0.5
|
||||||
|
### Method 1: the easy way
|
||||||
|
u1 = b.unit_tangent(t)
|
||||||
|
|
||||||
|
### Method 2: another easy way
|
||||||
|
# Note: This way will fail if it encounters a removable singularity.
|
||||||
|
u2 = b.derivative(t)/abs(b.derivative(t))
|
||||||
|
|
||||||
|
### Method 2: a third easy way
|
||||||
|
# Note: This way will also fail if it encounters a removable singularity.
|
||||||
|
dp = p.deriv()
|
||||||
|
u3 = dp(t)/abs(dp(t))
|
||||||
|
|
||||||
|
### Method 4: the removable-singularity-proof numpy.poly1d way
|
||||||
|
# Note: This is roughly how Method 1 works
|
||||||
|
from svgpathtools import real, imag, rational_limit
|
||||||
|
dx, dy = real(dp), imag(dp) # dp == dx + 1j*dy
|
||||||
|
p_mag2 = dx**2 + dy**2 # p_mag2(t) = |p(t)|**2
|
||||||
|
# Note: abs(dp) isn't a polynomial, but abs(dp)**2 is, and,
|
||||||
|
# the limit_{t->t0}[f(t) / abs(f(t))] ==
|
||||||
|
# sqrt(limit_{t->t0}[f(t)**2 / abs(f(t))**2])
|
||||||
|
from cmath import sqrt
|
||||||
|
u4 = sqrt(rational_limit(dp**2, p_mag2, t))
|
||||||
|
|
||||||
|
print("unit tangent check:", u1 == u2 == u3 == u4)
|
||||||
|
|
||||||
|
# Let's do a visual check
|
||||||
|
mag = b.length()/4 # so it's not hard to see the tangent line
|
||||||
|
tangent_line = Line(b.point(t), b.point(t) + mag*u1)
|
||||||
|
disvg([b, tangent_line], 'bg', nodes=[b.point(t)])
|
||||||
|
```
|
||||||
|
|
||||||
|
unit tangent check: True
|
||||||
|
|
||||||
|
|
||||||
|
### Translations (shifts), reversing orientation, and normal vectors
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Speaking of tangents, let's add a normal vector to the picture
|
||||||
|
n = b.normal(t)
|
||||||
|
normal_line = Line(b.point(t), b.point(t) + mag*n)
|
||||||
|
disvg([b, tangent_line, normal_line], 'bgp', nodes=[b.point(t)])
|
||||||
|
|
||||||
|
# and let's reverse the orientation of b!
|
||||||
|
# the tangent and normal lines should be sent to their opposites
|
||||||
|
br = b.reversed()
|
||||||
|
|
||||||
|
# Let's also shift b_r over a bit to the right so we can view it next to b
|
||||||
|
# The simplest way to do this is br = br.translated(3*mag), but let's use
|
||||||
|
# the .bpoints() instead, which returns a Bezier's control points
|
||||||
|
br.start, br.control1, br.control2, br.end = [3*mag + bpt for bpt in br.bpoints()] #
|
||||||
|
|
||||||
|
tangent_line_r = Line(br.point(t), br.point(t) + mag*br.unit_tangent(t))
|
||||||
|
normal_line_r = Line(br.point(t), br.point(t) + mag*br.normal(t))
|
||||||
|
wsvg([b, tangent_line, normal_line, br, tangent_line_r, normal_line_r],
|
||||||
|
'bgpkgp', nodes=[b.point(t), br.point(t)], filename='vectorframes.svg',
|
||||||
|
text=["b's tangent", "br's tangent"], text_path=[tangent_line, tangent_line_r])
|
||||||
|
```
|
||||||
|
|
||||||
|
![vectorframes.svg](vectorframes.svg)
|
||||||
|
|
||||||
|
### Rotations and Translations
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Let's take a Line and an Arc and make some pictures
|
||||||
|
top_half = Arc(start=-1, radius=1+2j, rotation=0, large_arc=1, sweep=1, end=1)
|
||||||
|
midline = Line(-1.5, 1.5)
|
||||||
|
|
||||||
|
# First let's make our ellipse whole
|
||||||
|
bottom_half = top_half.rotated(180)
|
||||||
|
decorated_ellipse = Path(top_half, bottom_half)
|
||||||
|
|
||||||
|
# Now let's add the decorations
|
||||||
|
for k in range(12):
|
||||||
|
decorated_ellipse.append(midline.rotated(30*k))
|
||||||
|
|
||||||
|
# Let's move it over so we can see the original Line and Arc object next
|
||||||
|
# to the final product
|
||||||
|
decorated_ellipse = decorated_ellipse.translated(4+0j)
|
||||||
|
wsvg([top_half, midline, decorated_ellipse], filename='decorated_ellipse.svg')
|
||||||
|
```
|
||||||
|
|
||||||
|
![decorated_ellipse.svg](decorated_ellipse.svg)
|
||||||
|
|
||||||
|
### arc length and inverse arc length
|
||||||
|
|
||||||
|
Here we'll create an SVG that shows off the parametric and geometric midpoints of the paths from ``test.svg``. We'll need to compute use the ``Path.length()``, ``Line.length()``, ``QuadraticBezier.length()``, ``CubicBezier.length()``, and ``Arc.length()`` methods, as well as the related inverse arc length methods ``.ilength()`` function to do this.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# First we'll load the path data from the file test.svg
|
||||||
|
paths, attributes = svg2paths('test.svg')
|
||||||
|
|
||||||
|
# Let's mark the parametric midpoint of each segment
|
||||||
|
# I say "parametric" midpoint because Bezier curves aren't
|
||||||
|
# parameterized by arclength
|
||||||
|
# If they're also the geometric midpoint, let's mark them
|
||||||
|
# purple and otherwise we'll mark the geometric midpoint green
|
||||||
|
min_depth = 5
|
||||||
|
error = 1e-4
|
||||||
|
dots = []
|
||||||
|
ncols = []
|
||||||
|
nradii = []
|
||||||
|
for path in paths:
|
||||||
|
for seg in path:
|
||||||
|
parametric_mid = seg.point(0.5)
|
||||||
|
seg_length = seg.length()
|
||||||
|
if seg.length(0.5)/seg.length() == 1/2:
|
||||||
|
dots += [parametric_mid]
|
||||||
|
ncols += ['purple']
|
||||||
|
nradii += [5]
|
||||||
|
else:
|
||||||
|
t_mid = seg.ilength(seg_length/2)
|
||||||
|
geo_mid = seg.point(t_mid)
|
||||||
|
dots += [parametric_mid, geo_mid]
|
||||||
|
ncols += ['red', 'green']
|
||||||
|
nradii += [5] * 2
|
||||||
|
|
||||||
|
# In 'output2.svg' the paths will retain their original attributes
|
||||||
|
wsvg(paths, nodes=dots, node_colors=ncols, node_radii=nradii,
|
||||||
|
attributes=attributes, filename='output2.svg')
|
||||||
|
```
|
||||||
|
|
||||||
|
![output2.svg](output2.svg)
|
||||||
|
|
||||||
|
### Intersections between Bezier curves
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Let's find all intersections between redpath and the other
|
||||||
|
redpath = paths[0]
|
||||||
|
redpath_attribs = attributes[0]
|
||||||
|
intersections = []
|
||||||
|
for path in paths[1:]:
|
||||||
|
for (T1, seg1, t1), (T2, seg2, t2) in redpath.intersect(path):
|
||||||
|
intersections.append(redpath.point(T1))
|
||||||
|
|
||||||
|
disvg(paths, filename='output_intersections.svg', attributes=attributes,
|
||||||
|
nodes = intersections, node_radii = [5]*len(intersections))
|
||||||
|
```
|
||||||
|
|
||||||
|
![output_intersections.svg](output_intersections.svg)
|
||||||
|
|
||||||
|
### An Advanced Application: Offsetting Paths
|
||||||
|
Here we'll find the [offset curve](https://en.wikipedia.org/wiki/Parallel_curve) for a few paths.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
from svgpathtools import parse_path, Line, Path, wsvg
|
||||||
|
def offset_curve(path, offset_distance, steps=1000):
|
||||||
|
"""Takes in a Path object, `path`, and a distance,
|
||||||
|
`offset_distance`, and outputs an piecewise-linear approximation
|
||||||
|
of the 'parallel' offset curve."""
|
||||||
|
nls = []
|
||||||
|
for seg in path:
|
||||||
|
ct = 1
|
||||||
|
for k in range(steps):
|
||||||
|
t = k / steps
|
||||||
|
offset_vector = offset_distance * seg.normal(t)
|
||||||
|
nl = Line(seg.point(t), seg.point(t) + offset_vector)
|
||||||
|
nls.append(nl)
|
||||||
|
connect_the_dots = [Line(nls[k].end, nls[k+1].end) for k in range(len(nls)-1)]
|
||||||
|
if path.isclosed():
|
||||||
|
connect_the_dots.append(Line(nls[-1].end, nls[0].end))
|
||||||
|
offset_path = Path(*connect_the_dots)
|
||||||
|
return offset_path
|
||||||
|
|
||||||
|
# Examples:
|
||||||
|
path1 = parse_path("m 288,600 c -52,-28 -42,-61 0,-97 ")
|
||||||
|
path2 = parse_path("M 151,395 C 407,485 726.17662,160 634,339").translated(300)
|
||||||
|
path3 = parse_path("m 117,695 c 237,-7 -103,-146 457,0").translated(500+400j)
|
||||||
|
paths = [path1, path2, path3]
|
||||||
|
|
||||||
|
offset_distances = [10*k for k in range(1,51)]
|
||||||
|
offset_paths = []
|
||||||
|
for path in paths:
|
||||||
|
for distances in offset_distances:
|
||||||
|
offset_paths.append(offset_curve(path, distances))
|
||||||
|
|
||||||
|
# Let's take a look
|
||||||
|
wsvg(paths + offset_paths, 'g'*len(paths) + 'r'*len(offset_paths), filename='offset_curves.svg')
|
||||||
|
```
|
||||||
|
|
||||||
|
![offset_curves.svg](offset_curves.svg)
|
||||||
|
|
||||||
|
## Compatibility Notes for users of svg.path (v2.0)
|
||||||
|
|
||||||
|
- renamed Arc.arc attribute as Arc.large_arc
|
||||||
|
|
||||||
|
- Path.d() : For behavior similar<sup id="a2">[2](#f2)</sup> to svg.path (v2.0), set both useSandT and use_closed_attrib to be True.
|
||||||
|
|
||||||
|
<u id="f2">2</u> The behavior would be identical, but the string formatting used in this method has been changed to use default format (instead of the General format, {:G}), for inceased precision. [↩](#a2)
|
||||||
|
|
||||||
|
|
||||||
|
Licence
|
||||||
|
-------
|
||||||
|
|
||||||
|
This module is under a MIT License.
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
```
|
635
README.rst
|
@ -1,635 +0,0 @@
|
||||||
|
|
||||||
svgpathtools
|
|
||||||
============
|
|
||||||
|
|
||||||
svgpathtools is a collection of tools for manipulating and analyzing SVG
|
|
||||||
Path objects and Bézier curves.
|
|
||||||
|
|
||||||
Features
|
|
||||||
--------
|
|
||||||
|
|
||||||
svgpathtools contains functions designed to **easily read, write and
|
|
||||||
display SVG files** as well as *a large selection of
|
|
||||||
geometrically-oriented tools* to **transform and analyze path
|
|
||||||
elements**.
|
|
||||||
|
|
||||||
Additionally, the submodule *bezier.py* contains tools for for working
|
|
||||||
with general **nth order Bezier curves stored as n-tuples**.
|
|
||||||
|
|
||||||
Some included tools:
|
|
||||||
|
|
||||||
- **read**, **write**, and **display** SVG files containing Path (and
|
|
||||||
other) SVG elements
|
|
||||||
- convert Bézier path segments to **numpy.poly1d** (polynomial) objects
|
|
||||||
- convert polynomials (in standard form) to their Bézier form
|
|
||||||
- compute **tangent vectors** and (right-hand rule) **normal vectors**
|
|
||||||
- compute **curvature**
|
|
||||||
- break discontinuous paths into their **continuous subpaths**.
|
|
||||||
- efficiently compute **intersections** between paths and/or segments
|
|
||||||
- find a **bounding box** for a path or segment
|
|
||||||
- **reverse** segment/path orientation
|
|
||||||
- **crop** and **split** paths and segments
|
|
||||||
- **smooth** paths (i.e. smooth away kinks to make paths
|
|
||||||
differentiable)
|
|
||||||
- **transition maps** from path domain to segment domain and back (T2t
|
|
||||||
and t2T)
|
|
||||||
- compute **area** enclosed by a closed path
|
|
||||||
- compute **arc length**
|
|
||||||
- compute **inverse arc length**
|
|
||||||
- convert RGB color tuples to hexadecimal color strings and back
|
|
||||||
|
|
||||||
Prerequisites
|
|
||||||
-------------
|
|
||||||
|
|
||||||
- **numpy**
|
|
||||||
- **svgwrite**
|
|
||||||
|
|
||||||
Setup
|
|
||||||
-----
|
|
||||||
|
|
||||||
If not already installed, you can **install the prerequisites** using
|
|
||||||
pip.
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
$ pip install numpy
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
$ pip install svgwrite
|
|
||||||
|
|
||||||
Then **install svgpathtools**:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
$ pip install svgpathtools
|
|
||||||
|
|
||||||
Alternative Setup
|
|
||||||
~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
You can download the source from Github and install by using the command
|
|
||||||
(from inside the folder containing setup.py):
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
$ python setup.py install
|
|
||||||
|
|
||||||
Credit where credit's due
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
Much of the core of this module was taken from `the svg.path (v2.0)
|
|
||||||
module <https://github.com/regebro/svg.path>`__. Interested svg.path
|
|
||||||
users should see the compatibility notes at bottom of this readme.
|
|
||||||
|
|
||||||
Basic Usage
|
|
||||||
-----------
|
|
||||||
|
|
||||||
Classes
|
|
||||||
~~~~~~~
|
|
||||||
|
|
||||||
The svgpathtools module is primarily structured around four path segment
|
|
||||||
classes: ``Line``, ``QuadraticBezier``, ``CubicBezier``, and ``Arc``.
|
|
||||||
There is also a fifth class, ``Path``, whose objects are sequences of
|
|
||||||
(connected or disconnected\ `1 <#f1>`__\ ) path segment objects.
|
|
||||||
|
|
||||||
- ``Line(start, end)``
|
|
||||||
|
|
||||||
- ``Arc(start, radius, rotation, large_arc, sweep, end)`` Note: See
|
|
||||||
docstring for a detailed explanation of these parameters
|
|
||||||
|
|
||||||
- ``QuadraticBezier(start, control, end)``
|
|
||||||
|
|
||||||
- ``CubicBezier(start, control1, control2, end)``
|
|
||||||
|
|
||||||
- ``Path(*segments)``
|
|
||||||
|
|
||||||
See the relevant docstrings in *path.py* or the `official SVG
|
|
||||||
specifications <http://www.w3.org/TR/SVG/paths.html>`__ for more
|
|
||||||
information on what each parameter means.
|
|
||||||
|
|
||||||
1 Warning: Some of the functionality in this library has not been tested
|
|
||||||
on discontinuous Path objects. A simple workaround is provided, however,
|
|
||||||
by the ``Path.continuous_subpaths()`` method. `↩ <#a1>`__
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
from __future__ import division, print_function
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Coordinates are given as points in the complex plane
|
|
||||||
from svgpathtools import Path, Line, QuadraticBezier, CubicBezier, Arc
|
|
||||||
seg1 = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j) # A cubic beginning at (300, 100) and ending at (200, 300)
|
|
||||||
seg2 = Line(200+300j, 250+350j) # A line beginning at (200, 300) and ending at (250, 350)
|
|
||||||
path = Path(seg1, seg2) # A path traversing the cubic and then the line
|
|
||||||
|
|
||||||
# We could alternatively created this Path object using a d-string
|
|
||||||
from svgpathtools import parse_path
|
|
||||||
path_alt = parse_path('M 300 100 C 100 100 200 200 200 300 L 250 350')
|
|
||||||
|
|
||||||
# Let's check that these two methods are equivalent
|
|
||||||
print(path)
|
|
||||||
print(path_alt)
|
|
||||||
print(path == path_alt)
|
|
||||||
|
|
||||||
# On a related note, the Path.d() method returns a Path object's d-string
|
|
||||||
print(path.d())
|
|
||||||
print(parse_path(path.d()) == path)
|
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
|
||||||
|
|
||||||
Path(CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j)),
|
|
||||||
Line(start=(200+300j), end=(250+350j)))
|
|
||||||
Path(CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j)),
|
|
||||||
Line(start=(200+300j), end=(250+350j)))
|
|
||||||
True
|
|
||||||
M 300.0,100.0 C 100.0,100.0 200.0,200.0 200.0,300.0 L 250.0,350.0
|
|
||||||
True
|
|
||||||
|
|
||||||
|
|
||||||
The ``Path`` class is a mutable sequence, so it behaves much like a
|
|
||||||
list. So segments can **append**\ ed, **insert**\ ed, set by index,
|
|
||||||
**del**\ eted, **enumerate**\ d, **slice**\ d out, etc.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Let's append another to the end of it
|
|
||||||
path.append(CubicBezier(250+350j, 275+350j, 250+225j, 200+100j))
|
|
||||||
print(path)
|
|
||||||
|
|
||||||
# Let's replace the first segment with a Line object
|
|
||||||
path[0] = Line(200+100j, 200+300j)
|
|
||||||
print(path)
|
|
||||||
|
|
||||||
# You may have noticed that this path is connected and now is also closed (i.e. path.start == path.end)
|
|
||||||
print("path is continuous? ", path.iscontinuous())
|
|
||||||
print("path is closed? ", path.isclosed())
|
|
||||||
|
|
||||||
# The curve the path follows is not, however, smooth (differentiable)
|
|
||||||
from svgpathtools import kinks, smoothed_path
|
|
||||||
print("path contains non-differentiable points? ", len(kinks(path)) > 0)
|
|
||||||
|
|
||||||
# If we want, we can smooth these out (Experimental and only for line/cubic paths)
|
|
||||||
# Note: smoothing will always works (except on 180 degree turns), but you may want
|
|
||||||
# to play with the maxjointsize and tightness parameters to get pleasing results
|
|
||||||
# Note also: smoothing will increase the number of segments in a path
|
|
||||||
spath = smoothed_path(path)
|
|
||||||
print("spath contains non-differentiable points? ", len(kinks(spath)) > 0)
|
|
||||||
print(spath)
|
|
||||||
|
|
||||||
# Let's take a quick look at the path and its smoothed relative
|
|
||||||
# The following commands will open two browser windows to display path and spaths
|
|
||||||
from svgpathtools import disvg
|
|
||||||
from time import sleep
|
|
||||||
disvg(path)
|
|
||||||
sleep(1) # needed when not giving the SVGs unique names (or not using timestamp)
|
|
||||||
disvg(spath)
|
|
||||||
print("Notice that path contains {} segments and spath contains {} segments."
|
|
||||||
"".format(len(path), len(spath)))
|
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
|
||||||
|
|
||||||
Path(CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j)),
|
|
||||||
Line(start=(200+300j), end=(250+350j)),
|
|
||||||
CubicBezier(start=(250+350j), control1=(275+350j), control2=(250+225j), end=(200+100j)))
|
|
||||||
Path(Line(start=(200+100j), end=(200+300j)),
|
|
||||||
Line(start=(200+300j), end=(250+350j)),
|
|
||||||
CubicBezier(start=(250+350j), control1=(275+350j), control2=(250+225j), end=(200+100j)))
|
|
||||||
path is continuous? True
|
|
||||||
path is closed? True
|
|
||||||
path contains non-differentiable points? True
|
|
||||||
spath contains non-differentiable points? False
|
|
||||||
Path(Line(start=(200+101.5j), end=(200+298.5j)),
|
|
||||||
CubicBezier(start=(200+298.5j), control1=(200+298.505j), control2=(201.057124638+301.057124638j), end=(201.060660172+301.060660172j)),
|
|
||||||
Line(start=(201.060660172+301.060660172j), end=(248.939339828+348.939339828j)),
|
|
||||||
CubicBezier(start=(248.939339828+348.939339828j), control1=(249.649982143+349.649982143j), control2=(248.995+350j), end=(250+350j)),
|
|
||||||
CubicBezier(start=(250+350j), control1=(275+350j), control2=(250+225j), end=(200+100j)),
|
|
||||||
CubicBezier(start=(200+100j), control1=(199.62675237+99.0668809257j), control2=(200+100.495j), end=(200+101.5j)))
|
|
||||||
Notice that path contains 3 segments and spath contains 6 segments.
|
|
||||||
|
|
||||||
|
|
||||||
Reading SVGSs
|
|
||||||
~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
| The **svg2paths()** function converts an svgfile to a list of Path
|
|
||||||
objects and a separate list of dictionaries containing the attributes
|
|
||||||
of each said path.
|
|
||||||
| Note: Line, Polyline, Polygon, and Path SVG elements can all be
|
|
||||||
converted to Path objects using this function.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Read SVG into a list of path objects and list of dictionaries of attributes
|
|
||||||
from svgpathtools import svg2paths, wsvg
|
|
||||||
paths, attributes = svg2paths('test.svg')
|
|
||||||
|
|
||||||
# Update: You can now also extract the svg-attributes by setting
|
|
||||||
# return_svg_attributes=True, or with the convenience function svg2paths2
|
|
||||||
from svgpathtools import svg2paths2
|
|
||||||
paths, attributes, svg_attributes = svg2paths2('test.svg')
|
|
||||||
|
|
||||||
# Let's print out the first path object and the color it was in the SVG
|
|
||||||
# We'll see it is composed of two CubicBezier objects and, in the SVG file it
|
|
||||||
# came from, it was red
|
|
||||||
redpath = paths[0]
|
|
||||||
redpath_attribs = attributes[0]
|
|
||||||
print(redpath)
|
|
||||||
print(redpath_attribs['stroke'])
|
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
|
||||||
|
|
||||||
Path(CubicBezier(start=(10.5+80j), control1=(40+10j), control2=(65+10j), end=(95+80j)),
|
|
||||||
CubicBezier(start=(95+80j), control1=(125+150j), control2=(150+150j), end=(180+80j)))
|
|
||||||
red
|
|
||||||
|
|
||||||
|
|
||||||
Writing SVGSs (and some geometric functions and methods)
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
The **wsvg()** function creates an SVG file from a list of path. This
|
|
||||||
function can do many things (see docstring in *paths2svg.py* for more
|
|
||||||
information) and is meant to be quick and easy to use. Note: Use the
|
|
||||||
convenience function **disvg()** (or set 'openinbrowser=True') to
|
|
||||||
automatically attempt to open the created svg file in your default SVG
|
|
||||||
viewer.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Let's make a new SVG that's identical to the first
|
|
||||||
wsvg(paths, attributes=attributes, svg_attributes=svg_attributes, filename='output1.svg')
|
|
||||||
|
|
||||||
.. figure:: https://cdn.rawgit.com/mathandy/svgpathtools/master/output1.svg
|
|
||||||
:alt: output1.svg
|
|
||||||
|
|
||||||
output1.svg
|
|
||||||
|
|
||||||
There will be many more examples of writing and displaying path data
|
|
||||||
below.
|
|
||||||
|
|
||||||
The .point() method and transitioning between path and path segment parameterizations
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
SVG Path elements and their segments have official parameterizations.
|
|
||||||
These parameterizations can be accessed using the ``Path.point()``,
|
|
||||||
``Line.point()``, ``QuadraticBezier.point()``, ``CubicBezier.point()``,
|
|
||||||
and ``Arc.point()`` methods. All these parameterizations are defined
|
|
||||||
over the domain 0 <= t <= 1.
|
|
||||||
|
|
||||||
| **Note:** In this document and in inline documentation and doctrings,
|
|
||||||
I use a capital ``T`` when referring to the parameterization of a Path
|
|
||||||
object and a lower case ``t`` when referring speaking about path
|
|
||||||
segment objects (i.e. Line, QaudraticBezier, CubicBezier, and Arc
|
|
||||||
objects).
|
|
||||||
| Given a ``T`` value, the ``Path.T2t()`` method can be used to find the
|
|
||||||
corresponding segment index, ``k``, and segment parameter, ``t``, such
|
|
||||||
that ``path.point(T)=path[k].point(t)``.
|
|
||||||
| There is also a ``Path.t2T()`` method to solve the inverse problem.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Example:
|
|
||||||
|
|
||||||
# Let's check that the first segment of redpath starts
|
|
||||||
# at the same point as redpath
|
|
||||||
firstseg = redpath[0]
|
|
||||||
print(redpath.point(0) == firstseg.point(0) == redpath.start == firstseg.start)
|
|
||||||
|
|
||||||
# Let's check that the last segment of redpath ends on the same point as redpath
|
|
||||||
lastseg = redpath[-1]
|
|
||||||
print(redpath.point(1) == lastseg.point(1) == redpath.end == lastseg.end)
|
|
||||||
|
|
||||||
# This next boolean should return False as redpath is composed multiple segments
|
|
||||||
print(redpath.point(0.5) == firstseg.point(0.5))
|
|
||||||
|
|
||||||
# If we want to figure out which segment of redpoint the
|
|
||||||
# point redpath.point(0.5) lands on, we can use the path.T2t() method
|
|
||||||
k, t = redpath.T2t(0.5)
|
|
||||||
print(redpath[k].point(t) == redpath.point(0.5))
|
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
|
||||||
|
|
||||||
True
|
|
||||||
True
|
|
||||||
False
|
|
||||||
True
|
|
||||||
|
|
||||||
|
|
||||||
Bezier curves as NumPy polynomial objects
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
| Another great way to work with the parameterizations for ``Line``,
|
|
||||||
``QuadraticBezier``, and ``CubicBezier`` objects is to convert them to
|
|
||||||
``numpy.poly1d`` objects. This is done easily using the
|
|
||||||
``Line.poly()``, ``QuadraticBezier.poly()`` and ``CubicBezier.poly()``
|
|
||||||
methods.
|
|
||||||
| There's also a ``polynomial2bezier()`` function in the pathtools.py
|
|
||||||
submodule to convert polynomials back to Bezier curves.
|
|
||||||
|
|
||||||
**Note:** cubic Bezier curves are parameterized as
|
|
||||||
|
|
||||||
.. math:: \mathcal{B}(t) = P_0(1-t)^3 + 3P_1(1-t)^2t + 3P_2(1-t)t^2 + P_3t^3
|
|
||||||
|
|
||||||
where :math:`P_0`, :math:`P_1`, :math:`P_2`, and :math:`P_3` are the
|
|
||||||
control points ``start``, ``control1``, ``control2``, and ``end``,
|
|
||||||
respectively, that svgpathtools uses to define a CubicBezier object. The
|
|
||||||
``CubicBezier.poly()`` method expands this polynomial to its standard
|
|
||||||
form
|
|
||||||
|
|
||||||
.. math:: \mathcal{B}(t) = c_0t^3 + c_1t^2 +c_2t+c3
|
|
||||||
|
|
||||||
where
|
|
||||||
|
|
||||||
.. math::
|
|
||||||
|
|
||||||
\begin{bmatrix}c_0\\c_1\\c_2\\c_3\end{bmatrix} =
|
|
||||||
\begin{bmatrix}
|
|
||||||
-1 & 3 & -3 & 1\\
|
|
||||||
3 & -6 & -3 & 0\\
|
|
||||||
-3 & 3 & 0 & 0\\
|
|
||||||
1 & 0 & 0 & 0\\
|
|
||||||
\end{bmatrix}
|
|
||||||
\begin{bmatrix}P_0\\P_1\\P_2\\P_3\end{bmatrix}
|
|
||||||
|
|
||||||
``QuadraticBezier.poly()`` and ``Line.poly()`` are `defined
|
|
||||||
similarly <https://en.wikipedia.org/wiki/B%C3%A9zier_curve#General_definition>`__.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Example:
|
|
||||||
b = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j)
|
|
||||||
p = b.poly()
|
|
||||||
|
|
||||||
# p(t) == b.point(t)
|
|
||||||
print(p(0.235) == b.point(0.235))
|
|
||||||
|
|
||||||
# What is p(t)? It's just the cubic b written in standard form.
|
|
||||||
bpretty = "{}*(1-t)^3 + 3*{}*(1-t)^2*t + 3*{}*(1-t)*t^2 + {}*t^3".format(*b.bpoints())
|
|
||||||
print("The CubicBezier, b.point(x) = \n\n" +
|
|
||||||
bpretty + "\n\n" +
|
|
||||||
"can be rewritten in standard form as \n\n" +
|
|
||||||
str(p).replace('x','t'))
|
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
|
||||||
|
|
||||||
True
|
|
||||||
The CubicBezier, b.point(x) =
|
|
||||||
|
|
||||||
(300+100j)*(1-t)^3 + 3*(100+100j)*(1-t)^2*t + 3*(200+200j)*(1-t)*t^2 + (200+300j)*t^3
|
|
||||||
|
|
||||||
can be rewritten in standard form as
|
|
||||||
|
|
||||||
3 2
|
|
||||||
(-400 + -100j) t + (900 + 300j) t - 600 t + (300 + 100j)
|
|
||||||
|
|
||||||
|
|
||||||
The ability to convert between Bezier objects to NumPy polynomial
|
|
||||||
objects is very useful. For starters, we can take turn a list of Bézier
|
|
||||||
segments into a NumPy array
|
|
||||||
|
|
||||||
Numpy Array operations on Bézier path segments
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
`Example available
|
|
||||||
here <https://github.com/mathandy/svgpathtools/blob/master/examples/compute-many-points-quickly-using-numpy-arrays.py>`__
|
|
||||||
|
|
||||||
To further illustrate the power of being able to convert our Bezier
|
|
||||||
curve objects to numpy.poly1d objects and back, lets compute the unit
|
|
||||||
tangent vector of the above CubicBezier object, b, at t=0.5 in four
|
|
||||||
different ways.
|
|
||||||
|
|
||||||
Tangent vectors (and more on NumPy polynomials)
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
t = 0.5
|
|
||||||
### Method 1: the easy way
|
|
||||||
u1 = b.unit_tangent(t)
|
|
||||||
|
|
||||||
### Method 2: another easy way
|
|
||||||
# Note: This way will fail if it encounters a removable singularity.
|
|
||||||
u2 = b.derivative(t)/abs(b.derivative(t))
|
|
||||||
|
|
||||||
### Method 2: a third easy way
|
|
||||||
# Note: This way will also fail if it encounters a removable singularity.
|
|
||||||
dp = p.deriv()
|
|
||||||
u3 = dp(t)/abs(dp(t))
|
|
||||||
|
|
||||||
### Method 4: the removable-singularity-proof numpy.poly1d way
|
|
||||||
# Note: This is roughly how Method 1 works
|
|
||||||
from svgpathtools import real, imag, rational_limit
|
|
||||||
dx, dy = real(dp), imag(dp) # dp == dx + 1j*dy
|
|
||||||
p_mag2 = dx**2 + dy**2 # p_mag2(t) = |p(t)|**2
|
|
||||||
# Note: abs(dp) isn't a polynomial, but abs(dp)**2 is, and,
|
|
||||||
# the limit_{t->t0}[f(t) / abs(f(t))] ==
|
|
||||||
# sqrt(limit_{t->t0}[f(t)**2 / abs(f(t))**2])
|
|
||||||
from cmath import sqrt
|
|
||||||
u4 = sqrt(rational_limit(dp**2, p_mag2, t))
|
|
||||||
|
|
||||||
print("unit tangent check:", u1 == u2 == u3 == u4)
|
|
||||||
|
|
||||||
# Let's do a visual check
|
|
||||||
mag = b.length()/4 # so it's not hard to see the tangent line
|
|
||||||
tangent_line = Line(b.point(t), b.point(t) + mag*u1)
|
|
||||||
disvg([b, tangent_line], 'bg', nodes=[b.point(t)])
|
|
||||||
|
|
||||||
|
|
||||||
.. parsed-literal::
|
|
||||||
|
|
||||||
unit tangent check: True
|
|
||||||
|
|
||||||
|
|
||||||
Translations (shifts), reversing orientation, and normal vectors
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Speaking of tangents, let's add a normal vector to the picture
|
|
||||||
n = b.normal(t)
|
|
||||||
normal_line = Line(b.point(t), b.point(t) + mag*n)
|
|
||||||
disvg([b, tangent_line, normal_line], 'bgp', nodes=[b.point(t)])
|
|
||||||
|
|
||||||
# and let's reverse the orientation of b!
|
|
||||||
# the tangent and normal lines should be sent to their opposites
|
|
||||||
br = b.reversed()
|
|
||||||
|
|
||||||
# Let's also shift b_r over a bit to the right so we can view it next to b
|
|
||||||
# The simplest way to do this is br = br.translated(3*mag), but let's use
|
|
||||||
# the .bpoints() instead, which returns a Bezier's control points
|
|
||||||
br.start, br.control1, br.control2, br.end = [3*mag + bpt for bpt in br.bpoints()] #
|
|
||||||
|
|
||||||
tangent_line_r = Line(br.point(t), br.point(t) + mag*br.unit_tangent(t))
|
|
||||||
normal_line_r = Line(br.point(t), br.point(t) + mag*br.normal(t))
|
|
||||||
wsvg([b, tangent_line, normal_line, br, tangent_line_r, normal_line_r],
|
|
||||||
'bgpkgp', nodes=[b.point(t), br.point(t)], filename='vectorframes.svg',
|
|
||||||
text=["b's tangent", "br's tangent"], text_path=[tangent_line, tangent_line_r])
|
|
||||||
|
|
||||||
.. figure:: https://cdn.rawgit.com/mathandy/svgpathtools/master/vectorframes.svg
|
|
||||||
:alt: vectorframes.svg
|
|
||||||
|
|
||||||
vectorframes.svg
|
|
||||||
|
|
||||||
Rotations and Translations
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Let's take a Line and an Arc and make some pictures
|
|
||||||
top_half = Arc(start=-1, radius=1+2j, rotation=0, large_arc=1, sweep=1, end=1)
|
|
||||||
midline = Line(-1.5, 1.5)
|
|
||||||
|
|
||||||
# First let's make our ellipse whole
|
|
||||||
bottom_half = top_half.rotated(180)
|
|
||||||
decorated_ellipse = Path(top_half, bottom_half)
|
|
||||||
|
|
||||||
# Now let's add the decorations
|
|
||||||
for k in range(12):
|
|
||||||
decorated_ellipse.append(midline.rotated(30*k))
|
|
||||||
|
|
||||||
# Let's move it over so we can see the original Line and Arc object next
|
|
||||||
# to the final product
|
|
||||||
decorated_ellipse = decorated_ellipse.translated(4+0j)
|
|
||||||
wsvg([top_half, midline, decorated_ellipse], filename='decorated_ellipse.svg')
|
|
||||||
|
|
||||||
.. figure:: https://cdn.rawgit.com/mathandy/svgpathtools/master/decorated_ellipse.svg
|
|
||||||
:alt: decorated\_ellipse.svg
|
|
||||||
|
|
||||||
decorated\_ellipse.svg
|
|
||||||
|
|
||||||
arc length and inverse arc length
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Here we'll create an SVG that shows off the parametric and geometric
|
|
||||||
midpoints of the paths from ``test.svg``. We'll need to compute use the
|
|
||||||
``Path.length()``, ``Line.length()``, ``QuadraticBezier.length()``,
|
|
||||||
``CubicBezier.length()``, and ``Arc.length()`` methods, as well as the
|
|
||||||
related inverse arc length methods ``.ilength()`` function to do this.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# First we'll load the path data from the file test.svg
|
|
||||||
paths, attributes = svg2paths('test.svg')
|
|
||||||
|
|
||||||
# Let's mark the parametric midpoint of each segment
|
|
||||||
# I say "parametric" midpoint because Bezier curves aren't
|
|
||||||
# parameterized by arclength
|
|
||||||
# If they're also the geometric midpoint, let's mark them
|
|
||||||
# purple and otherwise we'll mark the geometric midpoint green
|
|
||||||
min_depth = 5
|
|
||||||
error = 1e-4
|
|
||||||
dots = []
|
|
||||||
ncols = []
|
|
||||||
nradii = []
|
|
||||||
for path in paths:
|
|
||||||
for seg in path:
|
|
||||||
parametric_mid = seg.point(0.5)
|
|
||||||
seg_length = seg.length()
|
|
||||||
if seg.length(0.5)/seg.length() == 1/2:
|
|
||||||
dots += [parametric_mid]
|
|
||||||
ncols += ['purple']
|
|
||||||
nradii += [5]
|
|
||||||
else:
|
|
||||||
t_mid = seg.ilength(seg_length/2)
|
|
||||||
geo_mid = seg.point(t_mid)
|
|
||||||
dots += [parametric_mid, geo_mid]
|
|
||||||
ncols += ['red', 'green']
|
|
||||||
nradii += [5] * 2
|
|
||||||
|
|
||||||
# In 'output2.svg' the paths will retain their original attributes
|
|
||||||
wsvg(paths, nodes=dots, node_colors=ncols, node_radii=nradii,
|
|
||||||
attributes=attributes, filename='output2.svg')
|
|
||||||
|
|
||||||
.. figure:: https://cdn.rawgit.com/mathandy/svgpathtools/master/output2.svg
|
|
||||||
:alt: output2.svg
|
|
||||||
|
|
||||||
output2.svg
|
|
||||||
|
|
||||||
Intersections between Bezier curves
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
# Let's find all intersections between redpath and the other
|
|
||||||
redpath = paths[0]
|
|
||||||
redpath_attribs = attributes[0]
|
|
||||||
intersections = []
|
|
||||||
for path in paths[1:]:
|
|
||||||
for (T1, seg1, t1), (T2, seg2, t2) in redpath.intersect(path):
|
|
||||||
intersections.append(redpath.point(T1))
|
|
||||||
|
|
||||||
disvg(paths, filename='output_intersections.svg', attributes=attributes,
|
|
||||||
nodes = intersections, node_radii = [5]*len(intersections))
|
|
||||||
|
|
||||||
.. figure:: https://cdn.rawgit.com/mathandy/svgpathtools/master/output_intersections.svg
|
|
||||||
:alt: output\_intersections.svg
|
|
||||||
|
|
||||||
output\_intersections.svg
|
|
||||||
|
|
||||||
An Advanced Application: Offsetting Paths
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Here we'll find the `offset
|
|
||||||
curve <https://en.wikipedia.org/wiki/Parallel_curve>`__ for a few paths.
|
|
||||||
|
|
||||||
.. code:: ipython2
|
|
||||||
|
|
||||||
from svgpathtools import parse_path, Line, Path, wsvg
|
|
||||||
def offset_curve(path, offset_distance, steps=1000):
|
|
||||||
"""Takes in a Path object, `path`, and a distance,
|
|
||||||
`offset_distance`, and outputs an piecewise-linear approximation
|
|
||||||
of the 'parallel' offset curve."""
|
|
||||||
nls = []
|
|
||||||
for seg in path:
|
|
||||||
for k in range(steps):
|
|
||||||
t = k / float(steps)
|
|
||||||
offset_vector = offset_distance * seg.normal(t)
|
|
||||||
nl = Line(seg.point(t), seg.point(t) + offset_vector)
|
|
||||||
nls.append(nl)
|
|
||||||
connect_the_dots = [Line(nls[k].end, nls[k+1].end) for k in range(len(nls)-1)]
|
|
||||||
if path.isclosed():
|
|
||||||
connect_the_dots.append(Line(nls[-1].end, nls[0].end))
|
|
||||||
offset_path = Path(*connect_the_dots)
|
|
||||||
return offset_path
|
|
||||||
|
|
||||||
# Examples:
|
|
||||||
path1 = parse_path("m 288,600 c -52,-28 -42,-61 0,-97 ")
|
|
||||||
path2 = parse_path("M 151,395 C 407,485 726.17662,160 634,339").translated(300)
|
|
||||||
path3 = parse_path("m 117,695 c 237,-7 -103,-146 457,0").translated(500+400j)
|
|
||||||
paths = [path1, path2, path3]
|
|
||||||
|
|
||||||
offset_distances = [10*k for k in range(1,51)]
|
|
||||||
offset_paths = []
|
|
||||||
for path in paths:
|
|
||||||
for distances in offset_distances:
|
|
||||||
offset_paths.append(offset_curve(path, distances))
|
|
||||||
|
|
||||||
# Note: This will take a few moments
|
|
||||||
wsvg(paths + offset_paths, 'g'*len(paths) + 'r'*len(offset_paths), filename='offset_curves.svg')
|
|
||||||
|
|
||||||
.. figure:: https://cdn.rawgit.com/mathandy/svgpathtools/master/offset_curves.svg
|
|
||||||
:alt: offset\_curves.svg
|
|
||||||
|
|
||||||
offset\_curves.svg
|
|
||||||
|
|
||||||
Compatibility Notes for users of svg.path (v2.0)
|
|
||||||
------------------------------------------------
|
|
||||||
|
|
||||||
- renamed Arc.arc attribute as Arc.large\_arc
|
|
||||||
|
|
||||||
- Path.d() : For behavior similar\ `2 <#f2>`__\ to svg.path (v2.0),
|
|
||||||
set both useSandT and use\_closed\_attrib to be True.
|
|
||||||
|
|
||||||
2 The behavior would be identical, but the string formatting used in
|
|
||||||
this method has been changed to use default format (instead of the
|
|
||||||
General format, {:G}), for inceased precision. `↩ <#a2>`__
|
|
||||||
|
|
||||||
Licence
|
|
||||||
-------
|
|
||||||
|
|
||||||
This module is under a MIT License.
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Security Policy
|
||||||
|
|
||||||
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
To report any security vulnerability, email andyaport@gmail.com
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?xml version="1.0" ?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink" baseProfile="full" height="50px" version="1.1" viewBox="-15.075 -5.075 180.15 60.15" width="150px">
|
||||||
|
<defs>
|
||||||
|
<path d="M 10.0,24.0 L 140.0,24.0" id="tp0"/>
|
||||||
|
<path d="M 10.0,40.0 L 140.0,40.0" id="tp1"/>
|
||||||
|
</defs>
|
||||||
|
<a href="https://www.paypal.com/donate?business=4SKJ27AM4EYYA&no_recurring=0&item_name=Support+the+creator+of+svgpathtools?++He%27s+a+student+and+would+appreciate+it.&currency_code=USD">
|
||||||
|
<path d="M 0.0,25.0 C 0.0,0.0 0.0,0.0 75.0,0.0 C 150.0,0.0 150.0,0.0 150.0,25.0 C 150.0,50.0 150.0,50.0 75.0,50.0 C 0.0,50.0 0.0,50.0 0.0,25.0" fill="#34eb86" stroke="#000000" stroke-width="0.15"/>
|
||||||
|
<text font-size="15" font-weight="bold">
|
||||||
|
<textPath startOffset="50%" text-anchor="middle" xlink:href="#tp0">Donate to the creator</textPath>
|
||||||
|
</text>
|
||||||
|
<text font-size="16">
|
||||||
|
<textPath startOffset="50%" text-anchor="middle" xlink:href="#tp1">(He's a student.)</textPath>
|
||||||
|
</text>
|
||||||
|
</a>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 1.0 KiB |
|
@ -8,7 +8,7 @@ Note: The relevant matrix transformation for quadratics can be found in the
|
||||||
svgpathtools.bezier module."""
|
svgpathtools.bezier module."""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from svgpathtools import *
|
from svgpathtools import bezier_point, Path, bpoints2bezier, polynomial2bezier
|
||||||
|
|
||||||
|
|
||||||
class HigherOrderBezier:
|
class HigherOrderBezier:
|
||||||
|
|
|
@ -7,7 +7,8 @@ Path.continuous_subpaths() method to split a paths into a list of its
|
||||||
continuous subpaths.
|
continuous subpaths.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from svgpathtools import *
|
from svgpathtools import Path, Line
|
||||||
|
|
||||||
|
|
||||||
def path1_is_contained_in_path2(path1, path2):
|
def path1_is_contained_in_path2(path1, path2):
|
||||||
assert path2.isclosed() # This question isn't well-defined otherwise
|
assert path2.isclosed() # This question isn't well-defined otherwise
|
||||||
|
@ -16,11 +17,11 @@ def path1_is_contained_in_path2(path1, path2):
|
||||||
|
|
||||||
# find a point that's definitely outside path2
|
# find a point that's definitely outside path2
|
||||||
xmin, xmax, ymin, ymax = path2.bbox()
|
xmin, xmax, ymin, ymax = path2.bbox()
|
||||||
B = (xmin + 1) + 1j*(ymax + 1)
|
b = (xmin + 1) + 1j*(ymax + 1)
|
||||||
|
|
||||||
A = path1.start # pick an arbitrary point in path1
|
a = path1.start # pick an arbitrary point in path1
|
||||||
AB_line = Path(Line(A, B))
|
ab_line = Path(Line(a, b))
|
||||||
number_of_intersections = len(AB_line.intersect(path2))
|
number_of_intersections = len(ab_line.intersect(path2))
|
||||||
if number_of_intersections % 2: # if number of intersections is odd
|
if number_of_intersections % 2: # if number of intersections is odd
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
from svgpathtools import *
|
from svgpathtools import disvg, Line, CubicBezier
|
||||||
|
from scipy.optimize import fminbound
|
||||||
|
|
||||||
# create some example paths
|
# create some example paths
|
||||||
path1 = CubicBezier(1,2+3j,3-5j,4+1j)
|
path1 = CubicBezier(1,2+3j,3-5j,4+1j)
|
||||||
path2 = path1.rotated(60).translated(3)
|
path2 = path1.rotated(60).translated(3)
|
||||||
|
|
||||||
# find minimizer
|
|
||||||
from scipy.optimize import fminbound
|
|
||||||
def dist(t):
|
def dist(t):
|
||||||
return path1.radialrange(path2.point(t))[0][0]
|
return path1.radialrange(path2.point(t))[0][0]
|
||||||
|
|
||||||
|
|
||||||
|
# find minimizer
|
||||||
T2 = fminbound(dist, 0, 1)
|
T2 = fminbound(dist, 0, 1)
|
||||||
|
|
||||||
# Let's do a visual check
|
# Let's do a visual check
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<script src="https://cdn.jsdelivr.net/pyodide/v0.18.1/full/pyodide.js"></script>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>svgpathtools in JS!</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<button id="go_button" onclick="tick()" hidden>Click Me!</button>
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
<div>Output:</div>
|
||||||
|
<label for="output"></label>
|
||||||
|
<textarea id="output" style="width: 100%;" rows="6" disabled></textarea>
|
||||||
|
|
||||||
|
<svg height="100" width="100">
|
||||||
|
<circle cx="50" cy="50" r="40" stroke-width="2" stroke="black" fill="blue"/>
|
||||||
|
<path id="ticker" d="M 50 50 L 50 15" stroke-width="2" stroke="black"/>
|
||||||
|
<circle cx="50" cy="50" r="3" stroke-width="2" stroke="black" fill="green"/>
|
||||||
|
Sorry, your browser does not support inline SVG.
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// init Pyodide environment and install svgpathtools
|
||||||
|
async function main() {
|
||||||
|
let pyodide = await loadPyodide({
|
||||||
|
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/",
|
||||||
|
});
|
||||||
|
await pyodide.loadPackage("micropip");
|
||||||
|
pyodide.runPythonAsync(`
|
||||||
|
import micropip
|
||||||
|
await micropip.install('svgpathtools')
|
||||||
|
`);
|
||||||
|
output.value += "svgpathtools is ready!\n";
|
||||||
|
return pyodide;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function tick() {
|
||||||
|
let clock_hand = document.getElementById("ticker");
|
||||||
|
let pyodide = await pyodideReadyPromise;
|
||||||
|
try {
|
||||||
|
let result = pyodide.runPython(`
|
||||||
|
from svgpathtools import parse_path
|
||||||
|
parse_path('${clock_hand.getAttribute('d')}').rotated(45, origin=50+50j).d()
|
||||||
|
`);
|
||||||
|
clock_hand.setAttribute('d', result);
|
||||||
|
} catch (err) {
|
||||||
|
output.value += err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pyodideReadyPromise = main();
|
||||||
|
$(document).ready(function(){
|
||||||
|
const output = document.getElementById("output");
|
||||||
|
output.value = "Initializing...\n";
|
||||||
|
document.getElementById("go_button").removeAttribute('hidden');
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,22 @@
|
||||||
|
<svg version="1.1"
|
||||||
|
baseProfile="full"
|
||||||
|
width="1000" height="1000"
|
||||||
|
xmlns="http://www.w3.org/2000/svg">
|
||||||
|
|
||||||
|
<path d="M50,50 A 40 40 0 1 0 100 100Z" stroke="blue" stroke-width="4" fill="yellow"/>
|
||||||
|
<circle cx="50" cy="50" r="5" stroke="green" stroke-width="1" fill="none"/>
|
||||||
|
<circle cx="100" cy="100" r="5" stroke="red" stroke-width="1" fill="none"/>
|
||||||
|
|
||||||
|
<path d="M150,150 A 0 40 0 1 0 200 200Z" stroke="beige" stroke-width="4" fill="yellow"/>
|
||||||
|
<circle cx="150" cy="150" r="5" stroke="green" stroke-width="1" fill="none"/>
|
||||||
|
<circle cx="200" cy="200" r="5" stroke="red" stroke-width="1" fill="none"/>
|
||||||
|
|
||||||
|
<path d="M250,250 A 40 0 0 1 0 300 300Z" stroke="purple" stroke-width="4" fill="yellow"/>
|
||||||
|
<circle cx="250" cy="250" r="5" stroke="green" stroke-width="1" fill="none"/>
|
||||||
|
<circle cx="300" cy="300" r="5" stroke="red" stroke-width="1" fill="none"/>
|
||||||
|
|
||||||
|
<path d="M350,350 A 0 0 0 1 0 400 400Z" stroke="orange" stroke-width="4" fill="yellow"/>
|
||||||
|
<circle cx="350" cy="350" r="5" stroke="green" stroke-width="1" fill="none"/>
|
||||||
|
<circle cx="400" cy="400" r="5" stroke="red" stroke-width="1" fill="none"/>
|
||||||
|
</svg>
|
||||||
|
|
After Width: | Height: | Size: 1.1 KiB |
|
@ -0,0 +1,3 @@
|
||||||
|
numpy
|
||||||
|
svgwrite
|
||||||
|
scipy
|
37
setup.py
|
@ -3,18 +3,17 @@ import codecs
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
VERSION = '1.3.3'
|
VERSION = '1.6.1'
|
||||||
AUTHOR_NAME = 'Andy Port'
|
AUTHOR_NAME = 'Andy Port'
|
||||||
AUTHOR_EMAIL = 'AndyAPort@gmail.com'
|
AUTHOR_EMAIL = 'AndyAPort@gmail.com'
|
||||||
|
GITHUB = 'https://github.com/mathandy/svgpathtools'
|
||||||
|
|
||||||
|
_here = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
def read(*parts):
|
def read(relative_path):
|
||||||
"""
|
"""Reads file at relative path, returning contents as string."""
|
||||||
Build an absolute path from *parts* and and return the contents of the
|
with codecs.open(os.path.join(_here, relative_path), "rb", "utf-8") as f:
|
||||||
resulting file. Assume UTF-8 encoding.
|
|
||||||
"""
|
|
||||||
HERE = os.path.abspath(os.path.dirname(__file__))
|
|
||||||
with codecs.open(os.path.join(HERE, *parts), "rb", "utf-8") as f:
|
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,18 +22,16 @@ setup(name='svgpathtools',
|
||||||
version=VERSION,
|
version=VERSION,
|
||||||
description=('A collection of tools for manipulating and analyzing SVG '
|
description=('A collection of tools for manipulating and analyzing SVG '
|
||||||
'Path objects and Bezier curves.'),
|
'Path objects and Bezier curves.'),
|
||||||
long_description=read("README.rst"),
|
long_description=read("README.md"),
|
||||||
# long_description=open('README.rst').read(),
|
long_description_content_type='text/markdown',
|
||||||
author=AUTHOR_NAME,
|
author=AUTHOR_NAME,
|
||||||
author_email=AUTHOR_EMAIL,
|
author_email=AUTHOR_EMAIL,
|
||||||
url='https://github.com/mathandy/svgpathtools',
|
url=GITHUB,
|
||||||
# download_url = 'http://github.com/mathandy/svgpathtools/tarball/'+VERSION,
|
download_url='{}/releases/download/{}/svgpathtools-{}-py2.py3-none-any.whl'
|
||||||
|
''.format(GITHUB, VERSION, VERSION),
|
||||||
license='MIT',
|
license='MIT',
|
||||||
|
install_requires=['numpy', 'svgwrite', 'scipy'],
|
||||||
install_requires=['numpy', 'svgwrite'],
|
|
||||||
platforms="OS Independent",
|
platforms="OS Independent",
|
||||||
# test_suite='tests',
|
|
||||||
requires=['numpy', 'svgwrite'],
|
|
||||||
keywords=['svg', 'svg path', 'svg.path', 'bezier', 'parse svg path', 'display svg'],
|
keywords=['svg', 'svg path', 'svg.path', 'bezier', 'parse svg path', 'display svg'],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"Development Status :: 4 - Beta",
|
"Development Status :: 4 - Beta",
|
||||||
|
@ -43,6 +40,14 @@ setup(name='svgpathtools',
|
||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
"Programming Language :: Python :: 2",
|
"Programming Language :: Python :: 2",
|
||||||
"Programming Language :: Python :: 3",
|
"Programming Language :: Python :: 3",
|
||||||
|
"Programming Language :: Python :: 2.7",
|
||||||
|
"Programming Language :: Python :: 3.5",
|
||||||
|
"Programming Language :: Python :: 3.6",
|
||||||
|
"Programming Language :: Python :: 3.7",
|
||||||
|
"Programming Language :: Python :: 3.8",
|
||||||
|
"Programming Language :: Python :: 3.9",
|
||||||
|
"Programming Language :: Python :: 3.10",
|
||||||
|
"Programming Language :: Python :: 3.11",
|
||||||
"Topic :: Multimedia :: Graphics :: Editors :: Vector-Based",
|
"Topic :: Multimedia :: Graphics :: Editors :: Vector-Based",
|
||||||
"Topic :: Scientific/Engineering",
|
"Topic :: Scientific/Engineering",
|
||||||
"Topic :: Scientific/Engineering :: Image Recognition",
|
"Topic :: Scientific/Engineering :: Image Recognition",
|
||||||
|
|
|
@ -8,13 +8,15 @@ from .path import (Path, Line, QuadraticBezier, CubicBezier, Arc,
|
||||||
closest_point_in_path, farthest_point_in_path,
|
closest_point_in_path, farthest_point_in_path,
|
||||||
path_encloses_pt, bbox2path, polygon, polyline)
|
path_encloses_pt, bbox2path, polygon, polyline)
|
||||||
from .parser import parse_path
|
from .parser import parse_path
|
||||||
from .paths2svg import disvg, wsvg
|
from .paths2svg import disvg, wsvg, paths2Drawing
|
||||||
from .polytools import polyroots, polyroots01, rational_limit, real, imag
|
from .polytools import polyroots, polyroots01, rational_limit, real, imag
|
||||||
from .misctools import hex2rgb, rgb2hex
|
from .misctools import hex2rgb, rgb2hex
|
||||||
from .smoothing import smoothed_path, smoothed_joint, is_differentiable, kinks
|
from .smoothing import smoothed_path, smoothed_joint, is_differentiable, kinks
|
||||||
from .document import Document, CONVERSIONS, CONVERT_ONLY_PATHS, SVG_GROUP_TAG, SVG_NAMESPACE
|
from .document import (Document, CONVERSIONS, CONVERT_ONLY_PATHS,
|
||||||
|
SVG_GROUP_TAG, SVG_NAMESPACE)
|
||||||
|
from .svg_io_sax import SaxDocument
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .svg_to_paths import svg2paths, svg2paths2
|
from .svg_to_paths import svg2paths, svg2paths2, svgstr2paths
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -13,17 +13,16 @@ An Historic Note:
|
||||||
Example:
|
Example:
|
||||||
Typical usage looks something like the following.
|
Typical usage looks something like the following.
|
||||||
|
|
||||||
>> from svgpathtools import *
|
>> from svgpathtools import Document
|
||||||
>> doc = Document('my_file.html')
|
>> doc = Document('my_file.html')
|
||||||
>> results = doc.flatten_all_paths()
|
>> for path in doc.paths():
|
||||||
>> for result in results:
|
|
||||||
>> path = result.path
|
|
||||||
>> # Do something with the transformed Path object.
|
>> # Do something with the transformed Path object.
|
||||||
>> element = result.element
|
>> foo(path)
|
||||||
>> # Inspect the raw SVG element. This gives access to the
|
>> # Inspect the raw SVG element, e.g. change its attributes
|
||||||
>> # path's attributes
|
>> foo(path.element)
|
||||||
>> transform = result.transform
|
>> transform = result.transform
|
||||||
>> # Use the transform that was applied to the path.
|
>> # Use the transform that was applied to the path.
|
||||||
|
>> foo(path.transform)
|
||||||
>> foo(doc.tree) # do stuff using ElementTree's functionality
|
>> foo(doc.tree) # do stuff using ElementTree's functionality
|
||||||
>> doc.display() # display doc in OS's default application
|
>> doc.display() # display doc in OS's default application
|
||||||
>> doc.save('my_new_file.html')
|
>> doc.save('my_new_file.html')
|
||||||
|
@ -40,7 +39,12 @@ import os
|
||||||
import collections
|
import collections
|
||||||
import xml.etree.ElementTree as etree
|
import xml.etree.ElementTree as etree
|
||||||
from xml.etree.ElementTree import Element, SubElement, register_namespace
|
from xml.etree.ElementTree import Element, SubElement, register_namespace
|
||||||
|
from xml.dom.minidom import parseString
|
||||||
import warnings
|
import warnings
|
||||||
|
from io import StringIO
|
||||||
|
from tempfile import gettempdir
|
||||||
|
from time import time
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
# Internal dependencies
|
# Internal dependencies
|
||||||
from .parser import parse_path
|
from .parser import parse_path
|
||||||
|
@ -48,13 +52,17 @@ from .parser import parse_transform
|
||||||
from .svg_to_paths import (path2pathd, ellipse2pathd, line2pathd,
|
from .svg_to_paths import (path2pathd, ellipse2pathd, line2pathd,
|
||||||
polyline2pathd, polygon2pathd, rect2pathd)
|
polyline2pathd, polygon2pathd, rect2pathd)
|
||||||
from .misctools import open_in_browser
|
from .misctools import open_in_browser
|
||||||
from .path import *
|
from .path import transform, Path, is_path_segment
|
||||||
|
|
||||||
# To maintain forward/backward compatibility
|
# To maintain forward/backward compatibility
|
||||||
try:
|
try:
|
||||||
str = basestring
|
string = basestring
|
||||||
except NameError:
|
except NameError:
|
||||||
pass
|
string = str
|
||||||
|
try:
|
||||||
|
from os import PathLike
|
||||||
|
except ImportError:
|
||||||
|
PathLike = string
|
||||||
|
|
||||||
# Let xml.etree.ElementTree know about the SVG namespace
|
# Let xml.etree.ElementTree know about the SVG namespace
|
||||||
SVG_NAMESPACE = {'svg': 'http://www.w3.org/2000/svg'}
|
SVG_NAMESPACE = {'svg': 'http://www.w3.org/2000/svg'}
|
||||||
|
@ -74,7 +82,7 @@ CONVERT_ONLY_PATHS = {'path': path2pathd}
|
||||||
SVG_GROUP_TAG = 'svg:g'
|
SVG_GROUP_TAG = 'svg:g'
|
||||||
|
|
||||||
|
|
||||||
def flatten_all_paths(group, group_filter=lambda x: True,
|
def flattened_paths(group, group_filter=lambda x: True,
|
||||||
path_filter=lambda x: True, path_conversions=CONVERSIONS,
|
path_filter=lambda x: True, path_conversions=CONVERSIONS,
|
||||||
group_search_xpath=SVG_GROUP_TAG):
|
group_search_xpath=SVG_GROUP_TAG):
|
||||||
"""Returns the paths inside a group (recursively), expressing the
|
"""Returns the paths inside a group (recursively), expressing the
|
||||||
|
@ -83,7 +91,7 @@ def flatten_all_paths(group, group_filter=lambda x: True,
|
||||||
Note that if the group being passed in is nested inside some parent
|
Note that if the group being passed in is nested inside some parent
|
||||||
group(s), we cannot take the parent group(s) into account, because
|
group(s), we cannot take the parent group(s) into account, because
|
||||||
xml.etree.Element has no pointer to its parent. You should use
|
xml.etree.Element has no pointer to its parent. You should use
|
||||||
Document.flatten_group(group) to flatten a specific nested group into
|
Document.flattened_paths_from_group(group) to flatten a specific nested group into
|
||||||
the root coordinates.
|
the root coordinates.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -101,6 +109,8 @@ def flatten_all_paths(group, group_filter=lambda x: True,
|
||||||
|
|
||||||
# Stop right away if the group_selector rejects this group
|
# Stop right away if the group_selector rejects this group
|
||||||
if not group_filter(group):
|
if not group_filter(group):
|
||||||
|
warnings.warn('The input group [{}] (id attribute: {}) was rejected by the group filter'
|
||||||
|
.format(group, group.get('id')))
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# To handle the transforms efficiently, we'll traverse the tree of
|
# To handle the transforms efficiently, we'll traverse the tree of
|
||||||
|
@ -124,10 +134,7 @@ def flatten_all_paths(group, group_filter=lambda x: True,
|
||||||
|
|
||||||
stack = [new_stack_element(group, np.identity(3))]
|
stack = [new_stack_element(group, np.identity(3))]
|
||||||
|
|
||||||
FlattenedPath = collections.namedtuple('FlattenedPath',
|
|
||||||
['path', 'element', 'transform'])
|
|
||||||
paths = []
|
paths = []
|
||||||
|
|
||||||
while stack:
|
while stack:
|
||||||
top = stack.pop()
|
top = stack.pop()
|
||||||
|
|
||||||
|
@ -140,15 +147,18 @@ def flatten_all_paths(group, group_filter=lambda x: True,
|
||||||
path_tf = top.transform.dot(
|
path_tf = top.transform.dot(
|
||||||
parse_transform(path_elem.get('transform')))
|
parse_transform(path_elem.get('transform')))
|
||||||
path = transform(parse_path(converter(path_elem)), path_tf)
|
path = transform(parse_path(converter(path_elem)), path_tf)
|
||||||
paths.append(FlattenedPath(path, path_elem, path_tf))
|
path.element = path_elem
|
||||||
|
path.transform = path_tf
|
||||||
|
paths.append(path)
|
||||||
|
|
||||||
stack.extend(get_relevant_children(top.group, top.transform))
|
stack.extend(get_relevant_children(top.group, top.transform))
|
||||||
|
|
||||||
return paths
|
return paths
|
||||||
|
|
||||||
|
|
||||||
def flatten_group(group_to_flatten, root, recursive=True,
|
def flattened_paths_from_group(group_to_flatten, root, recursive=True,
|
||||||
group_filter=lambda x: True, path_filter=lambda x: True,
|
group_filter=lambda x: True,
|
||||||
|
path_filter=lambda x: True,
|
||||||
path_conversions=CONVERSIONS,
|
path_conversions=CONVERSIONS,
|
||||||
group_search_xpath=SVG_GROUP_TAG):
|
group_search_xpath=SVG_GROUP_TAG):
|
||||||
"""Flatten all the paths in a specific group.
|
"""Flatten all the paths in a specific group.
|
||||||
|
@ -174,15 +184,53 @@ def flatten_group(group_to_flatten, root, recursive=True,
|
||||||
else:
|
else:
|
||||||
desired_groups.add(id(group_to_flatten))
|
desired_groups.add(id(group_to_flatten))
|
||||||
|
|
||||||
|
ignore_paths = set()
|
||||||
|
# Use breadth-first search to find the path to the group that we care about
|
||||||
|
if root is not group_to_flatten:
|
||||||
|
search = [[root]]
|
||||||
|
route = None
|
||||||
|
while search:
|
||||||
|
top = search.pop(0)
|
||||||
|
frontier = top[-1]
|
||||||
|
for child in frontier.iterfind(group_search_xpath, SVG_NAMESPACE):
|
||||||
|
if child is group_to_flatten:
|
||||||
|
route = top
|
||||||
|
break
|
||||||
|
future_top = list(top)
|
||||||
|
future_top.append(child)
|
||||||
|
search.append(future_top)
|
||||||
|
|
||||||
|
if route is not None:
|
||||||
|
for group in route:
|
||||||
|
# Add each group from the root to the parent of the desired group
|
||||||
|
# to the list of groups that we should traverse. This makes sure
|
||||||
|
# that paths will not stop before reaching the desired
|
||||||
|
# group.
|
||||||
|
desired_groups.add(id(group))
|
||||||
|
for key in path_conversions.keys():
|
||||||
|
for path_elem in group.iterfind('svg:'+key, SVG_NAMESPACE):
|
||||||
|
# Add each path in the parent groups to the list of paths
|
||||||
|
# that should be ignored. The user has not requested to
|
||||||
|
# flatten the paths of the parent groups, so we should not
|
||||||
|
# include any of these in the result.
|
||||||
|
ignore_paths.add(id(path_elem))
|
||||||
|
break
|
||||||
|
|
||||||
|
if route is None:
|
||||||
|
raise ValueError('The group_to_flatten is not a descendant of the root!')
|
||||||
|
|
||||||
def desired_group_filter(x):
|
def desired_group_filter(x):
|
||||||
return (id(x) in desired_groups) and group_filter(x)
|
return (id(x) in desired_groups) and group_filter(x)
|
||||||
|
|
||||||
return flatten_all_paths(root, desired_group_filter, path_filter,
|
def desired_path_filter(x):
|
||||||
|
return (id(x) not in ignore_paths) and path_filter(x)
|
||||||
|
|
||||||
|
return flattened_paths(root, desired_group_filter, desired_path_filter,
|
||||||
path_conversions, group_search_xpath)
|
path_conversions, group_search_xpath)
|
||||||
|
|
||||||
|
|
||||||
class Document:
|
class Document:
|
||||||
def __init__(self, filename):
|
def __init__(self, filepath=None):
|
||||||
"""A container for a DOM-style SVG document.
|
"""A container for a DOM-style SVG document.
|
||||||
|
|
||||||
The `Document` class provides a simple interface to modify and analyze
|
The `Document` class provides a simple interface to modify and analyze
|
||||||
|
@ -190,47 +238,61 @@ class Document:
|
||||||
parsed into an ElementTree object (stored in the `tree` attribute).
|
parsed into an ElementTree object (stored in the `tree` attribute).
|
||||||
|
|
||||||
This class provides functions for extracting SVG data into Path objects.
|
This class provides functions for extracting SVG data into Path objects.
|
||||||
The Path output objects will be transformed based on their parent groups.
|
The output Path objects will be transformed based on their parent groups.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename (str): The filename of the DOM-style object.
|
filepath (str or file-like): The filepath of the
|
||||||
|
DOM-style object or a file-like object containing it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# remember location of original svg file
|
# strings are interpreted as file location everything else is treated as
|
||||||
if filename is not None and os.path.dirname(filename) == '':
|
# file-like object and passed to the xml parser directly
|
||||||
self.original_filename = os.path.join(os.getcwd(), filename)
|
from_filepath = isinstance(filepath, string) or isinstance(filepath, PathLike)
|
||||||
else:
|
self.original_filepath = os.path.abspath(filepath) if from_filepath else None
|
||||||
self.original_filename = filename
|
|
||||||
|
|
||||||
if filename is not None:
|
if filepath is None:
|
||||||
# parse svg to ElementTree object
|
|
||||||
self.tree = etree.parse(filename)
|
|
||||||
else:
|
|
||||||
self.tree = etree.ElementTree(Element('svg'))
|
self.tree = etree.ElementTree(Element('svg'))
|
||||||
|
else:
|
||||||
|
# parse svg to ElementTree object
|
||||||
|
self.tree = etree.parse(filepath)
|
||||||
|
|
||||||
self.root = self.tree.getroot()
|
self.root = self.tree.getroot()
|
||||||
|
|
||||||
def flatten_all_paths(self, group_filter=lambda x: True,
|
@classmethod
|
||||||
path_filter=lambda x: True,
|
def from_svg_string(cls, svg_string):
|
||||||
path_conversions=CONVERSIONS):
|
"""Constructor for creating a Document object from a string."""
|
||||||
"""Forward the tree of this document into the more general
|
# wrap string into StringIO object
|
||||||
flatten_all_paths function and return the result."""
|
svg_file_obj = StringIO(svg_string)
|
||||||
return flatten_all_paths(self.tree.getroot(), group_filter,
|
# create document from file object
|
||||||
|
return Document(svg_file_obj)
|
||||||
|
|
||||||
|
def paths(self, group_filter=lambda x: True,
|
||||||
|
path_filter=lambda x: True, path_conversions=CONVERSIONS):
|
||||||
|
"""Returns a list of all paths in the document.
|
||||||
|
|
||||||
|
Note that any transform attributes are applied before returning
|
||||||
|
the paths.
|
||||||
|
"""
|
||||||
|
return flattened_paths(self.tree.getroot(), group_filter,
|
||||||
path_filter, path_conversions)
|
path_filter, path_conversions)
|
||||||
|
|
||||||
def flatten_group(self, group, recursive=True, group_filter=lambda x: True,
|
def paths_from_group(self, group, recursive=True, group_filter=lambda x: True,
|
||||||
path_filter=lambda x: True, path_conversions=CONVERSIONS):
|
path_filter=lambda x: True, path_conversions=CONVERSIONS):
|
||||||
if all(isinstance(s, str) for s in group):
|
if all(isinstance(s, string) for s in group):
|
||||||
# If we're given a list of strings, assume it represents a
|
# If we're given a list of strings, assume it represents a
|
||||||
# nested sequence
|
# nested sequence
|
||||||
group = self.get_or_add_group(group)
|
group = self.get_group(group)
|
||||||
elif not isinstance(group, Element):
|
elif not isinstance(group, Element):
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
'Must provide a list of strings that represent a nested '
|
'Must provide a list of strings that represent a nested '
|
||||||
'group name, or provide an xml.etree.Element object. '
|
'group name, or provide an xml.etree.Element object. '
|
||||||
'Instead you provided {0}'.format(group))
|
'Instead you provided {0}'.format(group))
|
||||||
|
|
||||||
return flatten_group(group, self.tree.getroot(), recursive,
|
if group is None:
|
||||||
|
warnings.warn("Could not find the requested group!")
|
||||||
|
return []
|
||||||
|
|
||||||
|
return flattened_paths_from_group(group, self.tree.getroot(), recursive,
|
||||||
group_filter, path_filter, path_conversions)
|
group_filter, path_filter, path_conversions)
|
||||||
|
|
||||||
def add_path(self, path, attribs=None, group=None):
|
def add_path(self, path, attribs=None, group=None):
|
||||||
|
@ -242,7 +304,7 @@ class Document:
|
||||||
|
|
||||||
# If given a list of strings (one or more), assume it represents
|
# If given a list of strings (one or more), assume it represents
|
||||||
# a sequence of nested group names
|
# a sequence of nested group names
|
||||||
elif all(isinstance(elem, str) for elem in group):
|
elif len(group) > 0 and all(isinstance(elem, str) for elem in group):
|
||||||
group = self.get_or_add_group(group)
|
group = self.get_or_add_group(group)
|
||||||
|
|
||||||
elif not isinstance(group, Element):
|
elif not isinstance(group, Element):
|
||||||
|
@ -261,7 +323,7 @@ class Document:
|
||||||
path_svg = path.d()
|
path_svg = path.d()
|
||||||
elif is_path_segment(path):
|
elif is_path_segment(path):
|
||||||
path_svg = Path(path).d()
|
path_svg = Path(path).d()
|
||||||
elif isinstance(path, str):
|
elif isinstance(path, string):
|
||||||
# Assume this is a valid d-string.
|
# Assume this is a valid d-string.
|
||||||
# TODO: Should we sanity check the input string?
|
# TODO: Should we sanity check the input string?
|
||||||
path_svg = path
|
path_svg = path
|
||||||
|
@ -282,6 +344,37 @@ class Document:
|
||||||
def contains_group(self, group):
|
def contains_group(self, group):
|
||||||
return any(group is owned for owned in self.tree.iter())
|
return any(group is owned for owned in self.tree.iter())
|
||||||
|
|
||||||
|
def get_group(self, nested_names, name_attr='id'):
|
||||||
|
"""Get a group from the tree, or None if the requested group
|
||||||
|
does not exist. Use get_or_add_group(~) if you want a new group
|
||||||
|
to be created if it did not already exist.
|
||||||
|
|
||||||
|
`nested_names` is a list of strings which represent group names.
|
||||||
|
Each group name will be nested inside of the previous group name.
|
||||||
|
|
||||||
|
`name_attr` is the group attribute that is being used to
|
||||||
|
represent the group's name. Default is 'id', but some SVGs may
|
||||||
|
contain custom name labels, like 'inkscape:label'.
|
||||||
|
|
||||||
|
Returns the request group. If the requested group did not
|
||||||
|
exist, this function will return a None value.
|
||||||
|
"""
|
||||||
|
group = self.tree.getroot()
|
||||||
|
# Drill down through the names until we find the desired group
|
||||||
|
while len(nested_names):
|
||||||
|
prev_group = group
|
||||||
|
next_name = nested_names.pop(0)
|
||||||
|
for elem in group.iterfind(SVG_GROUP_TAG, SVG_NAMESPACE):
|
||||||
|
if elem.get(name_attr) == next_name:
|
||||||
|
group = elem
|
||||||
|
break
|
||||||
|
|
||||||
|
if prev_group is group:
|
||||||
|
# The nested group could not be found, so we return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
return group
|
||||||
|
|
||||||
def get_or_add_group(self, nested_names, name_attr='id'):
|
def get_or_add_group(self, nested_names, name_attr='id'):
|
||||||
"""Get a group from the tree, or add a new one with the given
|
"""Get a group from the tree, or add a new one with the given
|
||||||
name structure.
|
name structure.
|
||||||
|
@ -337,18 +430,33 @@ class Document:
|
||||||
return SubElement(parent, '{{{0}}}g'.format(
|
return SubElement(parent, '{{{0}}}g'.format(
|
||||||
SVG_NAMESPACE['svg']), group_attribs)
|
SVG_NAMESPACE['svg']), group_attribs)
|
||||||
|
|
||||||
def save(self, filename):
|
def __repr__(self):
|
||||||
with open(filename, 'w') as output_svg:
|
return etree.tostring(self.tree.getroot()).decode()
|
||||||
output_svg.write(etree.tostring(self.tree.getroot()))
|
|
||||||
|
|
||||||
def display(self, filename=None):
|
def pretty(self, **kwargs):
|
||||||
|
return parseString(repr(self)).toprettyxml(**kwargs)
|
||||||
|
|
||||||
|
def save(self, filepath, prettify=False, **kwargs):
|
||||||
|
with open(filepath, 'w+') as output_svg:
|
||||||
|
if prettify:
|
||||||
|
output_svg.write(self.pretty(**kwargs))
|
||||||
|
else:
|
||||||
|
output_svg.write(repr(self))
|
||||||
|
|
||||||
|
def display(self, filepath=None):
|
||||||
"""Displays/opens the doc using the OS's default application."""
|
"""Displays/opens the doc using the OS's default application."""
|
||||||
|
|
||||||
if filename is None:
|
if filepath is None:
|
||||||
filename = self.original_filename
|
if self.original_filepath is None: # created from empty Document
|
||||||
|
orig_name, ext = 'unnamed', '.svg'
|
||||||
|
else:
|
||||||
|
orig_name, ext = \
|
||||||
|
os.path.splitext(os.path.basename(self.original_filepath))
|
||||||
|
tmp_name = orig_name + '_' + str(time()).replace('.', '-') + ext
|
||||||
|
filepath = os.path.join(gettempdir(), tmp_name)
|
||||||
|
|
||||||
# write to a (by default temporary) file
|
# write to a (by default temporary) file
|
||||||
with open(filename, 'w') as output_svg:
|
with open(filepath, 'w') as output_svg:
|
||||||
output_svg.write(etree.tostring(self.tree.getroot()))
|
output_svg.write(repr(self))
|
||||||
|
|
||||||
open_in_browser(filename)
|
open_in_browser(filepath)
|
||||||
|
|
|
@ -4,205 +4,15 @@ Note: This file was taken (nearly) as is from the svg.path module (v 2.0)."""
|
||||||
|
|
||||||
# External dependencies
|
# External dependencies
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
import re
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
# Internal dependencies
|
# Internal dependencies
|
||||||
from .path import Path, Line, QuadraticBezier, CubicBezier, Arc
|
from .path import Path
|
||||||
|
|
||||||
# To maintain forward/backward compatibility
|
|
||||||
try:
|
|
||||||
str = basestring
|
|
||||||
except NameError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
COMMANDS = set('MmZzLlHhVvCcSsQqTtAa')
|
|
||||||
UPPERCASE = set('MZLHVCSQTA')
|
|
||||||
|
|
||||||
COMMAND_RE = re.compile("([MmZzLlHhVvCcSsQqTtAa])")
|
|
||||||
FLOAT_RE = re.compile("[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?")
|
|
||||||
|
|
||||||
|
|
||||||
def _tokenize_path(pathdef):
|
|
||||||
for x in COMMAND_RE.split(pathdef):
|
|
||||||
if x in COMMANDS:
|
|
||||||
yield x
|
|
||||||
for token in FLOAT_RE.findall(x):
|
|
||||||
yield token
|
|
||||||
|
|
||||||
|
|
||||||
def parse_path(pathdef, current_pos=0j, tree_element=None):
|
def parse_path(pathdef, current_pos=0j, tree_element=None):
|
||||||
# In the SVG specs, initial movetos are absolute, even if
|
return Path(pathdef, current_pos=current_pos, tree_element=tree_element)
|
||||||
# specified as 'm'. This is the default behavior here as well.
|
|
||||||
# But if you pass in a current_pos variable, the initial moveto
|
|
||||||
# will be relative to that current_pos. This is useful.
|
|
||||||
elements = list(_tokenize_path(pathdef))
|
|
||||||
# Reverse for easy use of .pop()
|
|
||||||
elements.reverse()
|
|
||||||
|
|
||||||
if tree_element is None:
|
|
||||||
segments = Path()
|
|
||||||
else:
|
|
||||||
segments = Path(tree_element=tree_element)
|
|
||||||
|
|
||||||
start_pos = None
|
|
||||||
command = None
|
|
||||||
|
|
||||||
while elements:
|
|
||||||
|
|
||||||
if elements[-1] in COMMANDS:
|
|
||||||
# New command.
|
|
||||||
last_command = command # Used by S and T
|
|
||||||
command = elements.pop()
|
|
||||||
absolute = command in UPPERCASE
|
|
||||||
command = command.upper()
|
|
||||||
else:
|
|
||||||
# If this element starts with numbers, it is an implicit command
|
|
||||||
# and we don't change the command. Check that it's allowed:
|
|
||||||
if command is None:
|
|
||||||
raise ValueError("Unallowed implicit command in %s, position %s" % (
|
|
||||||
pathdef, len(pathdef.split()) - len(elements)))
|
|
||||||
|
|
||||||
if command == 'M':
|
|
||||||
# Moveto command.
|
|
||||||
x = elements.pop()
|
|
||||||
y = elements.pop()
|
|
||||||
pos = float(x) + float(y) * 1j
|
|
||||||
if absolute:
|
|
||||||
current_pos = pos
|
|
||||||
else:
|
|
||||||
current_pos += pos
|
|
||||||
|
|
||||||
# when M is called, reset start_pos
|
|
||||||
# This behavior of Z is defined in svg spec:
|
|
||||||
# http://www.w3.org/TR/SVG/paths.html#PathDataClosePathCommand
|
|
||||||
start_pos = current_pos
|
|
||||||
|
|
||||||
# Implicit moveto commands are treated as lineto commands.
|
|
||||||
# So we set command to lineto here, in case there are
|
|
||||||
# further implicit commands after this moveto.
|
|
||||||
command = 'L'
|
|
||||||
|
|
||||||
elif command == 'Z':
|
|
||||||
# Close path
|
|
||||||
if not (current_pos == start_pos):
|
|
||||||
segments.append(Line(current_pos, start_pos))
|
|
||||||
segments.closed = True
|
|
||||||
current_pos = start_pos
|
|
||||||
command = None
|
|
||||||
|
|
||||||
elif command == 'L':
|
|
||||||
x = elements.pop()
|
|
||||||
y = elements.pop()
|
|
||||||
pos = float(x) + float(y) * 1j
|
|
||||||
if not absolute:
|
|
||||||
pos += current_pos
|
|
||||||
segments.append(Line(current_pos, pos))
|
|
||||||
current_pos = pos
|
|
||||||
|
|
||||||
elif command == 'H':
|
|
||||||
x = elements.pop()
|
|
||||||
pos = float(x) + current_pos.imag * 1j
|
|
||||||
if not absolute:
|
|
||||||
pos += current_pos.real
|
|
||||||
segments.append(Line(current_pos, pos))
|
|
||||||
current_pos = pos
|
|
||||||
|
|
||||||
elif command == 'V':
|
|
||||||
y = elements.pop()
|
|
||||||
pos = current_pos.real + float(y) * 1j
|
|
||||||
if not absolute:
|
|
||||||
pos += current_pos.imag * 1j
|
|
||||||
segments.append(Line(current_pos, pos))
|
|
||||||
current_pos = pos
|
|
||||||
|
|
||||||
elif command == 'C':
|
|
||||||
control1 = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
control2 = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
|
|
||||||
if not absolute:
|
|
||||||
control1 += current_pos
|
|
||||||
control2 += current_pos
|
|
||||||
end += current_pos
|
|
||||||
|
|
||||||
segments.append(CubicBezier(current_pos, control1, control2, end))
|
|
||||||
current_pos = end
|
|
||||||
|
|
||||||
elif command == 'S':
|
|
||||||
# Smooth curve. First control point is the "reflection" of
|
|
||||||
# the second control point in the previous path.
|
|
||||||
|
|
||||||
if last_command not in 'CS':
|
|
||||||
# If there is no previous command or if the previous command
|
|
||||||
# was not an C, c, S or s, assume the first control point is
|
|
||||||
# coincident with the current point.
|
|
||||||
control1 = current_pos
|
|
||||||
else:
|
|
||||||
# The first control point is assumed to be the reflection of
|
|
||||||
# the second control point on the previous command relative
|
|
||||||
# to the current point.
|
|
||||||
control1 = current_pos + current_pos - segments[-1].control2
|
|
||||||
|
|
||||||
control2 = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
|
|
||||||
if not absolute:
|
|
||||||
control2 += current_pos
|
|
||||||
end += current_pos
|
|
||||||
|
|
||||||
segments.append(CubicBezier(current_pos, control1, control2, end))
|
|
||||||
current_pos = end
|
|
||||||
|
|
||||||
elif command == 'Q':
|
|
||||||
control = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
|
|
||||||
if not absolute:
|
|
||||||
control += current_pos
|
|
||||||
end += current_pos
|
|
||||||
|
|
||||||
segments.append(QuadraticBezier(current_pos, control, end))
|
|
||||||
current_pos = end
|
|
||||||
|
|
||||||
elif command == 'T':
|
|
||||||
# Smooth curve. Control point is the "reflection" of
|
|
||||||
# the second control point in the previous path.
|
|
||||||
|
|
||||||
if last_command not in 'QT':
|
|
||||||
# If there is no previous command or if the previous command
|
|
||||||
# was not an Q, q, T or t, assume the first control point is
|
|
||||||
# coincident with the current point.
|
|
||||||
control = current_pos
|
|
||||||
else:
|
|
||||||
# The control point is assumed to be the reflection of
|
|
||||||
# the control point on the previous command relative
|
|
||||||
# to the current point.
|
|
||||||
control = current_pos + current_pos - segments[-1].control
|
|
||||||
|
|
||||||
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
|
|
||||||
if not absolute:
|
|
||||||
end += current_pos
|
|
||||||
|
|
||||||
segments.append(QuadraticBezier(current_pos, control, end))
|
|
||||||
current_pos = end
|
|
||||||
|
|
||||||
elif command == 'A':
|
|
||||||
radius = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
rotation = float(elements.pop())
|
|
||||||
arc = float(elements.pop())
|
|
||||||
sweep = float(elements.pop())
|
|
||||||
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
||||||
|
|
||||||
if not absolute:
|
|
||||||
end += current_pos
|
|
||||||
|
|
||||||
segments.append(Arc(current_pos, radius, rotation, arc, sweep, end))
|
|
||||||
current_pos = end
|
|
||||||
|
|
||||||
return segments
|
|
||||||
|
|
||||||
|
|
||||||
def _check_num_parsed_values(values, allowed):
|
def _check_num_parsed_values(values, allowed):
|
||||||
|
|
1034
svgpathtools/path.py
|
@ -1,20 +1,24 @@
|
||||||
"""This submodule contains tools for creating svg files from paths and path
|
"""This submodule: basic tools for creating svg files from path data.
|
||||||
segments."""
|
|
||||||
|
See also the document.py submodule.
|
||||||
|
"""
|
||||||
|
|
||||||
# External dependencies:
|
# External dependencies:
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
from math import ceil
|
from math import ceil
|
||||||
from os import getcwd, path as os_path, makedirs
|
from os import path as os_path, makedirs
|
||||||
|
from tempfile import gettempdir
|
||||||
from xml.dom.minidom import parse as md_xml_parse
|
from xml.dom.minidom import parse as md_xml_parse
|
||||||
from svgwrite import Drawing, text as txt
|
from svgwrite import Drawing, text as txt
|
||||||
from time import time
|
from time import time
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
import re
|
||||||
|
|
||||||
# Internal dependencies
|
# Internal dependencies
|
||||||
from .path import Path, Line, is_path_segment
|
from .path import Path, Line, is_path_segment
|
||||||
from .misctools import open_in_browser
|
from .misctools import open_in_browser
|
||||||
|
|
||||||
# Used to convert a string colors (identified by single chars) to a list.
|
# color shorthand for inputting color list as string of chars.
|
||||||
color_dict = {'a': 'aqua',
|
color_dict = {'a': 'aqua',
|
||||||
'b': 'blue',
|
'b': 'blue',
|
||||||
'c': 'cyan',
|
'c': 'cyan',
|
||||||
|
@ -57,8 +61,16 @@ def is3tuple(c):
|
||||||
|
|
||||||
|
|
||||||
def big_bounding_box(paths_n_stuff):
|
def big_bounding_box(paths_n_stuff):
|
||||||
"""Finds a BB containing a collection of paths, Bezier path segments, and
|
"""returns minimal upright bounding box.
|
||||||
points (given as complex numbers)."""
|
|
||||||
|
Args:
|
||||||
|
paths_n_stuff: iterable of Paths, Bezier path segments, and
|
||||||
|
points (given as complex numbers).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
extrema of bounding box, (xmin, xmax, ymin, ymax)
|
||||||
|
|
||||||
|
"""
|
||||||
bbs = []
|
bbs = []
|
||||||
for thing in paths_n_stuff:
|
for thing in paths_n_stuff:
|
||||||
if is_path_segment(thing) or isinstance(thing, Path):
|
if is_path_segment(thing) or isinstance(thing, Path):
|
||||||
|
@ -71,9 +83,9 @@ def big_bounding_box(paths_n_stuff):
|
||||||
bbs.append((complexthing.real, complexthing.real,
|
bbs.append((complexthing.real, complexthing.real,
|
||||||
complexthing.imag, complexthing.imag))
|
complexthing.imag, complexthing.imag))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise TypeError(
|
raise TypeError("paths_n_stuff can only contains Path, "
|
||||||
"paths_n_stuff can only contains Path, CubicBezier, "
|
"CubicBezier, QuadraticBezier, Line, "
|
||||||
"QuadraticBezier, Line, and complex objects.")
|
"and complex objects.")
|
||||||
xmins, xmaxs, ymins, ymaxs = list(zip(*bbs))
|
xmins, xmaxs, ymins, ymaxs = list(zip(*bbs))
|
||||||
xmin = min(xmins)
|
xmin = min(xmins)
|
||||||
xmax = max(xmaxs)
|
xmax = max(xmaxs)
|
||||||
|
@ -82,14 +94,15 @@ def big_bounding_box(paths_n_stuff):
|
||||||
return xmin, xmax, ymin, ymax
|
return xmin, xmax, ymin, ymax
|
||||||
|
|
||||||
|
|
||||||
def disvg(paths=None, colors=None,
|
def disvg(paths=None, colors=None, filename=None, stroke_widths=None,
|
||||||
filename=os_path.join(getcwd(), 'disvg_output.svg'),
|
nodes=None, node_colors=None, node_radii=None,
|
||||||
stroke_widths=None, nodes=None, node_colors=None, node_radii=None,
|
openinbrowser=True, timestamp=None, margin_size=0.1,
|
||||||
openinbrowser=True, timestamp=False,
|
mindim=600, dimensions=None, viewbox=None, text=None,
|
||||||
margin_size=0.1, mindim=600, dimensions=None,
|
text_path=None, font_size=None, attributes=None,
|
||||||
viewbox=None, text=None, text_path=None, font_size=None,
|
svg_attributes=None, svgwrite_debug=False,
|
||||||
attributes=None, svg_attributes=None, svgwrite_debug=False):
|
paths2Drawing=False, baseunit='px'):
|
||||||
"""Takes in a list of paths and creates an SVG file containing said paths.
|
"""Creates (and optionally displays) an SVG file.
|
||||||
|
|
||||||
REQUIRED INPUTS:
|
REQUIRED INPUTS:
|
||||||
:param paths - a list of paths
|
:param paths - a list of paths
|
||||||
|
|
||||||
|
@ -104,8 +117,10 @@ def disvg(paths=None, colors=None,
|
||||||
3) a list of rgb 3-tuples -- e.g. colors = [(255, 0, 0), ...].
|
3) a list of rgb 3-tuples -- e.g. colors = [(255, 0, 0), ...].
|
||||||
|
|
||||||
:param filename - the desired location/filename of the SVG file
|
:param filename - the desired location/filename of the SVG file
|
||||||
created (by default the SVG will be stored in the current working
|
created (by default the SVG will be named 'disvg_output.svg' or
|
||||||
directory and named 'disvg_output.svg').
|
'disvg_output_<timestamp>.svg' and stored in the temporary
|
||||||
|
directory returned by `tempfile.gettempdir()`. See `timestamp`
|
||||||
|
for information on the timestamp.
|
||||||
|
|
||||||
:param stroke_widths - a list of stroke_widths to use for paths
|
:param stroke_widths - a list of stroke_widths to use for paths
|
||||||
(default is 0.5% of the SVG's width or length)
|
(default is 0.5% of the SVG's width or length)
|
||||||
|
@ -130,9 +145,11 @@ def disvg(paths=None, colors=None,
|
||||||
:param openinbrowser - Set to True to automatically open the created
|
:param openinbrowser - Set to True to automatically open the created
|
||||||
SVG in the user's default web browser.
|
SVG in the user's default web browser.
|
||||||
|
|
||||||
:param timestamp - if True, then the a timestamp will be appended to
|
:param timestamp - if true, then the a timestamp will be
|
||||||
the output SVG's filename. This will fix issues with rapidly opening
|
appended to the output SVG's filename. This is meant as a
|
||||||
multiple SVGs in your browser.
|
workaround for issues related to rapidly opening multiple
|
||||||
|
SVGs in your browser using `disvg`. This defaults to true if
|
||||||
|
`filename is None` and false otherwise.
|
||||||
|
|
||||||
:param margin_size - The min margin (empty area framing the collection
|
:param margin_size - The min margin (empty area framing the collection
|
||||||
of paths) size used for creating the canvas and background of the SVG.
|
of paths) size used for creating the canvas and background of the SVG.
|
||||||
|
@ -140,13 +157,19 @@ def disvg(paths=None, colors=None,
|
||||||
:param mindim - The minimum dimension (height or width) of the output
|
:param mindim - The minimum dimension (height or width) of the output
|
||||||
SVG (default is 600).
|
SVG (default is 600).
|
||||||
|
|
||||||
:param dimensions - The display dimensions of the output SVG. Using
|
:param dimensions - The (x,y) display dimensions of the output SVG.
|
||||||
this will override the mindim parameter.
|
I.e. this specifies the `width` and `height` SVG attributes. Note that
|
||||||
|
these also can be used to specify units other than pixels. Using this
|
||||||
|
will override the `mindim` parameter.
|
||||||
|
|
||||||
:param viewbox - This specifies what rectangular patch of R^2 will be
|
:param viewbox - This specifies the coordinated system used in the svg.
|
||||||
viewable through the outputSVG. It should be input in the form
|
The SVG `viewBox` attribute works together with the the `height` and
|
||||||
(min_x, min_y, width, height). This is different from the display
|
`width` attrinutes. Using these three attributes allows for shifting
|
||||||
dimension of the svg, which can be set through mindim or dimensions.
|
and scaling of the SVG canvas without changing the any values other
|
||||||
|
than those in `viewBox`, `height`, and `width`. `viewbox` should be
|
||||||
|
input as a 4-tuple, (min_x, min_y, width, height), or a string
|
||||||
|
"min_x min_y width height". Using this will override the `mindim`
|
||||||
|
parameter.
|
||||||
|
|
||||||
:param attributes - a list of dictionaries of attributes for the input
|
:param attributes - a list of dictionaries of attributes for the input
|
||||||
paths. Note: This will override any other conflicting settings.
|
paths. Note: This will override any other conflicting settings.
|
||||||
|
@ -158,6 +181,10 @@ def disvg(paths=None, colors=None,
|
||||||
speed and also prevents `svgwrite` from raising of an error when not
|
speed and also prevents `svgwrite` from raising of an error when not
|
||||||
all `svg_attributes` key-value pairs are understood.
|
all `svg_attributes` key-value pairs are understood.
|
||||||
|
|
||||||
|
:param paths2Drawing - If true, an `svgwrite.Drawing` object is
|
||||||
|
returned and no file is written. This `Drawing` can later be saved
|
||||||
|
using the `svgwrite.Drawing.save()` method.
|
||||||
|
|
||||||
NOTES:
|
NOTES:
|
||||||
* The `svg_attributes` parameter will override any other conflicting
|
* The `svg_attributes` parameter will override any other conflicting
|
||||||
settings.
|
settings.
|
||||||
|
@ -172,6 +199,9 @@ def disvg(paths=None, colors=None,
|
||||||
svgviewer/browser will likely fail to load some of the SVGs in time.
|
svgviewer/browser will likely fail to load some of the SVGs in time.
|
||||||
To fix this, use the timestamp attribute, or give the files unique
|
To fix this, use the timestamp attribute, or give the files unique
|
||||||
names, or use a pause command (e.g. time.sleep(1)) between uses.
|
names, or use a pause command (e.g. time.sleep(1)) between uses.
|
||||||
|
|
||||||
|
SEE ALSO:
|
||||||
|
* document.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_default_relative_node_radius = 5e-3
|
_default_relative_node_radius = 5e-3
|
||||||
|
@ -180,14 +210,17 @@ def disvg(paths=None, colors=None,
|
||||||
_default_node_color = '#ff0000' # red
|
_default_node_color = '#ff0000' # red
|
||||||
_default_font_size = 12
|
_default_font_size = 12
|
||||||
|
|
||||||
# append directory to filename (if not included)
|
if filename is None:
|
||||||
if os_path.dirname(filename) == '':
|
timestamp = True if timestamp is None else timestamp
|
||||||
filename = os_path.join(getcwd(), filename)
|
filename = os_path.join(gettempdir(), 'disvg_output.svg')
|
||||||
|
|
||||||
|
dirname = os_path.abspath(os_path.dirname(filename))
|
||||||
|
if not os_path.exists(dirname):
|
||||||
|
makedirs(dirname)
|
||||||
|
|
||||||
# append time stamp to filename
|
# append time stamp to filename
|
||||||
if timestamp:
|
if timestamp:
|
||||||
fbname, fext = os_path.splitext(filename)
|
fbname, fext = os_path.splitext(filename)
|
||||||
dirname = os_path.dirname(filename)
|
|
||||||
tstamp = str(time()).replace('.', '')
|
tstamp = str(time()).replace('.', '')
|
||||||
stfilename = os_path.split(fbname)[1] + '_' + tstamp + fext
|
stfilename = os_path.split(fbname)[1] + '_' + tstamp + fext
|
||||||
filename = os_path.join(dirname, stfilename)
|
filename = os_path.join(dirname, stfilename)
|
||||||
|
@ -227,7 +260,15 @@ def disvg(paths=None, colors=None,
|
||||||
assert paths or nodes
|
assert paths or nodes
|
||||||
stuff2bound = []
|
stuff2bound = []
|
||||||
if viewbox:
|
if viewbox:
|
||||||
szx, szy = viewbox[2:4]
|
if not isinstance(viewbox, str):
|
||||||
|
viewbox = '%s %s %s %s' % viewbox
|
||||||
|
if dimensions is None:
|
||||||
|
dimensions = viewbox.split(' ')[2:4]
|
||||||
|
elif dimensions:
|
||||||
|
dimensions = tuple(map(str, dimensions))
|
||||||
|
def strip_units(s):
|
||||||
|
return re.search(r'\d*\.?\d*', s.strip()).group()
|
||||||
|
viewbox = '0 0 %s %s' % tuple(map(strip_units, dimensions))
|
||||||
else:
|
else:
|
||||||
if paths:
|
if paths:
|
||||||
stuff2bound += paths
|
stuff2bound += paths
|
||||||
|
@ -274,25 +315,28 @@ def disvg(paths=None, colors=None,
|
||||||
dx += 2*margin_size*dx + extra_space_for_style
|
dx += 2*margin_size*dx + extra_space_for_style
|
||||||
dy += 2*margin_size*dy + extra_space_for_style
|
dy += 2*margin_size*dy + extra_space_for_style
|
||||||
viewbox = "%s %s %s %s" % (xmin, ymin, dx, dy)
|
viewbox = "%s %s %s %s" % (xmin, ymin, dx, dy)
|
||||||
if dimensions:
|
|
||||||
szx, szy = dimensions
|
if mindim is None:
|
||||||
|
szx = "{}{}".format(dx, baseunit)
|
||||||
|
szy = "{}{}".format(dy, baseunit)
|
||||||
else:
|
else:
|
||||||
if dx > dy:
|
if dx > dy:
|
||||||
szx = str(mindim) + 'px'
|
szx = str(mindim) + baseunit
|
||||||
szy = str(int(ceil(mindim * dy / dx))) + 'px'
|
szy = str(int(ceil(mindim * dy / dx))) + baseunit
|
||||||
else:
|
else:
|
||||||
szx = str(int(ceil(mindim * dx / dy))) + 'px'
|
szx = str(int(ceil(mindim * dx / dy))) + baseunit
|
||||||
szy = str(mindim) + 'px'
|
szy = str(mindim) + baseunit
|
||||||
|
dimensions = szx, szy
|
||||||
|
|
||||||
# Create an SVG file
|
# Create an SVG file
|
||||||
if svg_attributes is not None:
|
if svg_attributes is not None:
|
||||||
szx = svg_attributes.get("width", szx)
|
dimensions = (svg_attributes.get("width", dimensions[0]),
|
||||||
szy = svg_attributes.get("height", szy)
|
svg_attributes.get("height", dimensions[1]))
|
||||||
debug = svg_attributes.get("debug", svgwrite_debug)
|
debug = svg_attributes.get("debug", svgwrite_debug)
|
||||||
dwg = Drawing(filename=filename, size=(szx, szy), debug=debug,
|
dwg = Drawing(filename=filename, size=dimensions, debug=debug,
|
||||||
**svg_attributes)
|
**svg_attributes)
|
||||||
else:
|
else:
|
||||||
dwg = Drawing(filename=filename, size=(szx, szy), debug=svgwrite_debug,
|
dwg = Drawing(filename=filename, size=dimensions, debug=svgwrite_debug,
|
||||||
viewBox=viewbox)
|
viewBox=viewbox)
|
||||||
|
|
||||||
# add paths
|
# add paths
|
||||||
|
@ -363,9 +407,9 @@ def disvg(paths=None, colors=None,
|
||||||
txter = dwg.add(dwg.text('', font_size=font_size[idx]))
|
txter = dwg.add(dwg.text('', font_size=font_size[idx]))
|
||||||
txter.add(txt.TextPath('#'+pathid, s))
|
txter.add(txt.TextPath('#'+pathid, s))
|
||||||
|
|
||||||
# save svg
|
if paths2Drawing:
|
||||||
if not os_path.exists(os_path.dirname(filename)):
|
return dwg
|
||||||
makedirs(os_path.dirname(filename))
|
|
||||||
dwg.save()
|
dwg.save()
|
||||||
|
|
||||||
# re-open the svg, make the xml pretty, and save it again
|
# re-open the svg, make the xml pretty, and save it again
|
||||||
|
@ -382,20 +426,56 @@ def disvg(paths=None, colors=None,
|
||||||
print(filename)
|
print(filename)
|
||||||
|
|
||||||
|
|
||||||
def wsvg(paths=None, colors=None,
|
def wsvg(paths=None, colors=None, filename=None, stroke_widths=None,
|
||||||
filename=os_path.join(getcwd(), 'disvg_output.svg'),
|
nodes=None, node_colors=None, node_radii=None,
|
||||||
stroke_widths=None, nodes=None, node_colors=None, node_radii=None,
|
openinbrowser=False, timestamp=False, margin_size=0.1,
|
||||||
openinbrowser=False, timestamp=False,
|
mindim=600, dimensions=None, viewbox=None, text=None,
|
||||||
margin_size=0.1, mindim=600, dimensions=None,
|
text_path=None, font_size=None, attributes=None,
|
||||||
viewbox=None, text=None, text_path=None, font_size=None,
|
svg_attributes=None, svgwrite_debug=False,
|
||||||
attributes=None, svg_attributes=None, svgwrite_debug=False):
|
paths2Drawing=False, baseunit='px'):
|
||||||
"""Convenience function; identical to disvg() except that
|
"""Create SVG and write to disk.
|
||||||
openinbrowser=False by default. See disvg() docstring for more info."""
|
|
||||||
disvg(paths, colors=colors, filename=filename,
|
Note: This is identical to `disvg()` except that `openinbrowser`
|
||||||
|
is false by default and an assertion error is raised if `filename
|
||||||
|
is None`.
|
||||||
|
|
||||||
|
See `disvg()` docstring for more info.
|
||||||
|
"""
|
||||||
|
assert filename is not None
|
||||||
|
return disvg(paths, colors=colors, filename=filename,
|
||||||
stroke_widths=stroke_widths, nodes=nodes,
|
stroke_widths=stroke_widths, nodes=nodes,
|
||||||
node_colors=node_colors, node_radii=node_radii,
|
node_colors=node_colors, node_radii=node_radii,
|
||||||
openinbrowser=openinbrowser, timestamp=timestamp,
|
openinbrowser=openinbrowser, timestamp=timestamp,
|
||||||
margin_size=margin_size, mindim=mindim, dimensions=dimensions,
|
margin_size=margin_size, mindim=mindim,
|
||||||
viewbox=viewbox, text=text, text_path=text_path, font_size=font_size,
|
dimensions=dimensions, viewbox=viewbox, text=text,
|
||||||
|
text_path=text_path, font_size=font_size,
|
||||||
attributes=attributes, svg_attributes=svg_attributes,
|
attributes=attributes, svg_attributes=svg_attributes,
|
||||||
svgwrite_debug=svgwrite_debug)
|
svgwrite_debug=svgwrite_debug,
|
||||||
|
paths2Drawing=paths2Drawing, baseunit=baseunit)
|
||||||
|
|
||||||
|
|
||||||
|
def paths2Drawing(paths=None, colors=None, filename=None,
|
||||||
|
stroke_widths=None, nodes=None, node_colors=None,
|
||||||
|
node_radii=None, openinbrowser=False, timestamp=False,
|
||||||
|
margin_size=0.1, mindim=600, dimensions=None,
|
||||||
|
viewbox=None, text=None, text_path=None,
|
||||||
|
font_size=None, attributes=None, svg_attributes=None,
|
||||||
|
svgwrite_debug=False, paths2Drawing=True, baseunit='px'):
|
||||||
|
"""Create and return `svg.Drawing` object.
|
||||||
|
|
||||||
|
Note: This is identical to `disvg()` except that `paths2Drawing`
|
||||||
|
is true by default and an assertion error is raised if `filename
|
||||||
|
is None`.
|
||||||
|
|
||||||
|
See `disvg()` docstring for more info.
|
||||||
|
"""
|
||||||
|
return disvg(paths, colors=colors, filename=filename,
|
||||||
|
stroke_widths=stroke_widths, nodes=nodes,
|
||||||
|
node_colors=node_colors, node_radii=node_radii,
|
||||||
|
openinbrowser=openinbrowser, timestamp=timestamp,
|
||||||
|
margin_size=margin_size, mindim=mindim,
|
||||||
|
dimensions=dimensions, viewbox=viewbox, text=text,
|
||||||
|
text_path=text_path, font_size=font_size,
|
||||||
|
attributes=attributes, svg_attributes=svg_attributes,
|
||||||
|
svgwrite_debug=svgwrite_debug,
|
||||||
|
paths2Drawing=paths2Drawing, baseunit=baseunit)
|
||||||
|
|
|
@ -0,0 +1,199 @@
|
||||||
|
"""(Experimental) replacement for import/export functionality SAX
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# External dependencies
|
||||||
|
from __future__ import division, absolute_import, print_function
|
||||||
|
import os
|
||||||
|
from xml.etree.ElementTree import iterparse, Element, ElementTree, SubElement
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# Internal dependencies
|
||||||
|
from .parser import parse_path
|
||||||
|
from .parser import parse_transform
|
||||||
|
from .svg_to_paths import (path2pathd, ellipse2pathd, line2pathd,
|
||||||
|
polyline2pathd, polygon2pathd, rect2pathd)
|
||||||
|
from .misctools import open_in_browser
|
||||||
|
from .path import transform
|
||||||
|
|
||||||
|
# To maintain forward/backward compatibility
|
||||||
|
try:
|
||||||
|
string = basestring
|
||||||
|
except NameError:
|
||||||
|
string = str
|
||||||
|
|
||||||
|
NAME_SVG = "svg"
|
||||||
|
ATTR_VERSION = "version"
|
||||||
|
VALUE_SVG_VERSION = "1.1"
|
||||||
|
ATTR_XMLNS = "xmlns"
|
||||||
|
VALUE_XMLNS = "http://www.w3.org/2000/svg"
|
||||||
|
ATTR_XMLNS_LINK = "xmlns:xlink"
|
||||||
|
VALUE_XLINK = "http://www.w3.org/1999/xlink"
|
||||||
|
ATTR_XMLNS_EV = "xmlns:ev"
|
||||||
|
VALUE_XMLNS_EV = "http://www.w3.org/2001/xml-events"
|
||||||
|
ATTR_WIDTH = "width"
|
||||||
|
ATTR_HEIGHT = "height"
|
||||||
|
ATTR_VIEWBOX = "viewBox"
|
||||||
|
NAME_PATH = "path"
|
||||||
|
ATTR_DATA = "d"
|
||||||
|
ATTR_FILL = "fill"
|
||||||
|
ATTR_STROKE = "stroke"
|
||||||
|
ATTR_STROKE_WIDTH = "stroke-width"
|
||||||
|
ATTR_TRANSFORM = "transform"
|
||||||
|
VALUE_NONE = "none"
|
||||||
|
|
||||||
|
|
||||||
|
class SaxDocument:
|
||||||
|
def __init__(self, filename):
|
||||||
|
"""A container for a SAX SVG light tree objects document.
|
||||||
|
|
||||||
|
This class provides functions for extracting SVG data into Path objects.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filename (str): The filename of the SVG file
|
||||||
|
"""
|
||||||
|
self.root_values = {}
|
||||||
|
self.tree = []
|
||||||
|
# remember location of original svg file
|
||||||
|
if filename is not None and os.path.dirname(filename) == '':
|
||||||
|
self.original_filename = os.path.join(os.getcwd(), filename)
|
||||||
|
else:
|
||||||
|
self.original_filename = filename
|
||||||
|
|
||||||
|
if filename is not None:
|
||||||
|
self.sax_parse(filename)
|
||||||
|
|
||||||
|
def sax_parse(self, filename):
|
||||||
|
self.root_values = {}
|
||||||
|
self.tree = []
|
||||||
|
stack = []
|
||||||
|
values = {}
|
||||||
|
matrix = None
|
||||||
|
for event, elem in iterparse(filename, events=('start', 'end')):
|
||||||
|
if event == 'start':
|
||||||
|
stack.append((values, matrix))
|
||||||
|
if matrix is not None:
|
||||||
|
matrix = matrix.copy() # copy of matrix
|
||||||
|
current_values = values
|
||||||
|
values = {}
|
||||||
|
values.update(current_values) # copy of dictionary
|
||||||
|
attrs = elem.attrib
|
||||||
|
values.update(attrs)
|
||||||
|
name = elem.tag[28:]
|
||||||
|
if "style" in attrs:
|
||||||
|
for equate in attrs["style"].split(";"):
|
||||||
|
equal_item = equate.split(":")
|
||||||
|
values[equal_item[0]] = equal_item[1]
|
||||||
|
if "transform" in attrs:
|
||||||
|
transform_matrix = parse_transform(attrs["transform"])
|
||||||
|
if matrix is None:
|
||||||
|
matrix = np.identity(3)
|
||||||
|
matrix = transform_matrix.dot(matrix)
|
||||||
|
if "svg" == name:
|
||||||
|
current_values = values
|
||||||
|
values = {}
|
||||||
|
values.update(current_values)
|
||||||
|
self.root_values = current_values
|
||||||
|
continue
|
||||||
|
elif "g" == name:
|
||||||
|
continue
|
||||||
|
elif 'path' == name:
|
||||||
|
values['d'] = path2pathd(values)
|
||||||
|
elif 'circle' == name:
|
||||||
|
values["d"] = ellipse2pathd(values)
|
||||||
|
elif 'ellipse' == name:
|
||||||
|
values["d"] = ellipse2pathd(values)
|
||||||
|
elif 'line' == name:
|
||||||
|
values["d"] = line2pathd(values)
|
||||||
|
elif 'polyline' == name:
|
||||||
|
values["d"] = polyline2pathd(values)
|
||||||
|
elif 'polygon' == name:
|
||||||
|
values["d"] = polygon2pathd(values)
|
||||||
|
elif 'rect' == name:
|
||||||
|
values["d"] = rect2pathd(values)
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
values["matrix"] = matrix
|
||||||
|
values["name"] = name
|
||||||
|
self.tree.append(values)
|
||||||
|
else:
|
||||||
|
v = stack.pop()
|
||||||
|
values = v[0]
|
||||||
|
matrix = v[1]
|
||||||
|
|
||||||
|
def flatten_all_paths(self):
|
||||||
|
flat = []
|
||||||
|
for values in self.tree:
|
||||||
|
pathd = values['d']
|
||||||
|
matrix = values['matrix']
|
||||||
|
parsed_path = parse_path(pathd)
|
||||||
|
if matrix is not None:
|
||||||
|
transform(parsed_path, matrix)
|
||||||
|
flat.append(parsed_path)
|
||||||
|
return flat
|
||||||
|
|
||||||
|
def get_pathd_and_matrix(self):
|
||||||
|
flat = []
|
||||||
|
for values in self.tree:
|
||||||
|
pathd = values['d']
|
||||||
|
matrix = values['matrix']
|
||||||
|
flat.append((pathd, matrix))
|
||||||
|
return flat
|
||||||
|
|
||||||
|
def generate_dom(self):
|
||||||
|
root = Element(NAME_SVG)
|
||||||
|
root.set(ATTR_VERSION, VALUE_SVG_VERSION)
|
||||||
|
root.set(ATTR_XMLNS, VALUE_XMLNS)
|
||||||
|
root.set(ATTR_XMLNS_LINK, VALUE_XLINK)
|
||||||
|
root.set(ATTR_XMLNS_EV, VALUE_XMLNS_EV)
|
||||||
|
width = self.root_values.get(ATTR_WIDTH, None)
|
||||||
|
height = self.root_values.get(ATTR_HEIGHT, None)
|
||||||
|
if width is not None:
|
||||||
|
root.set(ATTR_WIDTH, width)
|
||||||
|
if height is not None:
|
||||||
|
root.set(ATTR_HEIGHT, height)
|
||||||
|
viewbox = self.root_values.get(ATTR_VIEWBOX, None)
|
||||||
|
if viewbox is not None:
|
||||||
|
root.set(ATTR_VIEWBOX, viewbox)
|
||||||
|
identity = np.identity(3)
|
||||||
|
for values in self.tree:
|
||||||
|
pathd = values.get('d', '')
|
||||||
|
matrix = values.get('matrix', None)
|
||||||
|
# path_value = parse_path(pathd)
|
||||||
|
|
||||||
|
path = SubElement(root, NAME_PATH)
|
||||||
|
if matrix is not None and not np.all(np.equal(matrix, identity)):
|
||||||
|
matrix_string = "matrix("
|
||||||
|
matrix_string += " "
|
||||||
|
matrix_string += string(matrix[0][0])
|
||||||
|
matrix_string += " "
|
||||||
|
matrix_string += string(matrix[1][0])
|
||||||
|
matrix_string += " "
|
||||||
|
matrix_string += string(matrix[0][1])
|
||||||
|
matrix_string += " "
|
||||||
|
matrix_string += string(matrix[1][1])
|
||||||
|
matrix_string += " "
|
||||||
|
matrix_string += string(matrix[0][2])
|
||||||
|
matrix_string += " "
|
||||||
|
matrix_string += string(matrix[1][2])
|
||||||
|
matrix_string += ")"
|
||||||
|
path.set(ATTR_TRANSFORM, matrix_string)
|
||||||
|
if ATTR_DATA in values:
|
||||||
|
path.set(ATTR_DATA, values[ATTR_DATA])
|
||||||
|
if ATTR_FILL in values:
|
||||||
|
path.set(ATTR_FILL, values[ATTR_FILL])
|
||||||
|
if ATTR_STROKE in values:
|
||||||
|
path.set(ATTR_STROKE, values[ATTR_STROKE])
|
||||||
|
return ElementTree(root)
|
||||||
|
|
||||||
|
def save(self, filename):
|
||||||
|
with open(filename, 'wb') as output_svg:
|
||||||
|
dom_tree = self.generate_dom()
|
||||||
|
dom_tree.write(output_svg)
|
||||||
|
|
||||||
|
def display(self, filename=None):
|
||||||
|
"""Displays/opens the doc using the OS's default application."""
|
||||||
|
if filename is None:
|
||||||
|
filename = 'display_temp.svg'
|
||||||
|
self.save(filename)
|
||||||
|
open_in_browser(filename)
|
|
@ -4,8 +4,13 @@ The main tool being the svg2paths() function."""
|
||||||
# External dependencies
|
# External dependencies
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
from xml.dom.minidom import parse
|
from xml.dom.minidom import parse
|
||||||
from os import path as os_path, getcwd
|
import os
|
||||||
|
from io import StringIO
|
||||||
import re
|
import re
|
||||||
|
try:
|
||||||
|
from os import PathLike as FilePathLike
|
||||||
|
except ImportError:
|
||||||
|
FilePathLike = str
|
||||||
|
|
||||||
# Internal dependencies
|
# Internal dependencies
|
||||||
from .parser import parse_path
|
from .parser import parse_path
|
||||||
|
@ -17,15 +22,17 @@ COORD_PAIR_TMPLT = re.compile(
|
||||||
r'([\+-]?\d*[\.\d]\d*[eE][\+-]?\d+|[\+-]?\d*[\.\d]\d*)'
|
r'([\+-]?\d*[\.\d]\d*[eE][\+-]?\d+|[\+-]?\d*[\.\d]\d*)'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def path2pathd(path):
|
def path2pathd(path):
|
||||||
return path.get('d', '')
|
return path.get('d', '')
|
||||||
|
|
||||||
|
|
||||||
def ellipse2pathd(ellipse):
|
def ellipse2pathd(ellipse):
|
||||||
"""converts the parameters from an ellipse or a circle to a string for a
|
"""converts the parameters from an ellipse or a circle to a string for a
|
||||||
Path object d-attribute"""
|
Path object d-attribute"""
|
||||||
|
|
||||||
cx = ellipse.get('cx', None)
|
cx = ellipse.get('cx', 0)
|
||||||
cy = ellipse.get('cy', None)
|
cy = ellipse.get('cy', 0)
|
||||||
rx = ellipse.get('rx', None)
|
rx = ellipse.get('rx', None)
|
||||||
ry = ellipse.get('ry', None)
|
ry = ellipse.get('ry', None)
|
||||||
r = ellipse.get('r', None)
|
r = ellipse.get('r', None)
|
||||||
|
@ -44,13 +51,17 @@ def ellipse2pathd(ellipse):
|
||||||
d += 'a' + str(rx) + ',' + str(ry) + ' 0 1,0 ' + str(2 * rx) + ',0'
|
d += 'a' + str(rx) + ',' + str(ry) + ' 0 1,0 ' + str(2 * rx) + ',0'
|
||||||
d += 'a' + str(rx) + ',' + str(ry) + ' 0 1,0 ' + str(-2 * rx) + ',0'
|
d += 'a' + str(rx) + ',' + str(ry) + ' 0 1,0 ' + str(-2 * rx) + ',0'
|
||||||
|
|
||||||
return d
|
return d + 'z'
|
||||||
|
|
||||||
|
|
||||||
def polyline2pathd(polyline_d, is_polygon=False):
|
def polyline2pathd(polyline, is_polygon=False):
|
||||||
"""converts the string from a polyline points-attribute to a string for a
|
"""converts the string from a polyline points-attribute to a string for a
|
||||||
Path object d-attribute"""
|
Path object d-attribute"""
|
||||||
points = COORD_PAIR_TMPLT.findall(polyline_d)
|
if isinstance(polyline, str):
|
||||||
|
points = polyline
|
||||||
|
else:
|
||||||
|
points = COORD_PAIR_TMPLT.findall(polyline.get('points', ''))
|
||||||
|
|
||||||
closed = (float(points[0][0]) == float(points[-1][0]) and
|
closed = (float(points[0][0]) == float(points[-1][0]) and
|
||||||
float(points[0][1]) == float(points[-1][1]))
|
float(points[0][1]) == float(points[-1][1]))
|
||||||
|
|
||||||
|
@ -66,13 +77,13 @@ def polyline2pathd(polyline_d, is_polygon=False):
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def polygon2pathd(polyline_d):
|
def polygon2pathd(polyline):
|
||||||
"""converts the string from a polygon points-attribute to a string
|
"""converts the string from a polygon points-attribute to a string
|
||||||
for a Path object d-attribute.
|
for a Path object d-attribute.
|
||||||
Note: For a polygon made from n points, the resulting path will be
|
Note: For a polygon made from n points, the resulting path will be
|
||||||
composed of n lines (even if some of these lines have length zero).
|
composed of n lines (even if some of these lines have length zero).
|
||||||
"""
|
"""
|
||||||
return polyline2pathd(polyline_d, True)
|
return polyline2pathd(polyline, True)
|
||||||
|
|
||||||
|
|
||||||
def rect2pathd(rect):
|
def rect2pathd(rect):
|
||||||
|
@ -80,18 +91,48 @@ def rect2pathd(rect):
|
||||||
|
|
||||||
The rectangle will start at the (x,y) coordinate specified by the
|
The rectangle will start at the (x,y) coordinate specified by the
|
||||||
rectangle object and proceed counter-clockwise."""
|
rectangle object and proceed counter-clockwise."""
|
||||||
x0, y0 = float(rect.get('x', 0)), float(rect.get('y', 0))
|
x, y = float(rect.get('x', 0)), float(rect.get('y', 0))
|
||||||
w, h = float(rect.get('width', 0)), float(rect.get('height', 0))
|
w, h = float(rect.get('width', 0)), float(rect.get('height', 0))
|
||||||
x1, y1 = x0 + w, y0
|
if 'rx' in rect or 'ry' in rect:
|
||||||
x2, y2 = x0 + w, y0 + h
|
|
||||||
x3, y3 = x0, y0 + h
|
# if only one, rx or ry, is present, use that value for both
|
||||||
|
# https://developer.mozilla.org/en-US/docs/Web/SVG/Element/rect
|
||||||
|
rx = rect.get('rx', None)
|
||||||
|
ry = rect.get('ry', None)
|
||||||
|
if rx is None:
|
||||||
|
rx = ry or 0.
|
||||||
|
if ry is None:
|
||||||
|
ry = rx or 0.
|
||||||
|
rx, ry = float(rx), float(ry)
|
||||||
|
|
||||||
|
d = "M {} {} ".format(x + rx, y) # right of p0
|
||||||
|
d += "L {} {} ".format(x + w - rx, y) # go to p1
|
||||||
|
d += "A {} {} 0 0 1 {} {} ".format(rx, ry, x+w, y+ry) # arc for p1
|
||||||
|
d += "L {} {} ".format(x+w, y+h-ry) # above p2
|
||||||
|
d += "A {} {} 0 0 1 {} {} ".format(rx, ry, x+w-rx, y+h) # arc for p2
|
||||||
|
d += "L {} {} ".format(x+rx, y+h) # right of p3
|
||||||
|
d += "A {} {} 0 0 1 {} {} ".format(rx, ry, x, y+h-ry) # arc for p3
|
||||||
|
d += "L {} {} ".format(x, y+ry) # below p0
|
||||||
|
d += "A {} {} 0 0 1 {} {} z".format(rx, ry, x+rx, y) # arc for p0
|
||||||
|
return d
|
||||||
|
|
||||||
|
x0, y0 = x, y
|
||||||
|
x1, y1 = x + w, y
|
||||||
|
x2, y2 = x + w, y + h
|
||||||
|
x3, y3 = x, y + h
|
||||||
|
|
||||||
d = ("M{} {} L {} {} L {} {} L {} {} z"
|
d = ("M{} {} L {} {} L {} {} L {} {} z"
|
||||||
"".format(x0, y0, x1, y1, x2, y2, x3, y3))
|
"".format(x0, y0, x1, y1, x2, y2, x3, y3))
|
||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def line2pathd(l):
|
def line2pathd(l):
|
||||||
return 'M' + l['x1'] + ' ' + l['y1'] + 'L' + l['x2'] + ' ' + l['y2']
|
return (
|
||||||
|
'M' + l.attrib.get('x1', '0') + ' ' + l.attrib.get('y1', '0')
|
||||||
|
+ 'L' + l.attrib.get('x2', '0') + ' ' + l.attrib.get('y2', '0')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def svg2paths(svg_file_location,
|
def svg2paths(svg_file_location,
|
||||||
return_svg_attributes=False,
|
return_svg_attributes=False,
|
||||||
|
@ -108,7 +149,9 @@ def svg2paths(svg_file_location,
|
||||||
SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
|
SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
svg_file_location (string): the location of the svg file
|
svg_file_location (string or file-like object): the location of the
|
||||||
|
svg file on disk or a file-like object containing the content of a
|
||||||
|
svg file
|
||||||
return_svg_attributes (bool): Set to True and a dictionary of
|
return_svg_attributes (bool): Set to True and a dictionary of
|
||||||
svg-attributes will be extracted and returned. See also the
|
svg-attributes will be extracted and returned. See also the
|
||||||
`svg2paths2()` function.
|
`svg2paths2()` function.
|
||||||
|
@ -132,8 +175,10 @@ def svg2paths(svg_file_location,
|
||||||
list: The list of corresponding path attribute dictionaries.
|
list: The list of corresponding path attribute dictionaries.
|
||||||
dict (optional): A dictionary of svg-attributes (see `svg2paths2()`).
|
dict (optional): A dictionary of svg-attributes (see `svg2paths2()`).
|
||||||
"""
|
"""
|
||||||
if os_path.dirname(svg_file_location) == '':
|
# strings are interpreted as file location everything else is treated as
|
||||||
svg_file_location = os_path.join(getcwd(), svg_file_location)
|
# file-like object and passed to the xml parser directly
|
||||||
|
from_filepath = isinstance(svg_file_location, str) or isinstance(svg_file_location, FilePathLike)
|
||||||
|
svg_file_location = os.path.abspath(svg_file_location) if from_filepath else svg_file_location
|
||||||
|
|
||||||
doc = parse(svg_file_location)
|
doc = parse(svg_file_location)
|
||||||
|
|
||||||
|
@ -152,14 +197,14 @@ def svg2paths(svg_file_location,
|
||||||
# path strings, add to list
|
# path strings, add to list
|
||||||
if convert_polylines_to_paths:
|
if convert_polylines_to_paths:
|
||||||
plins = [dom2dict(el) for el in doc.getElementsByTagName('polyline')]
|
plins = [dom2dict(el) for el in doc.getElementsByTagName('polyline')]
|
||||||
d_strings += [polyline2pathd(pl['points']) for pl in plins]
|
d_strings += [polyline2pathd(pl) for pl in plins]
|
||||||
attribute_dictionary_list += plins
|
attribute_dictionary_list += plins
|
||||||
|
|
||||||
# Use minidom to extract polygon strings from input SVG, convert to
|
# Use minidom to extract polygon strings from input SVG, convert to
|
||||||
# path strings, add to list
|
# path strings, add to list
|
||||||
if convert_polygons_to_paths:
|
if convert_polygons_to_paths:
|
||||||
pgons = [dom2dict(el) for el in doc.getElementsByTagName('polygon')]
|
pgons = [dom2dict(el) for el in doc.getElementsByTagName('polygon')]
|
||||||
d_strings += [polygon2pathd(pg['points']) for pg in pgons]
|
d_strings += [polygon2pathd(pg) for pg in pgons]
|
||||||
attribute_dictionary_list += pgons
|
attribute_dictionary_list += pgons
|
||||||
|
|
||||||
if convert_lines_to_paths:
|
if convert_lines_to_paths:
|
||||||
|
@ -213,3 +258,26 @@ def svg2paths2(svg_file_location,
|
||||||
convert_polylines_to_paths=convert_polylines_to_paths,
|
convert_polylines_to_paths=convert_polylines_to_paths,
|
||||||
convert_polygons_to_paths=convert_polygons_to_paths,
|
convert_polygons_to_paths=convert_polygons_to_paths,
|
||||||
convert_rectangles_to_paths=convert_rectangles_to_paths)
|
convert_rectangles_to_paths=convert_rectangles_to_paths)
|
||||||
|
|
||||||
|
|
||||||
|
def svgstr2paths(svg_string,
|
||||||
|
return_svg_attributes=False,
|
||||||
|
convert_circles_to_paths=True,
|
||||||
|
convert_ellipses_to_paths=True,
|
||||||
|
convert_lines_to_paths=True,
|
||||||
|
convert_polylines_to_paths=True,
|
||||||
|
convert_polygons_to_paths=True,
|
||||||
|
convert_rectangles_to_paths=True):
|
||||||
|
"""Convenience function; identical to svg2paths() except that it takes the
|
||||||
|
svg object as string. See svg2paths() docstring for more
|
||||||
|
info."""
|
||||||
|
# wrap string into StringIO object
|
||||||
|
svg_file_obj = StringIO(svg_string)
|
||||||
|
return svg2paths(svg_file_location=svg_file_obj,
|
||||||
|
return_svg_attributes=return_svg_attributes,
|
||||||
|
convert_circles_to_paths=convert_circles_to_paths,
|
||||||
|
convert_ellipses_to_paths=convert_ellipses_to_paths,
|
||||||
|
convert_lines_to_paths=convert_lines_to_paths,
|
||||||
|
convert_polylines_to_paths=convert_polylines_to_paths,
|
||||||
|
convert_polygons_to_paths=convert_polygons_to_paths,
|
||||||
|
convert_rectangles_to_paths=convert_rectangles_to_paths)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
<svg height="100%" version="1.1" viewBox="0 0 365 365" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M10.0,50.0a40.0,40.0 0 1,0 80.0,0a40.0,40.0 0 1,0 -80.0,0" fill="red" stroke="black" transform="matrix( 1.5 0.0 0.0 0.5 -40.0 20.0)" /><path d="M 150,200 l -50,25" fill="black" stroke="black" transform="matrix( 1.5 0.0 0.0 0.5 -40.0 20.0)" /><path d="M 100 350 l 150 -300" fill="none" stroke="red" /><path d="M 250 50 l 150 300" fill="none" stroke="red" /><path d="M 175 200 l 150 0" fill="none" stroke="green" /><path d="M 100 350 q 150 -300 300 0" fill="none" stroke="blue" /><path d="M97.0,350.0a3.0,3.0 0 1,0 6.0,0a3.0,3.0 0 1,0 -6.0,0" fill="black" stroke="black" /><path d="M247.0,50.0a3.0,3.0 0 1,0 6.0,0a3.0,3.0 0 1,0 -6.0,0" fill="black" stroke="black" /><path d="M397.0,350.0a3.0,3.0 0 1,0 6.0,0a3.0,3.0 0 1,0 -6.0,0" fill="black" stroke="black" /><path d="M200 10L250 190L160 210z" fill="lime" stroke="purple" transform="matrix( 0.1 0.0 0.0 0.1 0.0 0.0)" /></svg>
|
After Width: | Height: | Size: 1.0 KiB |
|
@ -70,7 +70,8 @@
|
||||||
|
|
||||||
<g
|
<g
|
||||||
id="nested group - translate xy"
|
id="nested group - translate xy"
|
||||||
transform="translate(20, 30)">
|
transform="
|
||||||
|
translate(20, 30)">
|
||||||
|
|
||||||
<path
|
<path
|
||||||
d="M 150,200 l -50,25"
|
d="M 150,200 l -50,25"
|
||||||
|
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||||
|
<svg width="100mm" height="100mm" viewBox="-100 -200 500 500" xmlns="http://www.w3.org/2000/svg" version="1.1">
|
||||||
|
<g id="Sketch" transform="scale(1,-1)">
|
||||||
|
<path id="slot" d="
|
||||||
|
M 0 10
|
||||||
|
L 0 80
|
||||||
|
A 30 30 0 1 0 0 140
|
||||||
|
A 10 10 0 0 1 0 100
|
||||||
|
L 100 100
|
||||||
|
A 10 10 0 1 1 100 140
|
||||||
|
A 30 30 0 0 0 100 80
|
||||||
|
L 100 10
|
||||||
|
A 10 10 0 0 0 90 0
|
||||||
|
L 10 0
|
||||||
|
A 10 10 0 0 0 0 10
|
||||||
|
" stroke="#ff0000" stroke-width="0.35 px"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 665 B |
|
@ -1,7 +1,7 @@
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import unittest
|
import unittest
|
||||||
from svgpathtools.bezier import *
|
from svgpathtools.bezier import bezier_point, bezier2polynomial, polynomial2bezier
|
||||||
from svgpathtools.path import bpoints2bezier
|
from svgpathtools.path import bpoints2bezier
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
from __future__ import division, absolute_import, print_function
|
||||||
|
import unittest
|
||||||
|
from svgpathtools import Document
|
||||||
|
from io import StringIO
|
||||||
|
from io import open # overrides build-in open for compatibility with python2
|
||||||
|
from os.path import join, dirname
|
||||||
|
from sys import version_info
|
||||||
|
|
||||||
|
|
||||||
|
class TestDocument(unittest.TestCase):
|
||||||
|
def test_from_file_path_string(self):
|
||||||
|
"""Test reading svg from file provided as path"""
|
||||||
|
doc = Document(join(dirname(__file__), 'polygons.svg'))
|
||||||
|
|
||||||
|
self.assertEqual(len(doc.paths()), 2)
|
||||||
|
|
||||||
|
def test_from_file_path(self):
|
||||||
|
"""Test reading svg from file provided as path"""
|
||||||
|
if version_info >= (3, 6):
|
||||||
|
import pathlib
|
||||||
|
doc = Document(pathlib.Path(__file__).parent / 'polygons.svg')
|
||||||
|
|
||||||
|
self.assertEqual(len(doc.paths()), 2)
|
||||||
|
|
||||||
|
def test_from_file_object(self):
|
||||||
|
"""Test reading svg from file object that has already been opened"""
|
||||||
|
with open(join(dirname(__file__), 'polygons.svg'), 'r') as file:
|
||||||
|
doc = Document(file)
|
||||||
|
|
||||||
|
self.assertEqual(len(doc.paths()), 2)
|
||||||
|
|
||||||
|
def test_from_stringio(self):
|
||||||
|
"""Test reading svg object contained in a StringIO object"""
|
||||||
|
with open(join(dirname(__file__), 'polygons.svg'),
|
||||||
|
'r', encoding='utf-8') as file:
|
||||||
|
# read entire file into string
|
||||||
|
file_content = file.read()
|
||||||
|
# prepare stringio object
|
||||||
|
file_as_stringio = StringIO(file_content)
|
||||||
|
|
||||||
|
doc = Document(file_as_stringio)
|
||||||
|
|
||||||
|
self.assertEqual(len(doc.paths()), 2)
|
||||||
|
|
||||||
|
def test_from_string(self):
|
||||||
|
"""Test reading svg object contained in a string"""
|
||||||
|
with open(join(dirname(__file__), 'polygons.svg'),
|
||||||
|
'r', encoding='utf-8') as file:
|
||||||
|
# read entire file into string
|
||||||
|
file_content = file.read()
|
||||||
|
|
||||||
|
doc = Document.from_svg_string(file_content)
|
||||||
|
|
||||||
|
self.assertEqual(len(doc.paths()), 2)
|
|
@ -2,7 +2,7 @@
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
import unittest
|
import unittest
|
||||||
from svgpathtools import *
|
from svgpathtools import parse_path
|
||||||
|
|
||||||
|
|
||||||
class TestGeneration(unittest.TestCase):
|
class TestGeneration(unittest.TestCase):
|
||||||
|
|
|
@ -1,10 +1,19 @@
|
||||||
|
"""Tests related to SVG groups.
|
||||||
|
|
||||||
|
To run these tests, you can use (from root svgpathtools directory):
|
||||||
|
$ python -m unittest test.test_groups.TestGroups.test_group_flatten
|
||||||
|
"""
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
import unittest
|
import unittest
|
||||||
from svgpathtools import *
|
from svgpathtools import Document, SVG_NAMESPACE, parse_path, Line, Arc
|
||||||
from os.path import join, dirname
|
from os.path import join, dirname
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
# When an assert fails, show the full error message, don't truncate it.
|
||||||
|
unittest.util._MAX_LENGTH = 999999999
|
||||||
|
|
||||||
|
|
||||||
def get_desired_path(name, paths):
|
def get_desired_path(name, paths):
|
||||||
return next(p for p in paths
|
return next(p for p in paths
|
||||||
if p.element.get('{some://testuri}name') == name)
|
if p.element.get('{some://testuri}name') == name)
|
||||||
|
@ -26,7 +35,7 @@ class TestGroups(unittest.TestCase):
|
||||||
# end point relative to the start point
|
# end point relative to the start point
|
||||||
# * name is the path name (value of the test:name attribute in
|
# * name is the path name (value of the test:name attribute in
|
||||||
# the SVG document)
|
# the SVG document)
|
||||||
# * paths is the output of doc.flatten_all_paths()
|
# * paths is the output of doc.paths()
|
||||||
v_s_vals.append(1.0)
|
v_s_vals.append(1.0)
|
||||||
v_e_relative_vals.append(0.0)
|
v_e_relative_vals.append(0.0)
|
||||||
v_s = np.array(v_s_vals)
|
v_s = np.array(v_s_vals)
|
||||||
|
@ -34,11 +43,27 @@ class TestGroups(unittest.TestCase):
|
||||||
|
|
||||||
actual = get_desired_path(name, paths)
|
actual = get_desired_path(name, paths)
|
||||||
|
|
||||||
self.check_values(tf.dot(v_s), actual.path.start)
|
self.check_values(tf.dot(v_s), actual.start)
|
||||||
self.check_values(tf.dot(v_e), actual.path.end)
|
self.check_values(tf.dot(v_e), actual.end)
|
||||||
|
|
||||||
|
def test_group_transform(self):
|
||||||
|
# The input svg has a group transform of "scale(1,-1)", which
|
||||||
|
# can mess with Arc sweeps.
|
||||||
|
doc = Document(join(dirname(__file__), 'negative-scale.svg'))
|
||||||
|
path = doc.paths()[0]
|
||||||
|
self.assertEqual(path[0], Line(start=-10j, end=-80j))
|
||||||
|
self.assertEqual(path[1], Arc(start=-80j, radius=(30+30j), rotation=0.0, large_arc=True, sweep=True, end=-140j))
|
||||||
|
self.assertEqual(path[2], Arc(start=-140j, radius=(20+20j), rotation=0.0, large_arc=False, sweep=False, end=-100j))
|
||||||
|
self.assertEqual(path[3], Line(start=-100j, end=(100-100j)))
|
||||||
|
self.assertEqual(path[4], Arc(start=(100-100j), radius=(20+20j), rotation=0.0, large_arc=True, sweep=False, end=(100-140j)))
|
||||||
|
self.assertEqual(path[5], Arc(start=(100-140j), radius=(30+30j), rotation=0.0, large_arc=False, sweep=True, end=(100-80j)))
|
||||||
|
self.assertEqual(path[6], Line(start=(100-80j), end=(100-10j)))
|
||||||
|
self.assertEqual(path[7], Arc(start=(100-10j), radius=(10+10j), rotation=0.0, large_arc=False, sweep=True, end=(90+0j)))
|
||||||
|
self.assertEqual(path[8], Line(start=(90+0j), end=(10+0j)))
|
||||||
|
self.assertEqual(path[9], Arc(start=(10+0j), radius=(10+10j), rotation=0.0, large_arc=False, sweep=True, end=-10j))
|
||||||
|
|
||||||
def test_group_flatten(self):
|
def test_group_flatten(self):
|
||||||
# Test the Document.flatten_all_paths() function against the
|
# Test the Document.paths() function against the
|
||||||
# groups.svg test file.
|
# groups.svg test file.
|
||||||
# There are 12 paths in that file, with various levels of being
|
# There are 12 paths in that file, with various levels of being
|
||||||
# nested inside of group transforms.
|
# nested inside of group transforms.
|
||||||
|
@ -48,7 +73,7 @@ class TestGroups(unittest.TestCase):
|
||||||
# that are specified by the SVG standard.
|
# that are specified by the SVG standard.
|
||||||
doc = Document(join(dirname(__file__), 'groups.svg'))
|
doc = Document(join(dirname(__file__), 'groups.svg'))
|
||||||
|
|
||||||
result = doc.flatten_all_paths()
|
result = doc.paths()
|
||||||
self.assertEqual(12, len(result))
|
self.assertEqual(12, len(result))
|
||||||
|
|
||||||
tf_matrix_group = np.array([[1.5, 0.0, -40.0],
|
tf_matrix_group = np.array([[1.5, 0.0, -40.0],
|
||||||
|
@ -165,6 +190,14 @@ class TestGroups(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(expected_count, count)
|
self.assertEqual(expected_count, count)
|
||||||
|
|
||||||
|
def test_nested_group(self):
|
||||||
|
# A bug in the flattened_paths_from_group() implementation made it so that only top-level
|
||||||
|
# groups could have their paths flattened. This is a regression test to make
|
||||||
|
# sure that when a nested group is requested, its paths can also be flattened.
|
||||||
|
doc = Document(join(dirname(__file__), 'groups.svg'))
|
||||||
|
result = doc.paths_from_group(['matrix group', 'scale group'])
|
||||||
|
self.assertEqual(len(result), 5)
|
||||||
|
|
||||||
def test_add_group(self):
|
def test_add_group(self):
|
||||||
# Test `Document.add_group()` function and related Document functions.
|
# Test `Document.add_group()` function and related Document functions.
|
||||||
doc = Document(None)
|
doc = Document(None)
|
||||||
|
@ -223,3 +256,10 @@ class TestGroups(unittest.TestCase):
|
||||||
path = parse_path(path_d)
|
path = parse_path(path_d)
|
||||||
svg_path = doc.add_path(path, group=new_leaf)
|
svg_path = doc.add_path(path, group=new_leaf)
|
||||||
self.assertEqual(path_d, svg_path.get('d'))
|
self.assertEqual(path_d, svg_path.get('d'))
|
||||||
|
|
||||||
|
# Test that paths are added to the correct group
|
||||||
|
new_sibling = doc.get_or_add_group(
|
||||||
|
['base_group', 'new_parent', 'new_sibling'])
|
||||||
|
doc.add_path(path, group=new_sibling)
|
||||||
|
self.assertEqual(len(new_sibling), 1)
|
||||||
|
self.assertEqual(path_d, new_sibling[0].get('d'))
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
# Note: This file was taken mostly as is from the svg.path module (v 2.0)
|
# Note: This file was taken mostly as is from the svg.path module (v 2.0)
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
import unittest
|
import unittest
|
||||||
from svgpathtools import *
|
from svgpathtools import Path, Line, QuadraticBezier, CubicBezier, Arc, parse_path
|
||||||
import svgpathtools
|
import svgpathtools
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
@ -247,3 +248,41 @@ class TestParser(unittest.TestCase):
|
||||||
skewX(40)
|
skewX(40)
|
||||||
scale(10 0.5)""")
|
scale(10 0.5)""")
|
||||||
))
|
))
|
||||||
|
|
||||||
|
def test_pathd_init(self):
|
||||||
|
path0 = Path('')
|
||||||
|
path1 = parse_path("M 100 100 L 300 100 L 200 300 z")
|
||||||
|
path2 = Path("M 100 100 L 300 100 L 200 300 z")
|
||||||
|
self.assertEqual(path1, path2)
|
||||||
|
|
||||||
|
path1 = parse_path("m 100 100 L 300 100 L 200 300 z", current_pos=50+50j)
|
||||||
|
path2 = Path("m 100 100 L 300 100 L 200 300 z")
|
||||||
|
self.assertNotEqual(path1, path2)
|
||||||
|
|
||||||
|
path1 = parse_path("m 100 100 L 300 100 L 200 300 z")
|
||||||
|
path2 = Path("m 100 100 L 300 100 L 200 300 z", current_pos=50 + 50j)
|
||||||
|
self.assertNotEqual(path1, path2)
|
||||||
|
|
||||||
|
path1 = parse_path("m 100 100 L 300 100 L 200 300 z", current_pos=50 + 50j)
|
||||||
|
path2 = Path("m 100 100 L 300 100 L 200 300 z", current_pos=50 + 50j)
|
||||||
|
self.assertEqual(path1, path2)
|
||||||
|
|
||||||
|
path1 = parse_path("m 100 100 L 300 100 L 200 300 z", 50+50j)
|
||||||
|
path2 = Path("m 100 100 L 300 100 L 200 300 z")
|
||||||
|
self.assertNotEqual(path1, path2)
|
||||||
|
|
||||||
|
path1 = parse_path("m 100 100 L 300 100 L 200 300 z")
|
||||||
|
path2 = Path("m 100 100 L 300 100 L 200 300 z", 50 + 50j)
|
||||||
|
self.assertNotEqual(path1, path2)
|
||||||
|
|
||||||
|
path1 = parse_path("m 100 100 L 300 100 L 200 300 z", 50 + 50j)
|
||||||
|
path2 = Path("m 100 100 L 300 100 L 200 300 z", 50 + 50j)
|
||||||
|
self.assertEqual(path1, path2)
|
||||||
|
|
||||||
|
def test_issue_99(self):
|
||||||
|
p = Path("M 100 250 S 200 200 200 250 300 300 300 250")
|
||||||
|
self.assertEqual(p.d(useSandT=True), 'M 100.0,250.0 S 200.0,200.0 200.0,250.0 S 300.0,300.0 300.0,250.0')
|
||||||
|
self.assertEqual(p.d(),
|
||||||
|
'M 100.0,250.0 C 100.0,250.0 200.0,200.0 200.0,250.0 C 200.0,300.0 300.0,300.0 300.0,250.0')
|
||||||
|
self.assertNotEqual(p.d(),
|
||||||
|
'M 100.0,250.0 C 100.0,250.0 200.0,200.0 200.0,250.0 C 200.0,250.0 300.0,300.0 300.0,250.0')
|
||||||
|
|
1104
test/test_path.py
|
@ -4,7 +4,7 @@ import unittest
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
# Internal dependencies
|
# Internal dependencies
|
||||||
from svgpathtools import *
|
from svgpathtools import rational_limit
|
||||||
|
|
||||||
|
|
||||||
class Test_polytools(unittest.TestCase):
|
class Test_polytools(unittest.TestCase):
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
from __future__ import division, absolute_import, print_function
|
||||||
|
import unittest
|
||||||
|
from svgpathtools import SaxDocument
|
||||||
|
from os.path import join, dirname
|
||||||
|
|
||||||
|
|
||||||
|
class TestSaxGroups(unittest.TestCase):
|
||||||
|
|
||||||
|
def check_values(self, v, z):
|
||||||
|
# Check that the components of 2D vector v match the components
|
||||||
|
# of complex number z
|
||||||
|
self.assertAlmostEqual(v[0], z.real)
|
||||||
|
self.assertAlmostEqual(v[1], z.imag)
|
||||||
|
|
||||||
|
def test_parse_display(self):
|
||||||
|
doc = SaxDocument(join(dirname(__file__), 'transforms.svg'))
|
||||||
|
# doc.display()
|
||||||
|
for i, node in enumerate(doc.tree):
|
||||||
|
values = node
|
||||||
|
path_value = values['d']
|
||||||
|
matrix = values['matrix']
|
||||||
|
self.assertTrue(values is not None)
|
||||||
|
self.assertTrue(path_value is not None)
|
||||||
|
if i == 0:
|
||||||
|
self.assertEqual(values['fill'], 'red')
|
||||||
|
if i == 8 or i == 7:
|
||||||
|
self.assertEqual(matrix, None)
|
||||||
|
if i == 9:
|
||||||
|
self.assertEqual(values['fill'], 'lime')
|
|
@ -1,7 +1,16 @@
|
||||||
from __future__ import division, absolute_import, print_function
|
from __future__ import division, absolute_import, print_function
|
||||||
import unittest
|
import unittest
|
||||||
from svgpathtools import *
|
from svgpathtools import Path, Line, Arc, svg2paths, svgstr2paths
|
||||||
|
from io import StringIO
|
||||||
|
from io import open # overrides build-in open for compatibility with python2
|
||||||
|
import os
|
||||||
from os.path import join, dirname
|
from os.path import join, dirname
|
||||||
|
from sys import version_info
|
||||||
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from svgpathtools.svg_to_paths import rect2pathd
|
||||||
|
|
||||||
|
|
||||||
class TestSVG2Paths(unittest.TestCase):
|
class TestSVG2Paths(unittest.TestCase):
|
||||||
def test_svg2paths_polygons(self):
|
def test_svg2paths_polygons(self):
|
||||||
|
@ -50,3 +59,78 @@ class TestSVG2Paths(unittest.TestCase):
|
||||||
self.assertTrue(len(path_circle)==2)
|
self.assertTrue(len(path_circle)==2)
|
||||||
self.assertTrue(path_circle==path_circle_correct)
|
self.assertTrue(path_circle==path_circle_correct)
|
||||||
self.assertTrue(path_circle.isclosed())
|
self.assertTrue(path_circle.isclosed())
|
||||||
|
|
||||||
|
# test for issue #198 (circles not being closed)
|
||||||
|
svg = u"""<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" width="40mm" height="40mm"
|
||||||
|
viewBox="0 0 40 40" version="1.1">
|
||||||
|
|
||||||
|
<g id="layer">
|
||||||
|
<circle id="c1" cx="20.000" cy="20.000" r="11.000" />
|
||||||
|
<circle id="c2" cx="20.000" cy="20.000" r="5.15" />
|
||||||
|
</g>
|
||||||
|
</svg>"""
|
||||||
|
tmpdir = tempfile.mkdtemp()
|
||||||
|
svgfile = os.path.join(tmpdir, 'test.svg')
|
||||||
|
with open(svgfile, 'w') as f:
|
||||||
|
f.write(svg)
|
||||||
|
paths, _ = svg2paths(svgfile)
|
||||||
|
self.assertEqual(len(paths), 2)
|
||||||
|
self.assertTrue(paths[0].isclosed())
|
||||||
|
self.assertTrue(paths[1].isclosed())
|
||||||
|
shutil.rmtree(tmpdir)
|
||||||
|
|
||||||
|
def test_rect2pathd(self):
|
||||||
|
non_rounded = {"x":"10", "y":"10", "width":"100","height":"100"}
|
||||||
|
self.assertEqual(rect2pathd(non_rounded), 'M10.0 10.0 L 110.0 10.0 L 110.0 110.0 L 10.0 110.0 z')
|
||||||
|
rounded = {"x":"10", "y":"10", "width":"100","height":"100", "rx":"15", "ry": "12"}
|
||||||
|
self.assertEqual(rect2pathd(rounded), "M 25.0 10.0 L 95.0 10.0 A 15.0 12.0 0 0 1 110.0 22.0 L 110.0 98.0 A 15.0 12.0 0 0 1 95.0 110.0 L 25.0 110.0 A 15.0 12.0 0 0 1 10.0 98.0 L 10.0 22.0 A 15.0 12.0 0 0 1 25.0 10.0 z")
|
||||||
|
|
||||||
|
def test_from_file_path_string(self):
|
||||||
|
"""Test reading svg from file provided as path"""
|
||||||
|
paths, _ = svg2paths(join(dirname(__file__), 'polygons.svg'))
|
||||||
|
|
||||||
|
self.assertEqual(len(paths), 2)
|
||||||
|
|
||||||
|
def test_from_file_path(self):
|
||||||
|
"""Test reading svg from file provided as pathlib POSIXPath"""
|
||||||
|
if version_info >= (3, 6):
|
||||||
|
import pathlib
|
||||||
|
paths, _ = svg2paths(pathlib.Path(__file__).parent / 'polygons.svg')
|
||||||
|
|
||||||
|
self.assertEqual(len(paths), 2)
|
||||||
|
|
||||||
|
def test_from_file_object(self):
|
||||||
|
"""Test reading svg from file object that has already been opened"""
|
||||||
|
with open(join(dirname(__file__), 'polygons.svg'), 'r') as file:
|
||||||
|
paths, _ = svg2paths(file)
|
||||||
|
|
||||||
|
self.assertEqual(len(paths), 2)
|
||||||
|
|
||||||
|
def test_from_stringio(self):
|
||||||
|
"""Test reading svg object contained in a StringIO object"""
|
||||||
|
with open(join(dirname(__file__), 'polygons.svg'),
|
||||||
|
'r', encoding='utf-8') as file:
|
||||||
|
# read entire file into string
|
||||||
|
file_content = file.read()
|
||||||
|
# prepare stringio object
|
||||||
|
file_as_stringio = StringIO(file_content)
|
||||||
|
|
||||||
|
paths, _ = svg2paths(file_as_stringio)
|
||||||
|
|
||||||
|
self.assertEqual(len(paths), 2)
|
||||||
|
|
||||||
|
def test_from_string(self):
|
||||||
|
"""Test reading svg object contained in a string"""
|
||||||
|
with open(join(dirname(__file__), 'polygons.svg'),
|
||||||
|
'r', encoding='utf-8') as file:
|
||||||
|
# read entire file into string
|
||||||
|
file_content = file.read()
|
||||||
|
|
||||||
|
paths, _ = svgstr2paths(file_content)
|
||||||
|
|
||||||
|
self.assertEqual(len(paths), 2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?xml version="1.0" ?>
|
||||||
|
<svg
|
||||||
|
version="1.1"
|
||||||
|
viewBox="0 0 365 365"
|
||||||
|
height="100%"
|
||||||
|
width="100%"
|
||||||
|
xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g
|
||||||
|
id="matrix group"
|
||||||
|
transform="matrix(1.5 0.0 0.0 0.5 -40.0 20.0)" stroke="black" style="fill:red">
|
||||||
|
<circle cx="50" cy="50" r="40" stroke-width="3" />
|
||||||
|
<g id="scale group" transform="scale(1.25)"></g>
|
||||||
|
|
||||||
|
<g>
|
||||||
|
<path
|
||||||
|
d="M 150,200 l -50,25"
|
||||||
|
fill="black"
|
||||||
|
stroke="black"
|
||||||
|
stroke-width="3"
|
||||||
|
/>
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
<path id="lineAB" d="M 100 350 l 150 -300" stroke="red"
|
||||||
|
stroke-width="3" fill="none" />
|
||||||
|
<path id="lineBC" d="M 250 50 l 150 300" stroke="red"
|
||||||
|
stroke-width="3" fill="none" />
|
||||||
|
<path d="M 175 200 l 150 0" stroke="green" stroke-width="3"
|
||||||
|
fill="none" />
|
||||||
|
<path d="M 100 350 q 150 -300 300 0" stroke="blue"
|
||||||
|
stroke-width="5" fill="none" />
|
||||||
|
<!-- Mark relevant points -->
|
||||||
|
<g stroke="black" stroke-width="3" fill="black">
|
||||||
|
<circle id="pointA" cx="100" cy="350" r="3" />
|
||||||
|
<circle id="pointB" cx="250" cy="50" r="3" />
|
||||||
|
<circle id="pointC" cx="400" cy="350" r="3" />
|
||||||
|
</g>
|
||||||
|
<!-- Label the points -->
|
||||||
|
<g font-size="30" font-family="sans-serif" fill="black" stroke="none"
|
||||||
|
text-anchor="middle">
|
||||||
|
<text x="100" y="350" dx="-30">A</text>
|
||||||
|
<text x="250" y="50" dy="-10">B</text>
|
||||||
|
<text x="400" y="350" dx="30">C</text>
|
||||||
|
</g>
|
||||||
|
|
||||||
|
<g transform="scale(0.1)">
|
||||||
|
<polygon points="200,10 250,190 160,210" style="fill:lime;stroke:purple;stroke-width:1" />
|
||||||
|
</g>
|
||||||
|
|
||||||
|
</svg>
|
After Width: | Height: | Size: 1.5 KiB |