draw: Use font's horizontal advance value
This commit is contained in:
committed by
Kenny Levinsen
parent
43fe73bf09
commit
cf9fabb052
+11
-9
@@ -8,16 +8,16 @@ struct CachedGlyph {
|
|||||||
dimensions: (u32, u32),
|
dimensions: (u32, u32),
|
||||||
origin: (i32, i32),
|
origin: (i32, i32),
|
||||||
render: Vec<f32>,
|
render: Vec<f32>,
|
||||||
|
advance: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CachedGlyph {
|
impl CachedGlyph {
|
||||||
fn new(font: FontRef, size: f32, ch: char) -> CachedGlyph {
|
fn new(font: FontRef, size: f32, ch: char) -> CachedGlyph {
|
||||||
let scale = Scale::uniform(size);
|
let scale = Scale::uniform(size);
|
||||||
let v_metrics = font.v_metrics(scale);
|
let v_metrics = font.v_metrics(scale);
|
||||||
let glyph = font
|
let glyph = font.glyph(ch).scaled(scale);
|
||||||
.glyph(ch)
|
let advance = glyph.h_metrics().advance_width as i32;
|
||||||
.scaled(scale)
|
let glyph = glyph.positioned(point(0.0, v_metrics.ascent));
|
||||||
.positioned(point(0.0, v_metrics.ascent));
|
|
||||||
|
|
||||||
if let Some(bounding_box) = glyph.pixel_bounding_box() {
|
if let Some(bounding_box) = glyph.pixel_bounding_box() {
|
||||||
let origin = (bounding_box.min.x, bounding_box.min.y);
|
let origin = (bounding_box.min.x, bounding_box.min.y);
|
||||||
@@ -35,12 +35,14 @@ impl CachedGlyph {
|
|||||||
origin,
|
origin,
|
||||||
dimensions,
|
dimensions,
|
||||||
render,
|
render,
|
||||||
|
advance,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CachedGlyph {
|
CachedGlyph {
|
||||||
origin: (0, 0),
|
origin: (0, 0),
|
||||||
dimensions: ((size / 4.0) as u32, 0),
|
dimensions: (0, 0),
|
||||||
render: Vec::new(),
|
render: Vec::new(),
|
||||||
|
advance: (size / 4.0) as i32,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +120,7 @@ impl<'a> Font<'a> {
|
|||||||
}
|
}
|
||||||
for glyph in glyphs {
|
for glyph in glyphs {
|
||||||
glyph.draw(buf, (x_off, -off), bg, c);
|
glyph.draw(buf, (x_off, -off), bg, c);
|
||||||
x_off += glyph.dimensions.0 as i32 + glyph.origin.0;
|
x_off += glyph.advance;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((x_off as u32, self.size as u32))
|
Ok((x_off as u32, self.size as u32))
|
||||||
@@ -157,7 +159,7 @@ impl<'a> Font<'a> {
|
|||||||
self.draw_cursor(buf, c, x_off as u32, height)?;
|
self.draw_cursor(buf, c, x_off as u32, height)?;
|
||||||
}
|
}
|
||||||
glyph.draw(buf, (x_off, -off), bg, c);
|
glyph.draw(buf, (x_off, -off), bg, c);
|
||||||
x_off += glyph.dimensions.0 as i32 + glyph.origin.0;
|
x_off += glyph.advance;
|
||||||
}
|
}
|
||||||
if cursor == glyphs.len() {
|
if cursor == glyphs.len() {
|
||||||
self.draw_cursor(buf, c, x_off as u32, height)?;
|
self.draw_cursor(buf, c, x_off as u32, height)?;
|
||||||
@@ -193,7 +195,7 @@ impl<'a> Font<'a> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let width = glyph.dimensions.0 as i32 + glyph.origin.0;
|
let width = glyph.advance;
|
||||||
if width > max {
|
if width > max {
|
||||||
max = width
|
max = width
|
||||||
}
|
}
|
||||||
@@ -285,7 +287,7 @@ impl<'a> Font<'a> {
|
|||||||
}
|
}
|
||||||
for (idx, glyph) in glyphs.into_iter().enumerate() {
|
for (idx, glyph) in glyphs.into_iter().enumerate() {
|
||||||
glyph.draw(buf, (x_off, -off), bg, &color[idx]);
|
glyph.draw(buf, (x_off, -off), bg, &color[idx]);
|
||||||
x_off += glyph.dimensions.0 as i32 + glyph.origin.0;
|
x_off += glyph.advance;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((x_off as u32, self.size as u32))
|
Ok((x_off as u32, self.size as u32))
|
||||||
|
|||||||
Reference in New Issue
Block a user