Auto merge of #12783 - Wafflespeanut:word_spacing, r=Manishearth

Prefer length and percentage for word spacing

<!-- Please describe your changes on the following line: -->
The goal is to make use of `LengthOrPercentage` for word spacing in `ShapingOptions`, but since it makes use of `f32` which doesn't implement `Hash`, we're going for `NotNan<f32>` from [ordered-float](https://github.com/reem/rust-ordered-float/), which supports hashing. Instead of implementing `Hash` for `LengthOrPercentage` and thereby the inner types like `CSSFloat`, `CalcLengthOrPercentage`, etc., we convert it to `(Au, NotNan<f32>)`.

---
<!-- 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

<!-- Either: -->
- [ ] There are tests for these changes

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12783)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-08-09 10:13:30 -05:00 committed by GitHub
commit 3b676bc85d
15 changed files with 135 additions and 10 deletions

View file

@ -28,6 +28,7 @@ log = "0.3.5"
mime = "0.2" mime = "0.2"
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}
ordered-float = "0.2.2"
plugins = {path = "../plugins"} plugins = {path = "../plugins"}
profile_traits = {path = "../profile_traits"} profile_traits = {path = "../profile_traits"}
rand = "0.3" rand = "0.3"

View file

@ -5,6 +5,7 @@
use app_units::Au; use app_units::Au;
use euclid::{Point2D, Rect, Size2D}; use euclid::{Point2D, Rect, Size2D};
use font_template::FontTemplateDescriptor; use font_template::FontTemplateDescriptor;
use ordered_float::NotNaN;
use platform::font::{FontHandle, FontTable}; use platform::font::{FontHandle, FontTable};
use platform::font_context::FontContextHandle; use platform::font_context::FontContextHandle;
use platform::font_template::FontTemplateData; use platform::font_template::FontTemplateData;
@ -157,7 +158,7 @@ pub struct ShapingOptions {
/// NB: You will probably want to set the `IGNORE_LIGATURES_SHAPING_FLAG` if this is non-null. /// NB: You will probably want to set the `IGNORE_LIGATURES_SHAPING_FLAG` if this is non-null.
pub letter_spacing: Option<Au>, pub letter_spacing: Option<Au>,
/// Spacing to add between each word. Corresponds to the CSS 2.1 `word-spacing` property. /// Spacing to add between each word. Corresponds to the CSS 2.1 `word-spacing` property.
pub word_spacing: Au, pub word_spacing: (Au, NotNaN<f32>),
/// The Unicode script property of the characters in this run. /// The Unicode script property of the characters in this run.
pub script: Script, pub script: Script,
/// Various flags. /// Various flags.
@ -225,7 +226,9 @@ impl Font {
let mut advance = Au::from_f64_px(self.glyph_h_advance(glyph_id)); let mut advance = Au::from_f64_px(self.glyph_h_advance(glyph_id));
if character == ' ' { if character == ' ' {
advance += options.word_spacing; // https://drafts.csswg.org/css-text-3/#word-spacing-property
let (length, percent) = options.word_spacing;
advance = (advance + length) + Au((advance.0 as f32 * percent.into_inner()) as i32);
} }
if let Some(letter_spacing) = options.letter_spacing { if let Some(letter_spacing) = options.letter_spacing {
advance += letter_spacing; advance += letter_spacing;

View file

@ -60,6 +60,7 @@ extern crate log;
extern crate mime; extern crate mime;
extern crate msg; extern crate msg;
extern crate net_traits; extern crate net_traits;
extern crate ordered_float;
#[macro_use] #[macro_use]
extern crate profile_traits; extern crate profile_traits;
extern crate rand; extern crate rand;

View file

@ -403,7 +403,9 @@ impl Shaper {
// applied. The effect of the property on other word-separator characters is undefined." // applied. The effect of the property on other word-separator characters is undefined."
// We elect to only space the two required code points. // We elect to only space the two required code points.
if character == ' ' || character == '\u{a0}' { if character == ' ' || character == '\u{a0}' {
advance = advance + options.word_spacing // https://drafts.csswg.org/css-text-3/#word-spacing-property
let (length, percent) = options.word_spacing;
advance = (advance + length) + Au((advance.0 as f32 * percent.into_inner()) as i32);
} }
advance advance

View file

@ -26,6 +26,7 @@ libc = "0.2"
log = "0.3.5" log = "0.3.5"
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}
ordered-float = "0.2.2"
plugins = {path = "../plugins"} plugins = {path = "../plugins"}
profile_traits = {path = "../profile_traits"} profile_traits = {path = "../profile_traits"}
range = {path = "../range"} range = {path = "../range"}

View file

@ -35,6 +35,7 @@ extern crate libc;
extern crate log; extern crate log;
extern crate msg; extern crate msg;
extern crate net_traits; extern crate net_traits;
extern crate ordered_float;
#[macro_use] #[macro_use]
#[no_link] #[no_link]
extern crate plugins as servo_plugins; extern crate plugins as servo_plugins;

View file

@ -17,6 +17,7 @@ use gfx::text::text_run::TextRun;
use gfx::text::util::{self, CompressionMode}; use gfx::text::util::{self, CompressionMode};
use inline::{FIRST_FRAGMENT_OF_ELEMENT, InlineFragments, LAST_FRAGMENT_OF_ELEMENT}; use inline::{FIRST_FRAGMENT_OF_ELEMENT, InlineFragments, LAST_FRAGMENT_OF_ELEMENT};
use linked_list::split_off_head; use linked_list::split_off_head;
use ordered_float::NotNaN;
use range::Range; use range::Range;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::collections::LinkedList; use std::collections::LinkedList;
@ -164,7 +165,9 @@ impl TextRunScanner {
}; };
text_transform = inherited_text_style.text_transform; text_transform = inherited_text_style.text_transform;
letter_spacing = inherited_text_style.letter_spacing.0; letter_spacing = inherited_text_style.letter_spacing.0;
word_spacing = inherited_text_style.word_spacing.0.unwrap_or(Au(0)); word_spacing = inherited_text_style.word_spacing.0
.map(|lop| lop.to_hash_key())
.unwrap_or((Au(0), NotNaN::new(0.0).unwrap()));
text_rendering = inherited_text_style.text_rendering; text_rendering = inherited_text_style.text_rendering;
} }

View file

@ -766,6 +766,7 @@ dependencies = [
"mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1124,6 +1125,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"range 0.0.1", "range 0.0.1",
@ -1606,6 +1608,15 @@ dependencies = [
"openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "ordered-float"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"unreachable 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "osmesa-sys" name = "osmesa-sys"
version = "0.0.5" version = "0.0.5"
@ -2193,6 +2204,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2361,6 +2373,14 @@ dependencies = [
"harfbuzz-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "harfbuzz-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "unreachable"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"void 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "unreachable" name = "unreachable"
version = "0.1.1" version = "0.1.1"
@ -2453,6 +2473,11 @@ dependencies = [
"pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "void"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "void" name = "void"
version = "1.0.2" version = "1.0.2"

View file

@ -34,6 +34,7 @@ lazy_static = "0.2"
log = "0.3.5" log = "0.3.5"
matches = "0.1" matches = "0.1"
num-traits = "0.1.32" num-traits = "0.1.32"
ordered-float = "0.2.2"
rand = "0.3" rand = "0.3"
rustc-serialize = "0.3" rustc-serialize = "0.3"
selectors = "0.7" selectors = "0.7"

View file

@ -57,6 +57,7 @@ extern crate log;
#[macro_use] #[macro_use]
extern crate matches; extern crate matches;
extern crate num_traits; extern crate num_traits;
extern crate ordered_float;
extern crate rand; extern crate rand;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate selectors; extern crate selectors;

View file

@ -285,7 +285,7 @@
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum SpecifiedValue { pub enum SpecifiedValue {
Normal, Normal,
Specified(specified::Length), // FIXME(SimonSapin) support percentages Specified(specified::LengthOrPercentage),
} }
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
@ -298,10 +298,10 @@
} }
pub mod computed_value { pub mod computed_value {
use app_units::Au; use values::computed::LengthOrPercentage;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct T(pub Option<Au>); pub struct T(pub Option<LengthOrPercentage>);
} }
impl ToCss for computed_value::T { impl ToCss for computed_value::T {
@ -326,7 +326,7 @@
match *self { match *self {
SpecifiedValue::Normal => computed_value::T(None), SpecifiedValue::Normal => computed_value::T(None),
SpecifiedValue::Specified(l) => SpecifiedValue::Specified(l) =>
computed_value::T(Some(l.to_computed_value(context))) computed_value::T(Some(l.to_computed_value(context))),
} }
} }
} }
@ -335,7 +335,8 @@
if input.try(|input| input.expect_ident_matching("normal")).is_ok() { if input.try(|input| input.expect_ident_matching("normal")).is_ok() {
Ok(SpecifiedValue::Normal) Ok(SpecifiedValue::Normal)
} else { } else {
specified::Length::parse_non_negative(input).map(SpecifiedValue::Specified) specified::LengthOrPercentage::parse_non_negative(input)
.map(SpecifiedValue::Specified)
} }
} }
</%helpers:longhand> </%helpers:longhand>

