From 793b5cc3a9e16d25a3a623f0a00517da3b04c0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=BCrr?= Date: Sun, 24 Dec 2017 17:17:07 +0100 Subject: [PATCH] Move custom cursor block on ft Moved the custom cursor block on ft to the top, so no unnecessary operations are executed when trying to draw a custom cursor glyph. --- font/src/ft/mod.rs | 112 ++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/font/src/ft/mod.rs b/font/src/ft/mod.rs index b46b0086..7ed3b426 100644 --- a/font/src/ft/mod.rs +++ b/font/src/ft/mod.rs @@ -273,6 +273,54 @@ impl FreeTypeRasterizer { fn get_rendered_glyph(&mut self, glyph_key: &GlyphKey) -> Result { + // Render a custom symbol for the underline and beam cursor + match glyph_key.c { + super::UNDERLINE_CURSOR_CHAR => { + // Get the primary face metrics + // This always loads the default face + let face = self.faces.get(&glyph_key.font_key).unwrap(); + let size_metrics = face.ft_face + .size_metrics() + .ok_or(Error::MissingSizeMetrics)?; + + // Get the bottom of the bounding box + let descent = (size_metrics.descender / 64) as i32; + + // Get the width of the cell + let width = (size_metrics.max_advance / 64) as i32; + + // Return the new custom glyph + return super::get_underline_cursor_glyph(descent, width); + }, + super::BEAM_CURSOR_CHAR | super::BOX_CURSOR_CHAR => { + // Get the primary face metrics + // This always loads the default face + let face = self.faces.get(&glyph_key.font_key).unwrap(); + let size_metrics = face.ft_face + .size_metrics() + .ok_or(Error::MissingSizeMetrics)?; + + // Get the height of the cell + let height = (size_metrics.height / 64) as i32; + + // Get the top of the bounding box + let descent = (size_metrics.descender / 64) as i32; + let ascent = height + descent; + + // Get the width of the cell + let width = (size_metrics.max_advance / 64) as i32; + + // Return the new custom glyph + return if glyph_key.c == super::BEAM_CURSOR_CHAR { + super::get_beam_cursor_glyph(ascent, height, width) + } else { + super::get_box_cursor_glyph(ascent, height, width) + }; + }, + _ => (), + } + + // Render a normal character if it's not a cursor let font_key = self.face_for_glyph(glyph_key, false)?; let face = self.faces.get(&font_key).unwrap(); let index = face.ft_face.get_char_index(glyph_key.c as usize); @@ -294,62 +342,14 @@ impl FreeTypeRasterizer { let (pixel_width, buf) = Self::normalize_buffer(&glyph.bitmap())?; - // Render a custom symbol for the underline and beam cursor - match glyph_key.c { - super::UNDERLINE_CURSOR_CHAR => { - // Get the primary face metrics - // This always loads the default face - let face = self.faces.get(glyph_key.font_key)?; - let size_metrics = face.ft_face - .size_metrics() - .ok_or(Error::MissingSizeMetrics)?; - - // Get the bottom of the bounding box - let descent = (size_metrics.descender / 64) as i32; - - // Get the width of the cell - let width = (size_metrics.max_advance / 64) as i32; - - // Return the new custom glyph - super::get_underline_cursor_glyph(descent, width) - } - super::BEAM_CURSOR_CHAR | super::BOX_CURSOR_CHAR => { - // Get the primary face metrics - // This always loads the default face - let face = self.faces.get(glyph_key.font_key)?; - let size_metrics = face.ft_face - .size_metrics() - .ok_or(Error::MissingSizeMetrics)?; - - // Get the height of the cell - let height = (size_metrics.height / 64) as i32; - - // Get the top of the bounding box - let descent = (size_metrics.descender / 64) as i32; - let ascent = height + descent; - - // Get the width of the cell - let width = (size_metrics.max_advance / 64) as i32; - - // Return the new custom glyph - if glyph_key.c == super::BEAM_CURSOR_CHAR { - super::get_beam_cursor_glyph(ascent, height, width) - } else { - super::get_box_cursor_glyph(ascent, height, width) - } - } - _ => { - // If it's not a special char, return the normal glyph - Ok(RasterizedGlyph { - c: glyph_key.c, - top: glyph.bitmap_top(), - left: glyph.bitmap_left(), - width: pixel_width, - height: glyph.bitmap().rows(), - buf: buf, - }) - } - } + Ok(RasterizedGlyph { + c: glyph_key.c, + top: glyph.bitmap_top(), + left: glyph.bitmap_left(), + width: pixel_width, + height: glyph.bitmap().rows(), + buf: buf, + }) } fn ft_load_flags(pat: &fc::Pattern) -> freetype::face::LoadFlag {