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, WebFontDocumentContext,
25};
26pub use font_store::FontTemplates;
27pub use fonts_traits::*;
28pub(crate) use glyph::*;
29pub use glyph::{GlyphInfo, GlyphRun, GlyphStore};
30pub use platform::font_list::fallback_font_families;
31pub(crate) use shapers::*;
32pub use system_font_service::SystemFontService;
33use unicode_properties::{EmojiStatus, UnicodeEmoji, emoji};
34
35/// Whether or not font fallback selection prefers the emoji or text representation
36/// of a character. If `None` then either presentation is acceptable.
37#[derive(Clone, Copy, Debug, PartialEq)]
38pub(crate) enum EmojiPresentationPreference {
39    None,
40    Text,
41    Emoji,
42}
43
44#[derive(Clone, Debug)]
45pub struct FallbackFontSelectionOptions {
46    pub(crate) character: char,
47    pub(crate) presentation_preference: EmojiPresentationPreference,
48    #[cfg_attr(target_os = "windows", allow(dead_code))]
49    pub(crate) lang: Option<String>,
50}
51
52impl Default for FallbackFontSelectionOptions {
53    fn default() -> Self {
54        Self {
55            character: ' ',
56            presentation_preference: EmojiPresentationPreference::None,
57            lang: None,
58        }
59    }
60}
61
62impl FallbackFontSelectionOptions {
63    pub(crate) fn new(character: char, next_character: Option<char>, lang: Option<String>) -> Self {
64        let presentation_preference = match next_character {
65            Some(next_character) if emoji::is_emoji_presentation_selector(next_character) => {
66                EmojiPresentationPreference::Emoji
67            },
68            Some(next_character) if emoji::is_text_presentation_selector(next_character) => {
69                EmojiPresentationPreference::Text
70            },
71            // We don't want to select emoji prsentation for any possible character that might be an emoji, because
72            // that includes characters such as '0' that are also used outside of emoji clusters. Instead, only
73            // select the emoji font for characters that explicitly have an emoji presentation (in the absence
74            // of the emoji presentation selectors above).
75            _ if matches!(
76                character.emoji_status(),
77                EmojiStatus::EmojiPresentation |
78                    EmojiStatus::EmojiPresentationAndModifierBase |
79                    EmojiStatus::EmojiPresentationAndEmojiComponent |
80                    EmojiStatus::EmojiPresentationAndModifierAndEmojiComponent
81            ) =>
82            {
83                EmojiPresentationPreference::Emoji
84            },
85            _ if character.is_emoji_char() => EmojiPresentationPreference::Text,
86            _ => EmojiPresentationPreference::None,
87        };
88        Self {
89            character,
90            presentation_preference,
91            lang,
92        }
93    }
94}
95
96pub(crate) fn float_to_fixed(before: usize, f: f64) -> i32 {
97    ((1i32 << before) as f64 * f) as i32
98}
99
100pub(crate) fn fixed_to_float(before: usize, f: i32) -> f64 {
101    f as f64 * 1.0f64 / ((1i32 << before) as f64)
102}