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::ops::{Deref, Range};
13use std::sync::Arc;
14
15use base::generic_channel::GenericSharedMemory;
16pub use font_descriptor::*;
17pub use font_identifier::*;
18pub use font_template::*;
19use malloc_size_of_derive::MallocSizeOf;
20use num_derive::{NumOps, One, Zero};
21use serde::{Deserialize, Serialize};
22pub use system_font_service_proxy::*;
23use webrender_api::euclid::num::One;
24
25/// An index that refers to a byte offset in a text run. This could
26/// the middle of a glyph.
27#[derive(
28    Clone,
29    Copy,
30    Debug,
31    Default,
32    Deserialize,
33    Eq,
34    MallocSizeOf,
35    NumOps,
36    Ord,
37    One,
38    PartialEq,
39    PartialOrd,
40    Serialize,
41    Zero,
42)]
43pub struct ByteIndex(pub usize);
44
45impl ByteIndex {
46    pub fn get(&self) -> usize {
47        self.0
48    }
49}
50
51/// A range of UTF-8 bytes in a text run. This is used to identify glyphs in a `GlyphRun`
52/// by their original character byte offsets in the text.
53#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, PartialEq, Serialize)]
54pub struct TextByteRange(Range<ByteIndex>);
55
56impl TextByteRange {
57    pub fn len(&self) -> ByteIndex {
58        self.0.end - self.0.start
59    }
60
61    #[inline]
62    pub fn intersect(&self, other: &Self) -> Self {
63        let begin = self.start.max(other.start);
64        let end = self.end.min(other.end);
65
66        if end < begin {
67            Self::default()
68        } else {
69            Self::new(begin, end)
70        }
71    }
72
73    #[inline]
74    pub fn contains_inclusive(&self, index: ByteIndex) -> bool {
75        index >= self.start && index <= self.end
76    }
77}
78
79impl Deref for TextByteRange {
80    type Target = Range<ByteIndex>;
81    fn deref(&self) -> &Self::Target {
82        &self.0
83    }
84}
85
86impl Iterator for TextByteRange {
87    type Item = ByteIndex;
88
89    fn next(&mut self) -> Option<Self::Item> {
90        if self.0.start == self.0.end {
91            None
92        } else {
93            let next = self.0.start;
94            self.0.start = self.0.start + ByteIndex::one();
95            Some(next)
96        }
97    }
98}
99
100impl DoubleEndedIterator for TextByteRange {
101    fn next_back(&mut self) -> Option<Self::Item> {
102        if self.0.start == self.0.end {
103            None
104        } else {
105            self.0.end = self.0.end - ByteIndex::one();
106            Some(self.0.end)
107        }
108    }
109}
110
111impl TextByteRange {
112    pub fn new(start: ByteIndex, end: ByteIndex) -> Self {
113        Self(start..end)
114    }
115
116    pub fn iter(&self) -> Range<ByteIndex> {
117        self.0.clone()
118    }
119}
120
121pub type StylesheetWebFontLoadFinishedCallback = Arc<dyn Fn(bool) + Send + Sync + 'static>;
122
123/// A data structure to store data for fonts. Data is stored internally in an
124/// [`GenericSharedMemory`] handle, so that it can be sent without serialization
125/// across IPC channels.
126#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
127pub struct FontData(#[conditional_malloc_size_of] pub(crate) Arc<GenericSharedMemory>);
128
129impl FontData {
130    pub fn from_bytes(bytes: &[u8]) -> Self {
131        Self(Arc::new(GenericSharedMemory::from_bytes(bytes)))
132    }
133
134    pub fn as_ipc_shared_memory(&self) -> Arc<GenericSharedMemory> {
135        self.0.clone()
136    }
137}
138
139impl AsRef<[u8]> for FontData {
140    fn as_ref(&self) -> &[u8] {
141        &self.0
142    }
143}
144
145/// Raw font data and an index
146///
147/// If the font data is of a TTC (TrueType collection) file, then the index of a specific font within
148/// the collection. If the font data is for is single font then the index will always be 0.
149#[derive(Deserialize, Clone, Serialize)]
150pub struct FontDataAndIndex {
151    /// The raw font file data (.ttf, .otf, .ttc, etc)
152    pub data: FontData,
153    /// The index of the font within the file (0 if the file is not a ttc)
154    pub index: u32,
155}
156
157#[derive(Copy, Clone, PartialEq)]
158pub enum FontDataError {
159    FailedToLoad,
160}