fonts/
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;
8mod font_context;
9mod font_store;
10mod glyph;
11#[expect(unsafe_code)]
12pub mod platform; // Public because integration tests need this
13mod shapers;
14mod system_font_service;
15
16pub(crate) use font::*;
17// These items are not meant to be part of the public API but are used for integration tests
18pub use font::{Font, FontFamilyDescriptor, FontSearchScope, PlatformFontMethods};
19pub use font::{
20    FontBaseline, FontGroup, FontMetrics, FontRef, LAST_RESORT_GLYPH_ADVANCE, ShapingFlags,
21    ShapingOptions,
22};
23pub use font_context::{
24    CspViolationHandler, FontContext, FontContextWebFontMethods, NetworkTimingHandler,
25    WebFontDocumentContext,
26};
27pub use font_store::FontTemplates;
28pub use fonts_traits::*;
29pub(crate) use glyph::*;
30pub use glyph::{GlyphInfo, GlyphStore};
31pub use platform::font_list::fallback_font_families;
32pub(crate) use shapers::*;
33use style::values::computed::XLang;
34pub use system_font_service::SystemFontService;
35use unicode_properties::{EmojiStatus, UnicodeEmoji, emoji};
36
37/// Whether or not font fallback selection prefers the emoji or text representation
38/// of a character. If `None` then either presentation is acceptable.
39#[derive(Clone, Copy, Debug, PartialEq)]
40pub(crate) enum EmojiPresentationPreference {
41    None,
42    Text,
43    Emoji,
44}
45
46#[derive(Clone, Debug)]
47pub struct FallbackFontSelectionOptions {
48    pub(crate) character: char,
49    pub(crate) presentation_preference: EmojiPresentationPreference,
50    pub(crate) lang: XLang,
51}
52
53impl Default for FallbackFontSelectionOptions {
54    fn default() -> Self {
55        Self {
56            character: ' ',
57            presentation_preference: EmojiPresentationPreference::None,
58            lang: XLang::get_initial_value(),
59        }
60    }
61}
62
63impl FallbackFontSelectionOptions {
64    pub(crate) fn new(character: char, next_character: Option<char>, lang: XLang) -> Self {
65        let presentation_preference = match next_character {
66            Some(next_character) if emoji::is_emoji_presentation_selector(next_character) => {
67                EmojiPresentationPreference::Emoji
68            },
69            Some(next_character) if emoji::is_text_presentation_selector(next_character) => {
70                EmojiPresentationPreference::Text
71            },
72            // We don't want to select emoji prsentation for any possible character that might be an emoji, because
73            // that includes characters such as '0' that are also used outside of emoji clusters. Instead, only
74            // select the emoji font for characters that explicitly have an emoji presentation (in the absence
75            // of the emoji presentation selectors above).
76            _ if matches!(
77                character.emoji_status(),
78                EmojiStatus::EmojiPresentation |
79                    EmojiStatus::EmojiPresentationAndModifierBase |
80                    EmojiStatus::EmojiPresentationAndEmojiComponent |
81                    EmojiStatus::EmojiPresentationAndModifierAndEmojiComponent
82            ) =>
83            {
84                EmojiPresentationPreference::Emoji
85            },
86            _ if character.is_emoji_char() => EmojiPresentationPreference::Text,
87            _ => EmojiPresentationPreference::None,
88        };
89        Self {
90            character,
91            presentation_preference,
92            lang,
93        }
94    }
95}
96
97pub(crate) fn float_to_fixed(before: usize, f: f64) -> i32 {
98    ((1i32 << before) as f64 * f) as i32
99}
100
101pub(crate) fn fixed_to_float(before: usize, f: i32) -> f64 {
102    f as f64 * 1.0f64 / ((1i32 << before) as f64)
103}