diff --git a/Cargo.toml b/Cargo.toml index 87d36a5..4ab995b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ resolver = "2" wgpu = "0.14" glyph_brush = "0.7" log = "0.4" +ordered-float = "3" [dependencies.bytemuck] version = "1.9" diff --git a/examples/clipping.rs b/examples/clipping.rs index 306c00e..18a814d 100644 --- a/examples/clipping.rs +++ b/examples/clipping.rs @@ -1,6 +1,6 @@ use std::error::Error; use wgpu::CompositeAlphaMode; -use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Region, Section, Text}; +use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Region, Section, Text, TextExt}; fn main() -> Result<(), Box> { env_logger::init(); diff --git a/examples/depth.rs b/examples/depth.rs index cd3af76..3f3d6ce 100644 --- a/examples/depth.rs +++ b/examples/depth.rs @@ -1,6 +1,6 @@ use std::error::Error; use wgpu::CompositeAlphaMode; -use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text}; +use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text, TextExt}; const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb; diff --git a/examples/hello.rs b/examples/hello.rs index 03e8efa..e10522b 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,6 +1,6 @@ use std::error::Error; use wgpu::CompositeAlphaMode; -use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text}; +use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text, TextExt}; fn main() -> Result<(), Box> { env_logger::init(); diff --git a/src/lib.rs b/src/lib.rs index 03659b7..3b538c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,19 +14,92 @@ use pipeline::{Instance, Pipeline}; pub use builder::GlyphBrushBuilder; pub use glyph_brush::ab_glyph; pub use glyph_brush::{ - BuiltInLineBreaker, Extra, FontId, GlyphCruncher, GlyphPositioner, + BuiltInLineBreaker, FontId, GlyphCruncher, GlyphPositioner, HorizontalAlign, Layout, LineBreak, LineBreaker, OwnedSection, OwnedText, - Section, SectionGeometry, SectionGlyph, SectionGlyphIter, SectionText, - Text, VerticalAlign, + SectionGeometry, SectionGlyph, SectionGlyphIter, SectionText, + VerticalAlign, }; use ab_glyph::{Font, Rect}; -use core::hash::BuildHasher; +use core::hash::{BuildHasher, Hash}; use std::borrow::Cow; use glyph_brush::{BrushAction, BrushError, DefaultSectionHasher}; use log::{log_enabled, warn}; +#[derive(Debug, Clone, Copy)] +pub struct Extra { + pub extra: glyph_brush::Extra, + pub outline_color: glyph_brush::Color, +} + +impl Hash for Extra { + #[inline] + fn hash(&self, state: &mut H) { + use ordered_float::OrderedFloat; + self.extra.hash(state); + [ + OrderedFloat::from(self.outline_color[0]), + OrderedFloat::from(self.outline_color[1]), + OrderedFloat::from(self.outline_color[2]), + OrderedFloat::from(self.outline_color[3]), + ] + .hash(state) + } +} +impl PartialEq for Extra { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.extra == other.extra && self.outline_color == other.outline_color + } +} + +impl Default for Extra { + #[inline] + fn default() -> Self { + Self { + extra: Default::default(), + outline_color: [0.0, 0.0, 0.0, 1.0], + } + } +} +pub type Section<'a> = glyph_brush::Section<'a, Extra>; +pub type Text<'a> = glyph_brush::Text<'a, Extra>; + +pub trait TextExt { + fn with_color>(self, color: C) -> Self; + fn with_outline_color>(self, color: C) -> Self; + fn with_z>(self, z: Z) -> Self; +} + +impl<'a> TextExt for Text<'a> { + #[inline] + fn with_color>(mut self, color: C) -> Self { + self.extra.extra.color = color.into(); + self + } + #[inline] + fn with_outline_color>( + mut self, + color: C, + ) -> Self { + self.extra.outline_color = color.into(); + self + } + #[inline] + fn with_z>(mut self, z: Z) -> Self { + self.extra.extra.z = z.into(); + self + } +} + +impl std::ops::Deref for Extra { + type Target = glyph_brush::Extra; + fn deref(&self) -> &Self::Target { + &self.extra + } +} + /// Object allowing glyph drawing, containing cache state. Manages glyph positioning cacheing, /// glyph draw caching & efficient GPU texture cache updating and re-sizing on demand. /// @@ -469,7 +542,9 @@ pub fn orthographic_projection(width: u32, height: u32) -> [f32; 16] { ] } -impl GlyphCruncher for GlyphBrush { +impl GlyphCruncher + for GlyphBrush +{ #[inline] fn glyphs_custom_layout<'a, 'b, S, L>( &'b mut self, diff --git a/src/pipeline.rs b/src/pipeline.rs index b15b7ef..89b095a 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -461,7 +461,7 @@ impl Instance { pixel_coords, bounds, extra, - }: glyph_brush::GlyphVertex, + }: glyph_brush::GlyphVertex<'_, crate::Extra>, ) -> Instance { let gl_bounds = bounds;