From 4c1a83548e290f576241c501ae4c7cc249283448 Mon Sep 17 00:00:00 2001 From: grovesNL Date: Fri, 28 Oct 2022 01:24:45 -0230 Subject: [PATCH] Use default font color and remove `Color` --- examples/hello-world.rs | 3 ++- src/lib.rs | 16 +--------------- src/text_render.rs | 11 ++++++++--- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 9a271ad..56dfaef 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -1,4 +1,4 @@ -use cosmic_text::{Attrs, FontSystem, SwashCache, TextBuffer, TextMetrics}; +use cosmic_text::{Attrs, Color, FontSystem, SwashCache, TextBuffer, TextMetrics}; use glyphon::{Resolution, TextAtlas, TextRenderer}; use wgpu::{ Backends, CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance, @@ -101,6 +101,7 @@ async fn run() { height: config.height, }, &mut buffer, + Color::rgb(255, 255, 255), &mut cache, ) .unwrap(); diff --git a/src/lib.rs b/src/lib.rs index bd68490..bb9e738 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,20 +12,6 @@ pub use text_render::TextRenderer; pub use cosmic_text; -/// The color to use when rendering text. -#[repr(C)] -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct Color { - /// The red component of the color. - pub r: u8, - /// The green component of the color. - pub g: u8, - /// The blue component of the color. - pub b: u8, - /// The alpha component of the color. - pub a: u8, -} - pub(crate) enum GpuCache { InAtlas { x: u16, y: u16 }, SkipRasterization, @@ -46,7 +32,7 @@ pub(crate) struct GlyphToRender { pos: [i32; 2], dim: [u16; 2], uv: [u16; 2], - color: [u8; 4], + color: u32, } /// The screen resolution to use when rendering text. diff --git a/src/text_render.rs b/src/text_render.rs index bc8b74b..34a3186 100644 --- a/src/text_render.rs +++ b/src/text_render.rs @@ -1,4 +1,4 @@ -use cosmic_text::{CacheKey, SwashCache, SwashContent, TextBuffer}; +use cosmic_text::{CacheKey, Color, SwashCache, SwashContent, TextBuffer}; use etagere::{size2, Allocation}; use std::{collections::HashSet, iter, mem::size_of, num::NonZeroU32, slice}; @@ -64,6 +64,7 @@ impl TextRenderer { atlas: &mut TextAtlas, screen_resolution: Resolution, buffer: &mut TextBuffer<'a>, + default_color: Color, cache: &mut SwashCache, ) -> Result<(), PrepareError> { self.screen_resolution = screen_resolution; @@ -233,6 +234,11 @@ impl TextRenderer { let line_y = run.line_y; for glyph in run.glyphs.iter() { + let color = match glyph.color_opt { + Some(some) => some, + None => default_color, + }; + let details = atlas.glyph_cache.get(&glyph.cache_key).unwrap(); let mut x = glyph.x_int + details.left as i32; @@ -296,8 +302,7 @@ impl TextRenderer { pos: [x as i32, y as i32], dim: [width as u16, height as u16], uv: [atlas_x, atlas_y], - // TODO - color: [255, 255, 255, 255], + color: color.0, }) .take(4), );