View file

@ -4,6 +4,7 @@
use app_units::Au; use app_units::Au;
use euclid::size::Size2D; use euclid::size::Size2D;
use ordered_float::NotNaN;
use properties::ComputedValues; use properties::ComputedValues;
use std::fmt; use std::fmt;
use super::LocalToCss; use super::LocalToCss;
@ -241,6 +242,15 @@ impl LengthOrPercentage {
Length(_) | Percentage(_) | Calc(_) => false Length(_) | Percentage(_) | Calc(_) => false
} }
} }
pub fn to_hash_key(&self) -> (Au, NotNaN<f32>) {
use self::LengthOrPercentage::*;
match *self {
Length(l) => (l, NotNaN::new(0.0).unwrap()),
Percentage(p) => (Au(0), NotNaN::new(p).unwrap()),
Calc(c) => (c.length(), NotNaN::new(c.percentage()).unwrap()),
}
}
} }
impl fmt::Debug for LengthOrPercentage { impl fmt::Debug for LengthOrPercentage {

25
ports/cef/Cargo.lock generated
View file

@ -684,6 +684,7 @@ dependencies = [
"mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1033,6 +1034,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"range 0.0.1", "range 0.0.1",
@ -1480,6 +1482,15 @@ dependencies = [
"openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "ordered-float"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"unreachable 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "osmesa-sys" name = "osmesa-sys"
version = "0.0.5" version = "0.0.5"
@ -2078,6 +2089,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2230,6 +2242,14 @@ dependencies = [
"harfbuzz-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "harfbuzz-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "unreachable"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"void 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "unreachable" name = "unreachable"
version = "0.1.1" version = "0.1.1"
@ -2315,6 +2335,11 @@ dependencies = [
"pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "void"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "void" name = "void"
version = "1.0.2" version = "1.0.2"

View file

@ -214,6 +214,33 @@ dependencies = [
"libc 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "num"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-integer"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-iter"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.1.32" version = "0.1.32"
@ -227,6 +254,15 @@ dependencies = [
"libc 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "ordered-float"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"unreachable 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "quickersort" name = "quickersort"
version = "2.0.1" version = "2.0.1"
@ -325,6 +361,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"ordered-float 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"selectors 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "selectors 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -386,6 +423,14 @@ name = "unicode-normalization"
version = "0.1.2" version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unreachable"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"void 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "unreachable" name = "unreachable"
version = "0.1.1" version = "0.1.1"
@ -425,6 +470,11 @@ dependencies = [
"xdg 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "xdg 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "void"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "void" name = "void"
version = "1.0.2" version = "1.0.2"

View file

@ -261,7 +261,7 @@ def check_lock(file_name, contents):
raise StopIteration raise StopIteration
# package names to be neglected (as named by cargo) # package names to be neglected (as named by cargo)
exceptions = ["lazy_static"] exceptions = ["lazy_static", "unreachable", "void"]
import toml import toml
content = toml.loads(contents) content = toml.loads(contents)