summaryrefslogtreecommitdiff
path: root/src/graphics/font.rs
blob: d05682dfe641a8299f97feba464e327a4146ca6a (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use super::texture::TextureWrapper;
use ab_glyph::*;
use etagere::{AllocId, AtlasAllocator};
use glam::Vec4;
use lru::LruCache;
use std::sync::LazyLock;
use wgpu::*;

static FONT: LazyLock<FontRef<'static>> = LazyLock::new(|| {
    FontRef::try_from_slice(include_bytes!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/assets/KazukiReiwa-Regular.ttf"
    )))
    .expect("fatal: failed to load font")
});

/// スケールされたフォントのトップ-ベースライン間を取得するメソッド
pub fn ascent(scale: u32) -> f32 {
    FONT.as_scaled(scale as f32).ascent()
}

/// スケールされたフォントの行間を取得するメソッド
pub fn line_gap(scale: u32) -> f32 {
    FONT.as_scaled(scale as f32).line_gap()
}

/// スケールされたフォントのカーニング量を取得するメソッド
pub fn kern(scale: u32, first: char, second: char) -> f32 {
    let first = FONT.glyph_id(first);
    let second = FONT.glyph_id(second);
    FONT.as_scaled(scale as f32).kern(first, second)
}

const WIDTH: u32 = 128;
const HEIGHT: u32 = WIDTH;

// NOTE: macOSではswizzleをゼロコストで使えないのでRGBAにする。
const FORMAT: TextureFormat = if cfg!(target_os = "macos") {
    TextureFormat::Rgba8Unorm
} else {
    TextureFormat::R8Unorm
};

/// 1文字テクスチャの1辺におけるマージン
///
/// NOTE: 境界で隣の文字のピクセルを含まないように、テクスチャ上で四辺に2pxのマージンを取る。
///       文字情報(UV)はこのマージンを除いた領域を指定する。
const MARGIN: usize = 2;
/// 1文字テクスチャの1方向におけるマージン
const MARGIN_AXIS: usize = MARGIN * 2;

#[derive(Clone, Copy)]
pub enum CharInfo {
    Rasterizable {
        alloc_id: AllocId,
        width: f32,
        height: f32,
        x_offset: f32,
        y_offset: f32,
        advance: f32,
        uv: Vec4,
    },
    Unrasterizable {
        advance: f32,
    },
}

pub struct CharAtlas {
    /// ウィンドウの倍率
    ///
    /// 物理解像度と論理解像度の差を考慮するため、この倍率に従ってテクスチャアトラス全体をスケールする。
    ///
    /// NOTE: 実行中に変化しうるが、致命的ではないので初期化時の倍率に従うことにする。
    scale_factor: f32,
    width: f32,
    height: f32,
    atlas: AtlasAllocator,
    /// 文字テクスチャアトラスに登録されている文字とそのID
    ///
    /// NOTE: 文字テクスチャアトラスが満ちている場合に最も古く使われた文字を消すためにLRUキャッシュを用いている。
    registereds: LruCache<(char, u32), CharInfo>,
    pub texture: TextureWrapper,
}

impl CharAtlas {
    pub fn new(scale_factor: f32, device: &Device) -> Self {
        let width = WIDTH as f32 * scale_factor;
        let height = HEIGHT as f32 * scale_factor;

        Self {
            scale_factor,
            width,
            height,
            atlas: AtlasAllocator::new(etagere::size2(width as i32, height as i32)),
            registereds: LruCache::unbounded(),
            texture: TextureWrapper::new(
                device,
                width as u32,
                height as u32,
                FORMAT,
                TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
            ),
        }
    }

    /// 文字情報を取得するメソッド
    ///
    /// * queue - queue
    /// * c - 対象文字
    /// * scale - 論理解像度上の倍率 (雑に1文字の最大高と思って良い)
    ///
    /// 対象文字がテクスチャアトラス上に存在しない場合はラスタライズ・登録を行う。
    /// また、同時にLRUキャッシュの更新を行う。
    ///
    /// WARN: アトラス上の全ての文字が同時に使用される場合、
    ///       一部文字はテクスチャが更新されるため、意図しない結果を得る。
    pub fn acquire(&mut self, queue: &Queue, c: char, scale: u32) -> &CharInfo {
        if !self.registereds.contains(&(c, scale)) {
            self.register_character(queue, c, scale);
        }
        self.registereds.get(&(c, scale)).unwrap()
    }

