Rename Color trait to HasColor, add Color

This commit is contained in:
grovesNL 2022-05-09 22:53:09 -02:30
parent 5605879164
commit 8bf8533ead
2 changed files with 26 additions and 11 deletions

View file

@ -3,7 +3,7 @@ use glyphon::{
layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle}, layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle},
Font, FontSettings, Font, FontSettings,
}, },
Color, Resolution, TextRenderer, Color, HasColor, Resolution, TextRenderer,
}; };
use wgpu::{ use wgpu::{
Backends, CommandEncoderDescriptor, DeviceDescriptor, Features, Instance, Limits, LoadOp, Backends, CommandEncoderDescriptor, DeviceDescriptor, Features, Instance, Limits, LoadOp,
@ -21,11 +21,16 @@ fn main() {
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct UserData; struct GlyphUserData;
impl Color for UserData { impl HasColor for GlyphUserData {
fn color(&self) -> [u8; 4] { fn color(&self) -> Color {
[255, 255, 0, 255] Color {
r: 255,
g: 255,
b: 0,
a: 255,
}
} }
} }
@ -95,7 +100,7 @@ async fn run() {
"Hello world!\nI'm on a new line!", "Hello world!\nI'm on a new line!",
50.0, 50.0,
0, 0,
UserData {}, GlyphUserData,
), ),
); );

View file

@ -28,8 +28,16 @@ use wgpu::{
pub use fontdue; pub use fontdue;
pub trait Color: Copy { #[repr(C)]
fn color(&self) -> [u8; 4]; 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)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
@ -90,7 +98,7 @@ pub struct Params {
fn try_allocate( fn try_allocate(
atlas_packer: &mut BucketedAtlasAllocator, atlas_packer: &mut BucketedAtlasAllocator,
layout: &Layout<impl Color>, layout: &Layout<impl HasColor>,
glyph_cache: &mut HashMap<GlyphRasterConfig, GlyphDetails>, glyph_cache: &mut HashMap<GlyphRasterConfig, GlyphDetails>,
width: usize, width: usize,
height: usize, height: usize,
@ -350,7 +358,7 @@ impl TextRenderer {
queue: &Queue, queue: &Queue,
screen_resolution: Resolution, screen_resolution: Resolution,
fonts: &[Font], fonts: &[Font],
layouts: &[&Layout<impl Color>], layouts: &[&Layout<impl HasColor>],
) -> Result<(), PrepareError> { ) -> Result<(), PrepareError> {
if screen_resolution != self.params.screen_resolution { if screen_resolution != self.params.screen_resolution {
self.params.screen_resolution = screen_resolution; self.params.screen_resolution = screen_resolution;
@ -482,6 +490,8 @@ impl TextRenderer {
GpuCache::SkipRasterization => continue, GpuCache::SkipRasterization => continue,
}; };
let color = glyph.user_data.color();
glyph_vertices.extend( glyph_vertices.extend(
iter::repeat(GlyphToRender { iter::repeat(GlyphToRender {
// Note: subpixel positioning is not currently handled, so we always use // 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], pos: [glyph.x.round() as u32, glyph.y.round() as u32],
dim: [details.width, details.height], dim: [details.width, details.height],
uv: [atlas_x, atlas_y], uv: [atlas_x, atlas_y],
color: glyph.user_data.color(), color: [color.r, color.g, color.b, color.a],
}) })
.take(4), .take(4),
); );