mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Make use of FromStr and Default traits in lib canvas
fixup! Make use of FromStr and Default traits in lib canvas
This commit is contained in:
parent
3f9b6f8586
commit
a2a9c0489d
2 changed files with 110 additions and 83 deletions
|
@ -37,6 +37,8 @@ use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
|
||||||
use layers::platform::surface::NativeSurface;
|
use layers::platform::surface::NativeSurface;
|
||||||
use offscreen_gl_context::GLContextAttributes;
|
use offscreen_gl_context::GLContextAttributes;
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
use std::default::Default;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
use util::mem::HeapSizeOf;
|
use util::mem::HeapSizeOf;
|
||||||
|
|
||||||
|
@ -345,6 +347,19 @@ pub enum LineCapStyle {
|
||||||
Square = 2,
|
Square = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for LineCapStyle {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(string: &str) -> Result<LineCapStyle, ()> {
|
||||||
|
match string {
|
||||||
|
"butt" => Ok(LineCapStyle::Butt),
|
||||||
|
"round" => Ok(LineCapStyle::Round),
|
||||||
|
"square" => Ok(LineCapStyle::Square),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl LineCapStyle {
|
impl LineCapStyle {
|
||||||
pub fn to_azure_style(&self) -> CapStyle {
|
pub fn to_azure_style(&self) -> CapStyle {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -353,15 +368,6 @@ impl LineCapStyle {
|
||||||
LineCapStyle::Square => CapStyle::Square,
|
LineCapStyle::Square => CapStyle::Square,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(string: &str) -> Option<LineCapStyle> {
|
|
||||||
match string {
|
|
||||||
"butt" => Some(LineCapStyle::Butt),
|
|
||||||
"round" => Some(LineCapStyle::Round),
|
|
||||||
"square" => Some(LineCapStyle::Square),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Deserialize, Serialize, HeapSizeOf)]
|
#[derive(Copy, Clone, PartialEq, Deserialize, Serialize, HeapSizeOf)]
|
||||||
|
@ -371,6 +377,19 @@ pub enum LineJoinStyle {
|
||||||
Miter = 2,
|
Miter = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for LineJoinStyle {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(string: &str) -> Result<LineJoinStyle, ()> {
|
||||||
|
match string {
|
||||||
|
"round" => Ok(LineJoinStyle::Round),
|
||||||
|
"bevel" => Ok(LineJoinStyle::Bevel),
|
||||||
|
"miter" => Ok(LineJoinStyle::Miter),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl LineJoinStyle {
|
impl LineJoinStyle {
|
||||||
pub fn to_azure_style(&self) -> JoinStyle {
|
pub fn to_azure_style(&self) -> JoinStyle {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -379,15 +398,6 @@ impl LineJoinStyle {
|
||||||
LineJoinStyle::Miter => JoinStyle::Miter,
|
LineJoinStyle::Miter => JoinStyle::Miter,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(string: &str) -> Option<LineJoinStyle> {
|
|
||||||
match string {
|
|
||||||
"round" => Some(LineJoinStyle::Round),
|
|
||||||
"bevel" => Some(LineJoinStyle::Bevel),
|
|
||||||
"miter" => Some(LineJoinStyle::Miter),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Deserialize, Serialize)]
|
#[derive(Copy, Clone, PartialEq, Deserialize, Serialize)]
|
||||||
|
@ -398,14 +408,16 @@ pub enum RepetitionStyle {
|
||||||
NoRepeat,
|
NoRepeat,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RepetitionStyle {
|
impl FromStr for RepetitionStyle {
|
||||||
pub fn from_str(string: &str) -> Option<RepetitionStyle> {
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(string: &str) -> Result<RepetitionStyle, ()> {
|
||||||
match string {
|
match string {
|
||||||
"repeat" => Some(RepetitionStyle::Repeat),
|
"repeat" => Ok(RepetitionStyle::Repeat),
|
||||||
"repeat-x" => Some(RepetitionStyle::RepeatX),
|
"repeat-x" => Ok(RepetitionStyle::RepeatX),
|
||||||
"repeat-y" => Some(RepetitionStyle::RepeatY),
|
"repeat-y" => Ok(RepetitionStyle::RepeatY),
|
||||||
"no-repeat" => Some(RepetitionStyle::NoRepeat),
|
"no-repeat" => Ok(RepetitionStyle::NoRepeat),
|
||||||
_ => None
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -425,6 +437,27 @@ pub enum CompositionStyle {
|
||||||
Xor,
|
Xor,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for CompositionStyle {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(string: &str) -> Result<CompositionStyle, ()> {
|
||||||
|
match string {
|
||||||
|
"source-in" => Ok(CompositionStyle::SrcIn),
|
||||||
|
"source-out" => Ok(CompositionStyle::SrcOut),
|
||||||
|
"source-over" => Ok(CompositionStyle::SrcOver),
|
||||||
|
"source-atop" => Ok(CompositionStyle::SrcAtop),
|
||||||
|
"destination-in" => Ok(CompositionStyle::DestIn),
|
||||||
|
"destination-out" => Ok(CompositionStyle::DestOut),
|
||||||
|
"destination-over" => Ok(CompositionStyle::DestOver),
|
||||||
|
"destination-atop" => Ok(CompositionStyle::DestAtop),
|
||||||
|
"copy" => Ok(CompositionStyle::Copy),
|
||||||
|
"lighter" => Ok(CompositionStyle::Lighter),
|
||||||
|
"xor" => Ok(CompositionStyle::Xor),
|
||||||
|
_ => Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CompositionStyle {
|
impl CompositionStyle {
|
||||||
pub fn to_azure_style(&self) -> CompositionOp {
|
pub fn to_azure_style(&self) -> CompositionOp {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -442,23 +475,6 @@ impl CompositionStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(string: &str) -> Option<CompositionStyle> {
|
|
||||||
match string {
|
|
||||||
"source-in" => Some(CompositionStyle::SrcIn),
|
|
||||||
"source-out" => Some(CompositionStyle::SrcOut),
|
|
||||||
"source-over" => Some(CompositionStyle::SrcOver),
|
|
||||||
"source-atop" => Some(CompositionStyle::SrcAtop),
|
|
||||||
"destination-in" => Some(CompositionStyle::DestIn),
|
|
||||||
"destination-out" => Some(CompositionStyle::DestOut),
|
|
||||||
"destination-over" => Some(CompositionStyle::DestOver),
|
|
||||||
"destination-atop" => Some(CompositionStyle::DestAtop),
|
|
||||||
"copy" => Some(CompositionStyle::Copy),
|
|
||||||
"lighter" => Some(CompositionStyle::Lighter),
|
|
||||||
"xor" => Some(CompositionStyle::Xor),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_str(&self) -> &str {
|
pub fn to_str(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
CompositionStyle::SrcIn => "source-in",
|
CompositionStyle::SrcIn => "source-in",
|
||||||
|
@ -495,6 +511,31 @@ pub enum BlendingStyle {
|
||||||
Luminosity,
|
Luminosity,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for BlendingStyle {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(string: &str) -> Result<BlendingStyle, ()> {
|
||||||
|
match string {
|
||||||
|
"multiply" => Ok(BlendingStyle::Multiply),
|
||||||
|
"screen" => Ok(BlendingStyle::Screen),
|
||||||
|
"overlay" => Ok(BlendingStyle::Overlay),
|
||||||
|
"darken" => Ok(BlendingStyle::Darken),
|
||||||
|
"lighten" => Ok(BlendingStyle::Lighten),
|
||||||
|
"color-dodge" => Ok(BlendingStyle::ColorDodge),
|
||||||
|
"color-burn" => Ok(BlendingStyle::ColorBurn),
|
||||||
|
"hard-light" => Ok(BlendingStyle::HardLight),
|
||||||
|
"soft-light" => Ok(BlendingStyle::SoftLight),
|
||||||
|
"difference" => Ok(BlendingStyle::Difference),
|
||||||
|
"exclusion" => Ok(BlendingStyle::Exclusion),
|
||||||
|
"hue" => Ok(BlendingStyle::Hue),
|
||||||
|
"saturation" => Ok(BlendingStyle::Saturation),
|
||||||
|
"color" => Ok(BlendingStyle::Color),
|
||||||
|
"luminosity" => Ok(BlendingStyle::Luminosity),
|
||||||
|
_ => Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl BlendingStyle {
|
impl BlendingStyle {
|
||||||
pub fn to_azure_style(&self) -> CompositionOp {
|
pub fn to_azure_style(&self) -> CompositionOp {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -516,27 +557,6 @@ impl BlendingStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(string: &str) -> Option<BlendingStyle> {
|
|
||||||
match string {
|
|
||||||
"multiply" => Some(BlendingStyle::Multiply),
|
|
||||||
"screen" => Some(BlendingStyle::Screen),
|
|
||||||
"overlay" => Some(BlendingStyle::Overlay),
|
|
||||||
"darken" => Some(BlendingStyle::Darken),
|
|
||||||
"lighten" => Some(BlendingStyle::Lighten),
|
|
||||||
"color-dodge" => Some(BlendingStyle::ColorDodge),
|
|
||||||
"color-burn" => Some(BlendingStyle::ColorBurn),
|
|
||||||
"hard-light" => Some(BlendingStyle::HardLight),
|
|
||||||
"soft-light" => Some(BlendingStyle::SoftLight),
|
|
||||||
"difference" => Some(BlendingStyle::Difference),
|
|
||||||
"exclusion" => Some(BlendingStyle::Exclusion),
|
|
||||||
"hue" => Some(BlendingStyle::Hue),
|
|
||||||
"saturation" => Some(BlendingStyle::Saturation),
|
|
||||||
"color" => Some(BlendingStyle::Color),
|
|
||||||
"luminosity" => Some(BlendingStyle::Luminosity),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_str(&self) -> &str {
|
pub fn to_str(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
BlendingStyle::Multiply => "multiply",
|
BlendingStyle::Multiply => "multiply",
|
||||||
|
@ -564,6 +584,28 @@ pub enum CompositionOrBlending {
|
||||||
Blending(BlendingStyle),
|
Blending(BlendingStyle),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for CompositionOrBlending {
|
||||||
|
fn default() -> CompositionOrBlending {
|
||||||
|
CompositionOrBlending::Composition(CompositionStyle::SrcOver)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for CompositionOrBlending {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(string: &str) -> Result<CompositionOrBlending, ()> {
|
||||||
|
if let Ok(op) = CompositionStyle::from_str(string) {
|
||||||
|
return Ok(CompositionOrBlending::Composition(op));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(op) = BlendingStyle::from_str(string) {
|
||||||
|
return Ok(CompositionOrBlending::Blending(op));
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CompositionOrBlending {
|
impl CompositionOrBlending {
|
||||||
pub fn to_azure_style(&self) -> CompositionOp {
|
pub fn to_azure_style(&self) -> CompositionOp {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -571,22 +613,6 @@ impl CompositionOrBlending {
|
||||||
CompositionOrBlending::Blending(op) => op.to_azure_style(),
|
CompositionOrBlending::Blending(op) => op.to_azure_style(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default() -> CompositionOrBlending {
|
|
||||||
CompositionOrBlending::Composition(CompositionStyle::SrcOver)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_str(string: &str) -> Option<CompositionOrBlending> {
|
|
||||||
if let Some(op) = CompositionStyle::from_str(string) {
|
|
||||||
return Some(CompositionOrBlending::Composition(op));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(op) = BlendingStyle::from_str(string) {
|
|
||||||
return Some(CompositionOrBlending::Blending(op));
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ToAzColor {
|
pub trait ToAzColor {
|
||||||
|
|
|
@ -45,6 +45,7 @@ use std::borrow::ToOwned;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -568,7 +569,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
|
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
|
||||||
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
|
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
|
||||||
if let Some(op) = CompositionOrBlending::from_str(&op_str) {
|
if let Ok(op) = CompositionOrBlending::from_str(&op_str) {
|
||||||
self.state.borrow_mut().global_composition = op;
|
self.state.borrow_mut().global_composition = op;
|
||||||
self.ipc_renderer
|
self.ipc_renderer
|
||||||
.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetGlobalComposition(op)))
|
.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetGlobalComposition(op)))
|
||||||
|
@ -1006,7 +1007,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(rep) = RepetitionStyle::from_str(&repetition) {
|
if let Ok(rep) = RepetitionStyle::from_str(&repetition) {
|
||||||
return Ok(CanvasPattern::new(self.global.root().r(),
|
return Ok(CanvasPattern::new(self.global.root().r(),
|
||||||
image_data,
|
image_data,
|
||||||
Size2D::new(image_size.width as i32, image_size.height as i32),
|
Size2D::new(image_size.width as i32, image_size.height as i32),
|
||||||
|
@ -1045,7 +1046,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
|
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
|
||||||
fn SetLineCap(&self, cap_str: DOMString) {
|
fn SetLineCap(&self, cap_str: DOMString) {
|
||||||
if let Some(cap) = LineCapStyle::from_str(&cap_str) {
|
if let Ok(cap) = LineCapStyle::from_str(&cap_str) {
|
||||||
self.state.borrow_mut().line_cap = cap;
|
self.state.borrow_mut().line_cap = cap;
|
||||||
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetLineCap(cap))).unwrap()
|
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetLineCap(cap))).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -1063,7 +1064,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
|
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
|
||||||
fn SetLineJoin(&self, join_str: DOMString) {
|
fn SetLineJoin(&self, join_str: DOMString) {
|
||||||
if let Some(join) = LineJoinStyle::from_str(&join_str) {
|
if let Ok(join) = LineJoinStyle::from_str(&join_str) {
|
||||||
self.state.borrow_mut().line_join = join;
|
self.state.borrow_mut().line_join = join;
|
||||||
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetLineJoin(join))).unwrap()
|
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetLineJoin(join))).unwrap()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue