Use atoms for font template structures.

This commit is contained in:
Glenn Watson 2015-07-13 08:00:28 +10:00
parent d7cf58d6f1
commit f8eeef0eb4
5 changed files with 27 additions and 26 deletions

View file

@ -61,9 +61,9 @@ impl FontFamily {
None None
} }
fn add_template(&mut self, identifier: &str, maybe_data: Option<Vec<u8>>) { fn add_template(&mut self, identifier: Atom, maybe_data: Option<Vec<u8>>) {
for template in self.templates.iter() { for template in self.templates.iter() {
if template.identifier() == identifier { if *template.identifier() == identifier {
return; return;
} }
} }
@ -140,7 +140,7 @@ impl FontCache {
match maybe_resource { match maybe_resource {
Ok((_, bytes)) => { Ok((_, bytes)) => {
let family = &mut self.web_families.get_mut(&family_name).unwrap(); let family = &mut self.web_families.get_mut(&family_name).unwrap();
family.add_template(&url.to_string(), Some(bytes)); family.add_template(Atom::from_slice(&url.to_string()), Some(bytes));
}, },
Err(_) => { Err(_) => {
debug!("Failed to load web font: family={:?} url={}", family_name, url); debug!("Failed to load web font: family={:?} url={}", family_name, url);
@ -150,7 +150,7 @@ impl FontCache {
Source::Local(ref local_family_name) => { Source::Local(ref local_family_name) => {
let family = &mut self.web_families.get_mut(&family_name).unwrap(); let family = &mut self.web_families.get_mut(&family_name).unwrap();
get_variations_for_family(&local_family_name, |path| { get_variations_for_family(&local_family_name, |path| {
family.add_template(&path, None); family.add_template(Atom::from_slice(&path), None);
}); });
} }
} }
@ -192,7 +192,7 @@ impl FontCache {
if s.templates.len() == 0 { if s.templates.len() == 0 {
get_variations_for_family(family_name, |path| { get_variations_for_family(family_name, |path| {
s.add_template(&path, None); s.add_template(Atom::from_slice(&path), None);
}); });
} }

View file

@ -14,6 +14,7 @@ use fnv::FnvHasher;
use platform::font::FontHandle; use platform::font::FontHandle;
use platform::font_template::FontTemplateData; use platform::font_template::FontTemplateData;
use smallvec::SmallVec8; use smallvec::SmallVec8;
use string_cache::Atom;
use util::cache::HashCache; use util::cache::HashCache;
use util::geometry::Au; use util::geometry::Au;
use util::mem::HeapSizeOf; use util::mem::HeapSizeOf;
@ -60,7 +61,7 @@ struct FallbackFontCacheEntry {
/// can be shared by multiple text runs. /// can be shared by multiple text runs.
struct PaintFontCacheEntry { struct PaintFontCacheEntry {
pt_size: Au, pt_size: Au,
identifier: String, identifier: Atom,
font: Rc<RefCell<ScaledFont>>, font: Rc<RefCell<ScaledFont>>,
} }

View file

@ -6,8 +6,8 @@ use font::FontHandleMethods;
use platform::font_context::FontContextHandle; use platform::font_context::FontContextHandle;
use platform::font::FontHandle; use platform::font::FontHandle;
use platform::font_template::FontTemplateData; use platform::font_template::FontTemplateData;
use string_cache::Atom;
use std::borrow::ToOwned;
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use style::computed_values::{font_stretch, font_weight}; use style::computed_values::{font_stretch, font_weight};
@ -46,7 +46,7 @@ impl PartialEq for FontTemplateDescriptor {
/// font instance handles. It contains a unique /// font instance handles. It contains a unique
/// FontTemplateData structure that is platform specific. /// FontTemplateData structure that is platform specific.
pub struct FontTemplate { pub struct FontTemplate {
identifier: String, identifier: Atom,
descriptor: Option<FontTemplateDescriptor>, descriptor: Option<FontTemplateDescriptor>,
weak_ref: Option<Weak<FontTemplateData>>, weak_ref: Option<Weak<FontTemplateData>>,
// GWTODO: Add code path to unset the strong_ref for web fonts! // GWTODO: Add code path to unset the strong_ref for web fonts!
@ -58,9 +58,9 @@ pub struct 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: &str, maybe_bytes: Option<Vec<u8>>) -> FontTemplate { pub fn new(identifier: Atom, maybe_bytes: Option<Vec<u8>>) -> FontTemplate {
let maybe_data = match maybe_bytes { let maybe_data = match maybe_bytes {
Some(_) => Some(FontTemplateData::new(identifier, maybe_bytes)), Some(_) => Some(FontTemplateData::new(identifier.clone(), maybe_bytes)),
None => None, None => None,
}; };
@ -75,7 +75,7 @@ impl FontTemplate {
}; };
FontTemplate { FontTemplate {
identifier: identifier.to_owned(), 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,
@ -83,8 +83,8 @@ impl FontTemplate {
} }
} }
pub fn identifier<'a>(&'a self) -> &'a str { pub fn identifier(&self) -> &Atom {
&*self.identifier &self.identifier
} }
/// Get the data for creating a font if it matches a given descriptor. /// Get the data for creating a font if it matches a given descriptor.
@ -158,7 +158,7 @@ impl FontTemplate {
} }
assert!(self.strong_ref.is_none()); assert!(self.strong_ref.is_none());
let template_data = Arc::new(FontTemplateData::new(&self.identifier, None)); let template_data = Arc::new(FontTemplateData::new(self.identifier.clone(), None));
self.weak_ref = Some(template_data.downgrade()); self.weak_ref = Some(template_data.downgrade());
template_data template_data
} }

View file

@ -2,9 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* 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::borrow::ToOwned;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use string_cache::Atom;
/// Platform specific font representation for Linux. /// Platform specific font representation for Linux.
/// The identifier is an absolute path, and the bytes /// The identifier is an absolute path, and the bytes
@ -12,18 +12,18 @@ use std::io::Read;
/// freetype and azure directly. /// freetype and azure directly.
pub struct FontTemplateData { pub struct FontTemplateData {
pub bytes: Vec<u8>, pub bytes: Vec<u8>,
pub identifier: String, pub identifier: Atom,
} }
impl FontTemplateData { impl FontTemplateData {
pub fn new(identifier: &str, font_data: Option<Vec<u8>>) -> FontTemplateData { pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> FontTemplateData {
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 = File::open(identifier.as_slice()).unwrap();
let mut buffer = vec![]; let mut buffer = vec![];
file.read_to_end(&mut buffer).unwrap(); file.read_to_end(&mut buffer).unwrap();
buffer buffer
@ -32,7 +32,7 @@ impl FontTemplateData {
FontTemplateData { FontTemplateData {
bytes: bytes, bytes: bytes,
identifier: identifier.to_owned(), identifier: identifier,
} }
} }
} }

View file

@ -7,7 +7,7 @@ use core_graphics::font::CGFont;
use core_text::font::CTFont; use core_text::font::CTFont;
use core_text; use core_text;
use std::borrow::ToOwned; use string_cache::Atom;
/// Platform specific font representation for mac. /// Platform specific font representation for mac.
/// The identifier is a PostScript font name. The /// The identifier is a PostScript font name. The
@ -15,7 +15,7 @@ use std::borrow::ToOwned;
/// paint functions that create CGFont references. /// paint functions that create CGFont references.
pub struct FontTemplateData { pub struct FontTemplateData {
pub ctfont: Option<CTFont>, pub ctfont: Option<CTFont>,
pub identifier: String, pub identifier: Atom,
pub font_data: Option<Vec<u8>> pub font_data: Option<Vec<u8>>
} }
@ -23,7 +23,7 @@ unsafe impl Send for FontTemplateData {}
unsafe impl Sync for FontTemplateData {} unsafe impl Sync for FontTemplateData {}
impl FontTemplateData { impl FontTemplateData {
pub fn new(identifier: &str, font_data: Option<Vec<u8>>) -> FontTemplateData { pub fn new(identifier: Atom, font_data: Option<Vec<u8>>) -> FontTemplateData {
let ctfont = match font_data { let ctfont = match font_data {
Some(ref bytes) => { Some(ref bytes) => {
let fontprov = CGDataProvider::from_buffer(bytes); let fontprov = CGDataProvider::from_buffer(bytes);
@ -34,7 +34,7 @@ impl FontTemplateData {
} }
}, },
None => { None => {
Some(core_text::font::new_from_name(identifier, 0.0).unwrap()) Some(core_text::font::new_from_name(identifier.as_slice(), 0.0).unwrap())
} }
}; };