Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outlined text. #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -135,7 +135,7 @@ fn main() -> Result<(), Box<dyn Error>> {
text: vec![Text::new("Hello wgpu_glyph!")
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)],
..Section::default()
..Section::new()
});

// Draw the text!
Expand All @@ -156,7 +156,7 @@ fn main() -> Result<(), Box<dyn Error>> {
text: vec![Text::new("Hello wgpu_glyph!")
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(40.0)],
..Section::default()
..Section::new()
});

// Draw the text!
Expand Down
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -131,7 +131,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_scale(95.0)
.with_color([0.8, 0.8, 0.8, 1.0])
.with_z(0.9)],
..Section::default()
..Section::new()
});

// Queue background text next.
Expand All @@ -148,7 +148,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_scale(30.0)
.with_color([0.05, 0.05, 0.1, 1.0])
.with_z(0.2)],
..Section::default()
..Section::new()
});

// Draw all the text!
Expand Down
8 changes: 5 additions & 3 deletions 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 Expand Up @@ -134,17 +134,19 @@ fn main() -> Result<(), Box<dyn Error>> {
bounds: (size.width as f32, size.height as f32),
text: vec![Text::new("Hello wgpu_glyph!")
.with_color([0.0, 0.0, 0.0, 1.0])
.with_outline_color([1.0, 1.0, 1.0, 1.0])
.with_scale(40.0)],
..Section::default()
..Section::new()
});

glyph_brush.queue(Section {
screen_position: (30.0, 90.0),
bounds: (size.width as f32, size.height as f32),
text: vec![Text::new("Hello wgpu_glyph!")
.with_color([1.0, 1.0, 1.0, 1.0])
.with_outline_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)],
..Section::default()
..Section::new()
});

// Draw the text!
Expand Down
90 changes: 85 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,97 @@ 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
}
}
impl std::ops::DerefMut for Extra {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut 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 +547,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
5 changes: 4 additions & 1 deletion src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ fn build<D>(
2 => Float32x2,
3 => Float32x2,
4 => Float32x4,
5 => Float32x4,
],
}],
},
Expand Down Expand Up @@ -448,6 +449,7 @@ pub struct Instance {
tex_left_top: [f32; 2],
tex_right_bottom: [f32; 2],
color: [f32; 4],
outline_color: [f32; 4],
}

impl Instance {
Expand All @@ -459,7 +461,7 @@ impl Instance {
pixel_coords,
bounds,
extra,
}: glyph_brush::GlyphVertex,
}: glyph_brush::GlyphVertex<'_, crate::Extra>,
) -> Instance {
let gl_bounds = bounds;

Expand Down Expand Up @@ -503,6 +505,7 @@ impl Instance {
tex_left_top: [tex_coords.min.x, tex_coords.max.y],
tex_right_bottom: [tex_coords.max.x, tex_coords.min.y],
color: extra.color,
outline_color: extra.outline_color,
}
}
}
25 changes: 23 additions & 2 deletions src/shader/glyph.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ struct VertexInput {
@location(2) tex_left_top: vec2<f32>,
@location(3) tex_right_bottom: vec2<f32>,
@location(4) color: vec4<f32>,
@location(5) outline_color: vec4<f32>,
}

struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) f_tex_pos: vec2<f32>,
@location(1) f_color: vec4<f32>,
@location(2) f_outline_color: vec4<f32>,
}

@vertex
Expand Down Expand Up @@ -52,18 +54,37 @@ fn vs_main(input: VertexInput) -> VertexOutput {
}

out.f_color = input.color;
out.f_outline_color = input.outline_color;
out.position = globals.transform * vec4<f32>(pos, input.left_top.z, 1.0);

return out;
}

@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
var alpha: f32 = textureSample(font_tex, font_sampler, input.f_tex_pos).r;
var pixel_size: vec2<f32> = (1.0 / vec2<f32>(textureDimensions(font_tex)));

var alpha: f32 = textureSample(font_tex, font_sampler, input.f_tex_pos + 0.5*pixel_size).r;

var border = false;
for(var i = -1; i <= 1 && !border; i += 1) {
for(var j = -1; j <= 1 && !border; j += 1) {
if i == 0 && j == 0 {
continue;
}
if textureSample(font_tex, font_sampler, input.f_tex_pos + 0.5*pixel_size + pixel_size*vec2<f32>(f32(i), f32(j))).r <= 0.0 {
border = true;
}
}
}

if (alpha <= 0.0) {
discard;
}

return input.f_color * vec4<f32>(1.0, 1.0, 1.0, alpha);
if border {
return input.f_outline_color * vec4<f32>(1.0, 1.0, 1.0, alpha);
} else {
return input.f_color * vec4<f32>(1.0, 1.0, 1.0, alpha);
}
}