Skip to main content

read_fonts/tables/glyf/outline/
borrowed.rs

1//! A loaded outline that borrows the caller's buffers.
2
3use super::{outline_to_path, PathContourStart};
4use crate::{
5    model::glyph::outline::PathSink,
6    tables::glyf::{PointCoord, PointFlags, PHANTOM_POINT_COUNT},
7    types::Point,
8};
9
10/// A loaded outline that borrows the caller's buffers.
11///
12/// Has the same accessors as [`Outline`](super::Outline).
13#[derive(Clone, Debug)]
14pub struct OutlineRef<'a, C: PointCoord> {
15    pub(super) points: &'a [Point<C>],
16    pub(super) flags: &'a [PointFlags],
17    pub(super) contours: &'a [u16],
18    pub(super) phantom: [Point<C>; PHANTOM_POINT_COUNT],
19}
20
21impl<C: PointCoord> OutlineRef<'_, C> {
22    /// The loaded points, excluding phantom points.
23    pub fn points(&self) -> &[Point<C>] {
24        self.points
25    }
26
27    /// The flag for each point in [`Self::points`].
28    pub fn flags(&self) -> &[PointFlags] {
29        self.flags
30    }
31
32    /// The index of the last point of each contour.
33    pub fn contours(&self) -> &[u16] {
34        self.contours
35    }
36
37    /// The four phantom points, after scaling, variation and hinting.
38    pub fn phantom_points(&self) -> &[Point<C>; PHANTOM_POINT_COUNT] {
39        &self.phantom
40    }
41
42    /// The left side bearing after scaling, variation and hinting.
43    pub fn adjusted_lsb(&self) -> C {
44        self.phantom[0].x
45    }
46
47    /// The advance width after scaling, variation and hinting.
48    pub fn adjusted_advance_width(&self) -> C {
49        self.phantom[1].x - self.phantom[0].x
50    }
51
52    /// Converts the outline to path commands, calling `sink` for each.
53    ///
54    /// `start` decides where a contour begins when its first point is
55    /// off-curve. The point stream leaves that open.
56    ///
57    /// Returns `false` if the points do not describe a path, on the same terms
58    /// as [`outline_to_path`].
59    #[must_use]
60    pub fn to_path(&self, start: PathContourStart, sink: &mut impl PathSink) -> bool {
61        outline_to_path(self.points, self.flags, self.contours, start, sink)
62    }
63}