Skip to content

Commit

Permalink
Accomplish outlining with a custom Extra type instead of changing `…
Browse files Browse the repository at this point in the history
…glyph_brush::Extra`.
  • Loading branch information
aweinstock314 committed Feb 21, 2023
1 parent d688ed7 commit 3e21d8b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ resolver = "2"
wgpu = "0.14"
glyph_brush = "0.7"
log = "0.4"
ordered-float = "3"

[dependencies.bytemuck]
version = "1.9"
Expand Down
2 changes: 1 addition & 1 deletion examples/clipping.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion examples/depth.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
env_logger::init();
Expand Down
85 changes: 80 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: core::hash::Hasher>(&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<C: Into<glyph_brush::Color>>(self, color: C) -> Self;
fn with_outline_color<C: Into<glyph_brush::Color>>(self, color: C) -> Self;
fn with_z<Z: Into<f32>>(self, z: Z) -> Self;
}

impl<'a> TextExt for Text<'a> {
#[inline]
fn with_color<C: Into<glyph_brush::Color>>(mut self, color: C) -> Self {
self.extra.extra.color = color.into();
self
}
#[inline]
fn with_outline_color<C: Into<glyph_brush::Color>>(
mut self,
color: C,
) -> Self {
self.extra.outline_color = color.into();
self
}
#[inline]
fn with_z<Z: Into<f32>>(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.
///
Expand Down Expand Up @@ -469,7 +542,9 @@ pub fn orthographic_projection(width: u32, height: u32) -> [f32; 16] {
]
}

impl<D, F: Font, H: BuildHasher> GlyphCruncher<F> for GlyphBrush<D, F, H> {
impl<D, F: Font, H: BuildHasher> GlyphCruncher<F, Extra>
for GlyphBrush<D, F, H>
{
#[inline]
fn glyphs_custom_layout<'a, 'b, S, L>(
&'b mut self,
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ impl Instance {
pixel_coords,
bounds,
extra,
}: glyph_brush::GlyphVertex,
}: glyph_brush::GlyphVertex<'_, crate::Extra>,
) -> Instance {
let gl_bounds = bounds;

Expand Down

0 comments on commit 3e21d8b

Please sign in to comment.