Skip to content

Commit 778dc01

Browse files
authored
Auto merge of #27548 - jdm:canvas-text-panic, r=<try>
Canvas text and macOS font fixes These changes work around a source of panics by skipping glyphs that don't exist when drawing text to the canvas. Ideally we would have font fallback and/or tofu characters, but that's a larger project than I wanted to get into right now. Relatedly, while investigating the non-panic errors observed in #27476, I realized that the underlying source of #24622 could be dealt with because we shouldn't actually need to open the CTFont by a file path to get the bytes, since we already have bytes available that we use to create the CTFont. --- - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #24622 and fix #27476 and fix the panic observed in #20445. - [x] There are tests for these changes
2 parents 53467b8 + 1e743a7 commit 778dc01

5 files changed

Lines changed: 56 additions & 21 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/canvas/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ipc-channel = "0.14"
3131
log = "0.4"
3232
lyon_geom = "0.14"
3333
num-traits = "0.2"
34+
pathfinder_geometry = "0.5"
3435
pixels = { path = "../pixels" }
3536
raqote = { version = "0.8", features = ["text"] }
3637
servo_arc = { path = "../servo_arc" }

components/canvas/canvas_data.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,10 @@ impl<'a> CanvasData<'a> {
523523
.first(font_context)
524524
.expect("couldn't find font");
525525
let font = font.borrow_mut();
526-
// Retrieving bytes from font template seems to panic for some core text fonts.
527-
// This check avoids having to obtain bytes from the font template data if they
528-
// are not already in the memory.
529-
if let Some(bytes) = font.handle.template().bytes_if_in_memory() {
530-
Font::from_bytes(Arc::new(bytes), 0)
531-
.ok()
532-
.or_else(|| load_system_font_from_style(Some(style)))
533-
} else {
534-
load_system_font_from_style(Some(style))
535-
}
526+
let template = font.handle.template();
527+
Font::from_bytes(Arc::new(template.bytes()), 0)
528+
.ok()
529+
.or_else(|| load_system_font_from_style(Some(style)))
536530
})
537531
},
538532
);

components/canvas/raqote_backend.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,33 @@ impl GenericDrawTarget for raqote::DrawTarget {
527527
pattern: &canvas_data::Pattern,
528528
options: &DrawOptions,
529529
) {
530-
self.draw_text(
530+
let mut start = pathfinder_geometry::vector::vec2f(start.x, start.y);
531+
let mut ids = Vec::new();
532+
let mut positions = Vec::new();
533+
for c in text.chars() {
534+
let id = match font.glyph_for_char(c) {
535+
Some(id) => id,
536+
None => {
537+
warn!("Skipping non-existent glyph {}", c);
538+
continue;
539+
},
540+
};
541+
ids.push(id);
542+
positions.push(Point2D::new(start.x(), start.y()));
543+
let advance = match font.advance(id) {
544+
Ok(advance) => advance,
545+
Err(e) => {
546+
warn!("Skipping glyph {} with missing advance: {:?}", c, e);
547+
continue;
548+
},
549+
};
550+
start += advance * point_size / 24. / 96.;
551+
}
552+
self.draw_glyphs(
531553
font,
532554
point_size,
533-
text,
534-
start,
555+
&ids,
556+
&positions,
535557
&pattern.source(),
536558
options.as_raqote(),
537559
);

components/gfx/platform/macos/font_template.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::fs::{self, File};
2323
use std::io::{Error as IoError, Read};
2424
use std::ops::Deref;
2525
use std::path::Path;
26-
use std::sync::{Arc, Mutex};
26+
use std::sync::{Arc, Mutex, RwLock};
2727
use webrender_api::NativeFontHandle;
2828

2929
/// Platform specific font representation for mac.
@@ -43,7 +43,7 @@ pub struct FontTemplateData {
4343
ctfont: CachedCTFont,
4444

4545
pub identifier: Atom,
46-
pub font_data: Option<Arc<Vec<u8>>>,
46+
pub font_data: RwLock<Option<Arc<Vec<u8>>>>,
4747
}
4848

4949
impl fmt::Debug for FontTemplateData {
@@ -55,6 +55,8 @@ impl fmt::Debug for FontTemplateData {
5555
"font_data",
5656
&self
5757
.font_data
58+
.read()
59+
.unwrap()
5860
.as_ref()
5961
.map(|bytes| format!("[{} bytes]", bytes.len())),
6062
)
@@ -70,7 +72,7 @@ impl FontTemplateData {
7072
Ok(FontTemplateData {
7173
ctfont: CachedCTFont(Mutex::new(HashMap::new())),
7274
identifier: identifier.to_owned(),
73-
font_data: font_data.map(Arc::new),
75+
font_data: RwLock::new(font_data.map(Arc::new)),
7476
})
7577
}
7678

@@ -82,7 +84,8 @@ impl FontTemplateData {
8284
// If you pass a zero font size to one of the Core Text APIs, it'll replace it with
8385
// 12.0. We don't want that! (Issue #10492.)
8486
let clamped_pt_size = pt_size.max(0.01);
85-
let ctfont = match self.font_data {
87+
let mut font_data = self.font_data.write().unwrap();
88+
let ctfont = match *font_data {
8689
Some(ref bytes) => {
8790
let fontprov = CGDataProvider::from_buffer(bytes.clone());
8891
let cgfont_result = CGFont::from_data_provider(fontprov);
@@ -114,8 +117,10 @@ impl FontTemplateData {
114117
let descriptor = descriptors.get(0).unwrap();
115118
let font_path = Path::new(&descriptor.font_path().unwrap()).to_owned();
116119
fs::read(&font_path).ok().and_then(|bytes| {
117-
let fontprov = CGDataProvider::from_buffer(Arc::new(bytes));
120+
let font_bytes = Arc::new(bytes);
121+
let fontprov = CGDataProvider::from_buffer(font_bytes.clone());
118122
CGFont::from_data_provider(fontprov).ok().map(|cgfont| {
123+
*font_data = Some(font_bytes);
119124
core_text::font::new_from_CGFont(&cgfont, clamped_pt_size)
120125
})
121126
})
@@ -137,9 +142,17 @@ impl FontTemplateData {
137142
return font_data;
138143
}
139144

145+
// This is spooky action at a distance, but getting a CTFont from this template
146+
// will (in the common case) bring the bytes into memory if they were not there
147+
// already. This also helps work around intermittent panics like
148+
// https://github.com/servo/servo/issues/24622 that occur for unclear reasons.
149+
let ctfont = self.ctfont(0.0);
150+
if let Some(font_data) = self.bytes_if_in_memory() {
151+
return font_data;
152+
}
153+
140154
let path = ServoUrl::parse(
141-
&*self
142-
.ctfont(0.0)
155+
&*ctfont
143156
.expect("No Core Text font available!")
144157
.url()
145158
.expect("No URL for Core Text font!")
@@ -161,7 +174,11 @@ impl FontTemplateData {
161174
/// Returns a clone of the bytes in this font if they are in memory. This function never
162175
/// performs disk I/O.
163176
pub fn bytes_if_in_memory(&self) -> Option<Vec<u8>> {
164-
self.font_data.as_ref().map(|bytes| (**bytes).clone())
177+
self.font_data
178+
.read()
179+
.unwrap()
180+
.as_ref()
181+
.map(|bytes| (**bytes).clone())
165182
}
166183

167184
/// Returns the native font that underlies this font template, if applicable.

0 commit comments

Comments
 (0)