Merge pull request #359 from hecrj/improvement/update-wgpu_glyph
Update `wgpu_glyph` and `glyph_brush`
This commit is contained in:
commit
8a864fcce9
@ -13,10 +13,10 @@ canvas = ["iced_graphics/canvas"]
|
||||
|
||||
[dependencies]
|
||||
wgpu = "0.5"
|
||||
wgpu_glyph = "0.8"
|
||||
wgpu_glyph = "0.9"
|
||||
glyph_brush = "0.7"
|
||||
zerocopy = "0.3"
|
||||
bytemuck = "1.2"
|
||||
glyph_brush = "0.6"
|
||||
raw-window-handle = "0.3"
|
||||
glam = "0.8"
|
||||
log = "0.4"
|
||||
|
@ -159,7 +159,6 @@ impl Backend {
|
||||
for text in layer.text.iter() {
|
||||
// Target physical coordinates directly to avoid blurry text
|
||||
let text = wgpu_glyph::Section {
|
||||
text: text.content,
|
||||
// TODO: We `round` here to avoid rerasterizing text when
|
||||
// its position changes slightly. This can make text feel a
|
||||
// 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.height * scale_factor).ceil(),
|
||||
),
|
||||
scale: wgpu_glyph::Scale {
|
||||
x: text.size * scale_factor,
|
||||
y: text.size * scale_factor,
|
||||
},
|
||||
color: text.color,
|
||||
font_id: self.text_pipeline.find_font(text.font),
|
||||
text: vec![wgpu_glyph::Text {
|
||||
text: text.content,
|
||||
scale: wgpu_glyph::ab_glyph::PxScale {
|
||||
x: text.size * scale_factor,
|
||||
y: text.size * scale_factor,
|
||||
},
|
||||
font_id: self.text_pipeline.find_font(text.font),
|
||||
extra: wgpu_glyph::Extra {
|
||||
color: text.color,
|
||||
z: 0.0,
|
||||
},
|
||||
}],
|
||||
layout: wgpu_glyph::Layout::default()
|
||||
.h_align(match text.horizontal_alignment {
|
||||
HorizontalAlignment::Left => {
|
||||
|
@ -1,12 +1,13 @@
|
||||
use crate::Transformation;
|
||||
use iced_graphics::font;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
use wgpu_glyph::ab_glyph;
|
||||
|
||||
#[derive(Debug)]
|
||||
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>>,
|
||||
measure_brush: RefCell<glyph_brush::GlyphBrush<'static, ()>>,
|
||||
measure_brush: RefCell<glyph_brush::GlyphBrush<()>>,
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
@ -25,28 +26,25 @@ impl Pipeline {
|
||||
.unwrap_or_else(|_| font::FALLBACK.to_vec())
|
||||
});
|
||||
|
||||
let load_glyph_brush = |font: Vec<u8>| {
|
||||
let builder =
|
||||
wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(vec![
|
||||
font.clone()
|
||||
])?;
|
||||
let font = ab_glyph::FontArc::try_from_vec(default_font)
|
||||
.unwrap_or_else(|_| {
|
||||
log::warn!(
|
||||
"System font failed to load. Falling back to \
|
||||
embedded font..."
|
||||
);
|
||||
|
||||
Ok((
|
||||
builder,
|
||||
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")
|
||||
ab_glyph::FontArc::try_from_slice(font::FALLBACK)
|
||||
.expect("Load fallback font")
|
||||
});
|
||||
|
||||
let draw_brush = brush_builder
|
||||
.initial_cache_size((2048, 2048))
|
||||
.build(device, format);
|
||||
let draw_brush =
|
||||
wgpu_glyph::GlyphBrushBuilder::using_font(font.clone())
|
||||
.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 {
|
||||
draw_brush: RefCell::new(draw_brush),
|
||||
@ -91,10 +89,13 @@ impl Pipeline {
|
||||
let wgpu_glyph::FontId(font_id) = self.find_font(font);
|
||||
|
||||
let section = wgpu_glyph::Section {
|
||||
text: content,
|
||||
scale: wgpu_glyph::Scale { x: size, y: size },
|
||||
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()
|
||||
};
|
||||
|
||||
@ -139,11 +140,12 @@ impl Pipeline {
|
||||
return *font_id;
|
||||
}
|
||||
|
||||
// TODO: Find a way to share font data
|
||||
let _ = self.measure_brush.borrow_mut().add_font_bytes(bytes);
|
||||
let font = ab_glyph::FontArc::try_from_slice(bytes)
|
||||
.expect("Load font");
|
||||
|
||||
let font_id =
|
||||
self.draw_brush.borrow_mut().add_font_bytes(bytes);
|
||||
let _ = self.measure_brush.borrow_mut().add_font(font.clone());
|
||||
|
||||
let font_id = self.draw_brush.borrow_mut().add_font(font);
|
||||
|
||||
let _ = self
|
||||
.draw_font_map
|
||||
|
Loading…
Reference in New Issue
Block a user