Implement text alignment support in iced_tiny_skia
This commit is contained in:
parent
64fb722dfe
commit
3386402f5a
1 changed files with 25 additions and 6 deletions
|
|
@ -76,8 +76,8 @@ impl Pipeline {
|
||||||
color: Color,
|
color: Color,
|
||||||
size: f32,
|
size: f32,
|
||||||
font: Font,
|
font: Font,
|
||||||
_horizontal_alignment: alignment::Horizontal, // TODO
|
horizontal_alignment: alignment::Horizontal,
|
||||||
_vertical_alignment: alignment::Vertical, // TODO
|
vertical_alignment: alignment::Vertical,
|
||||||
pixels: &mut tiny_skia::PixmapMut<'_>,
|
pixels: &mut tiny_skia::PixmapMut<'_>,
|
||||||
clip_mask: Option<&tiny_skia::ClipMask>,
|
clip_mask: Option<&tiny_skia::ClipMask>,
|
||||||
) {
|
) {
|
||||||
|
|
@ -91,6 +91,27 @@ impl Pipeline {
|
||||||
|
|
||||||
let (_, buffer) = fields.render_cache.allocate(&fields.fonts, key);
|
let (_, buffer) = fields.render_cache.allocate(&fields.fonts, key);
|
||||||
|
|
||||||
|
let (total_lines, max_width) = buffer
|
||||||
|
.layout_runs()
|
||||||
|
.enumerate()
|
||||||
|
.fold((0, 0.0), |(_, max), (i, buffer)| {
|
||||||
|
(i + 1, buffer.line_w.max(max))
|
||||||
|
});
|
||||||
|
|
||||||
|
let total_height = total_lines as f32 * size * 1.2;
|
||||||
|
|
||||||
|
let x = match horizontal_alignment {
|
||||||
|
alignment::Horizontal::Left => bounds.x,
|
||||||
|
alignment::Horizontal::Center => bounds.x - max_width / 2.0,
|
||||||
|
alignment::Horizontal::Right => bounds.x - max_width,
|
||||||
|
};
|
||||||
|
|
||||||
|
let y = match vertical_alignment {
|
||||||
|
alignment::Vertical::Top => bounds.y,
|
||||||
|
alignment::Vertical::Center => bounds.y - total_height / 2.0,
|
||||||
|
alignment::Vertical::Bottom => bounds.y - total_height,
|
||||||
|
};
|
||||||
|
|
||||||
let mut swash = cosmic_text::SwashCache::new(&fields.fonts);
|
let mut swash = cosmic_text::SwashCache::new(&fields.fonts);
|
||||||
|
|
||||||
for run in buffer.layout_runs() {
|
for run in buffer.layout_runs() {
|
||||||
|
|
@ -159,10 +180,8 @@ impl Pipeline {
|
||||||
.expect("Create glyph pixel map");
|
.expect("Create glyph pixel map");
|
||||||
|
|
||||||
pixels.draw_pixmap(
|
pixels.draw_pixmap(
|
||||||
bounds.x as i32
|
x as i32 + glyph.x_int + image.placement.left,
|
||||||
+ glyph.x_int
|
y as i32 - glyph.y_int - image.placement.top
|
||||||
+ image.placement.left,
|
|
||||||
bounds.y as i32 - glyph.y_int - image.placement.top
|
|
||||||
+ run.line_y as i32,
|
+ run.line_y as i32,
|
||||||
pixmap,
|
pixmap,
|
||||||
&tiny_skia::PixmapPaint::default(),
|
&tiny_skia::PixmapPaint::default(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue