Format the rest of gfx #21373

This commit is contained in:
kingdido999 2018-09-08 08:05:42 +08:00
parent f7630dad87
commit 3a3c4b8c8e
8 changed files with 502 additions and 401 deletions

View file

@ -58,26 +58,24 @@ impl TestFontSource {
}
fn add_face(family: &mut FontTemplates, name: &str, identifier: Option<&str>) {
let mut path: PathBuf = [
env!("CARGO_MANIFEST_DIR"),
"tests",
"support",
"CSSTest",
].iter().collect();
let mut path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "support", "CSSTest"]
.iter()
.collect();
path.push(format!("{}.ttf", name));
let file = File::open(path).unwrap();
let identifier = Atom::from(identifier.unwrap_or(name));
family.add_template(
identifier,
Some(file.bytes().map(|b| b.unwrap()).collect())
)
family.add_template(identifier, Some(file.bytes().map(|b| b.unwrap()).collect()))
}
}
impl FontSource for TestFontSource {
fn get_font_instance(&mut self, _key: webrender_api::FontKey, _size: Au) -> webrender_api::FontInstanceKey {
fn get_font_instance(
&mut self,
_key: webrender_api::FontKey,
_size: Au,
) -> webrender_api::FontInstanceKey {
webrender_api::FontInstanceKey(webrender_api::IdNamespace(0), 0)
}
@ -92,11 +90,9 @@ impl FontSource for TestFontSource {
self.families
.get_mut(family_descriptor.name())
.and_then(|family| family.find_font_for_style(&template_descriptor, handle))
.map(|template| {
FontTemplateInfo {
font_template: template,
font_key: webrender_api::FontKey(webrender_api::IdNamespace(0), 0),
}
.map(|template| FontTemplateInfo {
font_template: template,
font_key: webrender_api::FontKey(webrender_api::IdNamespace(0), 0),
})
}
}
@ -116,12 +112,14 @@ fn style() -> FontStyleStruct {
}
fn font_family(names: Vec<&str>) -> FontFamily {
let names: Vec<SingleFontFamily> = names.into_iter().map(|name|
SingleFontFamily::FamilyName(FamilyName {
name: Atom::from(name),
syntax: FamilyNameSyntax::Quoted,
})
).collect();
let names: Vec<SingleFontFamily> = names
.into_iter()
.map(|name| {
SingleFontFamily::FamilyName(FamilyName {
name: Atom::from(name),
syntax: FamilyNameSyntax::Quoted,
})
}).collect();
FontFamily(FontFamilyList::new(names.into_boxed_slice()))
}
@ -156,19 +154,36 @@ fn test_font_group_find_by_codepoint() {
let mut context = FontContext::new(source);
let mut style = style();
style.set_font_family(font_family(vec!("CSSTest ASCII", "CSSTest Basic")));
style.set_font_family(font_family(vec!["CSSTest ASCII", "CSSTest Basic"]));
let group = context.font_group(Arc::new(style));
let font = group.borrow_mut().find_by_codepoint(&mut context, 'a').unwrap();
let font = group
.borrow_mut()
.find_by_codepoint(&mut context, 'a')
.unwrap();
assert_eq!(&*font.borrow().identifier(), "csstest-ascii");
assert_eq!(count.get(), 1, "only the first font in the list should have been loaded");
assert_eq!(
count.get(),
1,
"only the first font in the list should have been loaded"
);
let font = group.borrow_mut().find_by_codepoint(&mut context, 'a').unwrap();
let font = group
.borrow_mut()
.find_by_codepoint(&mut context, 'a')
.unwrap();
assert_eq!(&*font.borrow().identifier(), "csstest-ascii");
assert_eq!(count.get(), 1, "we shouldn't load the same font a second time");
assert_eq!(
count.get(),
1,
"we shouldn't load the same font a second time"
);
let font = group.borrow_mut().find_by_codepoint(&mut context, 'á').unwrap();
let font = group
.borrow_mut()
.find_by_codepoint(&mut context, 'á')
.unwrap();
assert_eq!(&*font.borrow().identifier(), "csstest-basic-regular");
assert_eq!(count.get(), 2, "both fonts should now have been loaded");
}
@ -179,19 +194,27 @@ fn test_font_fallback() {
let mut context = FontContext::new(source);
let mut style = style();
style.set_font_family(font_family(vec!("CSSTest ASCII")));
style.set_font_family(font_family(vec!["CSSTest ASCII"]));
let group = context.font_group(Arc::new(style));
let font = group.borrow_mut().find_by_codepoint(&mut context, 'a').unwrap();
let font = group
.borrow_mut()
.find_by_codepoint(&mut context, 'a')
.unwrap();
assert_eq!(
&*font.borrow().identifier(), "csstest-ascii",
&*font.borrow().identifier(),
"csstest-ascii",
"a family in the group should be used if there is a matching glyph"
);
let font = group.borrow_mut().find_by_codepoint(&mut context, 'á').unwrap();
let font = group
.borrow_mut()
.find_by_codepoint(&mut context, 'á')
.unwrap();
assert_eq!(
&*font.borrow().identifier(), "fallback",
&*font.borrow().identifier(),
"fallback",
"a fallback font should be used if there is no matching glyph in the group"
);
}
@ -212,10 +235,8 @@ fn test_font_template_is_cached() {
pt_size: Au(10),
};
let family_descriptor = FontFamilyDescriptor::new(
FontFamilyName::from("CSSTest Basic"),
FontSearchScope::Any,
);
let family_descriptor =
FontFamilyDescriptor::new(FontFamilyName::from("CSSTest Basic"), FontSearchScope::Any);
let font1 = context.font(&font_descriptor, &family_descriptor).unwrap();
@ -228,5 +249,9 @@ fn test_font_template_is_cached() {
"the same font should not have been returned"
);
assert_eq!(count.get(), 1, "we should only have fetched the template data from the cache thread once");
assert_eq!(
count.get(),
1,
"we should only have fetched the template data from the cache thread once"
);
}

View file

@ -2,9 +2,12 @@
* 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/. */
#[cfg(not(target_os = "macos"))] extern crate gfx;
#[cfg(not(target_os = "macos"))] extern crate servo_atoms;
#[cfg(not(target_os = "macos"))] extern crate style;
#[cfg(not(target_os = "macos"))]
extern crate gfx;
#[cfg(not(target_os = "macos"))]
extern crate servo_atoms;
#[cfg(not(target_os = "macos"))]
extern crate style;
// Test doesn't yet run on Mac, see https://github.com/servo/servo/pull/19928 for explanation.
#[cfg(not(target_os = "macos"))]
@ -28,14 +31,16 @@ fn test_font_template_descriptor() {
"support",
"dejavu-fonts-ttf-2.37",
"ttf",
].iter().collect();
]
.iter()
.collect();
path.push(format!("{}.ttf", filename));
let file = File::open(path).unwrap();
let mut template = FontTemplate::new(
Atom::from(filename),
Some(file.bytes().map(|b| b.unwrap()).collect())
Some(file.bytes().map(|b| b.unwrap()).collect()),
).unwrap();
let context = FontContextHandle::new();
@ -43,27 +48,39 @@ fn test_font_template_descriptor() {
template.descriptor(&context).unwrap()
}
assert_eq!(descriptor("DejaVuSans"), FontTemplateDescriptor {
weight: FontWeight::normal(),
stretch: FontStretch::hundred(),
style: FontStyle::Normal,
});
assert_eq!(
descriptor("DejaVuSans"),
FontTemplateDescriptor {
weight: FontWeight::normal(),
stretch: FontStretch::hundred(),
style: FontStyle::Normal,
}
);
assert_eq!(descriptor("DejaVuSans-Bold"), FontTemplateDescriptor {
weight: FontWeight::bold(),
stretch: FontStretch::hundred(),
style: FontStyle::Normal,
});
assert_eq!(
descriptor("DejaVuSans-Bold"),
FontTemplateDescriptor {
weight: FontWeight::bold(),
stretch: FontStretch::hundred(),
style: FontStyle::Normal,
}
);
assert_eq!(descriptor("DejaVuSans-Oblique"), FontTemplateDescriptor {
weight: FontWeight::normal(),
stretch: FontStretch::hundred(),
style: FontStyle::Italic,
});
assert_eq!(
descriptor("DejaVuSans-Oblique"),
FontTemplateDescriptor {
weight: FontWeight::normal(),
stretch: FontStretch::hundred(),
style: FontStyle::Italic,
}
);
assert_eq!(descriptor("DejaVuSansCondensed-BoldOblique"), FontTemplateDescriptor {
weight: FontWeight::bold(),
stretch: FontStretch(NonNegative(Percentage(0.875))),
style: FontStyle::Italic,
});
assert_eq!(
descriptor("DejaVuSansCondensed-BoldOblique"),
FontTemplateDescriptor {
weight: FontWeight::bold(),
stretch: FontStretch(NonNegative(Percentage(0.875))),
style: FontStyle::Italic,
}
);
}

