Move text logic in iced_wgpu to a text module

This commit is contained in:
Héctor Ramón Jiménez 2019-11-13 03:54:36 +01:00
parent 73f3c90007
commit f0b1e65ba4
6 changed files with 131 additions and 82 deletions

View file

@ -1,7 +1,7 @@
use crate::{Primitive, Renderer};
use iced_native::{layout, text, Color, Layout, MouseCursor, Size, Text};
use wgpu_glyph::{GlyphCruncher, Section};
use wgpu_glyph::Section;
use std::f32;
@ -21,13 +21,7 @@ impl text::Renderer for Renderer {
..Default::default()
};
let (width, height) = if let Some(bounds) =
self.text_measurements.borrow_mut().glyph_bounds(&section)
{
(bounds.width().ceil(), bounds.height().ceil())
} else {
(0.0, 0.0)
};
let (width, height) = self.text_pipeline.measure(&section);
let size = limits.resolve(Size::new(width, height));

View file

@ -70,41 +70,26 @@ impl text_input::Renderer for Renderer {
};
let (contents_primitive, offset) = if text_input.state.is_focused {
use wgpu_glyph::{GlyphCruncher, Scale, Section};
use wgpu_glyph::{Scale, Section};
let text_before_cursor = &text_input
.value
.until(text_input.state.cursor_position(&text_input.value))
.to_string();
let mut text_value_width = self
.text_measurements
.borrow_mut()
.glyph_bounds(Section {
let (mut text_value_width, _) =
self.text_pipeline.measure(&Section {
text: text_before_cursor,
bounds: (f32::INFINITY, text_bounds.height),
scale: Scale { x: size, y: size },
..Default::default()
})
.map(|bounds| bounds.width().round())
.unwrap_or(0.0);
});
let spaces_at_the_end =
text_before_cursor.len() - text_before_cursor.trim_end().len();
if spaces_at_the_end > 0 {
let space_width = {
let glyph_brush = self.text_measurements.borrow();
// TODO: Select appropriate font
let font = &glyph_brush.fonts()[0];
font.glyph(' ')
.scaled(Scale { x: size, y: size })
.h_metrics()
.advance_width
};
let space_width = self.text_pipeline.space_width(size);
text_value_width += spaces_at_the_end as f32 * space_width;
}