Skip to main content

khora_io/asset/decoders/
font.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Font decoder: TTF/OTF bytes → `Font`.
16//!
17//! Auto-registered via [`inventory::submit!`] under the canonical `"font"`
18//! slot. Single canonical implementation — no swap needed.
19
20use khora_core::asset::font::Font;
21
22use crate::asset::{AssetDecoder, DecoderRegistration};
23
24/// Decodes TTF/OTF font files into a `Font` asset.
25///
26/// For now, the decoder simply wraps the raw bytes; actual parsing happens
27/// in the text renderer downstream.
28#[derive(Clone, Default)]
29pub struct FontDecoder;
30
31impl AssetDecoder<Font> for FontDecoder {
32    fn load(
33        &self,
34        bytes: &[u8],
35    ) -> Result<Font, Box<dyn std::error::Error + Send + Sync + 'static>> {
36        Ok(Font {
37            name: "Unknown Font".to_string(),
38            data: bytes.to_vec(),
39        })
40    }
41}
42
43inventory::submit! {
44    DecoderRegistration {
45        type_name: "font",
46        register: |svc| {
47            svc.register_decoder::<Font>("font", FontDecoder);
48        },
49    }
50}