Skip to content

Commit

Permalink
font_rasterizer の anyhow を thiserror に変更。
Browse files Browse the repository at this point in the history
  • Loading branch information
mitoma committed Dec 27, 2024
1 parent f3b7d14 commit 6c0ab8f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion font_rasterizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
cfg-if = { workspace = true }
anyhow = { workspace = true }
thiserror = { workspace = true }
env_logger = { workspace = true }
log = { workspace = true }
wgpu = { workspace = true }
Expand Down
21 changes: 21 additions & 0 deletions font_rasterizer/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use rustybuzz::ttf_parser::GlyphId;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum FontRasterizerError {
#[error("glyph not found. char: {0}")]
GlyphNotFound(char),
#[error("glyph index not found")]
GlyphIndexNotFound,
// バッファの確保に失敗しているエラー
#[error("Failed to allocate buffer. kind: {0:?}")]
BufferAllocationFailed(BufferKind),
#[error("outline glyph is failed. glyph_id:{0:?}")]
NoOutlineGlyph(GlyphId),
}

#[derive(Debug)]
pub enum BufferKind {
Vertex,
Index,
}
23 changes: 15 additions & 8 deletions font_rasterizer/src/font_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use std::{
sync::Arc,
};

use anyhow::Context;

use font_collector::FontData;
use log::{debug, info};
use text_buffer::editor::CharWidthResolver;
use wgpu::BufferUsages;

use crate::{
char_width_calcurator::{CharWidth, CharWidthCalculator},
errors::{BufferKind, FontRasterizerError},
font_converter::{FontVertexConverter, GlyphVertex, GlyphVertexData, Vertex},
};

Expand Down Expand Up @@ -151,11 +150,15 @@ impl GlyphVertexBuffer {
}
}

pub(crate) fn draw_info(&self, c: &char, direction: &Direction) -> anyhow::Result<DrawInfo> {
pub(crate) fn draw_info(
&self,
c: &char,
direction: &Direction,
) -> Result<DrawInfo, FontRasterizerError> {
let index = &self
.buffer_index
.get(c)
.with_context(|| format!("get char from buffer index. c:{}", c))?
.ok_or(FontRasterizerError::GlyphIndexNotFound)?
.get_entry(direction);

let vertex_buffer = &self.vertex_buffers[index.vertex_buffer_index];
Expand Down Expand Up @@ -199,7 +202,7 @@ impl GlyphVertexBuffer {
device: &wgpu::Device,
queue: &wgpu::Queue,
chars: HashSet<char>,
) -> anyhow::Result<()> {
) -> Result<(), FontRasterizerError> {
// 既にバッファに登録済みの char は除外する。
let chars = chars
.into_iter()
Expand Down Expand Up @@ -253,16 +256,20 @@ impl GlyphVertexBuffer {
queue: &wgpu::Queue,
c: char,
glyph_data: GlyphVertexData,
) -> anyhow::Result<BufferIndex> {
) -> Result<BufferIndex, FontRasterizerError> {
self.ensure_buffer_capacity(device, queue, &glyph_data);

let vertex_buffer_index = self
.appendable_vertex_buffer_index(glyph_data.vertex_size())
.with_context(|| "fail ensure vertex_buffer")?;
.ok_or(FontRasterizerError::BufferAllocationFailed(
BufferKind::Vertex,
))?;

let index_buffer_index = self
.appendable_index_buffer_index(glyph_data.index_size())
.with_context(|| "fail ensure index_buffer")?;
.ok_or(FontRasterizerError::BufferAllocationFailed(
BufferKind::Index,
))?;

// buffer に書き込むキューを登録する
let vertex_buffer = self.vertex_buffers.get_mut(vertex_buffer_index).unwrap();
Expand Down
19 changes: 12 additions & 7 deletions font_rasterizer/src/font_converter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use anyhow::Context;
use bezier_converter::CubicBezier;
use font_collector::FontData;
use log::debug;
Expand All @@ -10,7 +9,9 @@ use rustybuzz::{
Direction, Face, UnicodeBuffer,
};

use crate::{char_width_calcurator::CharWidth, debug_mode::DEBUG_FLAGS};
use crate::{
char_width_calcurator::CharWidth, debug_mode::DEBUG_FLAGS, errors::FontRasterizerError,
};

pub(crate) struct FontVertexConverter {
fonts: Arc<Vec<FontData>>,
Expand All @@ -28,7 +29,7 @@ impl FontVertexConverter {
.collect::<Vec<Face>>()
}

fn get_face_and_glyph_ids(&self, c: char) -> anyhow::Result<(Face, CharGlyphIds)> {
fn get_face_and_glyph_ids(&self, c: char) -> Result<(Face, CharGlyphIds), FontRasterizerError> {
let mut buf = UnicodeBuffer::new();
buf.set_direction(Direction::TopToBottom);
buf.add(c, 0);
Expand All @@ -51,10 +52,14 @@ impl FontVertexConverter {
));
}
}
anyhow::bail!("no glyph. char:{}", c)
Err(FontRasterizerError::GlyphNotFound(c))
}

pub(crate) fn convert(&self, c: char, width: CharWidth) -> anyhow::Result<GlyphVertex> {
pub(crate) fn convert(
&self,
c: char,
width: CharWidth,
) -> Result<GlyphVertex, FontRasterizerError> {
let (
face,
CharGlyphIds {
Expand Down Expand Up @@ -169,10 +174,10 @@ impl GlyphVertexBuilder {
glyph_id: GlyphId,
width: CharWidth,
face: &Face,
) -> anyhow::Result<GlyphVertexData> {
) -> Result<GlyphVertexData, FontRasterizerError> {
let rect = face
.outline_glyph(glyph_id, &mut self)
.with_context(|| format!("ougline glyph is afiled. glyph_id:{:?}", glyph_id))?;
.ok_or(FontRasterizerError::NoOutlineGlyph(glyph_id))?;

let global = face.global_bounding_box();
let global_width = global.width() as f32;
Expand Down
1 change: 1 addition & 0 deletions font_rasterizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod char_width_calcurator;
pub mod color_theme;
pub mod context;
mod debug_mode;
pub mod errors;
pub mod font_buffer;
pub mod font_converter;
pub mod instances;
Expand Down

0 comments on commit 6c0ab8f

Please sign in to comment.