fyrox_ui/font/
loader.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Font loader.
22
23use crate::{
24    core::{reflect::prelude::*, uuid::Uuid, TypeUuidProvider},
25    font::Font,
26};
27use fyrox_resource::{
28    io::ResourceIo,
29    loader::{BoxedLoaderFuture, LoaderPayload, ResourceLoader},
30    options::{try_get_import_settings, ImportOptions},
31    state::LoadError,
32};
33use serde::{Deserialize, Serialize};
34use std::{path::PathBuf, sync::Arc};
35
36fn default_page_size() -> usize {
37    1024
38}
39
40#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Reflect, Eq)]
41pub struct FontImportOptions {
42    #[serde(default = "default_page_size")]
43    pub page_size: usize,
44}
45
46impl Default for FontImportOptions {
47    fn default() -> Self {
48        Self {
49            page_size: default_page_size(),
50        }
51    }
52}
53
54impl ImportOptions for FontImportOptions {}
55
56/// Default implementation for font loading.
57#[derive(Default)]
58pub struct FontLoader {
59    default_import_options: FontImportOptions,
60}
61
62impl ResourceLoader for FontLoader {
63    fn extensions(&self) -> &[&str] {
64        &["ttf", "otf"]
65    }
66
67    fn data_type_uuid(&self) -> Uuid {
68        Font::type_uuid()
69    }
70
71    fn load(&self, path: PathBuf, io: Arc<dyn ResourceIo>) -> BoxedLoaderFuture {
72        let default_import_options = self.default_import_options.clone();
73        Box::pin(async move {
74            let io = io.as_ref();
75
76            let import_options = try_get_import_settings(&path, io)
77                .await
78                .unwrap_or(default_import_options);
79
80            let font = Font::from_file(&path, import_options.page_size, io)
81                .await
82                .map_err(LoadError::new)?;
83            Ok(LoaderPayload::new(font))
84        })
85    }
86}