Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions wrench/reftests/filters/filter-grayscale-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
root:
items:
-
bounds: [10, 10, 200, 200]
type: rect
color: [182, 182, 182]
12 changes: 12 additions & 0 deletions wrench/reftests/filters/filter-grayscale.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
root:
items:
-
bounds: [10, 10, 200, 200]
type: stacking_context
filters: grayscale(1)
items:
-
bounds: [0, 0, 200, 200]
type: rect
color: [0, 255, 0]
1 change: 1 addition & 0 deletions wrench/reftests/filters/reftest.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
== filter-grayscale.yaml filter-grayscale-ref.yaml
1 change: 1 addition & 0 deletions wrench/reftests/reftest.list
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include blend/reftest.list
include mask/reftest.list
include scrolling/reftest.list
include filters/reftest.list
9 changes: 7 additions & 2 deletions wrench/src/parse_function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::str::CharIndices;
// A crapy parser for parsing strings like "translate(1, 3)"

// support arguments like '4', 'ab', '4.0'
fn acceptable_arg_character(c: char) -> bool {
c.is_alphanumeric() || c == '.'
}

// A crapy parser for parsing strings like "translate(1, 3)"
pub fn parse_function(s: &str) -> (&str, Vec<&str>) {
// XXX: This it not particular easy to read. Sorry.
struct Parser<'a> {
Expand Down Expand Up @@ -53,7 +58,7 @@ pub fn parse_function(s: &str) -> (&str, Vec<&str>) {

let mut end = p.start;
while let Some(k) = p.o {
if !k.1.is_alphanumeric() {
if !acceptable_arg_character(k.1) {
break;
}
end = k.0 + 1;
Expand Down
3 changes: 1 addition & 2 deletions wrench/src/yaml_frame_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,7 @@ impl YamlFrameReader {
}
}

// FIXME handle these
let filters: Vec<FilterOp> = Vec::new();
let filters = yaml["filters"].as_vec_filter_op().unwrap_or(vec![]);

self.builder().push_stacking_context(scroll_policy,
bounds,
Expand Down
47 changes: 47 additions & 0 deletions wrench/src/yaml_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub trait YamlHelper {
fn as_border_radius(&self) -> Option<BorderRadius>;
fn as_mix_blend_mode(&self) -> Option<MixBlendMode>;
fn as_scroll_policy(&self) -> Option<ScrollPolicy>;
fn as_filter_op(&self) -> Option<FilterOp>;
fn as_vec_filter_op(&self) -> Option<Vec<FilterOp>>;
}

fn string_to_color(color: &str) -> Option<ColorF> {
Expand Down Expand Up @@ -319,4 +321,49 @@ impl YamlHelper for Yaml {
fn as_scroll_policy(&self) -> Option<ScrollPolicy> {
return self.as_str().and_then(|string| string_to_scroll_policy(string))
}

fn as_filter_op(&self) -> Option<FilterOp> {
if let Some(s) = self.as_str() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function body is overly verbose and could be reduced by some refactoring
not required for this PR though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I definitely agree. It'd be nice to have something like define-enum-conversion that works with more general enums.

match parse_function(s) {
("blur", ref args) if args.len() == 1 => {
Some(FilterOp::Blur(Au(args[0].parse().unwrap())))
}
("brightness", ref args) if args.len() == 1 => {
Some(FilterOp::Brightness(args[0].parse().unwrap()))
}
("contrast", ref args) if args.len() == 1 => {
Some(FilterOp::Contrast(args[0].parse().unwrap()))
}
("grayscale", ref args) if args.len() == 1 => {
Some(FilterOp::Grayscale(args[0].parse().unwrap()))
}
("hue-rotate", ref args) if args.len() == 1 => {
Some(FilterOp::HueRotate(args[0].parse().unwrap()))
}
("invert", ref args) if args.len() == 1 => {
Some(FilterOp::Invert(args[0].parse().unwrap()))
}
("opacity", ref args) if args.len() == 1 => {
Some(FilterOp::Opacity(args[0].parse().unwrap()))
}
("saturate", ref args) if args.len() == 1 => {
Some(FilterOp::Saturate(args[0].parse().unwrap()))
}
("sepia", ref args) if args.len() == 1 => {
Some(FilterOp::Sepia(args[0].parse().unwrap()))
}
(_, _) => { None }
}
} else {
None
}
}

fn as_vec_filter_op(&self) -> Option<Vec<FilterOp>> {
if let Some(v) = self.as_vec() {
Some(v.iter().map(|x| x.as_filter_op().unwrap()).collect())
} else {
self.as_filter_op().map(|op| vec![op])
}
}
}