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

Update glyph_brush to 0.7 #43

Merged
merged 4 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"

[dependencies]
wgpu = "0.5"
glyph_brush = "0.6"
glyph_brush = "0.7"
log = "0.4"
zerocopy = "0.3"

Expand Down
27 changes: 16 additions & 11 deletions examples/clipping.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use wgpu_glyph::{GlyphBrushBuilder, Region, Scale, Section};
use std::error::Error;
use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Region, Section, Text};

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

// Open window and create a surface
Expand Down Expand Up @@ -51,9 +52,11 @@ fn main() -> Result<(), String> {
);

// Prepare glyph_brush
let inconsolata: &[u8] = include_bytes!("Inconsolata-Regular.ttf");
let mut glyph_brush = GlyphBrushBuilder::using_font_bytes(inconsolata)
.expect("Load font")
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"Inconsolata-Regular.ttf"
))?;

let mut glyph_brush = GlyphBrushBuilder::using_font(inconsolata)
.build(&device, render_format);

// Render loop
Expand Down Expand Up @@ -118,11 +121,12 @@ fn main() -> Result<(), String> {
}

glyph_brush.queue(Section {
text: "Hello wgpu_glyph!",
screen_position: (30.0, 30.0),
color: [0.0, 0.0, 0.0, 1.0],
scale: Scale { x: 40.0, y: 40.0 },
bounds: (size.width as f32, size.height as f32),
text: vec![Text::default()
.with_text("Hello wgpu_glyph!")
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)],
..Section::default()
});

Expand All @@ -138,11 +142,12 @@ fn main() -> Result<(), String> {
.expect("Draw queued");

glyph_brush.queue(Section {
text: "Hello wgpu_glyph!",
screen_position: (30.0, 90.0),
color: [1.0, 1.0, 1.0, 1.0],
scale: Scale { x: 40.0, y: 40.0 },
bounds: (size.width as f32, size.height as f32),
text: vec![Text::default()
.with_text("Hello wgpu_glyph!")
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(40.0)],
..Section::default()
});

Expand Down
36 changes: 21 additions & 15 deletions examples/depth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use wgpu_glyph::{GlyphBrushBuilder, Scale, Section};
use std::error::Error;
use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text};

const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

// Open window and create a surface
Expand Down Expand Up @@ -52,10 +53,11 @@ fn main() -> Result<(), String> {
create_frame_views(&device, &surface, size);

// Prepare glyph_brush
let inconsolata: &[u8] = include_bytes!("Inconsolata-Regular.ttf");
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"Inconsolata-Regular.ttf"
))?;

let mut glyph_brush = GlyphBrushBuilder::using_font_bytes(inconsolata)
.expect("Load fonts")
let mut glyph_brush = GlyphBrushBuilder::using_font(inconsolata)
.depth_stencil_state(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: true,
Expand Down Expand Up @@ -130,10 +132,11 @@ fn main() -> Result<(), String> {
// Depth buffer will make it appear on top.
glyph_brush.queue(Section {
screen_position: (30.0, 30.0),
text: "On top",
scale: Scale::uniform(95.0),
color: [0.8, 0.8, 0.8, 1.0],
z: 0.9,
text: vec![Text::default()
.with_text("On top")
.with_scale(95.0)
.with_color([0.8, 0.8, 0.8, 1.0])
.with_z(0.9)],
..Section::default()
});

Expand All @@ -142,12 +145,15 @@ fn main() -> Result<(), String> {
// previous queued text.
glyph_brush.queue(Section {
bounds: (size.width as f32, size.height as f32),
text: &include_str!("lipsum.txt")
.replace("\n\n", "")
.repeat(10),
scale: Scale::uniform(30.0),
color: [0.05, 0.05, 0.1, 1.0],
z: 0.2,
text: vec![Text::default()
.with_text(
&include_str!("lipsum.txt")
.replace("\n\n", "")
.repeat(10),
)
.with_scale(30.0)
.with_color([0.05, 0.05, 0.1, 1.0])
.with_z(0.2)],
..Section::default()
});

Expand Down
27 changes: 16 additions & 11 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use wgpu_glyph::{GlyphBrushBuilder, Scale, Section};
use std::error::Error;
use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text};

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

// Open window and create a surface
Expand Down Expand Up @@ -51,9 +52,11 @@ fn main() -> Result<(), String> {
);

// Prepare glyph_brush
let inconsolata: &[u8] = include_bytes!("Inconsolata-Regular.ttf");
let mut glyph_brush = GlyphBrushBuilder::using_font_bytes(inconsolata)
.expect("Load fonts")
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"Inconsolata-Regular.ttf"
))?;

