diff --git a/examples/hello-world.rs b/examples/hello-world.rs index b513f38..107d5a4 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -3,7 +3,7 @@ use glyphon::{ layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle}, Font, FontSettings, }, - Color, Resolution, TextRenderer, + Color, HasColor, Resolution, TextRenderer, }; use wgpu::{ Backends, CommandEncoderDescriptor, DeviceDescriptor, Features, Instance, Limits, LoadOp, @@ -21,11 +21,16 @@ fn main() { } #[derive(Clone, Copy)] -struct UserData; +struct GlyphUserData; -impl Color for UserData { - fn color(&self) -> [u8; 4] { - [255, 255, 0, 255] +impl HasColor for GlyphUserData { + fn color(&self) -> Color { + Color { + r: 255, + g: 255, + b: 0, + a: 255, + } } } @@ -95,7 +100,7 @@ async fn run() { "Hello world!\nI'm on a new line!", 50.0, 0, - UserData {}, + GlyphUserData, ), ); diff --git a/src/lib.rs b/src/lib.rs index 72d404a..0140356 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,8 +28,16 @@ use wgpu::{ pub use fontdue; -pub trait Color: Copy { - fn color(&self) -> [u8; 4]; +#[repr(C)] +pub struct Color { + pub r: u8, + pub g: u8, + pub b: u8, + pub a: u8, +} + +pub trait HasColor: Copy { + fn color(&self) -> Color; } #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -90,7 +98,7 @@ pub struct Params { fn try_allocate( atlas_packer: &mut BucketedAtlasAllocator, - layout: &Layout, + layout: &Layout, glyph_cache: &mut HashMap, width: usize, height: usize, @@ -350,7 +358,7 @@ impl TextRenderer { queue: &Queue, screen_resolution: Resolution, fonts: &[Font], - layouts: &[&Layout], + layouts: &[&Layout], ) -> Result<(), PrepareError> { if screen_resolution != self.params.screen_resolution { self.params.screen_resolution = screen_resolution; @@ -482,6 +490,8 @@ impl TextRenderer { GpuCache::SkipRasterization => continue, }; + let color = glyph.user_data.color(); + glyph_vertices.extend( iter::repeat(GlyphToRender { // Note: subpixel positioning is not currently handled, so we always use @@ -489,7 +499,7 @@ impl TextRenderer { pos: [glyph.x.round() as u32, glyph.y.round() as u32], dim: [details.width, details.height], uv: [atlas_x, atlas_y], - color: glyph.user_data.color(), + color: [color.r, color.g, color.b, color.a], }) .take(4), );