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;
13
14pub use font_descriptor::*;
15pub use font_identifier::*;
16pub use font_template::*;
17use ipc_channel::ipc::IpcSharedMemory;
18use malloc_size_of_derive::MallocSizeOf;
19use range::{RangeIndex, int_range_index};
20use serde::{Deserialize, Serialize};
21pub use system_font_service_proxy::*;
22
23int_range_index! {
24    #[derive(Deserialize, MallocSizeOf, Serialize)]
25    /// An index that refers to a byte offset in a text run. This could
26    /// the middle of a glyph.
27    struct ByteIndex(isize)
28}
29
30pub type StylesheetWebFontLoadFinishedCallback = Arc<dyn Fn(bool) + Send + Sync + 'static>;
31
32/// A data structure to store data for fonts. Data is stored internally in an
33/// [`IpcSharedMemory`] handle, so that it can be sent without serialization
34/// across IPC channels.
35#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
36pub struct FontData(#[conditional_malloc_size_of] pub(crate) Arc<IpcSharedMemory>);
37
38impl FontData {
39    pub fn from_bytes(bytes: &[u8]) -> Self {
40        Self(Arc::new(IpcSharedMemory::from_bytes(bytes)))
41    }
42
43    pub fn as_ipc_shared_memory(&self) -> Arc<IpcSharedMemory> {
44        self.0.clone()
45    }
46}
47
48impl AsRef<[u8]> for FontData {
49    fn as_ref(&self) -> &[u8] {
50        &self.0
51    }
52}
53
54/// Raw font data and an index
55///
56/// If the font data is of a TTC (TrueType collection) file, then the index of a specific font within
57/// the collection. If the font data is for is single font then the index will always be 0.
58#[derive(Deserialize, Clone, Serialize)]
59pub struct FontDataAndIndex {
60    /// The raw font file data (.ttf, .otf, .ttc, etc)
61    pub data: FontData,
62    /// The index of the font within the file (0 if the file is not a ttc)
63    pub index: u32,
64}
65
66#[derive(Copy, Clone, PartialEq)]
67pub enum FontDataError {
68    FailedToLoad,
69}