Metadata-Version: 2.5
Name: vec_io
Version: 0.7.1
Summary: Vector graphics in Python: read SVG and PDF; write PDF, SVG, EPS, PNG, JPEG, JPEG XL, AVIF
Project-URL: Repository, https://github.com/ajgouhier/vec_io
Author-email: Arthur Gouhier <ajgouhier@gmail.com>
License-Expression: BSD-3-Clause
License-File: LICENSE
Requires-Python: >=3.11
Provides-Extra: dev
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: numpy; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: ziglang==0.15.1; extra == 'dev'
Description-Content-Type: text/markdown

# vec_io

Vector graphics in Python: read SVG and PDF, draw pages and plot data, write PDF, SVG,
SVGZ, EPS, PNG, JPEG, JPEG XL, AVIF or TIFF.
No dependencies: a bundled C library does the heavy lifting.

## Install

```sh
pip install vec_io
```

Python 3.11+. Wheels hold the C library prebuilt for Linux (x86_64, aarch64; glibc or
musl), Windows (x86_64, arm64) and macOS 13+ (arm64). Elsewhere pip builds it from the
sdist with the local C compiler (gcc or clang; on Windows clang or MinGW, not MSVC). Without
the library, drawing calls still record, but every output and the bulk calls raise
`NativeLibraryError`. `vec_io doctor` checks the installation.

## Example

```python
import vec_io

svg_text = ('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">'
            '<circle cx="50" cy="50" r="40" fill="#e30613"/></svg>')
vec_io.convert(svg_text, "circle.pdf")                 # SVG text, a path or bytes
vec_io.convert("drawing.svg", "drawing.png", dpi=192)  # the format from the extension

doc = vec_io.Document(unit="mm", title="Report")       # one unit for pages, calls and fonts
page = doc.add_page(width=120, height=60)              # origin top-left
page.draw_rect(10, 10, 100, 40, rx=3, fill="#1f6fb2")
page.draw_text("Hello", 20, 35, fill="white", font_size=12, font_weight="bold")
doc.save("hello.pdf")
doc.save("hello.svg")                                  # the same record, another format
```

A document records what is drawn; each output (`write`, `save`, `render`) is made from the
record with its own options, and drawing may go on after a save. In a notebook, pages,
documents, drawings and images show themselves as SVG.

## Features

* **Convert** SVG and PDF to every output format: shapes, text with embedded font subsets,
  gradients, patterns, clips, masks, markers, filters, images and CSS. Blank borders cut
  (`--trim`), the output resized (`--width 8.5in`, `--size us --margin 1in`), images
  resampled (`--dpi 300`).
* **Draw on pages** with `Document` and `Page` (SVG's style names, a style per call or per
  `state()`, any unit): pages, links, bookmarks, metadata, placed SVG and PDF files, text
  along paths, filters on any group; or build drawings of elements with `vec_io.draw`.
* **Read PDF** (and Illustrator `.ai`) into documents: text stays text, every font kind,
  encrypted and damaged files, colour spaces and overprint kept; `PDFFile` merges, splits,
  rotates and crops PDF files without interpreting them.
* **Lay out text**: paragraphs in boxes, justified, hyphenated, greedy or Knuth–Plass,
  rich text with links, overflow into further boxes; bidi; shaping and measuring without a
  page (`vec_io.text`).
* **Plot data**: markers, lines of millions of points, areas, segments, bars, polygons and
  grids of cells, from sequences or buffers (numpy arrays); `simplify` leaves out what
  does not show. Axes and colour maps are [plot_io](https://github.com/ajgouhier/plot_io)'s.
* **Fonts**: installed fonts, font files, `@font-face`, variable fonts; a family that is not
  installed is downloaded once and cached (Google Fonts, URW, CTAN, Fontsource, Fontshare).
* **Print and archive**: CMYK, spot colours, ink limit, ICC output intent; PDF/A-2b, 2u and
  3b with attachments; transparency flattened for EPS and PDF.
* **Untrusted input**: `vec_io.Limits` bounds what a call may spend; past it, `LimitError`.
* **Small files**: PNG in its smallest lossless layout or a lossy palette, progressive
  JPEG, JPEG XL and AVIF when their libraries are installed.

## Command line

```sh
vec_io convert drawing.svg out.pdf                 # any output format
vec_io convert --trim --margin 2mm figure.svg figure.png
vec_io convert report.pdf "page-{page:02}.svg"     # one file per page
vec_io pages a.pdf b.svg -o out.pdf --select 1-3,5 --nup 2x2 --size a4
vec_io info report.pdf --json                      # pages, fonts, images, metadata
vec_io fonts find "Helvetica"                      # the file a family resolves to
vec_io text report.pdf                             # the text, in reading order
```

Every command takes `-` for stdin or stdout; `vec_io COMMAND --help` lists its flags.

## Documentation

* [Tutorial](https://github.com/ajgouhier/vec_io/blob/main/docs/tutorial.md): first steps.
* [Examples](https://github.com/ajgouhier/vec_io/blob/main/docs/examples.md): nine scripts
  with their images, and recipes.
* [API](https://github.com/ajgouhier/vec_io/blob/main/docs/api.md): the guide by topic:
  conventions, drawing, text, output options, SVG support, fonts, limits, the command line.
* [Reference](https://github.com/ajgouhier/vec_io/tree/main/docs/reference): a page per
  class.

## Limits

* Complex scripts (Arabic, Indic, Thai…) are joined only when HarfBuzz is installed; without
  it they are drawn unjoined, with a warning.
* Text needs an outline font; with none installed, supplied or downloaded, `NoFontError`.
* Filters are pixels in PDF and EPS, `<filter>` elements in SVG.
* URLs an SVG refers to are fetched unless `network=False` (use it for untrusted input).
* Overprint is kept in PDF output, not simulated in images. Animation and scripts are
  ignored.

## Development

```sh
python tools/check.py                      # every check, in one gate (--fast, --list)
python -m unittest discover -s tests -t tests
python tools/gen_docs.py                   # the generated parts of docs/ (--check, --images)
python tools/regress.py --render --check   # output against HEAD on tests/corpus
pip install --require-hashes -r tools/zig.lock
python tools/build_native.py               # vec_io/native/bin/* (not in git)
```

`check.py` runs the suite, ruff, mypy, the docs (generated parts, code blocks, links,
images), the examples (their SVGs equal the committed ones where Helvetica is installed),
the binaries rebuilt byte for byte, the fuzz suite, the corpus through poppler and Chrome.
Tests use their own font (`tests/fonts`) and never download. The C library and its rules
are described in
[vec_io/native/README.md](https://github.com/ajgouhier/vec_io/blob/main/vec_io/native/README.md).
