Skip to content

Commit 2405d41

Browse files
committed
Start combining stuff WIP
1 parent 4b32e12 commit 2405d41

File tree

3 files changed

+69
-47
lines changed

3 files changed

+69
-47
lines changed

internal/renderers/femtovg/fonts.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ thread_local! {
4747
}
4848

4949
pub struct LayoutOptions {
50-
pub max_width: Option<PhysicalLength>,
50+
pub max_width: Option<LogicalLength>,
51+
pub max_height: Option<LogicalLength>,
5152
pub horizontal_align: TextHorizontalAlignment,
53+
pub vertical_align: TextVerticalAlignment,
5254
pub stroke: Option<sharedfontique::BrushTextStrokeStyle>,
5355
pub selection: Option<std::ops::Range<usize>>,
5456
pub font_request: Option<FontRequest>,
@@ -59,7 +61,9 @@ impl Default for LayoutOptions {
5961
fn default() -> Self {
6062
Self {
6163
max_width: None,
64+
max_height: None,
6265
horizontal_align: TextHorizontalAlignment::Left,
66+
vertical_align: TextVerticalAlignment::Top,
6367
stroke: None,
6468
selection: None,
6569
font_request: None,
@@ -68,11 +72,11 @@ impl Default for LayoutOptions {
6872
}
6973
}
7074

71-
pub fn layout(text: &str, options: LayoutOptions) -> parley::Layout<sharedfontique::Brush> {
75+
pub fn layout(text: &str, scale_factor: f32, options: LayoutOptions) -> Layout {
7276
let mut font_context = sharedfontique::font_context();
7377
let mut layout_context = sharedfontique::layout_context();
7478

75-
let mut builder = layout_context.ranged_builder(&mut font_context, text, 1.0, true);
79+
let mut builder = layout_context.ranged_builder(&mut font_context, text, scale_factor, true);
7680
if let Some(ref font_request) = options.font_request {
7781
if let Some(family) = &font_request.family {
7882
builder.push_default(parley::StyleProperty::FontStack(
@@ -131,17 +135,19 @@ pub fn layout(text: &str, options: LayoutOptions) -> parley::Layout<sharedfontiq
131135
},
132136
parley::AlignmentOptions::default(),
133137
);
134-
layout
138+
139+
let y_offset = match (options.max_height, options.vertical_align) {
140+
(Some(max_height), TextVerticalAlignment::Center) => {
141+
(max_height.get() - layout.height()) / 2.0
142+
}
143+
(Some(max_height), TextVerticalAlignment::Bottom) => max_height.get() - layout.height(),
144+
(None, _) | (Some(_), TextVerticalAlignment::Top) => 0.0,
145+
};
146+
147+
Layout { inner: layout, y_offset }
135148
}
136149

137-
pub fn get_offset(
138-
vertical_align: TextVerticalAlignment,
139-
max_height: PhysicalLength,
140-
layout: &parley::Layout<sharedfontique::Brush>,
141-
) -> f32 {
142-
match vertical_align {
143-
TextVerticalAlignment::Top => 0.0,
144-
TextVerticalAlignment::Center => (max_height.get() - layout.height()) / 2.0,
145-
TextVerticalAlignment::Bottom => max_height.get() - layout.height(),
146-
}
150+
pub struct Layout {
151+
pub inner: parley::Layout<sharedfontique::Brush>,
152+
pub y_offset: f32,
147153
}

internal/renderers/femtovg/itemrenderer.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,13 @@ impl<'a, R: femtovg::Renderer + TextureImporter> GLItemRenderer<'a, R> {
196196
}
197197

198198
fn draw_glyphs<R: femtovg::Renderer + TextureImporter>(
199-
layout: &parley::Layout<sharedfontique::Brush>,
199+
layout: &fonts::Layout,
200200
canvas: &mut Canvas<R>,
201201
paint: &femtovg::Paint,
202-
offset: f32,
203202
selection_foreground_color: Color,
204203
selection_background_color: Color,
205204
) {
206-
for line in layout.lines() {
205+
for line in layout.inner.lines() {
207206
for item in line.items() {
208207
match item {
209208
parley::PositionedLayoutItem::GlyphRun(glyph_run) => {
@@ -220,9 +219,12 @@ fn draw_glyphs<R: femtovg::Renderer + TextureImporter>(
220219
let selection_rect = PhysicalRect::new(
221220
PhysicalPoint::new(
222221
start.x,
223-
start.y + offset - glyph_run.run().font_size(),
222+
start.y + layout.y_offset - run.font_size(),
223+
),
224+
PhysicalSize::new(
225+
glyph_run.advance(),
226+
run.font_size() + run.metrics().descent,
224227
),
225-
PhysicalSize::new(glyph_run.advance(), glyph_run.run().font_size()),
226228
);
227229
canvas.fill_path(
228230
&rect_to_path(selection_rect),
@@ -238,7 +240,7 @@ fn draw_glyphs<R: femtovg::Renderer + TextureImporter>(
238240
let glyphs = glyph_run.positioned_glyphs().map(|glyph: parley::Glyph| {
239241
femtovg::PositionedGlyph {
240242
x: glyph.x,
241-
y: glyph.y + offset,
243+
y: glyph.y + layout.y_offset,
242244
font_id,
243245
glyph_id: glyph.id,
244246
}
@@ -399,8 +401,8 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
399401
size: LogicalSize,
400402
_cache: &CachedRenderingData,
401403
) {
402-
let max_width = size.width_length() * self.scale_factor;
403-
let max_height = size.height_length() * self.scale_factor;
404+
let max_width = size.width_length();
405+
let max_height = size.height_length();
404406

405407
if max_width.get() <= 0. || max_height.get() <= 0. {
406408
return;
@@ -450,8 +452,11 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
450452

451453
let layout = fonts::layout(
452454
text.text().as_str(),
455+
self.scale_factor.get(),
453456
fonts::LayoutOptions {
454457
horizontal_align,
458+
vertical_align,
459+
max_height: Some(max_height),
455460
max_width: Some(max_width),
456461
stroke: stroke_paint.is_some().then_some(stroke_style).map(|style| match style {
457462
TextStrokeStyle::Outside => sharedfontique::BrushTextStrokeStyle::Outside,
@@ -464,9 +469,8 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
464469
);
465470

466471
let mut canvas = self.canvas.borrow_mut();
467-
let offset = fonts::get_offset(vertical_align, max_height, &layout);
468472

469-
draw_glyphs(&layout, &mut canvas, &paint, offset, Color::default(), Color::default());
473+
draw_glyphs(&layout, &mut canvas, &paint, Color::default(), Color::default());
470474
}
471475

472476
fn draw_text_input(
@@ -475,8 +479,8 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
475479
self_rc: &ItemRc,
476480
size: LogicalSize,
477481
) {
478-
let width = size.width_length() * self.scale_factor;
479-
let height = size.height_length() * self.scale_factor;
482+
let width = size.width_length();
483+
let height = size.height_length();
480484
if width.get() <= 0. || height.get() <= 0. {
481485
return;
482486
}
@@ -515,36 +519,36 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
515519

516520
let layout = fonts::layout(
517521
&text,
522+
self.scale_factor.get(),
518523
fonts::LayoutOptions {
519524
max_width: Some(width),
525+
max_height: Some(height),
526+
vertical_align: text_input.vertical_alignment(),
520527
selection: Some(min_select..max_select),
521528
font_request: Some(font_request),
522529
..Default::default()
523530
},
524531
);
525-
let offset = fonts::get_offset(text_input.vertical_alignment(), height, &layout);
526532
draw_glyphs(
527533
&layout,
528534
&mut canvas,
529535
&paint,
530-
offset,
531536
text_input.selection_foreground_color(),
532537
text_input.selection_background_color(),
533538
);
534539

535540
if cursor_visible {
536541
let cursor = parley::layout::cursor::Cursor::from_byte_index(
537-
&layout,
542+
&layout.inner,
538543
cursor_pos,
539544
Default::default(),
540545
);
541-
let rect = cursor
542-
.geometry(&layout, (text_input.text_cursor_width() * self.scale_factor).get());
546+
let rect = cursor.geometry(&layout.inner, (text_input.text_cursor_width()).get());
543547

544548
let mut cursor_rect = femtovg::Path::new();
545549
cursor_rect.rect(
546-
rect.center().x as _,
547-
rect.center().y as _,
550+
rect.min_x() as _,
551+
rect.min_y() as f32 + layout.y_offset,
548552
rect.width() as _,
549553
rect.height() as _,
550554
);
@@ -980,10 +984,10 @@ impl<'a, R: femtovg::Renderer + TextureImporter> ItemRenderer for GLItemRenderer
980984
}
981985

982986
fn draw_string(&mut self, string: &str, color: Color) {
983-
let layout = fonts::layout(string, Default::default());
987+
let layout = fonts::layout(string, self.scale_factor.get(), Default::default());
984988
let paint = femtovg::Paint::color(to_femtovg_color(&color));
985989
let mut canvas = self.canvas.borrow_mut();
986-
draw_glyphs(&layout, &mut canvas, &paint, 0.0, color, color);
990+
draw_glyphs(&layout, &mut canvas, &paint, color, color);
987991
}
988992

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

internal/renderers/femtovg/lib.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,15 @@ impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
289289
) -> LogicalSize {
290290
let layout = fonts::layout(
291291
text,
292+
scale_factor.get(),
292293
fonts::LayoutOptions {
293-
max_width: max_width.map(|max_width| max_width * scale_factor),
294+
max_width,
294295
text_wrap,
295296
font_request: Some(font_request),
296297
..Default::default()
297298
},
298299
);
299-
LogicalSize::new(layout.width(), layout.height())
300+
LogicalSize::new(layout.inner.width(), layout.inner.height())
300301
}
301302

302303
fn font_metrics(
@@ -317,23 +318,29 @@ impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
317318
let pos = pos * scale_factor;
318319
let text = text_input.text();
319320

320-
let width = text_input.width() * scale_factor;
321-
let height = text_input.height() * scale_factor;
321+
let width = text_input.width();
322+
let height = text_input.height();
322323
if width.get() <= 0. || height.get() <= 0. || pos.y < 0. {
323324
return 0;
324325
}
325326

326327
let layout = fonts::layout(
327328
&text,
329+
scale_factor.get(),
328330
fonts::LayoutOptions {
329331
font_request: Some(font_request),
330332
max_width: Some(width),
333+
max_height: Some(height),
334+
vertical_align: text_input.vertical_alignment(),
331335

332336
..Default::default()
333337
},
334338
);
335-
let offset = fonts::get_offset(text_input.vertical_alignment(), height, &layout);
336-
let cursor = parley::layout::cursor::Cursor::from_point(&layout, pos.x, pos.y - offset);
339+
let cursor = parley::layout::cursor::Cursor::from_point(
340+
&layout.inner,
341+
pos.x,
342+
pos.y - layout.y_offset,
343+
);
337344

338345
let visual_representation = text_input.visual_representation(None);
339346
visual_representation.map_byte_offset_from_byte_offset_in_visual_text(cursor.index())
@@ -350,8 +357,8 @@ impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
350357

351358
let font_size = font_request.pixel_size.unwrap_or(fonts::DEFAULT_FONT_SIZE);
352359

353-
let width = text_input.width() * scale_factor;
354-
let height = text_input.height() * scale_factor;
360+
let width = text_input.width();
361+
let height = text_input.height();
355362
if width.get() <= 0. || height.get() <= 0. {
356363
return LogicalRect::new(
357364
LogicalPoint::default(),
@@ -361,16 +368,21 @@ impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
361368

362369
let layout = fonts::layout(
363370
&text,
364-
fonts::LayoutOptions { max_width: Some(width), ..Default::default() },
371+
scale_factor.get(),
372+
fonts::LayoutOptions {
373+
max_width: Some(width),
374+
max_height: Some(height),
375+
..Default::default()
376+
},
365377
);
366378
let cursor = parley::layout::cursor::Cursor::from_byte_index(
367-
&layout,
379+
&layout.inner,
368380
byte_offset,
369381
Default::default(),
370382
);
371-
let rect = cursor.geometry(&layout, (text_input.text_cursor_width() * scale_factor).get());
383+
let rect = cursor.geometry(&layout.inner, (text_input.text_cursor_width()).get());
372384
LogicalRect::new(
373-
LogicalPoint::new(rect.min_x() as _, rect.min_y() as _),
385+
LogicalPoint::new(rect.min_x() as _, rect.min_y() as f32 + layout.y_offset),
374386
LogicalSize::new(rect.width() as _, rect.height() as _),
375387
)
376388
}

0 commit comments

Comments
 (0)