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;
|
servo: Merge #14043 - Update to string-cache 0.3 (from servo:string-cache-up); r=nox
Previously, `string-cache` defined:
* An string-like `Atom` type,
* An `atom!("foo")` macro that expands to a value of that type, for a set of strings known at compile-time,
* A `struct Namespace(Atom);` type
* A `ns!(html)` macro that maps known prefixed to `Namespace` values with the corresponding namespace URL.
Adding a string to the static set required making a change to the `string-cache` crate.
With 0.3, the `Atom` type is now generic, with a type parameter that provides a set of static strings. We can have multiple such sets, defined in different crates. The `string_cache_codegen` crate, to be used in build scripts, generates code that defines such a set, a new atom type (a type alias for `Atom<_>` with the type parameter set), and an `atom!`-like macro.
The html5ever repository has a new `html5ever_atoms` crate that defines three such types: `Prefix`, `Namespace`, and `LocalName` (with respective `namespace_prefix!`, `namespace_url!`, and `local_name!` macros). It also defines the `ns!` macro like before.
This repository has a new `servo_atoms` crate in `components/atoms` that, for now, defines a single `Atom` type (and `atom!`) macro. (`servo_atoms::Atom` is defined as something like `type Atom = string_cache::Atom<ServoStaticStringSet>;`, so overall there’s now two types named `Atom`.)
In this PR, `servo_atoms::Atom` is used for everything else that was `string_cache::Atom` before. But more atom types can be defined as needed. Two reasons to do this are to auto-generate the set of static strings (I’m planning to do this for CSS property names, which is the motivation for this change), or to have the type system help us avoid mix up unrelated things (this is why we had a `Namespace` type ever before this change).
Introducing new types helped me find a bug: when creating a new attribute `dom::Element::set_style_attr`, would pass `Some(atom!("style"))` instead of `None` (now `Option<html5ever_atoms::Prefix>` instead of `Option<string_cache::Atom>`) to the `prefix` argument of `Attr::new`. I suppose the author of that code confused it with the `local_name` argument.
---
Note that Stylo is not affected by any of this. The `gecko_string_cache` module is unchanged, with a single `Atom` type. The `style` crate conditionally compiles `Prefix` and `LocalName` re-exports for that are both `gecko_string_cache::Atom` on stylo.
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 5b4cc9568dbd5c15e5d2fbc62719172f11566ffa
2016-11-03 19:19:44 +03:00
|
|
|
use servo_atoms::Atom;
|
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-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
|
|
|
}
|