Merge pull request #359 from hecrj/improvement/update-wgpu_glyph

Update `wgpu_glyph` and `glyph_brush`
This commit is contained in:
Héctor Ramón 2020-05-29 02:13:26 +02:00 committed by GitHub
commit 8a864fcce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 37 deletions

View File

@ -13,10 +13,10 @@ canvas = ["iced_graphics/canvas"]
[dependencies] [dependencies]
wgpu = "0.5" wgpu = "0.5"
wgpu_glyph = "0.8" wgpu_glyph = "0.9"
glyph_brush = "0.7"
zerocopy = "0.3" zerocopy = "0.3"
bytemuck = "1.2" bytemuck = "1.2"
glyph_brush = "0.6"
raw-window-handle = "0.3" raw-window-handle = "0.3"
glam = "0.8" glam = "0.8"
log = "0.4" log = "0.4"

View File

@ -159,7 +159,6 @@ impl Backend {
for text in layer.text.iter() { for text in layer.text.iter() {
// Target physical coordinates directly to avoid blurry text // Target physical coordinates directly to avoid blurry text
let text = wgpu_glyph::Section { let text = wgpu_glyph::Section {
text: text.content,
// TODO: We `round` here to avoid rerasterizing text when // TODO: We `round` here to avoid rerasterizing text when
// its position changes slightly. This can make text feel a // its position changes slightly. This can make text feel a
// bit "jumpy". We may be able to do better once we improve // bit "jumpy". We may be able to do better once we improve
@ -181,12 +180,18 @@ impl Backend {
(text.bounds.width * scale_factor).ceil(), (text.bounds.width * scale_factor).ceil(),
(text.bounds.height * scale_factor).ceil(), (text.bounds.height * scale_factor).ceil(),
), ),
scale: wgpu_glyph::Scale { text: vec![wgpu_glyph::Text {
x: text.size * scale_factor, text: text.content,
y: text.size * scale_factor, scale: wgpu_glyph::ab_glyph::PxScale {
}, x: text.size * scale_factor,
color: text.color, y: text.size * scale_factor,
font_id: self.text_pipeline.find_font(text.font), },
font_id: self.text_pipeline.find_font(text.font),
extra: wgpu_glyph::Extra {
color: text.color,
z: 0.0,
},
}],
layout: wgpu_glyph::Layout::default() layout: wgpu_glyph::Layout::default()
.h_align(match text.horizontal_alignment { .h_align(match text.horizontal_alignment {
HorizontalAlignment::Left => { HorizontalAlignment::Left => {

View File

@ -1,12 +1,13 @@
use crate::Transformation; use crate::Transformation;
use iced_graphics::font; use iced_graphics::font;
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap};
use wgpu_glyph::ab_glyph;
#[derive(Debug)] #[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
draw_brush: RefCell<wgpu_glyph::GlyphBrush<'static, ()>>, draw_brush: RefCell<wgpu_glyph::GlyphBrush<()>>,
draw_font_map: RefCell<HashMap<String, wgpu_glyph::FontId>>, draw_font_map: RefCell<HashMap<String, wgpu_glyph::FontId>>,
measure_brush: RefCell<glyph_brush::GlyphBrush<'static, ()>>, measure_brush: RefCell<glyph_brush::GlyphBrush<()>>,
} }
impl Pipeline { impl Pipeline {
@ -25,28 +26,25 @@ impl Pipeline {
.unwrap_or_else(|_| font::FALLBACK.to_vec()) .unwrap_or_else(|_| font::FALLBACK.to_vec())
}); });
let load_glyph_brush = |font: Vec<u8>| { let font = ab_glyph::FontArc::try_from_vec(default_font)
let builder = .unwrap_or_else(|_| {
wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(vec![ log::warn!(
font.clone() "System font failed to load. Falling back to \
])?; embedded font..."
);
Ok(( ab_glyph::FontArc::try_from_slice(font::FALLBACK)
builder, .expect("Load fallback font")
glyph_brush::GlyphBrushBuilder::using_font_bytes(font).build(),
))
};
let (brush_builder, measure_brush) = load_glyph_brush(default_font)
.unwrap_or_else(|_: wgpu_glyph::rusttype::Error| {
log::warn!("System font failed to load. Falling back to embedded font...");
load_glyph_brush(font::FALLBACK.to_vec()).expect("Load fallback font")
}); });
let draw_brush = brush_builder let draw_brush =
.initial_cache_size((2048, 2048)) wgpu_glyph::GlyphBrushBuilder::using_font(font.clone())
.build(device, format); .initial_cache_size((2048, 2048))
.draw_cache_multithread(false) // TODO: Expose as a configuration flag
.build(device, format);
let measure_brush =
glyph_brush::GlyphBrushBuilder::using_font(font).build();
Pipeline { Pipeline {
draw_brush: RefCell::new(draw_brush), draw_brush: RefCell::new(draw_brush),
@ -91,10 +89,13 @@ impl Pipeline {
let wgpu_glyph::FontId(font_id) = self.find_font(font); let wgpu_glyph::FontId(font_id) = self.find_font(font);
let section = wgpu_glyph::Section { let section = wgpu_glyph::Section {
text: content,
scale: wgpu_glyph::Scale { x: size, y: size },
bounds: (bounds.width, bounds.height), bounds: (bounds.width, bounds.height),
font_id: wgpu_glyph::FontId(font_id), text: vec![wgpu_glyph::Text {
text: content,
scale: size.into(),
font_id: wgpu_glyph::FontId(font_id),
extra: wgpu_glyph::Extra::default(),
}],
..Default::default() ..Default::default()
}; };
@ -139,11 +140,12 @@ impl Pipeline {
return *font_id; return *font_id;
} }
// TODO: Find a way to share font data let font = ab_glyph::FontArc::try_from_slice(bytes)
let _ = self.measure_brush.borrow_mut().add_font_bytes(bytes); .expect("Load font");
let font_id = let _ = self.measure_brush.borrow_mut().add_font(font.clone());
self.draw_brush.borrow_mut().add_font_bytes(bytes);
let font_id = self.draw_brush.borrow_mut().add_font(font);
let _ = self let _ = self
.draw_font_map .draw_font_map