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},
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,
),
);

View file

@ -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<impl Color>,
layout: &Layout<impl HasColor>,
glyph_cache: &mut HashMap<GlyphRasterConfig, GlyphDetails>,
width: usize,
height: usize,
@ -350,7 +358,7 @@ impl TextRenderer {
queue: &Queue,
screen_resolution: Resolution,
fonts: &[Font],
layouts: &[&Layout<impl Color>],
layouts: &[&Layout<impl HasColor>],
) -> 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),
);