zng_view_api/
font.rs

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