diff --git a/Cargo.lock b/Cargo.lock index 1e383d2c7..57bba762b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1317,12 +1317,6 @@ dependencies = [ "bytemuck", ] -[[package]] -name = "font8x8" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "875488b8711a968268c7cf5d139578713097ca4635a76044e8fe8eedf831d07e" - [[package]] name = "fontconfig-parser" version = "0.5.8" @@ -2346,7 +2340,6 @@ name = "moli-canvas" version = "0.1.0" dependencies = [ "base64 0.22.1", - "font8x8", "moli-image", "moli-web-mime", ] @@ -2621,6 +2614,8 @@ dependencies = [ "moli-image", "parking_lot", "parley", + "read-fonts", + "skrifa", "stylo", "stylo_taffy", "taffy", diff --git a/moli-canvas/Cargo.toml b/moli-canvas/Cargo.toml index b3a377d2f..eeb7d3e78 100644 --- a/moli-canvas/Cargo.toml +++ b/moli-canvas/Cargo.toml @@ -6,7 +6,6 @@ edition = "2024" [dependencies] base64 = "0.22" -font8x8 = "0.3" moli-image = { path = "../moli-image" } moli-web-mime = { path = "../moli-web-mime" } diff --git a/moli-canvas/src/lib.rs b/moli-canvas/src/lib.rs index 82f584c86..d85f90b40 100644 --- a/moli-canvas/src/lib.rs +++ b/moli-canvas/src/lib.rs @@ -2,7 +2,6 @@ mod blit; mod encode; mod pixel; mod rect; -mod text; mod types; pub use blit::{blit_draw_image, blit_draw_image_filtered, blit_image_data, extract_image_data}; @@ -15,7 +14,6 @@ pub use pixel::{ scale_rgba8, scale_rgba8_bilinear, scale_rgba8_nearest, }; pub use rect::{canonicalize_fill_style, fill_style_rgba, normalize_rect, paint_rect}; -pub use text::{draw_text, measure_text_width}; pub use types::{ CanvasRect, DEFAULT_FILL_STYLE, DEFAULT_FONT, DrawImageBlit, MAX_RGBA8_BYTE_LENGTH, Rgba8Rect, ScaleFilter, byte_len, @@ -351,26 +349,6 @@ mod tests { assert_eq!(hot, 4); } - #[test] - fn measure_text_scales_with_font_size_and_draw_text_changes_pixels() { - let small = measure_text_width("Hi", "10px sans-serif"); - let large = measure_text_width("Hi", "24px sans-serif"); - assert!(large > small); - - let mut pixels = vec![0; byte_len(96, 48).expect("buffer len")]; - draw_text( - &mut pixels, - 96, - 48, - "Moli", - 4.0, - 24.0, - "16px sans-serif", - fill_style_rgba("#ff0000"), - ); - assert!(pixels.iter().any(|&value| value != 0)); - } - #[test] fn draw_image_blit_validation_and_normalized_rect_cover_invalid_inputs() { assert!( diff --git a/moli-canvas/src/text.rs b/moli-canvas/src/text.rs deleted file mode 100644 index 9085aa34a..000000000 --- a/moli-canvas/src/text.rs +++ /dev/null @@ -1,89 +0,0 @@ -use font8x8::{BASIC_FONTS, UnicodeFonts}; - -use crate::rect::paint_rect; -use crate::types::surface_matches_len; - -pub fn measure_text_width(text: &str, font: &str) -> f64 { - let scale = text_scale(font); - let glyph_advance = 8u32.saturating_mul(scale) + scale; - text.chars().count() as f64 * glyph_advance as f64 -} - -pub fn draw_text( - pixels: &mut [u8], - canvas_width: u32, - canvas_height: u32, - text: &str, - x: f64, - y: f64, - font: &str, - rgba: [u8; 4], -) { - if !surface_matches_len(pixels, canvas_width, canvas_height) { - return; - } - let scale = text_scale(font); - let glyph_height = (8 * scale) as i32; - let mut cursor_x = x.round() as i32; - let top = y.round() as i32 - glyph_height + scale as i32; - for ch in text.chars() { - if let Some(glyph) = BASIC_FONTS.get(ch) { - draw_glyph( - pixels, - canvas_width, - canvas_height, - glyph, - cursor_x, - top, - scale, - rgba, - ); - } - cursor_x += (8 * scale + scale) as i32; - } -} - -fn parse_font_size_px(font: &str) -> u32 { - font.split_whitespace() - .find_map(|part| part.strip_suffix("px")) - .and_then(|value| value.parse::().ok()) - .filter(|value| *value > 0) - .unwrap_or(10) -} - -fn text_scale(font: &str) -> u32 { - (parse_font_size_px(font) / 8).max(1) -} - -fn draw_glyph( - pixels: &mut [u8], - canvas_width: u32, - canvas_height: u32, - glyph: [u8; 8], - origin_x: i32, - origin_y: i32, - scale: u32, - rgba: [u8; 4], -) { - for (row, bits) in glyph.into_iter().enumerate() { - for col in 0..8 { - if (bits & (1 << col)) == 0 { - continue; - } - let pixel_x = origin_x + ((7 - col) as u32 * scale) as i32; - let pixel_y = origin_y + (row as u32 * scale) as i32; - paint_rect( - pixels, - canvas_width, - canvas_height, - ( - pixel_x, - pixel_y, - pixel_x + scale as i32, - pixel_y + scale as i32, - ), - rgba, - ); - } - } -} diff --git a/moli-core/tests/fixtures/runtime/chrome_fontfaceset_events_subset.html b/moli-core/tests/fixtures/runtime/chrome_fontfaceset_events_subset.html index 4350f7687..05cc7dab8 100644 --- a/moli-core/tests/fixtures/runtime/chrome_fontfaceset_events_subset.html +++ b/moli-core/tests/fixtures/runtime/chrome_fontfaceset_events_subset.html @@ -2,6 +2,9 @@ diff --git a/moli-core/tests/fixtures/runtime/local_event_target_callback_exception.html b/moli-core/tests/fixtures/runtime/local_event_target_callback_exception.html index 91ba69495..b365c92d6 100644 --- a/moli-core/tests/fixtures/runtime/local_event_target_callback_exception.html +++ b/moli-core/tests/fixtures/runtime/local_event_target_callback_exception.html @@ -12,6 +12,9 @@ data-simple-error="" >