diff --git a/Cargo.lock b/Cargo.lock index da5f6ff..39e7705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -940,7 +940,6 @@ dependencies = [ name = "font_rasterizer" version = "0.1.0" dependencies = [ - "anyhow", "bezier_converter", "bitflags 2.6.0", "bytemuck", @@ -960,6 +959,7 @@ dependencies = [ "rustybuzz", "stroke_parser", "text_buffer", + "thiserror 2.0.9", "unicode-width 0.2.0", "wasm-bindgen", "wasm-bindgen-futures", diff --git a/font_rasterizer/Cargo.toml b/font_rasterizer/Cargo.toml index a9db434..72acf78 100644 --- a/font_rasterizer/Cargo.toml +++ b/font_rasterizer/Cargo.toml @@ -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 } diff --git a/font_rasterizer/src/errors.rs b/font_rasterizer/src/errors.rs new file mode 100644 index 0000000..3093f77 --- /dev/null +++ b/font_rasterizer/src/errors.rs @@ -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, +} diff --git a/font_rasterizer/src/font_buffer.rs b/font_rasterizer/src/font_buffer.rs index 1685672..83bf774 100644 --- a/font_rasterizer/src/font_buffer.rs +++ b/font_rasterizer/src/font_buffer.rs @@ -4,8 +4,6 @@ use std::{ sync::Arc, }; -use anyhow::Context; - use font_collector::FontData; use log::{debug, info}; use text_buffer::editor::CharWidthResolver; @@ -13,6 +11,7 @@ use wgpu::BufferUsages; use crate::{ char_width_calcurator::{CharWidth, CharWidthCalculator}, + errors::{BufferKind, FontRasterizerError}, font_converter::{FontVertexConverter, GlyphVertex, GlyphVertexData, Vertex}, }; @@ -151,11 +150,15 @@ impl GlyphVertexBuffer { } } - pub(crate) fn draw_info(&self, c: &char, direction: &Direction) -> anyhow::Result { + pub(crate) fn draw_info( + &self, + c: &char, + direction: &Direction, + ) -> Result { 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]; @@ -199,7 +202,7 @@ impl GlyphVertexBuffer { device: &wgpu::Device, queue: &wgpu::Queue, chars: HashSet, - ) -> anyhow::Result<()> { + ) -> Result<(), FontRasterizerError> { // 既にバッファに登録済みの char は除外する。 let chars = chars .into_iter() @@ -253,16 +256,20 @@ impl GlyphVertexBuffer { queue: &wgpu::Queue, c: char, glyph_data: GlyphVertexData, - ) -> anyhow::Result { + ) -> Result { 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(); diff --git a/font_rasterizer/src/font_converter.rs b/font_rasterizer/src/font_converter.rs index 90d67e5..e59d0d1 100644 --- a/font_rasterizer/src/font_converter.rs +++ b/font_rasterizer/src/font_converter.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use anyhow::Context; use bezier_converter::CubicBezier; use font_collector::FontData; use log::debug; @@ -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>, @@ -28,7 +29,7 @@ impl FontVertexConverter { .collect::>() } - 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); @@ -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 { + pub(crate) fn convert( + &self, + c: char, + width: CharWidth, + ) -> Result { let ( face, CharGlyphIds { @@ -169,10 +174,10 @@ impl GlyphVertexBuilder { glyph_id: GlyphId, width: CharWidth, face: &Face, - ) -> anyhow::Result { + ) -> Result { 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; diff --git a/font_rasterizer/src/lib.rs b/font_rasterizer/src/lib.rs index 7114f4c..0ef3f0b 100644 --- a/font_rasterizer/src/lib.rs +++ b/font_rasterizer/src/lib.rs @@ -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;