Skip to content

Commit

Permalink
scale other fonts whenever is necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Sep 7, 2024
1 parent 8006035 commit decf4c5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
17 changes: 13 additions & 4 deletions sugarloaf/examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ApplicationHandler for Application {
);
content.finish_line();
content.add_text(
"㏑¼",
"㏑¼",
FragmentStyle {
color: [0.0, 0.0, 0.0, 1.0],
background_color: Some([1.0, 1.0, 1.0, 1.0]),
Expand All @@ -152,7 +152,7 @@ impl ApplicationHandler for Application {
);
content.finish_line();
content.add_text(
" regular -> ",
"regular -> ",
FragmentStyle {
decoration: Some(FragmentStyleDecoration::Underline(
UnderlineInfo {
Expand Down Expand Up @@ -202,7 +202,7 @@ impl ApplicationHandler for Application {
},
);
content.add_text(
"curly",
"curly",
FragmentStyle {
decoration: Some(FragmentStyleDecoration::Underline(
UnderlineInfo {
Expand All @@ -219,7 +219,7 @@ impl ApplicationHandler for Application {
);
content.finish_line();
content.add_text(
"dashed",
"dashed",
FragmentStyle {
decoration: Some(FragmentStyleDecoration::Underline(
UnderlineInfo {
Expand Down Expand Up @@ -259,6 +259,15 @@ impl ApplicationHandler for Application {
..FragmentStyle::default()
},
);
content.finish_line();
content.add_text(
"│ \u{E0B6}Hello There!\u{e0b4}",
FragmentStyle {
color: [1.0, 1.0, 1.0, 1.0],
background_color: Some([0.5, 0.5, 1.0, 1.0]),
..FragmentStyle::default()
},
);
sugarloaf.set_content(content.build());
sugarloaf.render();
event_loop.set_control_flow(ControlFlow::Wait);
Expand Down
43 changes: 33 additions & 10 deletions sugarloaf/src/components/rich_text/image_cache/glyph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct GlyphCache {
scx: ScaleContext,
fonts: FxHashMap<FontKey, FontEntry>,
img: GlyphImage,
max_height: u16,
}

impl GlyphCache {
Expand All @@ -29,6 +30,7 @@ impl GlyphCache {
scx: ScaleContext::new(),
fonts: FxHashMap::default(),
img: GlyphImage::new(),
max_height: 0,
}
}

Expand All @@ -55,11 +57,17 @@ impl GlyphCache {
entry,
images,
scaler,
max_height: &self.max_height,
scaled_image: &mut self.img,
quant_size,
}
}

#[inline]
pub fn set_max_height(&mut self, max_height: u16) {
self.max_height = max_height;
}

// pub fn prune(&mut self, images: &mut ImageCache) {
// self.fonts.retain(|_, entry| {
// for glyph in &entry.glyphs {
Expand All @@ -68,14 +76,6 @@ impl GlyphCache {
// false
// });
// }

#[allow(unused)]
pub fn clear_evicted(&mut self, images: &mut ImageCache) {
self.fonts.retain(|_, entry| {
entry.glyphs.retain(|_, g| images.is_valid(g.image));
!entry.glyphs.is_empty()
});
}
}

fn get_entry<'a>(
Expand Down Expand Up @@ -103,6 +103,7 @@ pub struct GlyphCacheSession<'a> {
scaler: Scaler<'a>,
scaled_image: &'a mut GlyphImage,
quant_size: u16,
max_height: &'a u16,
}

impl<'a> GlyphCacheSession<'a> {
Expand Down Expand Up @@ -149,11 +150,33 @@ impl<'a> GlyphCacheSession<'a> {
data: ImageData::Borrowed(&self.scaled_image.data),
};
let image = self.images.allocate(req)?;

let mut top = p.top;
let mut height = h;

// If dimension is None it means that we are running
// for the first time and in this case, we will obtain
// what the next glyph entries should respect in terms of
// top and height values
//
// e.g: Placement { left: 11, top: 42, width: 8, height: 50 }
//
// The calculation is made based on max_height
// If the rect max height is 50 and the glyph height is 68
// and 48 top, then (68 - 50 = 18) height as difference and
// apply it to the top (bigger the top == up ^).
if self.max_height > &0 && &h > self.max_height {
let difference = h - self.max_height;

top -= difference as i32;
height = *self.max_height;
}

let entry = GlyphEntry {
left: p.left,
top: p.top,
top,
width: w,
height: h,
height,
image,
is_bitmap: self.scaled_image.content == Content::Color,
};
Expand Down
3 changes: 2 additions & 1 deletion sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ fn draw_layout(
}
}

glyphs_cache.set_max_height(rect.height as u16);

let mut session = glyphs_cache.session(
image_cache,
font_library[current_font].as_ref(),
Expand Down Expand Up @@ -503,7 +505,6 @@ fn draw_layout(
let char_width = run.char_width();
let mut cached_run = CachedRun::new(char_width);
let font = *run.font();
let char_width = run.char_width();

let run_x = px;
glyphs.clear();
Expand Down
6 changes: 3 additions & 3 deletions sugarloaf/src/layout/layout_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,12 @@ fn commit_line(
hash: run_data.hash,
ascent: run_data.ascent.round(),
descent: run_data.descent.round(),
leading: (run_data.leading * 0.5).round() * 2.,
leading: (run_data.leading).round() * 2.,
..Default::default()
};

let above = (line.ascent + line.leading * 0.5).round();
let below = (line.descent + line.leading * 0.5).round();
let above = line.ascent;
let below = line.descent;
line.baseline = *y + above;
*y = line.baseline + below;

Expand Down
9 changes: 0 additions & 9 deletions sugarloaf/src/sugarloaf/compositors/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl Advanced {

#[inline]
pub fn update_layout(&mut self, tree: &SugarTree) {
// let start = std::time::Instant::now();
self.render_data = RenderData::default();

let mut lb = self
Expand All @@ -74,19 +73,11 @@ impl Advanced {
self.render_data
.break_lines()
.break_without_advance_or_alignment();

// let duration = start.elapsed();
// println!(" - advanced::update_layout() is: {:?}", duration);
}

#[inline]
pub fn calculate_dimensions(&mut self, tree: &SugarTree) {
let mut content_builder = Content::builder();
// content_builder.enter_span(&[
// SpanStyle::FontId(0),
// SpanStyle::Size(tree.layout.font_size),
// // S::features(&[("dlig", 1).into(), ("hlig", 1).into()][..]),
// ]);
content_builder.add_char(' ', FragmentStyle::default());

let mut lb = self
Expand Down

0 comments on commit decf4c5

Please sign in to comment.