2014-07-09 03:31:07 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2015-01-28 23:57:58 +03:00
|
|
|
use font::FontHandleMethods;
|
2014-07-09 03:31:07 +04:00
|
|
|
use platform::font::FontHandle;
|
2015-08-20 16:43:56 +03:00
|
|
|
use platform::font_context::FontContextHandle;
|
2014-07-09 03:31:07 +04:00
|
|
|
use platform::font_template::FontTemplateData;
|
2016-05-11 00:11:35 +03:00
|
|
|
use std::fmt::{Debug, Error, Formatter};
|
2016-09-19 17:14:55 +03:00
|
|
|
use std::io::Error as IoError;
|
2015-01-08 18:00:57 +03:00
|
|
|
use std::sync::{Arc, Weak};
|
2016-05-11 00:11:35 +03:00
|
|
|
use std::u32;
|
2015-09-19 22:34:51 +03:00
|
|
|
use string_cache::Atom;
|
2015-01-28 23:57:58 +03:00
|
|
|
use style::computed_values::{font_stretch, font_weight};
|
2014-07-09 03:31:07 +04:00
|
|
|
|
2015-04-01 19:00:52 +03:00
|
|
|
/// Describes how to select a font from a given family. This is very basic at the moment and needs
|
|
|
|
/// to be expanded or refactored when we support more of the font styling parameters.
|
|
|
|
///
|
|
|
|
/// NB: If you change this, you will need to update `style::properties::compute_font_hash()`.
|
2015-12-18 11:53:15 +03:00
|
|
|
#[derive(Clone, Copy, Eq, Hash, Deserialize, Serialize, Debug)]
|
2014-07-09 03:31:07 +04:00
|
|
|
pub struct FontTemplateDescriptor {
|
|
|
|
pub weight: font_weight::T,
|
2015-01-28 23:57:58 +03:00
|
|
|
pub stretch: font_stretch::T,
|
2014-07-09 03:31:07 +04:00
|
|
|
pub italic: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FontTemplateDescriptor {
|
2015-01-28 23:57:58 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn new(weight: font_weight::T, stretch: font_stretch::T, italic: bool)
|
|
|
|
-> FontTemplateDescriptor {
|
2014-07-09 03:31:07 +04:00
|
|
|
FontTemplateDescriptor {
|
|
|
|
weight: weight,
|
2015-01-28 23:57:58 +03:00
|
|
|
stretch: stretch,
|
2014-07-09 03:31:07 +04:00
|
|
|
italic: italic,
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 00:11:35 +03:00
|
|
|
|
|
|
|
/// Returns a score indicating how far apart visually the two font descriptors are. This is
|
|
|
|
/// used for fuzzy font selection.
|
|
|
|
///
|
|
|
|
/// The smaller the score, the better the fonts match. 0 indicates an exact match. This must
|
|
|
|
/// be commutative (distance(A, B) == distance(B, A)).
|
|
|
|
#[inline]
|
|
|
|
fn distance_from(&self, other: &FontTemplateDescriptor) -> u32 {
|
|
|
|
if self.stretch != other.stretch || self.italic != other.italic {
|
|
|
|
// A value higher than all weights.
|
|
|
|
return 1000
|
|
|
|
}
|
|
|
|
((self.weight as i16) - (other.weight as i16)).abs() as u32
|
|
|
|
}
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for FontTemplateDescriptor {
|
|
|
|
fn eq(&self, other: &FontTemplateDescriptor) -> bool {
|
2016-05-11 00:11:35 +03:00
|
|
|
self.weight == other.weight && self.stretch == other.stretch && self.italic == other.italic
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This describes all the information needed to create
|
2014-07-11 15:57:48 +04:00
|
|
|
/// font instance handles. It contains a unique
|
2014-07-09 03:31:07 +04:00
|
|
|
/// FontTemplateData structure that is platform specific.
|
|
|
|
pub struct FontTemplate {
|
2015-07-12 19:29:33 +03:00
|
|
|
identifier: Atom,
|
2014-07-09 03:31:07 +04:00
|
|
|
descriptor: Option<FontTemplateDescriptor>,
|
2014-08-06 03:12:41 +04:00
|
|
|
weak_ref: Option<Weak<FontTemplateData>>,
|
2015-01-28 23:57:58 +03:00
|
|
|
// GWTODO: Add code path to unset the strong_ref for web fonts!
|
|
|
|
strong_ref: Option<Arc<FontTemplateData>>,
|
2014-08-06 03:12:41 +04:00
|
|
|
is_valid: bool,
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
|
|
|
|
2016-05-11 00:11:35 +03:00
|
|
|
impl Debug for FontTemplate {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
|
|
|
self.identifier.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 03:31:07 +04:00
|
|
|
/// Holds all of the template information for a font that
|
|
|
|
/// is common, regardless of the number of instances of
|
|
|
|
/// this font handle per thread.
|
|
|
|
impl FontTemplate {
|
2016-09-19 17:14:55 +03:00
|
|
|
pub fn new(identifier: Atom, maybe_bytes: Option<Vec<u8>>) -> Result<FontTemplate, IoError> {
|
2014-08-06 03:12:41 +04:00
|
|
|
let maybe_data = match maybe_bytes {
|
2016-09-19 17:14:55 +03:00
|
|
|
Some(_) => Some(try!(FontTemplateData::new(identifier.clone(), maybe_bytes))),
|
2014-08-06 03:12:41 +04:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let maybe_strong_ref = match maybe_data {
|
|
|
|
Some(data) => Some(Arc::new(data)),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let maybe_weak_ref = match maybe_strong_ref {
|
2015-09-02 10:31:23 +03:00
|
|
|
Some(ref strong_ref) => Some(Arc::downgrade(strong_ref)),
|
2014-08-06 03:12:41 +04:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2016-09-19 17:14:55 +03:00
|
|
|
Ok(FontTemplate {
|
2015-07-12 19:29:33 +03:00
|
|
|
identifier: identifier,
|
2014-07-09 03:31:07 +04:00
|
|
|
descriptor: None,
|
2014-08-06 03:12:41 +04:00
|
|
|
weak_ref: maybe_weak_ref,
|
|
|
|
strong_ref: maybe_strong_ref,
|
|
|
|
is_valid: true,
|
2016-09-19 17:14:55 +03:00
|
|
|
})
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
|
|
|
|
2015-07-12 19:29:33 +03:00
|
|
|
pub fn identifier(&self) -> &Atom {
|
|
|
|
&self.identifier
|
2014-08-06 03:12:41 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 03:31:07 +04:00
|
|
|
/// Get the data for creating a font if it matches a given descriptor.
|
servo: Merge #7559 - Remove 'get_*' on getters as per RFC 0344 on canevas, compositing, devtools, gfx, layout, net, profile, servo and webdriver_server (from ddrmanxbxfr:RFC-0344-Work); r=nox
Hi guys,
I just gave a big pass of RFC-0344 as per issue #6224 .
Pretty much renamed all the get_* fn that were used to fetch values.
I hope I didn't rename too much.
As said in the issue discussion, I didn't touch at the scripts folder so we keep the unsafe ones pretty explicit.
I've ran the whole pass of test, everything seems to be still working right :).
Please give feedback on this PR.
Thanks for looking into it.
Source-Repo: https://github.com/servo/servo
Source-Revision: b05f4aa3aa3085e65cb75433ae37bf272216d033
2015-09-13 03:29:56 +03:00
|
|
|
pub fn data_for_descriptor(&mut self,
|
2016-05-11 00:11:35 +03:00
|
|
|
fctx: &FontContextHandle,
|
|
|
|
requested_desc: &FontTemplateDescriptor)
|
|
|
|
-> Option<Arc<FontTemplateData>> {
|
2014-07-09 03:31:07 +04:00
|
|
|
// The font template data can be unloaded when nothing is referencing
|
|
|
|
// it (via the Weak reference to the Arc above). However, if we have
|
|
|
|
// already loaded a font, store the style information about it separately,
|
|
|
|
// 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).
|
|
|
|
match self.descriptor {
|
2016-09-19 17:14:55 +03:00
|
|
|
Some(actual_desc) if *requested_desc == actual_desc => self.data().ok(),
|
2016-05-11 00:11:35 +03:00
|
|
|
Some(_) => None,
|
|
|
|
None => {
|
|
|
|
if self.instantiate(fctx).is_err() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.descriptor
|
|
|
|
.as_ref()
|
|
|
|
.expect("Instantiation succeeded but no descriptor?") == requested_desc {
|
2016-09-19 17:14:55 +03:00
|
|
|
self.data().ok()
|
2014-07-09 03:31:07 +04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-05-11 00:11:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the font data along with the distance between this font's descriptor and the given
|
|
|
|
/// descriptor, if the font can be loaded.
|
|
|
|
pub fn data_for_approximate_descriptor(&mut self,
|
|
|
|
font_context: &FontContextHandle,
|
|
|
|
requested_descriptor: &FontTemplateDescriptor)
|
|
|
|
-> Option<(Arc<FontTemplateData>, u32)> {
|
|
|
|
match self.descriptor {
|
|
|
|
Some(actual_descriptor) => {
|
2016-09-19 17:14:55 +03:00
|
|
|
self.data().ok().map(|data| {
|
|
|
|
(data, actual_descriptor.distance_from(requested_descriptor))
|
|
|
|
})
|
2016-05-11 00:11:35 +03:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if self.instantiate(font_context).is_ok() {
|
|
|
|
let distance = self.descriptor
|
|
|
|
.as_ref()
|
|
|
|
.expect("Instantiation successful but no descriptor?")
|
|
|
|
.distance_from(requested_descriptor);
|
2016-09-19 17:14:55 +03:00
|
|
|
self.data().ok().map(|data| (data, distance))
|
2016-05-11 00:11:35 +03:00
|
|
|
} else {
|
|
|
|
None
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 00:11:35 +03:00
|
|
|
fn instantiate(&mut self, font_context: &FontContextHandle) -> Result<(), ()> {
|
|
|
|
if !self.is_valid {
|
|
|
|
return Err(())
|
|
|
|
}
|
|
|
|
|
2016-09-19 17:14:55 +03:00
|
|
|
let data = try!(self.data().map_err(|_| ()));
|
2016-05-11 00:11:35 +03:00
|
|
|
let handle: Result<FontHandle, ()> = FontHandleMethods::new_from_template(font_context,
|
|
|
|
data,
|
|
|
|
None);
|
|
|
|
self.is_valid = handle.is_ok();
|
|
|
|
let handle = try!(handle);
|
|
|
|
self.descriptor = Some(FontTemplateDescriptor::new(handle.boldness(),
|
|
|
|
handle.stretchiness(),
|
|
|
|
handle.is_italic()));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-08-06 03:12:41 +04:00
|
|
|
/// Get the data for creating a font.
|
|
|
|
pub fn get(&mut self) -> Option<Arc<FontTemplateData>> {
|
2015-01-28 23:57:58 +03:00
|
|
|
if self.is_valid {
|
2016-09-19 17:14:55 +03:00
|
|
|
self.data().ok()
|
2015-01-28 23:57:58 +03:00
|
|
|
} else {
|
|
|
|
None
|
2014-08-06 03:12:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 03:31:07 +04:00
|
|
|
/// Get the font template data. If any strong references still
|
|
|
|
/// exist, it will return a clone, otherwise it will load the
|
|
|
|
/// font data and store a weak reference to it internally.
|
2016-09-19 17:14:55 +03:00
|
|
|
pub fn data(&mut self) -> Result<Arc<FontTemplateData>, IoError> {
|
2014-08-06 03:12:41 +04:00
|
|
|
let maybe_data = match self.weak_ref {
|
2014-07-09 03:31:07 +04:00
|
|
|
Some(ref data) => data.upgrade(),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2015-01-28 23:57:58 +03:00
|
|
|
if let Some(data) = maybe_data {
|
2016-09-19 17:14:55 +03:00
|
|
|
return Ok(data)
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
2015-01-28 23:57:58 +03:00
|
|
|
|
|
|
|
assert!(self.strong_ref.is_none());
|
2016-09-19 17:14:55 +03:00
|
|
|
let template_data = Arc::new(try!(FontTemplateData::new(self.identifier.clone(), None)));
|
2015-09-02 10:31:23 +03:00
|
|
|
self.weak_ref = Some(Arc::downgrade(&template_data));
|
2016-09-19 17:14:55 +03:00
|
|
|
Ok(template_data)
|
2014-07-09 03:31:07 +04:00
|
|
|
}
|
2014-07-11 15:57:48 +04:00
|
|
|
}
|