Skip missing glyphs when drawing 2d canvas text.

This commit is contained in:
Josh Matthews 2020-08-07 13:57:57 -04:00
parent 8067a2fe94
commit 8ecc1a80a7
3 changed files with 27 additions and 3 deletions

1
Cargo.lock generated
View file

@ -544,6 +544,7 @@ dependencies = [
"log",
"lyon_geom 0.14.1",
"num-traits",
"pathfinder_geometry",
"pixels",
"raqote",
"servo_arc",

View file

@ -31,6 +31,7 @@ ipc-channel = "0.14"
log = "0.4"
lyon_geom = "0.14"
num-traits = "0.2"
pathfinder_geometry = "0.5"
pixels = { path = "../pixels" }
raqote = { version = "0.8", features = ["text"] }
servo_arc = { path = "../servo_arc" }

View file

@ -527,11 +527,33 @@ impl GenericDrawTarget for raqote::DrawTarget {
pattern: &canvas_data::Pattern,
options: &DrawOptions,
) {
self.draw_text(
let mut start = pathfinder_geometry::vector::vec2f(start.x, start.y);
let mut ids = Vec::new();
let mut positions = Vec::new();
for c in text.chars() {
let id = match font.glyph_for_char(c) {
Some(id) => id,
None => {
warn!("Skipping non-existent glyph {}", c);
continue;
},
};
ids.push(id);
positions.push(Point2D::new(start.x(), start.y()));
let advance = match font.advance(id) {
Ok(advance) => advance,
Err(e) => {
warn!("Skipping glyph {} with missing advance: {:?}", c, e);
continue;
},
};
start += advance * point_size / 24. / 96.;
}
self.draw_glyphs(
font,
point_size,
text,
start,
&ids,
&positions,
&pattern.source(),
options.as_raqote(),
);