Skip to main content

dinamika_cpu/
lib.rs

1//! `dinamika-cpu` — a raster 2D renderer on the CPU.
2//!
3//! Creating images, building vector contours, anti-aliased fill and stroke,
4//! gradients and blend modes. The only external dependency is the
5//! [`png`](https://docs.rs/png) crate for saving frames.
6//!
7//! # Main types
8//!
9//! - [`Pixmap`] — a pixel buffer (premultiplied RGBA) and the entry point for
10//!   drawing. It can composite another [`Pixmap`] on top of itself
11//!   ([`Pixmap::draw_pixmap`]) and decode PNG ([`Pixmap::decode_png`]).
12//! - [`Path`] / [`PathBuilder`] — vector contours with Bézier curves; there are
13//!   ready-made rectangle, rounded rectangle, ellipse and circle.
14//! - [`Paint`] — a brush: color/gradient/texture ([`Shader`]), blend mode
15//!   ([`BlendMode`]), anti-aliasing.
16//! - [`Shader`] — a color source: solid, linear/radial/conic
17//!   gradient or texture ([`Pattern`]).
18//! - [`Stroke`] — stroke parameters (width, caps, joins, dashes,
19//!   "hairline").
20//! - [`Mask`] — a clipping mask (`overflow: hidden`).
21//! - [`Transform`] — an affine transformation.
22//! - [`Font`] — a TrueType/OpenType face ([`ttf-parser`]) that turns text into a
23//!   fillable [`Path`]; [`Pixmap::fill_text`] draws it in one call.
24//!
25//! # Example
26//!
27//! ```
28//! use dinamika_cpu::*;
29//!
30//! let mut pixmap = Pixmap::new(200, 200).unwrap();
31//! pixmap.fill(Color::WHITE);
32//!
33//! let path = PathBuilder::from_circle(100.0, 100.0, 80.0).unwrap();
34//! let paint = Paint::from_color(Color::from_rgba8(220, 40, 90, 255));
35//! pixmap.fill_path(&path, &paint, FillRule::NonZero, Transform::identity(), None);
36//!
37//! // The ready pixels (premultiplied RGBA) live in `pixmap.data()`.
38//! assert_eq!(pixmap.data().len(), 200 * 200 * 4);
39//! ```
40//!
41//! # Known limitations
42//!
43//! Deliberate trade-offs for the sake of MVP simplicity (details are in the
44//! documentation of the respective modules):
45//!
46//! - **Color is computed in sRGB, not in linear light.** Blending and
47//!   gradient interpolation operate directly on the gamma components — as in
48//!   most 8-bit engines. Gradients are slightly "dirtier", semi-transparent
49//!   edges darken a little (see the `paint` module).
50//! - **Stroke width is isotropic.** Under non-uniform scaling or shearing the
51//!   stroke comes out circular rather than elliptical (see
52//!   [`Pixmap::stroke_path`]).
53//! - **Thin AA seams are possible at the junctions of stroke stamps**
54//!   (segment↔join); acceptable for an MVP (see the `stroke` module).
55//! - **Text layout is minimal.** [`Font`] places glyphs by their horizontal
56//!   advance only — no kerning, shaping or bidi (see the `text` module).
57//!
58//! [`ttf-parser`]: https://docs.rs/ttf-parser
59
60mod color;
61mod geometry;
62mod paint;
63mod path;
64mod pixmap;
65mod raster;
66mod text;
67
68pub use color::{Color, ColorU8, PremultipliedColor, PremultipliedColorU8};
69pub use geometry::{Point, Rect, Transform};
70pub use paint::{
71    BlendMode, ConicGradient, FilterQuality, GradientStop, LinearGradient, Paint, Pattern,
72    RadialGradient, Shader, SpreadMode,
73};
74pub use path::stroke::{LineCap, LineJoin, Stroke};
75pub use path::{FillRule, Path, PathBuilder, PathSegment};
76pub use pixmap::Pixmap;
77pub use raster::mask::Mask;
78pub use text::{Font, FontError};