1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// font-kit/src/source.rs
//
// Copyright © 2018 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A database of installed fonts that can be queried.

use crate::error::SelectionError;
use crate::family::Family;
use crate::family_handle::FamilyHandle;
use crate::family_name::FamilyName;
use crate::font::Font;
use crate::handle::Handle;
use crate::matching;
use crate::properties::Properties;

#[cfg(all(target_os = "macos", not(feature = "source-fontconfig-default")))]
pub use crate::sources::core_text::CoreTextSource as SystemSource;
#[cfg(all(target_family = "windows", not(feature = "source-fontconfig-default")))]
pub use crate::sources::directwrite::DirectWriteSource as SystemSource;
#[cfg(any(
    not(any(
        target_os = "android",
        target_os = "macos",
        target_family = "windows",
        target_arch = "wasm32"
    )),
    feature = "source-fontconfig-default"
))]
pub use crate::sources::fontconfig::FontconfigSource as SystemSource;
#[cfg(all(target_os = "android", not(feature = "source-fontconfig-default")))]
pub use crate::sources::fs::FsSource as SystemSource;

// FIXME(pcwalton): These could expand to multiple fonts, and they could be language-specific.
#[cfg(any(target_family = "windows", target_os = "macos"))]
const DEFAULT_FONT_FAMILY_SERIF: &'static str = "Times New Roman";
#[cfg(any(target_family = "windows", target_os = "macos"))]
const DEFAULT_FONT_FAMILY_SANS_SERIF: &'static str = "Arial";
#[cfg(any(target_family = "windows", target_os = "macos"))]
const DEFAULT_FONT_FAMILY_MONOSPACE: &'static str = "Courier New";
#[cfg(any(target_family = "windows", target_os = "macos"))]
const DEFAULT_FONT_FAMILY_CURSIVE: &'static str = "Comic Sans MS";
#[cfg(target_family = "windows")]
const DEFAULT_FONT_FAMILY_FANTASY: &'static str = "Impact";
#[cfg(target_os = "macos")]
const DEFAULT_FONT_FAMILY_FANTASY: &'static str = "Papyrus";

#[cfg(not(any(target_family = "windows", target_os = "macos")))]
const DEFAULT_FONT_FAMILY_SERIF: &'static str = "serif";
#[cfg(not(any(target_family = "windows", target_os = "macos")))]
const DEFAULT_FONT_FAMILY_SANS_SERIF: &'static str = "sans-serif";
#[cfg(not(any(target_family = "windows", target_os = "macos")))]
const DEFAULT_FONT_FAMILY_MONOSPACE: &'static str = "monospace";
#[cfg(not(any(target_family = "windows", target_os = "macos")))]
const DEFAULT_FONT_FAMILY_CURSIVE: &'static str = "cursive";
#[cfg(not(any(target_family = "windows", target_os = "macos")))]
const DEFAULT_FONT_FAMILY_FANTASY: &'static str = "fantasy";

/// A database of installed fonts that can be queried.
///
/// This trait is object-safe.
pub trait Source {
    /// Returns paths of all fonts installed on the system.
    fn all_fonts(&self) -> Result<Vec<Handle>, SelectionError>;

    /// Returns the names of all families installed on the system.
    fn all_families(&self) -> Result<Vec<String>, SelectionError>;

    /// Looks up a font family by name and returns the handles of all the fonts in that family.
    fn select_family_by_name(&self, family_name: &str) -> Result<FamilyHandle, SelectionError>;

    /// Selects a font by PostScript name, which should be a unique identifier.
    ///
    /// The default implementation, which is used by the DirectWrite and the filesystem backends,
    /// does a brute-force search of installed fonts to find the one that matches.
    fn select_by_postscript_name(&self, postscript_name: &str) -> Result<Handle, SelectionError> {
        // TODO(pcwalton): Optimize this by searching for families with similar names first.
        for family_name in self.all_families()? {
            if let Ok(family_handle) = self.select_family_by_name(&family_name) {
                if let Ok(family) = Family::<Font>::from_handle(&family_handle) {
                    for (handle, font) in family_handle.fonts().iter().zip(family.fonts().iter()) {
                        if let Some(font_postscript_name) = font.postscript_name() {
                            if font_postscript_name == postscript_name {
                                return Ok((*handle).clone());
                            }
                        }
                    }
                }
            }
        }
        Err(SelectionError::NotFound)
    }

    // FIXME(pcwalton): This only returns one family instead of multiple families for the generic
    // family names.
    #[doc(hidden)]
    fn select_family_by_generic_name(
        &self,
        family_name: &FamilyName,
    ) -> Result<FamilyHandle, SelectionError> {
        match *family_name {
            FamilyName::Title(ref title) => self.select_family_by_name(title),
            FamilyName::Serif => self.select_family_by_name(DEFAULT_FONT_FAMILY_SERIF),
            FamilyName::SansSerif => self.select_family_by_name(DEFAULT_FONT_FAMILY_SANS_SERIF),
            FamilyName::Monospace => self.select_family_by_name(DEFAULT_FONT_FAMILY_MONOSPACE),
            FamilyName::Cursive => self.select_family_by_name(DEFAULT_FONT_FAMILY_CURSIVE),
            FamilyName::Fantasy => self.select_family_by_name(DEFAULT_FONT_FAMILY_FANTASY),
        }
    }

    /// Performs font matching according to the CSS Fonts Level 3 specification and returns the
    /// handle.
    #[inline]
    fn select_best_match(
        &self,
        family_names: &[FamilyName],
        properties: &Properties,
    ) -> Result<Handle, SelectionError> {
        for family_name in family_names {
            if let Ok(family_handle) = self.select_family_by_generic_name(family_name) {
                let candidates = self.select_descriptions_in_family(&family_handle)?;
                if let Ok(index) = matching::find_best_match(&candidates, properties) {
                    return Ok(family_handle.fonts[index].clone());
                }
            }
        }
        Err(SelectionError::NotFound)
    }

    #[doc(hidden)]
    fn select_descriptions_in_family(
        &self,
        family: &FamilyHandle,
    ) -> Result<Vec<Properties>, SelectionError> {
        let mut fields = vec![];
        for font_handle in family.fonts() {
            let font = Font::from_handle(font_handle).unwrap();
            fields.push(font.properties())
        }
        Ok(fields)
    }
}