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