Skip to content

Commit

Permalink
compute padding_y
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Jan 6, 2025
1 parent 6d965f6 commit 5db67f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions sugarloaf/src/components/rich_text/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl Compositor {
self.batches.add_rect(
&Rect::new(
rect.x,
style.topline,
style.topline + style.padding_y,
rect.width,
style.line_height_without_mod,
),
Expand All @@ -219,7 +219,7 @@ impl Compositor {
self.batches.add_rect(
&Rect::new(
rect.x,
style.topline,
style.topline + style.padding_y,
rect.width,
style.line_height_without_mod,
),
Expand All @@ -231,7 +231,7 @@ impl Compositor {
self.batches.add_rect(
&Rect::new(
rect.x + 2.0,
style.topline + 2.0,
style.topline + style.padding_y + 2.0,
rect.width - 4.0,
style.line_height_without_mod - 4.0,
),
Expand Down
21 changes: 15 additions & 6 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::components::core::orthographic_projection;
use crate::components::rich_text::image_cache::{GlyphCache, ImageCache};
use crate::context::Context;
use crate::font::FontLibrary;
use crate::layout::SugarDimensions;
use crate::layout::{RichTextLayout, SugarDimensions};
use crate::sugarloaf::graphics::GraphicRenderRequest;
use crate::Graphics;
use compositor::{Compositor, DisplayList, Rect, Vertex};
Expand Down Expand Up @@ -277,8 +277,7 @@ impl RichTextBrush {
&rt.lines,
position,
library,
&rt.layout.dimensions,
&rt.layout.line_height,
&rt.layout,
graphics,
);
}
Expand Down Expand Up @@ -412,10 +411,11 @@ fn draw_layout(
lines: &Vec<crate::layout::BuilderLine>,
pos: (f32, f32),
font_library: &FontLibrary,
rect: &SugarDimensions,
line_height_mod: &f32,
rte_layout: &RichTextLayout,
graphics: &mut Graphics,
) {
let rect = &rte_layout.dimensions;
let line_height_mod = &rte_layout.line_height;
// let start = std::time::Instant::now();
let (x, y) = pos;
let (image_cache, glyphs_cache) = caches;
Expand Down Expand Up @@ -458,6 +458,13 @@ fn draw_layout(
let line_height_without_mod = ascent + descent + leading;
let line_height = line_height_without_mod * line_height_mod;

// If line height is 10. and and modifier is 1.5, then 5
let padding_y = if line_height_mod > &1. {
(line_height - line_height_without_mod) / 2.
} else {
0.
};

let py = line_y;
for run in &line.render_data.runs {
glyphs.clear();
Expand All @@ -467,7 +474,7 @@ fn draw_layout(
let run_x = px;
for glyph in &run.glyphs {
let x = px;
let y = py;
let y = py + padding_y;
px += rect.width * char_width;
glyphs.push(Glyph {
id: glyph.simple_data().0,
Expand All @@ -483,6 +490,7 @@ fn draw_layout(
background_color: run.span.background_color,
baseline: py,
topline: py - ascent,
padding_y,
line_height,
line_height_without_mod,
advance: px - run_x,
Expand Down Expand Up @@ -602,6 +610,7 @@ fn fetch_dimensions(
cursor: run.span.cursor,
background_color: None,
baseline: py,
padding_y: 0.,
topline: py - ascent,
line_height,
line_height_without_mod: line_height,
Expand Down
2 changes: 2 additions & 0 deletions sugarloaf/src/components/rich_text/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct TextRunStyle<'a> {
pub topline: f32,
/// Absolute line height of the run.
pub line_height: f32,
/// Padding y
pub padding_y: f32,
/// Absolute line height of the run without mod.
pub line_height_without_mod: f32,
/// Total advance of the run.
Expand Down

0 comments on commit 5db67f3

Please sign in to comment.