thorvg 0.4.2

Safe Rust bindings to the ThorVG vector graphics library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Unicode text rendering: fonts, layout, and glyph metrics.
//!
//! Wraps the [`ThorVG` C API](https://www.thorvg.org/c-native).

use alloc::ffi::CString;
use alloc::string::String;

use crate::color::Rgb;
use crate::error::{Error, Result};
use crate::gradient::{LinearGradient, RadialGradient};
use crate::paint::{Paint, Point};
use thorvg_sys as sys;

/// Text wrapping mode for a [`Text`] object.
///
/// Controls how text that exceeds the layout box (set via
/// [`Text::set_layout`]) is broken across lines. Maps to the C
/// `Tvg_Text_Wrap` enum. The default is [`None`](Self::None).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TextWrap {
    /// No wrapping; text overflows the layout box.
    None,
    /// Breaks at any character that exceeds the layout width.
    Character,
    /// Breaks at word boundaries (whitespace).
    Word,
    /// Word-aware wrapping that falls back to character breaks for
    /// words too long to fit a line.
    Smart,
    /// Truncates overflowing text and appends an ellipsis (`…`).
    Ellipsis,
}

impl TextWrap {
    fn to_raw(self) -> sys::Tvg_Text_Wrap {
        match self {
            TextWrap::None => sys::Tvg_Text_Wrap::TVG_TEXT_WRAP_NONE,
            TextWrap::Character => sys::Tvg_Text_Wrap::TVG_TEXT_WRAP_CHARACTER,
            TextWrap::Word => sys::Tvg_Text_Wrap::TVG_TEXT_WRAP_WORD,
            TextWrap::Smart => sys::Tvg_Text_Wrap::TVG_TEXT_WRAP_SMART,
            TextWrap::Ellipsis => sys::Tvg_Text_Wrap::TVG_TEXT_WRAP_ELLIPSIS,
        }
    }
}

/// Vertical font metrics for a [`Text`] object.
///
/// Reflect the font size set on the text object but exclude any
/// transform (scale, rotation, translation). Obtained from
/// [`Text::text_metrics`].
#[derive(Debug, Clone, Copy)]
pub struct TextMetrics {
    /// Distance from the baseline to the top of the highest glyph
    /// (usually positive).
    pub ascent: f32,
    /// Distance from the baseline to the bottom of the lowest glyph
    /// (usually negative, following the TTF convention).
    pub descent: f32,
    /// Additional recommended spacing (leading) between lines.
    pub linegap: f32,
    /// Total vertical advance between lines: `ascent - descent +
    /// linegap`.
    pub advance: f32,
}

/// Layout metrics of a single glyph.
///
/// Reflect the font size set on the text object but exclude any
/// transform. The bounding box is given in the glyph's local
/// coordinate space. Obtained from [`Text::glyph_metrics`].
#[derive(Debug, Clone, Copy)]
pub struct GlyphMetrics {
    /// Advance of the pen position along the baseline (inline
    /// direction) to the next glyph's origin.
    pub advance: f32,
    /// Left-side bearing — offset from the inline-axis origin to the
    /// glyph's edge.
    pub bearing: f32,
    /// Minimum (lower-left) corner of the glyph's bounding box.
    pub min: Point,
    /// Maximum (upper-right) corner of the glyph's bounding box.
    pub max: Point,
}

/// A paint object for rendering Unicode text.
///
/// A renderable text object needs at least a font family
/// ([`set_font`](Self::set_font)), a size ([`set_size`](Self::set_size)),
/// and content ([`set_text`](Self::set_text)). Fonts themselves are
/// loaded into the engine globally; see
/// [`Thorvg::load_font_data`](crate::Thorvg::load_font_data).
///
/// The lifetime `'eng` ties this text object to a [`Thorvg`](crate::Thorvg) engine
/// instance. Create text objects via [`Thorvg::text()`](crate::Thorvg::text).
pub struct Text<'eng> {
    raw: sys::Tvg_Paint,
    owned: bool,
    _engine: core::marker::PhantomData<&'eng ()>,
}

// SAFETY: Same rationale as other ThorVG handle types — exclusive
// ownership of a C heap object; global state is mutex-protected.
unsafe impl Send for Text<'_> {}

