- Zig 99.6%
- Shell 0.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
`fromClamped` takes straight alpha and returns a pre-multiplied pixel. It rounded each channel to a byte first and then multiplied those bytes, which rounds twice -- and the two errors do not cancel. `rgba(0, 128, 128, 0.45)` is the case that shows it: the exact green is 255 * 0.502 * 0.45, and going through a byte first lands a level under that. Not a rounding you would notice on one pixel, and it is on *every* pixel of every shape painted with a partial alpha, so a flat fill comes out uniformly a level off. Multiplying while the values are still floating point and rounding once at the end fixes it. zig-svg's oracle has an `opacity="0.45"` fixture that went from a mean difference of 0.667 against resvg to 0.000, and none of its other 187 fixtures moved. The one test expectation that changed is the same half landing the other way: `fromClamped(0.3, 0.6, 0.9, 0.5)`'s green is exactly 76.5, which now rounds away from zero to 77 where two roundings gave 76. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fv1ixLGPVnP6vse6WonSBo |
||
| .github/workflows | ||
| build-support/scripts | ||
| spec | ||
| src | ||
| .envrc | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| CHANGELOG.md | ||
| CONTRIBUTING.md | ||
| COPYING | ||
| flake.lock | ||
| flake.nix | ||
| LICENSE | ||
| README.md | ||
See how the logo was made! | Usage | Read the docs | See more examples
About
z2d is a 2D graphics library whose main purpose is to raster shapes composed of vector primitives: lines and cubic Beziers, e.g., things you would need if you were rendering something like an SVG file, or rendering shapes directly for UI elements. It also provides a (growing) API for image manipulation, which mainly supports our vector rasterization features, but can also be worked with directly at the lower level.
Our drawing model is (loosely) inspired by
Cairo: most operations take place through the
Context, which connect Patterns (pixel/color sources) and Surfaces
(drawing targets/buffers). Paths contain the vector data for filling and
stroking operations, and so on.
Every component of z2d can be worked with directly in an unmanaged fashion
without the Context as well, if so desired. Surfaces can be interfaced with
directly, Surface and Path can be used with static buffers (in addition to
their traditional unmanaged variant), and the painter functions for filling
and stroking can be called directly with the output of these. For these cases,
Context serves as a reference example. Additionally, plumbing further into
the painter package can demonstrate how functions in the compositor package
can be worked with at the lower level. Additional supporting functionality
(e.g., gradient sources, text, etc.) can also be worked with individually to
the extent that it makes sense to do so.
What's supported
Currently:
- Basic rendering of lines and cubic Beziers, with helpers for arcs (circles native, ellipses through transformations).
- Filling and stroking:
- Miter, bevel, and round join supported for stroking.
- Butt, square, and round caps supported for stroking.
- Dashed lines supported along with offsets for tweaking alignment of patterns to shapes, and zero-length dash stops to draw dotted lines.
- Text rendering: Latin left-to-right text rendering, kerning/GPOS support, diacritics and composite glyphs supported. Fonts can be supplied either on the file system or via external buffer.
- Transformations: rotate, scale, translate, and other operations via direct manipulations of the affine matrix.
- Composition:
- Single pixel sources and linear, radial, and conic gradients supported. Access to lower-level compositor primitives is supplied to allow for manipulation of surfaces outside of higher-level drawing operations.
- 28 compositor operators supported across the set of Porter-Duff and PDF blend modes.
- Pixel formats:
- RGBA, RGB, and alpha-only in 8, 4, 2, and 1-bit formats.
- Color spaces:
- Linear, sRGB, and HSL currently supported for specifying high-level color. Interpolation supported in all color spaces. More color spaces are planed.
- Exporting:
- Rudimentary PNG export supported; alpha-channel formats export to greyscale.
- Support for explicitly specifying output RGB profile to assist with proper color management.
The current plan is to work towards writing a reasonably feature-complete SVG renderer, with the ability to utilize the same primitives to perform other vector rasterization, suitable for UI design and other similar tasks.
Usage
zig fetch --save git+https://github.com/vancluever/z2d#[tag or commit]
Note that Zig 0.16.0 or higher is required. For Zig 0.15.x support, the last supported version is 0.10.0.
The zig-0.17 branch can be used for the current development version of Zig.
Documentation and examples
View the documentation for the latest release at: https://z2d.vancluevertech.com/docs
See the spec/ directory for a number of rudimentary usage examples.
Contributing
Note that public pull requests to this repository are closed. See
CONTRIBUTING.md for more details.
LICENSE and acknowledgments
z2d itself is licensed MPL 2.0; see the LICENSE file for further details.
Code examples in the spec/ directory are licensed 0BSD, this means
you can use them freely to integrate z2d.
The z2d logo is Copyright © 2024-2026 Chris Marchesi and licensed CC-BY-SA 4.0. Portions of the z2d logo are derived from the Zig logo and logomark, which are also licensed CC-BY-SA 4.0. To view a copy of the license, visit https://creativecommons.org/licenses/by-sa/4.0/.