Skip to content

Commit a6513ff

Browse files
committed
FemtoVG: Simpilfy text layout drawing
As suggested by Ashley
1 parent 6cc1763 commit a6513ff

File tree

1 file changed

+27
-59
lines changed

1 file changed

+27
-59
lines changed

internal/renderers/femtovg/itemrenderer.rs

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -195,42 +195,40 @@ impl<'a, R: femtovg::Renderer + TextureImporter> GLItemRenderer<'a, R> {
195195
}
196196
}
197197

198-
fn draw_glyph_run<R: femtovg::Renderer + TextureImporter>(
198+
fn draw_text_layout<R: femtovg::Renderer + TextureImporter>(
199199
canvas: &mut Canvas<R>,
200200
layout: &sharedparley::Layout,
201-
font: &parley::Font,
202-
font_size: f32,
203-
stroke_style: &Option<TextStrokeStyle>,
204201
paint: &mut femtovg::Paint,
205-
glyphs_it: &mut dyn Iterator<Item = parley::layout::Glyph>,
206202
) {
207-
let font_id = font_cache::FONT_CACHE.with(|cache| cache.borrow_mut().font(font));
203+
layout.draw(&mut |font, font_size, stroke_style, glyphs_it| {
204+
let font_id = font_cache::FONT_CACHE.with(|cache| cache.borrow_mut().font(font));
208205

209-
let glyphs_it = glyphs_it.map(|glyph| femtovg::PositionedGlyph {
210-
x: glyph.x,
211-
y: glyph.y + layout.y_offset,
212-
glyph_id: glyph.id,
213-
});
206+
let glyphs_it = glyphs_it.map(|glyph| femtovg::PositionedGlyph {
207+
x: glyph.x,
208+
y: glyph.y + layout.y_offset,
209+
glyph_id: glyph.id,
210+
});
214211

215-
paint.set_font_size(font_size);
212+
paint.set_font_size(font_size);
216213

217-
match stroke_style {
218-
Some(i_slint_core::items::TextStrokeStyle::Outside) => {
219-
let glyphs = glyphs_it.collect::<Vec<_>>();
214+
match stroke_style {
215+
Some(i_slint_core::items::TextStrokeStyle::Outside) => {
216+
let glyphs = glyphs_it.collect::<Vec<_>>();
220217

221-
canvas.stroke_glyph_run(font_id, glyphs.clone(), &paint).unwrap();
222-
canvas.fill_glyph_run(font_id, glyphs, &paint).unwrap();
223-
}
224-
Some(i_slint_core::items::TextStrokeStyle::Center) => {
225-
let glyphs = glyphs_it.collect::<Vec<_>>();
218+
canvas.stroke_glyph_run(font_id, glyphs.clone(), &paint).unwrap();
219+
canvas.fill_glyph_run(font_id, glyphs, &paint).unwrap();
220+
}
221+
Some(i_slint_core::items::TextStrokeStyle::Center) => {
222+
let glyphs = glyphs_it.collect::<Vec<_>>();
226223

227-
canvas.fill_glyph_run(font_id, glyphs.clone(), &paint).unwrap();
228-
canvas.stroke_glyph_run(font_id, glyphs, &paint).unwrap();
229-
}
230-
None => {
231-
canvas.fill_glyph_run(font_id, glyphs_it, &paint).unwrap();
224+
canvas.fill_glyph_run(font_id, glyphs.clone(), &paint).unwrap();
225+
canvas.stroke_glyph_run(font_id, glyphs, &paint).unwrap();
226+
}
227+
None => {
228+
canvas.fill_glyph_run(font_id, glyphs_it, &paint).unwrap();
229+
}
232230
}
233-
}
231+
})
234232
}
235233

236234
impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer<'a, R> {
@@ -430,17 +428,7 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
430428

431429
let mut canvas = self.canvas.borrow_mut();
432430

433-
layout.draw(&mut |font, font_size, stroke_style, glyphs_it| {
434-
draw_glyph_run(
435-
&mut canvas,
436-
&layout,
437-
font,
438-
font_size,
439-
stroke_style,
440-
&mut paint,
441-
glyphs_it,
442-
);
443-
});
431+
draw_text_layout(&mut canvas, &layout, &mut paint);
444432
}
445433

446434
fn draw_text_input(
@@ -526,17 +514,7 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
526514
);
527515
});
528516

529-
layout.draw(&mut |font, font_size, stroke_style, glyphs_it| {
530-
draw_glyph_run(
531-
&mut canvas,
532-
&layout,
533-
font,
534-
font_size,
535-
stroke_style,
536-
&mut paint,
537-
glyphs_it,
538-
);
539-
});
517+
draw_text_layout(&mut canvas, &layout, &mut paint);
540518

541519
if cursor_visible {
542520
let cursor = parley::layout::cursor::Cursor::from_byte_index(
@@ -988,17 +966,7 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
988966
let layout = sharedparley::layout(string, self.scale_factor, Default::default());
989967
let mut paint = femtovg::Paint::color(to_femtovg_color(&color));
990968
let mut canvas = self.canvas.borrow_mut();
991-
layout.draw(&mut |font, font_size, stroke_style, glyphs_it| {
992-
draw_glyph_run(
993-
&mut canvas,
994-
&layout,
995-
font,
996-
font_size,
997-
stroke_style,
998-
&mut paint,
999-
glyphs_it,
1000-
);
1001-
});
969+
draw_text_layout(&mut canvas, &layout, &mut paint);
1002970
}
1003971

1004972
fn draw_image_direct(&mut self, image: i_slint_core::graphics::Image) {

0 commit comments

Comments
 (0)