Compute line-height as a multiple of font size

...instead of using the bounding box. This makes `PragmataPro` and other
fonts render more cleanly.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-03-15 16:35:06 +01:00
parent 8122abeb64
commit 447f350123
4 changed files with 6 additions and 8 deletions

View file

@ -146,8 +146,7 @@ impl Element for Label {
.ceil() .ceil()
.max(constraint.min.x()) .max(constraint.min.x())
.min(constraint.max.x()), .min(constraint.max.x()),
cx.font_cache cx.font_cache.line_height(self.style.text.font_size),
.line_height(self.style.text.font_id, self.style.text.font_size),
); );
(size, line) (size, line)

View file

@ -114,7 +114,7 @@ impl Element for Text {
max_line_width = max_line_width.max(shaped_line.width()); max_line_width = max_line_width.max(shaped_line.width());
} }
let line_height = cx.font_cache.line_height(font_id, self.style.font_size); let line_height = cx.font_cache.line_height(self.style.font_size);
let size = vec2f( let size = vec2f(
max_line_width max_line_width
.ceil() .ceil()

View file

@ -168,9 +168,8 @@ impl FontCache {
advance.x() * self.em_scale(font_id, font_size) advance.x() * self.em_scale(font_id, font_size)
} }
pub fn line_height(&self, font_id: FontId, font_size: f32) -> f32 { pub fn line_height(&self, font_size: f32) -> f32 {
let height = self.metric(font_id, |m| m.bounding_box.height()); (font_size * 1.618).round()
(height * self.em_scale(font_id, font_size)).ceil()
} }
pub fn cap_height(&self, font_id: FontId, font_size: f32) -> f32 { pub fn cap_height(&self, font_id: FontId, font_size: f32) -> f32 {
@ -194,7 +193,7 @@ impl FontCache {
} }
pub fn baseline_offset(&self, font_id: FontId, font_size: f32) -> f32 { pub fn baseline_offset(&self, font_id: FontId, font_size: f32) -> f32 {
let line_height = self.line_height(font_id, font_size); let line_height = self.line_height(font_size);
let ascent = self.ascent(font_id, font_size); let ascent = self.ascent(font_id, font_size);
let descent = self.descent(font_id, font_size); let descent = self.descent(font_id, font_size);
let padding_top = (line_height - ascent - descent) / 2.; let padding_top = (line_height - ascent - descent) / 2.;

View file

@ -188,7 +188,7 @@ impl TextStyle {
} }
pub fn line_height(&self, font_cache: &FontCache) -> f32 { pub fn line_height(&self, font_cache: &FontCache) -> f32 {
font_cache.line_height(self.font_id, self.font_size) font_cache.line_height(self.font_size)
} }
pub fn cap_height(&self, font_cache: &FontCache) -> f32 { pub fn cap_height(&self, font_cache: &FontCache) -> f32 {