Skip to main content

fonts_traits/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5#![deny(unsafe_code)]
6
7mod font_descriptor;
8mod font_identifier;
9mod font_template;
10mod system_font_service_proxy;
11pub mod tag;
12
13use std::sync::Arc;
14use std::sync::atomic::AtomicUsize;
15
16pub use font_descriptor::*;
17pub use font_identifier::*;
18pub use font_template::*;
19use malloc_size_of_derive::MallocSizeOf;
20pub use memmap2::Mmap;
21use read_fonts::{FontRef, ReadError};
22use serde::{Deserialize, Serialize};
23use servo_arc::Arc as ServoArc;
24use servo_base::generic_channel::GenericSharedMemory;
25use style::font_face::Descriptors;
26use style::stylesheets::LockedFontFaceRule;
27pub use system_font_service_proxy::*;
28
29#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
30pub enum WebFontLoadEvent {
31    LoadedSuccessfully,
32    UnblockedFontReadyPromise,
33}
34
35pub type StylesheetWebFontLoadFinishedCallback =
36    Arc<dyn Fn(WebFontLoadEvent) + Send + Sync + 'static>;
37
38#[derive(Serialize, Deserialize)]
39struct SerializableFontData(Arc<GenericSharedMemory>);
40
41impl From<FontData> for SerializableFontData {
42    fn from(value: FontData) -> Self {
43        match value {
44            FontData::MemoryMapped(mmap) => {
45                SerializableFontData(Arc::new(GenericSharedMemory::from_bytes(&mmap)))
46            },
47            FontData::SharedMemory(generic_shared_memory) => {
48                SerializableFontData(generic_shared_memory)
49            },
50        }
51    }
52}
53
54impl From<SerializableFontData> for FontData {
55    fn from(value: SerializableFontData) -> Self {
56        FontData::SharedMemory(value.0)
57    }
58}
59
60/// A data structure to store data for fonts. Data is stored internally in an
61/// [`GenericSharedMemory`] handle, so that it can be sent without serialization
62/// across IPC channels.
63#[derive(Clone, Debug, Deserialize, Serialize, MallocSizeOf)]
64#[serde(from = "SerializableFontData", into = "SerializableFontData")]
65pub enum FontData {
66    MemoryMapped(#[conditional_malloc_size_of] Arc<Mmap>),
67    SharedMemory(#[conditional_malloc_size_of] Arc<GenericSharedMemory>),
68}
69
70impl FontData {
71    pub fn from_bytes(bytes: &[u8]) -> Self {
72        Self::SharedMemory(Arc::new(GenericSharedMemory::from_bytes(bytes)))
73    }
74
75    pub fn from_mmap(mmap: Mmap) -> Self {
76        Self::MemoryMapped(Arc::new(mmap))
77    }
78
79    pub fn as_ipc_shared_memory(self) -> Arc<GenericSharedMemory> {
80        match self {
81            FontData::MemoryMapped(mmap) => Arc::new(GenericSharedMemory::from_bytes(&mmap)),
82            FontData::SharedMemory(generic_shared_memory) => generic_shared_memory,
83        }
84    }
85
86    /// This is in single process mode more efficient because we do not have to copy the vector.
87    pub fn from_vec(bytes: Vec<u8>) -> Self {
88        Self::SharedMemory(Arc::new(GenericSharedMemory::from_vec(bytes)))
89    }
90}
91
92impl AsRef<[u8]> for FontData {
93    fn as_ref(&self) -> &[u8] {
94        match &self {
95            FontData::MemoryMapped(mmap) => mmap,
96            FontData::SharedMemory(generic_shared_memory) => generic_shared_memory,
97        }
98    }
99}
100
101/// Raw font data and an index
102///
103/// If the font data is of a TTC (TrueType collection) file, then the index of a specific font within
104/// the collection. If the font data is for a single font then the index will always be 0.
105#[derive(Debug, Deserialize, Clone, Serialize, MallocSizeOf)]
106pub struct FontDataAndIndex {
107    /// The raw font file data (.ttf, .otf, .ttc, etc)
108    pub data: FontData,
109    /// The index of the font within the file. This will always be zero if the file is not
110    /// a TTC file.
111    pub index: u32,
112}
113
114impl FontDataAndIndex {
115    pub fn font_ref(&self) -> Result<FontRef<'_>, ReadError> {
116        FontRef::from_index(self.data.as_ref(), self.index)
117    }
118}
119
120#[derive(Copy, Clone, Debug, PartialEq)]
121pub enum FontDataError {
122    FailedToLoad,
123}
124
125/// Describes how the set of active `@font-face` rules was changed after a call to `FontContext::rebuild_font_face_set`.
126#[derive(Clone, Default)]
127pub struct WebFontSetDifference {
128    /// A list of `@font-face` rules that were added in this update.
129    pub added_font_faces: Vec<ServoArc<FontFaceRuleInfo>>,
130    /// A list of `@font-face` rules that were removed in this update.
131    pub removed_font_faces: Vec<ServoArc<FontFaceRuleInfo>>,
132    /// Whether the cascade index of any `@font-face` rule changed during this update.
133    ///
134    /// This can cause different fonts to be selected during font matching.
135    pub cascade_index_of_any_rule_changed: bool,
136}
137
138impl WebFontSetDifference {
139    /// Returns `true` iff the font face set remained unchanged by the update.
140    pub fn is_empty(&self) -> bool {
141        self.added_font_faces.is_empty() && self.removed_font_faces.is_empty()
142    }
143}
144
145#[derive(MallocSizeOf)]
146pub struct FontFaceRuleInfo {
147    /// The index of this `@font-face` in the cascade, relative to all
148    /// other `@font-face` rules.
149    pub cascade_index: AtomicUsize,
150    /// The descriptors on the `@font-face` rule.
151    pub descriptors: Descriptors,
152    /// The CSS rule that created this `@font-face`.
153    ///
154    /// This does *not* uniquely identify this struct across updates
155    /// to the set of live `@font-face` rules.
156    #[conditional_malloc_size_of]
157    pub rule: ServoArc<LockedFontFaceRule>,
158}