View file

@ -29,26 +29,13 @@ fn test_transform_compress_none() {
#[test]
fn test_transform_discard_newline() {
let test_strs = [
(" foo bar",
" foo bar"),
("foo bar ",
"foo bar "),
("foo\n bar",
"foo bar"),
("foo \nbar",
"foo bar"),
(" foo bar \nbaz",
" foo bar baz"),
("foo bar baz",
"foo bar baz"),
("foobarbaz\n\n",
"foobarbaz"),
(" foo bar", " foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo bar"),
("foo \nbar", "foo bar"),
(" foo bar \nbaz", " foo bar baz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz"),
];
let mode = CompressionMode::DiscardNewline;
@ -62,26 +49,13 @@ fn test_transform_discard_newline() {
#[test]
fn test_transform_compress_whitespace() {
let test_strs = [
(" foo bar",
"foo bar"),
("foo bar ",
"foo bar "),
("foo\n bar",
"foo\n bar"),
("foo \nbar",
"foo \nbar"),
(" foo bar \nbaz",
"foo bar \nbaz"),
("foo bar baz",
"foo bar baz"),
("foobarbaz\n\n",
"foobarbaz\n\n"),
(" foo bar", "foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo\n bar"),
("foo \nbar", "foo \nbar"),
(" foo bar \nbaz", "foo bar \nbaz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz\n\n"),
];
let mode = CompressionMode::CompressWhitespace;
@ -95,26 +69,13 @@ fn test_transform_compress_whitespace() {
#[test]
fn test_transform_compress_whitespace_newline() {
let test_strs = vec![
(" foo bar",
"foo bar"),
("foo bar ",
"foo bar "),
("foo\n bar",
"foo bar"),
("foo \nbar",
"foo bar"),
(" foo bar \nbaz",
"foo bar baz"),
("foo bar baz",
"foo bar baz"),
("foobarbaz\n\n",
"foobarbaz "),
(" foo bar", "foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo bar"),
("foo \nbar", "foo bar"),
(" foo bar \nbaz", "foo bar baz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz "),
];
let mode = CompressionMode::CompressWhitespaceNewline;
@ -128,29 +89,14 @@ fn test_transform_compress_whitespace_newline() {
#[test]
fn test_transform_compress_whitespace_newline_no_incoming() {
let test_strs = [
(" foo bar",
" foo bar"),
("\nfoo bar",
" foo bar"),
("foo bar ",
"foo bar "),
("foo\n bar",
"foo bar"),
("foo \nbar",
"foo bar"),
(" foo bar \nbaz",
" foo bar baz"),
("foo bar baz",
"foo bar baz"),
("foobarbaz\n\n",
"foobarbaz "),
(" foo bar", " foo bar"),
("\nfoo bar", " foo bar"),
("foo bar ", "foo bar "),
("foo\n bar", "foo bar"),
("foo \nbar", "foo bar"),
(" foo bar \nbaz", " foo bar baz"),
("foo bar baz", "foo bar baz"),
("foobarbaz\n\n", "foobarbaz "),
];
let mode = CompressionMode::CompressWhitespaceNewline;