    fn register_character(&mut self, queue: &Queue, c: char, scale: u32) {
        // ラスタライズ
        let rasterized = match RasterizedChar::new(&FONT, c, scale as f32 * self.scale_factor) {
            Ok(n) => n,
            Err(advance) => {
                self.registereds
                    .push((c, scale), CharInfo::Unrasterizable { advance });
                return;
            }
        };

        // アロケート
        let size = etagere::size2(rasterized.width as i32, rasterized.height as i32);
        let allocated = loop {
            if let Some(alloc) = self.atlas.allocate(size) {
                break alloc;
            } else {
                let least_used = self
                    .registereds
                    .pop_lru()
                    .expect("internal error: no character exists but character atlas is full");
                if let CharInfo::Rasterizable { alloc_id, .. } = least_used.1 {
                    self.atlas.deallocate(alloc_id);
                }
            }
        };

        // アップロード
        self.texture.write_partial(
            queue,
            &rasterized.data,
            // NOTE: 非負が保証されているため、`u32`にキャストして良い。
            allocated.rectangle.min.x as u32,
            // NOTE: 非負が保証されているため、`u32`にキャストして良い。
            allocated.rectangle.min.y as u32,
            rasterized.width,
            rasterized.height,
        );

        // 登録
        let width = rasterized.width as f32 - MARGIN_AXIS as f32;
        let height = rasterized.height as f32 - MARGIN_AXIS as f32;
        let info = CharInfo::Rasterizable {
            alloc_id: allocated.id,
            width: width / self.scale_factor,
            height: height / self.scale_factor,
            x_offset: rasterized.x_offset / self.scale_factor,
            y_offset: rasterized.y_offset / self.scale_factor,
            advance: rasterized.advance / self.scale_factor,
            uv: Vec4::new(
                (allocated.rectangle.min.x as f32 + MARGIN as f32) / self.width,
                (allocated.rectangle.min.y as f32 + MARGIN as f32) / self.height,
                width / self.width,
                height / self.height,
            ),
        };
        self.registereds.push((c, scale), info);
    }
}

struct RasterizedChar {
    data: Vec<u8>,
    width: u32,
    height: u32,
    x_offset: f32,
    y_offset: f32,
    advance: f32,
}

impl RasterizedChar {
    /// 文字をラスタライズするコンストラクタ
    ///
    /// * font - フォント
    /// * c - 対象文字
    /// * scale - 論理解像度上の倍率 (雑に1文字の最大高と思って良い)
    ///
    /// 物理解像度と論理解像度の違いを考慮しない。
    /// ラスタライザブルでない文字に対してはadvanceと共に`Err`を返す。
    fn new(font: &FontRef<'static>, c: char, scale: f32) -> Result<Self, f32> {
        let font = font.as_scaled(scale);
        let glyph = font.scaled_glyph(c);
        let advance = font.h_advance(glyph.id);

        let Some(outlined_glyph) = font.outline_glyph(glyph) else {
            return Err(advance);
        };

        let width = outlined_glyph.px_bounds().width().ceil() as usize + MARGIN_AXIS;
        let height = outlined_glyph.px_bounds().height().ceil() as usize + MARGIN_AXIS;
        let channel_count = FORMAT.block_copy_size(None).unwrap() as usize;
        let mut data = vec![0x00; channel_count * width * height];

        outlined_glyph.draw(|x, y, c| {
            let x = x as usize + MARGIN;
            let y = y as usize + MARGIN;
            let i = channel_count * width * y + channel_count * x;

            data[i] = (c * 255.0) as u8;

            #[cfg(target_os = "macos")]
            for j in 1..4 {
                data[i + j] = 255;
            }
        });

        Ok(Self {
            data,
            width: width as u32,
            height: height as u32,
            x_offset: outlined_glyph.px_bounds().min.x,
            y_offset: outlined_glyph.px_bounds().min.y,
            advance,
        })
    }
}