zng_view_api/
font.rs

1//! Font types.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6use zng_unit::Px;
7
8use crate::{config::FontAntiAliasing, declare_id, ipc::IpcBytes};
9
10declare_id! {
11    /// Font resource in a renderer cache.
12    ///
13    /// The View Process defines the ID.
14    pub struct FontFaceId(_);
15
16    /// Sized font in a renderer.
17    ///
18    /// The View Process defines the ID.
19    pub struct FontId(_);
20}
21
22/// Extra font options.
23#[derive(Default, Debug, PartialEq, Clone, Deserialize, Serialize)]
24#[non_exhaustive]
25pub struct FontOptions {
26    /// Font render mode.
27    ///
28    /// Default value must be already resolved here, it falls back to Subpixel.
29    pub aa: FontAntiAliasing,
30
31    /// If synthetic bold is enabled.
32    pub synthetic_bold: bool,
33    /// If synthetic skew is enabled.
34    pub synthetic_oblique: bool,
35}
36impl FontOptions {
37    /// New font options.
38    pub fn new(aa: FontAntiAliasing, synthetic_bold: bool, synthetic_oblique: bool) -> Self {
39        Self {
40            aa,
41            synthetic_bold,
42            synthetic_oblique,
43        }
44    }
45}
46
47/// Extra font options send with text glyphs.
48pub type GlyphOptions = FontOptions;
49
50/// Font feature name, `*b"hlig"` for example.
51pub type FontVariationName = [u8; 4];
52
53/// Glyph index with position.
54#[repr(C)]
55#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
56#[non_exhaustive]
57pub struct GlyphInstance {
58    /// Glyph id.
59    pub index: GlyphIndex,
60    /// Glyph position.
61    pub point: euclid::Point2D<f32, Px>,
62}
63impl GlyphInstance {
64    /// New glyph.
65    pub fn new(index: GlyphIndex, point: euclid::Point2D<f32, Px>) -> Self {
66        Self { index, point }
67    }
68}
69
70/// Glyph index in a font.
71pub type GlyphIndex = u32;
72
73/// Represents font face data.
74#[derive(Clone, Debug, Deserialize, Serialize)]
75pub enum IpcFontBytes {
76    /// Custom font bytes.
77    Bytes(IpcBytes),
78    /// Font file path.
79    ///
80    /// The path must be safe for potential memory mapping. If the file is
81    /// as restricted as the current executable if can be considered safe.
82    System(PathBuf),
83}