impl Text<'_> {
    /// Creates a new Text object.
    pub(crate) fn new() -> Result<Self> {
        let raw = unsafe { sys::tvg_text_new() };
        if raw.is_null() {
            return Err(Error::FailedAllocation);
        }
        Ok(Self {
            raw,
            owned: true,
            _engine: core::marker::PhantomData,
        })
    }

    /// Sets the font family used to render the text.
    ///
    /// `name` must match a font registered with the engine via
    /// [`Thorvg::load_font_data`](crate::Thorvg::load_font_data). This
    /// only selects the family; use [`set_size`](Self::set_size) for the
    /// size.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if `name` contains an
    /// interior NUL byte, or [`Error::InsufficientCondition`] if the
    /// named font cannot be found.
    pub fn set_font(&mut self, name: &str) -> Result<()> {
        let c_name = CString::new(name)?;
        Error::from_raw(unsafe { sys::tvg_text_set_font(self.raw, c_name.as_ptr()) })
    }

    /// Sets the font size in points.
    ///
    /// Fractional sizes are supported for sub-pixel rendering and
    /// animation.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if `size <= 0.0`.
    pub fn set_size(&mut self, size: f32) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_set_size(self.raw, size) })
    }

    /// Sets the UTF-8 text content to be rendered.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if `text` contains an
    /// interior NUL byte.
    pub fn set_text(&mut self, text: &str) -> Result<()> {
        let c_text = CString::new(text)?;
        Error::from_raw(unsafe { sys::tvg_text_set_text(self.raw, c_text.as_ptr()) })
    }

    /// Returns the currently assigned text (UTF-8), or `None` if none
    /// has been set.
    ///
    /// The returned [`String`] is a copy, so it is independent of the
    /// text object's later mutations.
    ///
    /// *Experimental in `ThorVG`; the API may change.*
    pub fn text(&self) -> Option<String> {
        let ptr = unsafe { sys::tvg_text_get_text(self.raw) };
        if ptr.is_null() {
            None
        } else {
            Some(
                unsafe { core::ffi::CStr::from_ptr(ptr) }
                    .to_string_lossy()
                    .into_owned(),
            )
        }
    }

    /// Sets the solid fill color of the text, with channels in
    /// `0..=255`.
    ///
    /// Text color is RGB only (no alpha), so this takes [`Rgb`] rather
    /// than [`Rgba`](crate::Rgba); use [`Paint::set_opacity`] for
    /// translucency. A solid color and a gradient fill are mutually
    /// exclusive — whichever was set last applies.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_color(&mut self, color: Rgb) -> Result<()> {
        let Rgb { r, g, b } = color;
        Error::from_raw(unsafe { sys::tvg_text_set_color(self.raw, r, g, b) })
    }

    /// Sets the per-axis text alignment or anchor.
    ///
    /// Each value is in `0.0..=1.0`: `0.0` is left/top, `0.5` is
    /// center/middle, `1.0` is right/bottom. On an axis constrained by
    /// [`set_layout`](Self::set_layout) this aligns within the layout
    /// box; on an unconstrained axis it anchors the text bounds to the
    /// paint position.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_align(&mut self, x: f32, y: f32) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_align(self.raw, x, y) })
    }

    /// Sets the virtual layout box (constraints) for the text.
    ///
    /// A non-zero `w` or `h` constrains that axis, so the text may
    /// wrap and align inside it; `0.0` leaves the axis unconstrained,
    /// and [`set_align`](Self::set_align) then anchors on that axis.
    /// This sets constraints only; alignment is controlled by
    /// [`set_align`](Self::set_align).
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_layout(&mut self, w: f32, h: f32) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_layout(self.raw, w, h) })
    }

    /// Applies an italic (oblique) slant by shearing along the X-axis.
    ///
    /// `shear` is in `0.0..=0.5` (`0.0` = upright); values outside the
    /// range are clamped by the engine. The recommended value is
    /// `0.18`. This simulates italics with a transform and does not
    /// require an italic font.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_italic(&mut self, shear: f32) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_set_italic(self.raw, shear) })
    }

    /// Sets the text wrapping mode.
    ///
    /// Wrapping applies within the layout box set by
    /// [`set_layout`](Self::set_layout).
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_wrap(&mut self, mode: TextWrap) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_wrap_mode(self.raw, mode.to_raw()) })
    }

    /// Returns the number of lines after layout and wrapping.
    ///
    /// Reflects the current [`set_wrap`](Self::set_wrap) configuration
    /// and is also increased by explicit line-feed (`\n`) characters in
    /// the text.
    ///
    /// *Experimental in `ThorVG`; the API may change.*
    pub fn line_count(&self) -> u32 {
        unsafe { sys::tvg_text_line_count(self.raw) }
    }

    /// Sets the letter and line spacing scale factors.
    ///
    /// Both are relative to the font's default metrics: `letter` scales
    /// the per-glyph advance width and `line` scales the line advance
    /// height. The default is `1.0`; values `> 1.0` increase spacing,
    /// `< 1.0` decrease it, and both must be `>= 0.0`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_spacing(&mut self, letter: f32, line: f32) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_spacing(self.raw, letter, line) })
    }

    /// Sets an outline (stroke) of the given `width` around the text.
    ///
    /// The outline color is RGB only (no alpha), with channels in
    /// `0..=255`, matching [`set_color`](Self::set_color). A `width` of
    /// `0.0` disables the outline.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if the engine rejects the
    /// request.
    pub fn set_outline(&mut self, width: f32, color: Rgb) -> Result<()> {
        let Rgb { r, g, b } = color;
        Error::from_raw(unsafe { sys::tvg_text_set_outline(self.raw, width, r, g, b) })
    }

    /// Sets a linear gradient fill for the text.
    ///
    /// Replaces any solid color set with [`set_color`](Self::set_color)
    /// — whichever was set last applies.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MemoryCorruption`] if the gradient object is
    /// invalid.
    pub fn set_linear_gradient(&mut self, grad: LinearGradient<'_>) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_set_gradient(self.raw, grad.into_raw()) })
    }

    /// Sets a radial gradient fill for the text.
    ///
    /// Replaces any solid color set with [`set_color`](Self::set_color)
    /// — whichever was set last applies.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MemoryCorruption`] if the gradient object is
    /// invalid.
    pub fn set_radial_gradient(&mut self, grad: RadialGradient<'_>) -> Result<()> {
        Error::from_raw(unsafe { sys::tvg_text_set_gradient(self.raw, grad.into_raw()) })
    }

    /// Returns the vertical font metrics of the text object.
    ///
    /// *Experimental in `ThorVG`; the API may change.*
    ///
    /// # Errors
    ///
    /// Returns [`Error::InsufficientCondition`] if no font or size has
    /// been set yet.
    pub fn text_metrics(&self) -> Result<TextMetrics> {
        let mut m = sys::Tvg_Text_Metrics {
            ascent: 0.0,
            descent: 0.0,
            linegap: 0.0,
            advance: 0.0,
        };
        Error::from_raw(unsafe { sys::tvg_text_get_text_metrics(self.raw, &raw mut m) })?;
        Ok(TextMetrics {
            ascent: m.ascent,
            descent: m.descent,
            linegap: m.linegap,
            advance: m.advance,
        })
    }

    /// Returns the layout metrics of a single glyph (horizontal layout
    /// only).
    ///
    /// `ch` must be a single UTF-8 encoded character.
    ///
    /// *Experimental in `ThorVG`; the API may change.*
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidArguments`] if `ch` contains an interior
    /// NUL byte or the engine reports the character as invalid or
    /// unsupported, or [`Error::InsufficientCondition`] if no font or
    /// size has been set yet.
    pub fn glyph_metrics(&self, ch: &str) -> Result<GlyphMetrics> {
        let c_ch = CString::new(ch)?;
        let mut m = sys::Tvg_Glyph_Metrics {
            advance: 0.0,
            bearing: 0.0,
            min: sys::Tvg_Point { x: 0.0, y: 0.0 },
            max: sys::Tvg_Point { x: 0.0, y: 0.0 },
        };
        Error::from_raw(unsafe {
            sys::tvg_text_get_glyph_metrics(self.raw, c_ch.as_ptr(), &raw mut m)
        })?;
        Ok(GlyphMetrics {
            advance: m.advance,
            bearing: m.bearing,
            min: Point {
                x: m.min.x,
                y: m.min.y,
            },
            max: Point {
                x: m.max.x,
                y: m.max.y,
            },
        })
    }

    // Font loading is engine-global state — see [`Thorvg::load_font_data`].
}

impl crate::paint::sealed::Sealed for Text<'_> {}

impl Paint for Text<'_> {
    fn raw(&self) -> sys::Tvg_Paint {
        self.raw
    }

    fn into_raw(mut self) -> sys::Tvg_Paint {
        self.owned = false;
        self.raw
    }

    unsafe fn from_raw_paint(raw: sys::Tvg_Paint) -> Self {
        Self {
            raw,
            owned: true,
            _engine: core::marker::PhantomData,
        }
    }
}

impl Drop for Text<'_> {
    fn drop(&mut self) {
        if self.owned {
            unsafe {
                sys::tvg_paint_rel(self.raw);
            }
        }
    }
}

impl core::fmt::Debug for Text<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Text").finish_non_exhaustive()
    }
}