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    pub fn as_ipc_shared_memory(&self) -> Arc<GenericSharedMemory> {
47        self.0.clone()
48    }
49}
50
51impl AsRef<[u8]> for FontData {
52    fn as_ref(&self) -> &[u8] {
53        &self.0
54    }
55}
56
57/// Raw font data and an index
58///
59/// If the font data is of a TTC (TrueType collection) file, then the index of a specific font within
60/// the collection. If the font data is for is single font then the index will always be 0.
61#[derive(Deserialize, Clone, Serialize, MallocSizeOf)]
62pub struct FontDataAndIndex {
63    /// The raw font file data (.ttf, .otf, .ttc, etc)
64    pub data: FontData,
65    /// The index of the font within the file (0 if the file is not a ttc)
66    pub index: u32,
67}
68
69#[derive(Copy, Clone, PartialEq)]
70pub enum FontDataError {
71    FailedToLoad,
72}
73
74/// Describes how the set of active `@font-face` rules was changed after a call to `FontContext::rebuild_font_face_set`.
75#[derive(Clone, Default)]
76pub struct WebFontSetDifference {
77    /// A list of `@font-face` rules that were added in this update.
78    pub added_font_faces: Vec<ServoArc<FontFaceRuleInfo>>,
79    /// A list of `@font-face` rules that were removed in this update.
80    pub removed_font_faces: Vec<ServoArc<FontFaceRuleInfo>>,
81    /// Whether the cascade index of any `@font-face` rule changed during this update.
82    ///
83    /// This can cause different fonts to be selected during font matching.
84    pub cascade_index_of_any_rule_changed: bool,
85}
86
87impl WebFontSetDifference {
88    /// Returns `true` iff the font face set remained unchanged by the update.
89    pub fn is_empty(&self) -> bool {
90        self.added_font_faces.is_empty() && self.removed_font_faces.is_empty()
91    }
92}
93
94#[derive(MallocSizeOf)]
95pub struct FontFaceRuleInfo {
96    /// The index of this `@font-face` in the cascade, relative to all
97    /// other `@font-face` rules.
98    pub cascade_index: AtomicUsize,
99    /// The descriptors on the `@font-face` rule.
100    pub descriptors: Descriptors,
101    /// The CSS rule that created this `@font-face`.
102    ///
103    /// This does *not* uniquely identify this struct across updates
104    /// to the set of live `@font-face` rules.
105    #[conditional_malloc_size_of]
106    pub rule: ServoArc<LockedFontFaceRule>,
107}