2021-08-26 14:41:33 +07:00
|
|
|
pub use iced_native::text::Hit;
|
|
|
|
|
|
2023-01-31 06:29:21 +01:00
|
|
|
use iced_graphics::layer::Text;
|
2023-02-01 03:24:14 +01:00
|
|
|
use iced_native::{Font, Rectangle, Size};
|
2023-01-31 06:29:21 +01:00
|
|
|
|
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
|
|
|
pub struct Pipeline {
|
2023-02-01 03:24:14 +01:00
|
|
|
renderers: Vec<glyphon::TextRenderer>,
|
2023-01-31 06:29:21 +01:00
|
|
|
atlas: glyphon::TextAtlas,
|
|
|
|
|
cache: glyphon::SwashCache<'static>,
|
2023-02-01 03:24:14 +01:00
|
|
|
layer: usize,
|
2023-01-31 06:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Share with `iced_graphics`
|
|
|
|
|
static FONT_SYSTEM: once_cell::sync::Lazy<glyphon::FontSystem> =
|
|
|
|
|
once_cell::sync::Lazy::new(glyphon::FontSystem::new);
|
2019-11-13 03:54:36 +01:00
|
|
|
|
|
|
|
|
impl Pipeline {
|
2020-02-24 20:08:40 +01:00
|
|
|
pub fn new(
|
2023-01-31 06:29:21 +01:00
|
|
|
device: &wgpu::Device,
|
|
|
|
|
queue: &wgpu::Queue,
|
|
|
|
|
format: wgpu::TextureFormat,
|
2023-01-06 23:29:38 +01:00
|
|
|
_default_font: Option<&[u8]>,
|
|
|
|
|
_multithreading: bool,
|
2020-02-24 20:08:40 +01:00
|
|
|
) -> Self {
|
2023-01-31 06:29:21 +01:00
|
|
|
Pipeline {
|
2023-02-01 03:24:14 +01:00
|
|
|
renderers: Vec::new(),
|
2023-01-31 06:29:21 +01:00
|
|
|
atlas: glyphon::TextAtlas::new(device, queue, format),
|
|
|
|
|
cache: glyphon::SwashCache::new(&FONT_SYSTEM),
|
2023-02-01 03:24:14 +01:00
|
|
|
layer: 0,
|
2023-01-31 06:29:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn prepare(
|
|
|
|
|
&mut self,
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
queue: &wgpu::Queue,
|
|
|
|
|
sections: &[Text<'_>],
|
2023-02-01 03:24:14 +01:00
|
|
|
bounds: Rectangle,
|
2023-01-31 06:29:21 +01:00
|
|
|
scale_factor: f32,
|
|
|
|
|
target_size: Size<u32>,
|
|
|
|
|
) {
|
2023-02-01 03:24:14 +01:00
|
|
|
if self.renderers.len() <= self.layer {
|
|
|
|
|
self.renderers
|
|
|
|
|
.push(glyphon::TextRenderer::new(device, queue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let renderer = &mut self.renderers[self.layer];
|
|
|
|
|
|
2023-01-31 06:29:21 +01:00
|
|
|
let buffers: Vec<_> = sections
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|section| {
|
|
|
|
|
let metrics = glyphon::Metrics::new(
|
|
|
|
|
(section.size * scale_factor) as i32,
|
|
|
|
|
(section.size * 1.2 * scale_factor) as i32,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut buffer = glyphon::Buffer::new(&FONT_SYSTEM, metrics);
|
|
|
|
|
|
|
|
|
|
buffer.set_size(
|
|
|
|
|
(section.bounds.width * scale_factor).ceil() as i32,
|
|
|
|
|
(section.bounds.height * scale_factor).ceil() as i32,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
buffer.set_text(
|
|
|
|
|
section.content,
|
|
|
|
|
glyphon::Attrs::new()
|
|
|
|
|
.color({
|
|
|
|
|
let [r, g, b, a] = section.color.into_rgba8();
|
|
|
|
|
glyphon::Color::rgba(r, g, b, a)
|
|
|
|
|
})
|
|
|
|
|
.family(match section.font {
|
|
|
|
|
Font::Default => glyphon::Family::SansSerif,
|
|
|
|
|
Font::External { name, .. } => {
|
|
|
|
|
glyphon::Family::Name(name)
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
buffer.shape_until_scroll();
|
|
|
|
|
|
|
|
|
|
buffer
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2023-02-01 03:24:14 +01:00
|
|
|
let bounds = glyphon::TextBounds {
|
|
|
|
|
left: (bounds.x * scale_factor) as i32,
|
|
|
|
|
top: (bounds.y * scale_factor) as i32,
|
|
|
|
|
right: ((bounds.x + bounds.width) * scale_factor) as i32,
|
|
|
|
|
bottom: ((bounds.y + bounds.height) * scale_factor) as i32,
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-31 06:29:21 +01:00
|
|
|
let text_areas: Vec<_> = sections
|
|
|
|
|
.iter()
|
|
|
|
|
.zip(buffers.iter())
|
|
|
|
|
.map(|(section, buffer)| glyphon::TextArea {
|
|
|
|
|
buffer,
|
|
|
|
|
left: (section.bounds.x * scale_factor) as i32,
|
|
|
|
|
top: (section.bounds.y * scale_factor) as i32,
|
2023-02-01 03:24:14 +01:00
|
|
|
bounds,
|
2023-01-31 06:29:21 +01:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2023-02-01 03:24:14 +01:00
|
|
|
renderer
|
2023-01-31 06:29:21 +01:00
|
|
|
.prepare(
|
|
|
|
|
device,
|
|
|
|
|
queue,
|
|
|
|
|
&mut self.atlas,
|
|
|
|
|
glyphon::Resolution {
|
|
|
|
|
width: target_size.width,
|
|
|
|
|
height: target_size.height,
|
|
|
|
|
},
|
|
|
|
|
&text_areas,
|
|
|
|
|
glyphon::Color::rgb(0, 0, 0),
|
|
|
|
|
&mut self.cache,
|
|
|
|
|
)
|
|
|
|
|
.expect("Prepare text sections");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn render(
|
|
|
|
|
&mut self,
|
|
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
|
|
|
|
target: &wgpu::TextureView,
|
|
|
|
|
) {
|
|
|
|
|
let mut render_pass =
|
|
|
|
|
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
|
|
label: None,
|
|
|
|
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
|
|
|
|
view: target,
|
|
|
|
|
resolve_target: None,
|
|
|
|
|
ops: wgpu::Operations {
|
|
|
|
|
load: wgpu::LoadOp::Load,
|
|
|
|
|
store: true,
|
|
|
|
|
},
|
|
|
|
|
})],
|
|
|
|
|
depth_stencil_attachment: None,
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-01 03:24:14 +01:00
|
|
|
let renderer = &mut self.renderers[self.layer];
|
|
|
|
|
|
|
|
|
|
renderer
|
2023-01-31 06:29:21 +01:00
|
|
|
.render(&self.atlas, &mut render_pass)
|
|
|
|
|
.expect("Render text");
|
2023-02-01 03:24:14 +01:00
|
|
|
|
|
|
|
|
self.layer += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn end_frame(&mut self) {
|
|
|
|
|
self.renderers.truncate(self.layer);
|
|
|
|
|
self.layer = 0;
|
2019-11-13 03:54:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-13 07:22:21 +01:00
|
|
|
pub fn measure(
|
|
|
|
|
&self,
|
2023-01-31 06:29:21 +01:00
|
|
|
content: &str,
|
|
|
|
|
size: f32,
|
|
|
|
|
font: Font,
|
|
|
|
|
bounds: Size,
|
2019-11-13 07:22:21 +01:00
|
|
|
) -> (f32, f32) {
|
2023-01-31 06:29:21 +01:00
|
|
|
let attrs = match font {
|
|
|
|
|
Font::Default => glyphon::Attrs::new(),
|
|
|
|
|
Font::External { name, .. } => glyphon::Attrs {
|
|
|
|
|
family: glyphon::Family::Name(name),
|
|
|
|
|
..glyphon::Attrs::new()
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut paragraph =
|
|
|
|
|
glyphon::BufferLine::new(content, glyphon::AttrsList::new(attrs));
|
|
|
|
|
|
|
|
|
|
// TODO: Cache layout
|
|
|
|
|
let layout = paragraph.layout(
|
|
|
|
|
&FONT_SYSTEM,
|
|
|
|
|
size as i32,
|
|
|
|
|
bounds.width as i32,
|
|
|
|
|
glyphon::Wrap::Word,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
layout.iter().fold(0.0, |max, line| line.w.max(max)),
|
|
|
|
|
size * 1.2 * layout.len() as f32,
|
|
|
|
|
)
|
2019-11-13 03:54:36 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-21 10:31:26 -07:00
|
|
|
pub fn hit_test(
|
|
|
|
|
&self,
|
2023-01-06 23:29:38 +01:00
|
|
|
_content: &str,
|
|
|
|
|
_size: f32,
|
|
|
|
|
_font: iced_native::Font,
|
|
|
|
|
_bounds: iced_native::Size,
|
|
|
|
|
_point: iced_native::Point,
|
|
|
|
|
_nearest_only: bool,
|
2021-09-15 14:49:13 +07:00
|
|
|
) -> Option<Hit> {
|
2023-01-06 23:29:38 +01:00
|
|
|
None
|
2021-08-21 10:31:26 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-06 23:29:38 +01:00
|
|
|
pub fn trim_measurement_cache(&mut self) {}
|
2019-11-13 03:54:36 +01:00
|
|
|
}
|