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 #1

Merged
merged 1 commit into from
May 27, 2020
Merged
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "glow_glyph"
version = "0.1.0"
version = "0.2.0"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2018"
description = "A fast text renderer for glow, powered by glyph_brush"
Expand All @@ -12,7 +12,7 @@ readme = "README.md"

[dependencies]
glow = "0.4"
glyph_brush = "0.6"
glyph_brush = "0.7"
log = "0.4"
bytemuck = "1.2"

Expand Down
28 changes: 16 additions & 12 deletions examples/clipping.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use glow::HasContext;
use glow_glyph::{GlyphBrushBuilder, Region, Scale, Section};
use glow_glyph::{ab_glyph, GlyphBrushBuilder, Region, Section, Text};
use std::error::Error;

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

// Open window and create a surface
Expand All @@ -26,10 +27,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")
.build(&gl);
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"Inconsolata-Regular.ttf"
))?;

let mut glyph_brush = GlyphBrushBuilder::using_font(inconsolata).build(&gl);

// Render loop
context.window().request_redraw();
Expand Down Expand Up @@ -66,11 +68,12 @@ fn main() -> Result<(), String> {
unsafe { gl.clear(glow::COLOR_BUFFER_BIT) }

glyph_brush.queue(Section {
text: "Hello glow_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 @@ -79,11 +82,12 @@ fn main() -> Result<(), String> {
.expect("Draw queued");

glyph_brush.queue(Section {
text: "Hello glow_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
28 changes: 16 additions & 12 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use glow::HasContext;
use glow_glyph::{GlyphBrushBuilder, Scale, Section};
use glow_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text};
use std::error::Error;

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

// Open window and create a surface
Expand All @@ -26,10 +27,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")
.build(&gl);
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"Inconsolata-Regular.ttf"
))?;

let mut glyph_brush = GlyphBrushBuilder::using_font(inconsolata).build(&gl);

// Render loop
context.window().request_redraw();
Expand Down Expand Up @@ -72,20 +74,22 @@ fn main() -> Result<(), String> {
unsafe { gl.clear(glow::COLOR_BUFFER_BIT) }

glyph_brush.queue(Section {
text: "Hello glow_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 glow_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
70 changes: 30 additions & 40 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,57 @@
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, H = DefaultSectionHasher> {
inner: glyph_brush::GlyphBrushBuilder<'a, H>,
pub struct GlyphBrushBuilder<F, H = DefaultSectionHasher> {
inner: glyph_brush::GlyphBrushBuilder<F, H>,
}

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

impl<'a, H: BuildHasher> GlyphBrushBuilder<'a, H> {
impl<F: Font, H: BuildHasher> GlyphBrushBuilder<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);

self
}

/// Sets the section hasher. `GlyphBrush` cannot handle absolute section
/// hash collisions so use a good hash algorithm.
///
Expand All @@ -72,14 +62,14 @@ impl<'a, H: BuildHasher> GlyphBrushBuilder<'a, H> {
pub fn section_hasher<T: BuildHasher>(
self,
section_hasher: T,
) -> GlyphBrushBuilder<'a, T> {
) -> GlyphBrushBuilder<F, T> {
GlyphBrushBuilder {
inner: self.inner.section_hasher(section_hasher),
}
}

/// Builds a `GlyphBrush` in the given `glow::Context`.
pub fn build(self, gl: &glow::Context) -> GlyphBrush<'a, H> {
GlyphBrush::<H>::new(gl, self.inner)
pub fn build(self, gl: &glow::Context) -> GlyphBrush<F, H> {
GlyphBrush::<F, H>::new(gl, self.inner)
}
}
Loading