mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Auto merge of #12076 - jdm:font-load, r=pcwalton
Make font template data load fallible Remove a TODO around dealing with a failed file operation. Can we write an automated test for this? I don't really know what font template data is, but this failure seems to be fontconfig-specific... --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #12037 - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12076) <!-- Reviewable:end -->
This commit is contained in:
commit
a82d5106bd
4 changed files with 29 additions and 25 deletions
|
@ -96,8 +96,9 @@ impl FontTemplates {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let template = FontTemplate::new(identifier, maybe_data);
|
if let Ok(template) = FontTemplate::new(identifier, maybe_data) {
|
||||||
self.templates.push(template);
|
self.templates.push(template);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ use platform::font::FontHandle;
|
||||||
use platform::font_context::FontContextHandle;
|
use platform::font_context::FontContextHandle;
|
||||||
use platform::font_template::FontTemplateData;
|
use platform::font_template::FontTemplateData;
|
||||||
use std::fmt::{Debug, Error, Formatter};
|
use std::fmt::{Debug, Error, Formatter};
|
||||||
|
use std::io::Error as IoError;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use std::u32;
|
use std::u32;
|
||||||
use string_cache::Atom;
|
use string_cache::Atom;
|
||||||
|
@ -77,9 +78,9 @@ impl Debug for FontTemplate {
|
||||||
/// is common, regardless of the number of instances of
|
/// is common, regardless of the number of instances of
|
||||||
/// this font handle per thread.
|
/// this font handle per thread.
|
||||||
impl FontTemplate {
|
impl FontTemplate {
|
||||||
pub fn new(identifier: Atom, maybe_bytes: Option<Vec<u8>>) -> FontTemplate {
|
pub fn new(identifier: Atom, maybe_bytes: Option<Vec<u8>>) -> Result<FontTemplate, IoError> {
|
||||||
let maybe_data = match maybe_bytes {
|
let maybe_data = match maybe_bytes {
|
||||||
Some(_) => Some(FontTemplateData::new(identifier.clone(), maybe_bytes)),
|
Some(_) => Some(try!(FontTemplateData::new(identifier.clone(), maybe_bytes))),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -93,13 +94,13 @@ impl FontTemplate {
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
FontTemplate {
|
Ok(FontTemplate {
|
||||||
identifier: identifier,
|
identifier: identifier,
|
||||||
descriptor: None,
|
descriptor: None,
|
||||||
weak_ref: maybe_weak_ref,
|
weak_ref: maybe_weak_ref,
|
||||||
strong_ref: maybe_strong_ref,
|
strong_ref: maybe_strong_ref,
|
||||||
is_valid: true,
|
is_valid: true,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn identifier(&self) -> &Atom {
|
pub fn identifier(&self) -> &Atom {
|
||||||
|
@ -117,7 +118,7 @@ impl FontTemplate {
|
||||||
// so that we can do font matching against it again in the future
|
// so that we can do font matching against it again in the future
|
||||||
// without having to reload the font (unless it is an actual match).
|
// without having to reload the font (unless it is an actual match).
|
||||||
match self.descriptor {
|
match self.descriptor {
|
||||||
Some(actual_desc) if *requested_desc == actual_desc => Some(self.data()),
|
Some(actual_desc) if *requested_desc == actual_desc => self.data().ok(),
|
||||||
Some(_) => None,
|
Some(_) => None,
|
||||||
None => {
|
None => {
|
||||||
if self.instantiate(fctx).is_err() {
|
if self.instantiate(fctx).is_err() {
|
||||||
|
@ -127,7 +128,7 @@ impl FontTemplate {
|
||||||
if self.descriptor
|
if self.descriptor
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("Instantiation succeeded but no descriptor?") == requested_desc {
|
.expect("Instantiation succeeded but no descriptor?") == requested_desc {
|
||||||
Some(self.data())
|
self.data().ok()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -143,7 +144,9 @@ impl FontTemplate {
|
||||||
-> Option<(Arc<FontTemplateData>, u32)> {
|
-> Option<(Arc<FontTemplateData>, u32)> {
|
||||||
match self.descriptor {
|
match self.descriptor {
|
||||||
Some(actual_descriptor) => {
|
Some(actual_descriptor) => {
|
||||||
Some((self.data(), actual_descriptor.distance_from(requested_descriptor)))
|
self.data().ok().map(|data| {
|
||||||
|
(data, actual_descriptor.distance_from(requested_descriptor))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
if self.instantiate(font_context).is_ok() {
|
if self.instantiate(font_context).is_ok() {
|
||||||
|
@ -151,7 +154,7 @@ impl FontTemplate {
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("Instantiation successful but no descriptor?")
|
.expect("Instantiation successful but no descriptor?")
|
||||||
.distance_from(requested_descriptor);
|
.distance_from(requested_descriptor);
|
||||||
Some((self.data(), distance))
|
self.data().ok().map(|data| (data, distance))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -164,7 +167,7 @@ impl FontTemplate {
|
||||||
return Err(())
|
return Err(())
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = self.data();
|
let data = try!(self.data().map_err(|_| ()));
|
||||||
let handle: Result<FontHandle, ()> = FontHandleMethods::new_from_template(font_context,
|
let handle: Result<FontHandle, ()> = FontHandleMethods::new_from_template(font_context,
|
||||||
data,
|
data,
|
||||||
None);
|
None);
|
||||||
|
@ -179,7 +182,7 @@ impl FontTemplate {
|
||||||
/// Get the data for creating a font.
|
/// Get the data for creating a font.
|
||||||
pub fn get(&mut self) -> Option<Arc<FontTemplateData>> {
|
pub fn get(&mut self) -> Option<Arc<FontTemplateData>> {
|
||||||
if self.is_valid {
|
if self.is_valid {
|
||||||
Some(self.data())
|
self.data().ok()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -188,19 +191,19 @@ impl FontTemplate {
|
||||||
/// Get the font template data. If any strong references still
|
/// Get the font template data. If any strong references still
|
||||||
/// exist, it will return a clone, otherwise it will load the
|
/// exist, it will return a clone, otherwise it will load the
|
||||||
/// font data and store a weak reference to it internally.
|
/// font data and store a weak reference to it internally.
|
||||||
pub fn data(&mut self) -> Arc<FontTemplateData> {
|
pub fn data(&mut self) -> Result<Arc<FontTemplateData>, IoError> {
|
||||||
let maybe_data = match self.weak_ref {
|
let maybe_data = match self.weak_ref {
|
||||||
Some(ref data) => data.upgrade(),
|
Some(ref data) => data.upgrade(),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(data) = maybe_data {
|
if let Some(data) = maybe_data {
|
||||||
return data
|
return Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(self.strong_ref.is_none());
|
assert!(self.strong_ref.is_none());
|
||||||
let template_data = Arc::new(FontTemplateData::new(self.identifier.clone(), None));
|
let template_data = Arc::new(try!(FontTemplateData::new(self.identifier.clone(), None)));
|
||||||
self.weak_ref = Some(Arc::downgrade(&template_data));
|
self.weak_ref = Some(Arc::downgrade(&template_data));
|
||||||
template_data
|
Ok(template_data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::{Read, Error};
|
||||||
use string_cache::Atom;
|
use string_cache::Atom;
|
||||||
use webrender_traits::NativeFontHandle;
|
use webrender_traits::NativeFontHandle;
|
||||||
|
|
||||||
|
@ -18,24 +18,24 @@ pub struct FontTemplateData {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FontTemplateData {
|
impl FontTemplateData {
|
||||||
pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> FontTemplateData {
|
pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> Result<FontTemplateData, Error> {
|
||||||
let bytes = match font_data {
|
let bytes = match font_data {
|
||||||
Some(bytes) => {
|
Some(bytes) => {
|
||||||
bytes
|
bytes
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
// TODO: Handle file load failure!
|
// TODO: Handle file load failure!
|
||||||
let mut file = File::open(&*identifier).unwrap();
|
let mut file = try!(File::open(&*identifier));
|
||||||
let mut buffer = vec![];
|
let mut buffer = vec![];
|
||||||
file.read_to_end(&mut buffer).unwrap();
|
file.read_to_end(&mut buffer).unwrap();
|
||||||
buffer
|
buffer
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
FontTemplateData {
|
Ok(FontTemplateData {
|
||||||
bytes: bytes,
|
bytes: bytes,
|
||||||
identifier: identifier,
|
identifier: identifier,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a clone of the data in this font. This may be a hugely expensive
|
/// Returns a clone of the data in this font. This may be a hugely expensive
|
||||||
|
|
|
@ -12,7 +12,7 @@ use serde::de::{Error, Visitor};
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::{Read, Error as IoError};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use string_cache::Atom;
|
use string_cache::Atom;
|
||||||
|
@ -41,12 +41,12 @@ unsafe impl Send for FontTemplateData {}
|
||||||
unsafe impl Sync for FontTemplateData {}
|
unsafe impl Sync for FontTemplateData {}
|
||||||
|
|
||||||
impl FontTemplateData {
|
impl FontTemplateData {
|
||||||
pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> FontTemplateData {
|
pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> Result<FontTemplateData, IoError> {
|
||||||
FontTemplateData {
|
Ok(FontTemplateData {
|
||||||
ctfont: CachedCTFont(Mutex::new(HashMap::new())),
|
ctfont: CachedCTFont(Mutex::new(HashMap::new())),
|
||||||
identifier: identifier.to_owned(),
|
identifier: identifier.to_owned(),
|
||||||
font_data: font_data
|
font_data: font_data
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the Core Text font instance, instantiating it if necessary.
|
/// Retrieves the Core Text font instance, instantiating it if necessary.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue