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;
11
12use std::sync::Arc;
13use std::sync::atomic::AtomicUsize;
14
15pub use font_descriptor::*;
16pub use font_identifier::*;
17pub use font_template::*;
18use malloc_size_of_derive::MallocSizeOf;
19use serde::{Deserialize, Serialize};
20use servo_arc::Arc as ServoArc;
21use servo_base::generic_channel::GenericSharedMemory;
22use style::font_face::Descriptors;
23use style::stylesheets::LockedFontFaceRule;
24pub use system_font_service_proxy::*;
25
26#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
27pub enum WebFontLoadEvent {
28    LoadedSuccessfully,
29    UnblockedFontReadyPromise,
30}
31
32pub type StylesheetWebFontLoadFinishedCallback =
33    Arc<dyn Fn(WebFontLoadEvent) + Send + Sync + 'static>;
34
35/// A data structure to store data for fonts. Data is stored internally in an
36/// [`GenericSharedMemory`] handle, so that it can be sent without serialization
37/// across IPC channels.
38#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
39pub struct FontData(#[conditional_malloc_size_of] pub(crate) Arc<GenericSharedMemory>);
40
41impl FontData {
42    pub fn from_bytes(bytes: &[u8]) -> Self {
43        Self(Arc::new(GenericSharedMemory::from_bytes(bytes)))
44    }
45
46    /// This is in single process mode more efficient because we do not have to copy the vector.
47    pub fn from_vec(bytes: Vec<u8>) -> Self {
48        Self(Arc::new(GenericSharedMemory::from_vec(bytes)))
49    }
50
51    pub fn as_ipc_shared_memory(&self) -> Arc<GenericSharedMemory> {
52        self.0.clone()
53    }
54}
55
56impl AsRef<[u8]> for FontData {
57    fn as_ref(&self) -> &[u8] {
58        &self.0
59    }
60}
61
62/// Raw font data and an index
63///
64/// If the font data is of a TTC (TrueType collection) file, then the index of a specific font within
65/// the collection. If the font data is for is single font then the index will always be 0.
66#[derive(Deserialize, Clone, Serialize, MallocSizeOf)]
67pub struct FontDataAndIndex {
68    /// The raw font file data (.ttf, .otf, .ttc, etc)
69    pub data: FontData,
70    /// The index of the font within the file (0 if the file is not a ttc)
71    pub index: u32,
72}
73
74#[derive(Copy, Clone, PartialEq)]
75pub enum FontDataError {
76    FailedToLoad,
77}
78
79/// Describes how the set of active `@font-face` rules was changed after a call to `FontContext::rebuild_font_face_set`.
80#[derive(Clone, Default)]
81pub struct WebFontSetDifference {
82    /// A list of `@font-face` rules that were added in this update.
83    pub added_font_faces: Vec<ServoArc<FontFaceRuleInfo>>,
84    /// A list of `@font-face` rules that were removed in this update.
85    pub removed_font_faces: Vec<ServoArc<FontFaceRuleInfo>>,
86    /// Whether the cascade index of any `@font-face` rule changed during this update.
87    ///
88    /// This can cause different fonts to be selected during font matching.
89    pub cascade_index_of_any_rule_changed: bool,
90}
91
92impl WebFontSetDifference {
93    /// Returns `true` iff the font face set remained unchanged by the update.
94    pub fn is_empty(&self) -> bool {
95        self.added_font_faces.is_empty() && self.removed_font_faces.is_empty()
96    }
97}
98
99#[derive(MallocSizeOf)]
100pub struct FontFaceRuleInfo {
101    /// The index of this `@font-face` in the cascade, relative to all
102    /// other `@font-face` rules.
103    pub cascade_index: AtomicUsize,
104    /// The descriptors on the `@font-face` rule.
105    pub descriptors: Descriptors,
106    /// The CSS rule that created this `@font-face`.
107    ///
108    /// This does *not* uniquely identify this struct across updates
109    /// to the set of live `@font-face` rules.
110    #[conditional_malloc_size_of]
111    pub rule: ServoArc<LockedFontFaceRule>,
112}