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:
bors-servo 2016-09-19 09:14:55 -05:00 committed by GitHub
commit a82d5106bd
4 changed files with 29 additions and 25 deletions

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::fs::File;
use std::io::Read;
use std::io::{Read, Error};
use string_cache::Atom;
use webrender_traits::NativeFontHandle;
@ -18,24 +18,24 @@ pub struct 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 {
Some(bytes) => {
bytes
},
None => {
// TODO: Handle file load failure!
let mut file = File::open(&*identifier).unwrap();
let mut file = try!(File::open(&*identifier));
let mut buffer = vec![];
file.read_to_end(&mut buffer).unwrap();
buffer
},
};
FontTemplateData {
Ok(FontTemplateData {
bytes: bytes,
identifier: identifier,
}
})
}
/// Returns a clone of the data in this font. This may be a hugely expensive

View file

@ -12,7 +12,7 @@ use serde::de::{Error, Visitor};
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::io::{Read, Error as IoError};
use std::ops::Deref;
use std::sync::Mutex;
use string_cache::Atom;
@ -41,12 +41,12 @@ unsafe impl Send for FontTemplateData {}
unsafe impl Sync for FontTemplateData {}
impl FontTemplateData {
pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> FontTemplateData {
FontTemplateData {
pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> Result<FontTemplateData, IoError> {
Ok(FontTemplateData {
ctfont: CachedCTFont(Mutex::new(HashMap::new())),
identifier: identifier.to_owned(),
font_data: font_data
}
})
}
/// Retrieves the Core Text font instance, instantiating it if necessary.