let mut glyph_brush = GlyphBrushBuilder::using_font(inconsolata)
.build(&device, render_format);

// Render loop
Expand Down Expand Up @@ -118,20 +121,22 @@ fn main() -> Result<(), String> {
}

glyph_brush.queue(Section {
text: "Hello wgpu_glyph!",
screen_position: (30.0, 30.0),
color: [0.0, 0.0, 0.0, 1.0],
scale: Scale { x: 40.0, y: 40.0 },
bounds: (size.width as f32, size.height as f32),
text: vec![Text::default()
.with_text("Hello wgpu_glyph!")
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)],
..Section::default()
});

glyph_brush.queue(Section {
text: "Hello wgpu_glyph!",
screen_position: (30.0, 90.0),
color: [1.0, 1.0, 1.0, 1.0],
scale: Scale { x: 40.0, y: 40.0 },
bounds: (size.width as f32, size.height as f32),
text: vec![Text::default()
.with_text("Hello wgpu_glyph!")
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(40.0)],
..Section::default()
});

Expand Down
83 changes: 36 additions & 47 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use core::hash::BuildHasher;

use glyph_brush::ab_glyph::Font;
use glyph_brush::delegate_glyph_brush_builder_fns;
use glyph_brush::{rusttype, DefaultSectionHasher};
use rusttype::{Error, Font, SharedBytes};
use glyph_brush::DefaultSectionHasher;

use super::GlyphBrush;

/// Builder for a [`GlyphBrush`](struct.GlyphBrush.html).
pub struct GlyphBrushBuilder<'a, D, H = DefaultSectionHasher> {
inner: glyph_brush::GlyphBrushBuilder<'a, H>,
pub struct GlyphBrushBuilder<D, F, H = DefaultSectionHasher> {
inner: glyph_brush::GlyphBrushBuilder<F, H>,
texture_filter_method: wgpu::FilterMode,
depth: D,
}

impl<'a, H> From<glyph_brush::GlyphBrushBuilder<'a, H>>
for GlyphBrushBuilder<'a, (), H>
impl<F, H> From<glyph_brush::GlyphBrushBuilder<F, H>>
for GlyphBrushBuilder<(), F, H>
{
fn from(inner: glyph_brush::GlyphBrushBuilder<'a, H>) -> Self {
fn from(inner: glyph_brush::GlyphBrushBuilder<F, H>) -> Self {
GlyphBrushBuilder {
inner,
texture_filter_method: wgpu::FilterMode::Linear,
Expand All @@ -25,41 +25,15 @@ impl<'a, H> From<glyph_brush::GlyphBrushBuilder<'a, H>>
}
}

impl<'a> GlyphBrushBuilder<'a, ()> {
/// Specifies the default font data used to render glyphs.
/// Referenced with `FontId(0)`, which is default.
#[inline]
pub fn using_font_bytes<B: Into<SharedBytes<'a>>>(
font_0_data: B,
) -> Result<Self, Error> {
let font = Font::from_bytes(font_0_data)?;

Ok(Self::using_font(font))
}

#[inline]
pub fn using_fonts_bytes<B, V>(font_data: V) -> Result<Self, Error>
where
B: Into<SharedBytes<'a>>,
V: Into<Vec<B>>,
{
let fonts = font_data
.into()
.into_iter()
.map(Font::from_bytes)
.collect::<Result<Vec<Font>, Error>>()?;

Ok(Self::using_fonts(fonts))
}

impl GlyphBrushBuilder<(), ()> {
/// Specifies the default font used to render glyphs.
/// Referenced with `FontId(0)`, which is default.
#[inline]
pub fn using_font(font_0: Font<'a>) -> Self {
Self::using_fonts(vec![font_0])
pub fn using_font<F: Font>(font: F) -> GlyphBrushBuilder<(), F> {
Self::using_fonts(vec![font])
}

pub fn using_fonts<V: Into<Vec<Font<'a>>>>(fonts: V) -> Self {
pub fn using_fonts<F: Font>(fonts: Vec<F>) -> GlyphBrushBuilder<(), F> {
GlyphBrushBuilder {
inner: glyph_brush::GlyphBrushBuilder::using_fonts(fonts),
texture_filter_method: wgpu::FilterMode::Linear,
Expand All @@ -68,9 +42,24 @@ impl<'a> GlyphBrushBuilder<'a, ()> {
}
}

impl<'a, D, H: BuildHasher> GlyphBrushBuilder<'a, D, H> {
impl<F: Font, D, H: BuildHasher> GlyphBrushBuilder<D, F, H> {
delegate_glyph_brush_builder_fns!(inner);

/// When multiple CPU cores are available spread rasterization work across
/// all cores.
///
/// Significantly reduces worst case latency in multicore environments.
///
/// # Platform-specific behaviour
///
/// This option has no effect on wasm32.
pub fn draw_cache_multithread(mut self, multithread: bool) -> Self {
self.inner.draw_cache_builder =
self.inner.draw_cache_builder.multithread(multithread);
hecrj marked this conversation as resolved.
Show resolved Hide resolved

self
}

/// Sets the texture filtering method.
pub fn texture_filter_method(
mut self,
Expand All @@ -90,7 +79,7 @@ impl<'a, D, H: BuildHasher> GlyphBrushBuilder<'a, D, H> {
pub fn section_hasher<T: BuildHasher>(
self,
section_hasher: T,
) -> GlyphBrushBuilder<'a, D, T> {
) -> GlyphBrushBuilder<D, F, T> {
GlyphBrushBuilder {
inner: self.inner.section_hasher(section_hasher),
texture_filter_method: self.texture_filter_method,
Expand All @@ -102,7 +91,7 @@ impl<'a, D, H: BuildHasher> GlyphBrushBuilder<'a, D, H> {
pub fn depth_stencil_state(
self,
depth_stencil_state: wgpu::DepthStencilStateDescriptor,
) -> GlyphBrushBuilder<'a, wgpu::DepthStencilStateDescriptor, H> {
) -> GlyphBrushBuilder<wgpu::DepthStencilStateDescriptor, F, H> {
GlyphBrushBuilder {
inner: self.inner,
texture_filter_method: self.texture_filter_method,
Expand All @@ -111,15 +100,15 @@ impl<'a, D, H: BuildHasher> GlyphBrushBuilder<'a, D, H> {
}
}

impl<'a, H: BuildHasher> GlyphBrushBuilder<'a, (), H> {
impl<F: Font + Sync, H: BuildHasher> GlyphBrushBuilder<(), F, H> {
/// Builds a `GlyphBrush` using the given `wgpu::Device` that can render
/// text for texture views with the given `render_format`.
pub fn build(
self,
device: &wgpu::Device,
render_format: wgpu::TextureFormat,
) -> GlyphBrush<'a, (), H> {
GlyphBrush::<(), H>::new(
) -> GlyphBrush<(), F, H> {
GlyphBrush::<(), F, H>::new(
device,
self.texture_filter_method,
render_format,
Expand All @@ -128,17 +117,17 @@ impl<'a, H: BuildHasher> GlyphBrushBuilder<'a, (), H> {
}
}

impl<'a, H: BuildHasher>
GlyphBrushBuilder<'a, wgpu::DepthStencilStateDescriptor, H>
impl<F: Font + Sync, H: BuildHasher>
GlyphBrushBuilder<wgpu::DepthStencilStateDescriptor, F, H>
{
/// Builds a `GlyphBrush` using the given `wgpu::Device` that can render
/// text for texture views with the given `render_format`.
pub fn build(
self,
device: &wgpu::Device,
render_format: wgpu::TextureFormat,
) -> GlyphBrush<'a, wgpu::DepthStencilStateDescriptor, H> {
GlyphBrush::<wgpu::DepthStencilStateDescriptor, H>::new(
) -> GlyphBrush<wgpu::DepthStencilStateDescriptor, F, H> {
GlyphBrush::<wgpu::DepthStencilStateDescriptor, F, H>::new(
device,
self.texture_filter_method,
render_format,
Expand Down
Loading