mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Use backend trait with associated types for 2d canvas backends abstraction (#36783)
Current abstraction was leaky and very hard to understand/impl because it was so intervened with actual implementation. Now we generalize both `CanvasState` and `CanvasData` over `B: Beckend`, meaning that every type/method must be part of trait interface (that lives in `beckend.rs`). Using associated trait types instead of `Box<dyn >` allows us too remove the need for wrapper types (and `to_raquote()` methods) as we can implement helper traits on (foreign) raquote types. The only time we actually do dispatch (by enum) is at `Canvas` methods. Implementation now only need to implement all backend traits and helpers. I tried to restrain myself from actually cleaning abstraction (where possible), to keep this change as much mechanical as possible, but we should absolutely do that as a follow up. Testing: Rust as we only do refactor, but there are also WPT tests try run: https://github.com/sagudev/servo/actions/runs/14760658522 Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
parent
8c318af307
commit
be0f4470c7
5 changed files with 876 additions and 647 deletions
192
components/canvas/backend.rs
Normal file
192
components/canvas/backend.rs
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
/* 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 https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use canvas_traits::canvas::{FillOrStrokeStyle, LineCapStyle, LineJoinStyle};
|
||||||
|
use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D};
|
||||||
|
use style::color::AbsoluteColor;
|
||||||
|
|
||||||
|
use crate::canvas_data::{CanvasPaintState, Filter, TextRun};
|
||||||
|
|
||||||
|
pub(crate) trait Backend: Clone + Sized {
|
||||||
|
type Pattern<'a>: PatternHelpers + Clone;
|
||||||
|
type StrokeOptions: StrokeOptionsHelpers + Clone;
|
||||||
|
type Color: Clone;
|
||||||
|
type DrawOptions: DrawOptionsHelpers + Clone;
|
||||||
|
type CompositionOp;
|
||||||
|
type CompositionOrBlending;
|
||||||
|
type DrawTarget: GenericDrawTarget<Self>;
|
||||||
|
type PathBuilder: GenericPathBuilder<Self>;
|
||||||
|
type SourceSurface;
|
||||||
|
type Path: PathHelpers<Self> + Clone;
|
||||||
|
type GradientStop;
|
||||||
|
type GradientStops;
|
||||||
|
|
||||||
|
fn get_composition_op(&self, opts: &Self::DrawOptions) -> Self::CompositionOp;
|
||||||
|
fn need_to_draw_shadow(&self, color: &Self::Color) -> bool;
|
||||||
|
fn set_shadow_color(&mut self, color: AbsoluteColor, state: &mut CanvasPaintState<'_, Self>);
|
||||||
|
fn set_fill_style(
|
||||||
|
&mut self,
|
||||||
|
style: FillOrStrokeStyle,
|
||||||
|
state: &mut CanvasPaintState<'_, Self>,
|
||||||
|
drawtarget: &Self::DrawTarget,
|
||||||
|
);
|
||||||
|
fn set_stroke_style(
|
||||||
|
&mut self,
|
||||||
|
style: FillOrStrokeStyle,
|
||||||
|
state: &mut CanvasPaintState<'_, Self>,
|
||||||
|
drawtarget: &Self::DrawTarget,
|
||||||
|
);
|
||||||
|
fn set_global_composition(
|
||||||
|
&mut self,
|
||||||
|
op: Self::CompositionOrBlending,
|
||||||
|
state: &mut CanvasPaintState<'_, Self>,
|
||||||
|
);
|
||||||
|
fn create_drawtarget(&self, size: Size2D<u64>) -> Self::DrawTarget;
|
||||||
|
fn new_paint_state<'a>(&self) -> CanvasPaintState<'a, Self>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This defines required methods for a DrawTarget (currently only implemented for raqote). The
|
||||||
|
// prototypes are derived from the now-removed Azure backend's methods.
|
||||||
|
pub(crate) trait GenericDrawTarget<B: Backend> {
|
||||||
|
fn clear_rect(&mut self, rect: &Rect<f32>);
|
||||||
|
fn copy_surface(
|
||||||
|
&mut self,
|
||||||
|
surface: B::SourceSurface,
|
||||||
|
source: Rect<i32>,
|
||||||
|
destination: Point2D<i32>,
|
||||||
|
);
|
||||||
|
fn create_path_builder(&self) -> B::PathBuilder;
|
||||||
|
fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Self;
|
||||||
|
fn create_source_surface_from_data(&self, data: &[u8]) -> Option<B::SourceSurface>;
|
||||||
|
fn draw_surface(
|
||||||
|
&mut self,
|
||||||
|
surface: B::SourceSurface,
|
||||||
|
dest: Rect<f64>,
|
||||||
|
source: Rect<f64>,
|
||||||
|
filter: Filter,
|
||||||
|
draw_options: &B::DrawOptions,
|
||||||
|
);
|
||||||
|
fn draw_surface_with_shadow(
|
||||||
|
&self,
|
||||||
|
surface: B::SourceSurface,
|
||||||
|
dest: &Point2D<f32>,
|
||||||
|
color: &B::Color,
|
||||||
|
offset: &Vector2D<f32>,
|
||||||
|
sigma: f32,
|
||||||
|
operator: B::CompositionOp,
|
||||||
|
);
|
||||||
|
fn fill(&mut self, path: &B::Path, pattern: B::Pattern<'_>, draw_options: &B::DrawOptions);
|
||||||
|
fn fill_text(
|
||||||
|
&mut self,
|
||||||
|
text_runs: Vec<TextRun>,
|
||||||
|
start: Point2D<f32>,
|
||||||
|
pattern: &B::Pattern<'_>,
|
||||||
|
draw_options: &B::DrawOptions,
|
||||||
|
);
|
||||||
|
fn fill_rect(
|
||||||
|
&mut self,
|
||||||
|
rect: &Rect<f32>,
|
||||||
|
pattern: B::Pattern<'_>,
|
||||||
|
draw_options: Option<&B::DrawOptions>,
|
||||||
|
);
|
||||||
|
fn get_size(&self) -> Size2D<i32>;
|
||||||
|
fn get_transform(&self) -> Transform2D<f32>;
|
||||||
|
fn pop_clip(&mut self);
|
||||||
|
fn push_clip(&mut self, path: &B::Path);
|
||||||
|
fn set_transform(&mut self, matrix: &Transform2D<f32>);
|
||||||
|
fn snapshot(&self) -> B::SourceSurface;
|
||||||
|
fn stroke(
|
||||||
|
&mut self,
|
||||||
|
path: &B::Path,
|
||||||
|
pattern: B::Pattern<'_>,
|
||||||
|
stroke_options: &B::StrokeOptions,
|
||||||
|
draw_options: &B::DrawOptions,
|
||||||
|
);
|
||||||
|
fn stroke_line(
|
||||||
|
&mut self,
|
||||||
|
start: Point2D<f32>,
|
||||||
|
end: Point2D<f32>,
|
||||||
|
pattern: B::Pattern<'_>,
|
||||||
|
stroke_options: &B::StrokeOptions,
|
||||||
|
draw_options: &B::DrawOptions,
|
||||||
|
);
|
||||||
|
fn stroke_rect(
|
||||||
|
&mut self,
|
||||||
|
rect: &Rect<f32>,
|
||||||
|
pattern: B::Pattern<'_>,
|
||||||
|
stroke_options: &B::StrokeOptions,
|
||||||
|
draw_options: &B::DrawOptions,
|
||||||
|
);
|
||||||
|
fn snapshot_data(&self) -> &[u8];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A generic PathBuilder that abstracts the interface for azure's and raqote's PathBuilder.
|
||||||
|
pub(crate) trait GenericPathBuilder<B: Backend> {
|
||||||
|
fn arc(
|
||||||
|
&mut self,
|
||||||
|
origin: Point2D<f32>,
|
||||||
|
radius: f32,
|
||||||
|
start_angle: f32,
|
||||||
|
end_angle: f32,
|
||||||
|
anticlockwise: bool,
|
||||||
|
);
|
||||||
|
fn bezier_curve_to(
|
||||||
|
&mut self,
|
||||||
|
control_point1: &Point2D<f32>,
|
||||||
|
control_point2: &Point2D<f32>,
|
||||||
|
control_point3: &Point2D<f32>,
|
||||||
|
);
|
||||||
|
fn close(&mut self);
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn ellipse(
|
||||||
|
&mut self,
|
||||||
|
origin: Point2D<f32>,
|
||||||
|
radius_x: f32,
|
||||||
|
radius_y: f32,
|
||||||
|
rotation_angle: f32,
|
||||||
|
start_angle: f32,
|
||||||
|
end_angle: f32,
|
||||||
|
anticlockwise: bool,
|
||||||
|
);
|
||||||
|
fn get_current_point(&mut self) -> Option<Point2D<f32>>;
|
||||||
|
fn line_to(&mut self, point: Point2D<f32>);
|
||||||
|
fn move_to(&mut self, point: Point2D<f32>);
|
||||||
|
fn quadratic_curve_to(&mut self, control_point: &Point2D<f32>, end_point: &Point2D<f32>);
|
||||||
|
fn svg_arc(
|
||||||
|
&mut self,
|
||||||
|
radius_x: f32,
|
||||||
|
radius_y: f32,
|
||||||
|
rotation_angle: f32,
|
||||||
|
large_arc: bool,
|
||||||
|
sweep: bool,
|
||||||
|
end_point: Point2D<f32>,
|
||||||
|
);
|
||||||
|
fn finish(&mut self) -> B::Path;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait PatternHelpers {
|
||||||
|
fn is_zero_size_gradient(&self) -> bool;
|
||||||
|
fn draw_rect(&self, rect: &Rect<f32>) -> Rect<f32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait StrokeOptionsHelpers {
|
||||||
|
fn set_line_width(&mut self, _val: f32);
|
||||||
|
fn set_miter_limit(&mut self, _val: f32);
|
||||||
|
fn set_line_join(&mut self, val: LineJoinStyle);
|
||||||
|
fn set_line_cap(&mut self, val: LineCapStyle);
|
||||||
|
fn set_line_dash(&mut self, items: Vec<f32>);
|
||||||
|
fn set_line_dash_offset(&mut self, offset: f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait DrawOptionsHelpers {
|
||||||
|
fn set_alpha(&mut self, val: f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait PathHelpers<B: Backend> {
|
||||||
|
fn transformed_copy_to_builder(&self, transform: &Transform2D<f32>) -> B::PathBuilder;
|
||||||
|
|
||||||
|
fn contains_point(&self, x: f64, y: f64, path_transform: &Transform2D<f32>) -> bool;
|
||||||
|
|
||||||
|
fn copy_to_builder(&self) -> B::PathBuilder;
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use std::marker::PhantomData;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -16,7 +17,6 @@ use fonts::{
|
||||||
};
|
};
|
||||||
use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
|
use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use num_traits::ToPrimitive;
|
|
||||||
use range::Range;
|
use range::Range;
|
||||||
use servo_arc::Arc as ServoArc;
|
use servo_arc::Arc as ServoArc;
|
||||||
use snapshot::Snapshot;
|
use snapshot::Snapshot;
|
||||||
|
@ -26,14 +26,17 @@ use unicode_script::Script;
|
||||||
use webrender_api::units::RectExt as RectExt_;
|
use webrender_api::units::RectExt as RectExt_;
|
||||||
use webrender_api::{ImageDescriptor, ImageDescriptorFlags, ImageFormat, ImageKey};
|
use webrender_api::{ImageDescriptor, ImageDescriptorFlags, ImageFormat, ImageKey};
|
||||||
|
|
||||||
use crate::raqote_backend::Repetition;
|
use crate::backend::{
|
||||||
|
Backend, DrawOptionsHelpers as _, GenericDrawTarget as _, GenericPathBuilder, PathHelpers,
|
||||||
|
PatternHelpers, StrokeOptionsHelpers as _,
|
||||||
|
};
|
||||||
|
|
||||||
// Asserts on WR texture cache update for zero sized image with raw data.
|
// Asserts on WR texture cache update for zero sized image with raw data.
|
||||||
// https://github.com/servo/webrender/blob/main/webrender/src/texture_cache.rs#L1475
|
// https://github.com/servo/webrender/blob/main/webrender/src/texture_cache.rs#L1475
|
||||||
const MIN_WR_IMAGE_SIZE: Size2D<u64> = Size2D::new(1, 1);
|
const MIN_WR_IMAGE_SIZE: Size2D<u64> = Size2D::new(1, 1);
|
||||||
|
|
||||||
fn to_path(path: &[PathSegment], mut builder: Box<dyn GenericPathBuilder>) -> Path {
|
fn to_path<B: Backend>(path: &[PathSegment], mut builder: B::PathBuilder) -> B::Path {
|
||||||
let mut build_ref = PathBuilderRef {
|
let mut build_ref = PathBuilderRef::<B> {
|
||||||
builder: &mut builder,
|
builder: &mut builder,
|
||||||
transform: Transform2D::identity(),
|
transform: Transform2D::identity(),
|
||||||
};
|
};
|
||||||
|
@ -112,20 +115,20 @@ fn to_path(path: &[PathSegment], mut builder: Box<dyn GenericPathBuilder>) -> Pa
|
||||||
/// draw the path, we convert it back to userspace and draw it
|
/// draw the path, we convert it back to userspace and draw it
|
||||||
/// with the correct transform applied.
|
/// with the correct transform applied.
|
||||||
/// TODO: De-abstract now that Azure is removed?
|
/// TODO: De-abstract now that Azure is removed?
|
||||||
enum PathState {
|
enum PathState<B: Backend> {
|
||||||
/// Path builder in user-space. If a transform has been applied
|
/// Path builder in user-space. If a transform has been applied
|
||||||
/// but no further path operations have occurred, it is stored
|
/// but no further path operations have occurred, it is stored
|
||||||
/// in the optional field.
|
/// in the optional field.
|
||||||
UserSpacePathBuilder(Box<dyn GenericPathBuilder>, Option<Transform2D<f32>>),
|
UserSpacePathBuilder(B::PathBuilder, Option<Transform2D<f32>>),
|
||||||
/// Path builder in device-space.
|
/// Path builder in device-space.
|
||||||
DeviceSpacePathBuilder(Box<dyn GenericPathBuilder>),
|
DeviceSpacePathBuilder(B::PathBuilder),
|
||||||
/// Path in user-space. If a transform has been applied but
|
/// Path in user-space. If a transform has been applied but
|
||||||
/// but no further path operations have occurred, it is stored
|
/// but no further path operations have occurred, it is stored
|
||||||
/// in the optional field.
|
/// in the optional field.
|
||||||
UserSpacePath(Path, Option<Transform2D<f32>>),
|
UserSpacePath(B::Path, Option<Transform2D<f32>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PathState {
|
impl<B: Backend> PathState<B> {
|
||||||
fn is_path(&self) -> bool {
|
fn is_path(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
PathState::UserSpacePath(..) => true,
|
PathState::UserSpacePath(..) => true,
|
||||||
|
@ -133,7 +136,7 @@ impl PathState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path(&self) -> &Path {
|
fn path(&self) -> &B::Path {
|
||||||
match *self {
|
match *self {
|
||||||
PathState::UserSpacePath(ref p, _) => p,
|
PathState::UserSpacePath(ref p, _) => p,
|
||||||
PathState::UserSpacePathBuilder(..) | PathState::DeviceSpacePathBuilder(..) => {
|
PathState::UserSpacePathBuilder(..) | PathState::DeviceSpacePathBuilder(..) => {
|
||||||
|
@ -143,84 +146,14 @@ impl PathState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Backend {
|
|
||||||
fn get_composition_op(&self, opts: &DrawOptions) -> CompositionOp;
|
|
||||||
fn need_to_draw_shadow(&self, color: &Color) -> bool;
|
|
||||||
fn set_shadow_color(&mut self, color: AbsoluteColor, state: &mut CanvasPaintState<'_>);
|
|
||||||
fn set_fill_style(
|
|
||||||
&mut self,
|
|
||||||
style: FillOrStrokeStyle,
|
|
||||||
state: &mut CanvasPaintState<'_>,
|
|
||||||
drawtarget: &dyn GenericDrawTarget,
|
|
||||||
);
|
|
||||||
fn set_stroke_style(
|
|
||||||
&mut self,
|
|
||||||
style: FillOrStrokeStyle,
|
|
||||||
state: &mut CanvasPaintState<'_>,
|
|
||||||
drawtarget: &dyn GenericDrawTarget,
|
|
||||||
);
|
|
||||||
fn set_global_composition(
|
|
||||||
&mut self,
|
|
||||||
op: CompositionOrBlending,
|
|
||||||
state: &mut CanvasPaintState<'_>,
|
|
||||||
);
|
|
||||||
fn create_drawtarget(&self, size: Size2D<u64>) -> Box<dyn GenericDrawTarget>;
|
|
||||||
fn recreate_paint_state<'a>(&self, state: &CanvasPaintState<'a>) -> CanvasPaintState<'a>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A generic PathBuilder that abstracts the interface for azure's and raqote's PathBuilder.
|
|
||||||
/// TODO: De-abstract now that Azure is removed?
|
|
||||||
pub trait GenericPathBuilder {
|
|
||||||
fn arc(
|
|
||||||
&mut self,
|
|
||||||
origin: Point2D<f32>,
|
|
||||||
radius: f32,
|
|
||||||
start_angle: f32,
|
|
||||||
end_angle: f32,
|
|
||||||
anticlockwise: bool,
|
|
||||||
);
|
|
||||||
fn bezier_curve_to(
|
|
||||||
&mut self,
|
|
||||||
control_point1: &Point2D<f32>,
|
|
||||||
control_point2: &Point2D<f32>,
|
|
||||||
control_point3: &Point2D<f32>,
|
|
||||||
);
|
|
||||||
fn close(&mut self);
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
fn ellipse(
|
|
||||||
&mut self,
|
|
||||||
origin: Point2D<f32>,
|
|
||||||
radius_x: f32,
|
|
||||||
radius_y: f32,
|
|
||||||
rotation_angle: f32,
|
|
||||||
start_angle: f32,
|
|
||||||
end_angle: f32,
|
|
||||||
anticlockwise: bool,
|
|
||||||
);
|
|
||||||
fn get_current_point(&mut self) -> Option<Point2D<f32>>;
|
|
||||||
fn line_to(&mut self, point: Point2D<f32>);
|
|
||||||
fn move_to(&mut self, point: Point2D<f32>);
|
|
||||||
fn quadratic_curve_to(&mut self, control_point: &Point2D<f32>, end_point: &Point2D<f32>);
|
|
||||||
fn svg_arc(
|
|
||||||
&mut self,
|
|
||||||
radius_x: f32,
|
|
||||||
radius_y: f32,
|
|
||||||
rotation_angle: f32,
|
|
||||||
large_arc: bool,
|
|
||||||
sweep: bool,
|
|
||||||
end_point: Point2D<f32>,
|
|
||||||
);
|
|
||||||
fn finish(&mut self) -> Path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A wrapper around a stored PathBuilder and an optional transformation that should be
|
/// A wrapper around a stored PathBuilder and an optional transformation that should be
|
||||||
/// applied to any points to ensure they are in the matching device space.
|
/// applied to any points to ensure they are in the matching device space.
|
||||||
struct PathBuilderRef<'a> {
|
struct PathBuilderRef<'a, B: Backend> {
|
||||||
builder: &'a mut Box<dyn GenericPathBuilder>,
|
builder: &'a mut B::PathBuilder,
|
||||||
transform: Transform2D<f32>,
|
transform: Transform2D<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PathBuilderRef<'_> {
|
impl<B: Backend> PathBuilderRef<'_, B> {
|
||||||
fn line_to(&mut self, pt: &Point2D<f32>) {
|
fn line_to(&mut self, pt: &Point2D<f32>) {
|
||||||
let pt = self.transform.transform_point(*pt);
|
let pt = self.transform.transform_point(*pt);
|
||||||
self.builder.line_to(pt);
|
self.builder.line_to(pt);
|
||||||
|
@ -341,7 +274,7 @@ impl PathBuilderRef<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn ellipse(
|
pub(crate) fn ellipse(
|
||||||
&mut self,
|
&mut self,
|
||||||
center: &Point2D<f32>,
|
center: &Point2D<f32>,
|
||||||
radius_x: f32,
|
radius_x: f32,
|
||||||
|
@ -430,9 +363,9 @@ impl UnshapedTextRun<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TextRun {
|
pub(crate) struct TextRun {
|
||||||
pub font: FontRef,
|
pub(crate) font: FontRef,
|
||||||
pub glyphs: Arc<GlyphStore>,
|
pub(crate) glyphs: Arc<GlyphStore>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextRun {
|
impl TextRun {
|
||||||
|
@ -458,149 +391,31 @@ impl TextRun {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This defines required methods for a DrawTarget (currently only implemented for raqote). The
|
|
||||||
// prototypes are derived from the now-removed Azure backend's methods.
|
|
||||||
pub trait GenericDrawTarget {
|
|
||||||
fn clear_rect(&mut self, rect: &Rect<f32>);
|
|
||||||
fn copy_surface(
|
|
||||||
&mut self,
|
|
||||||
surface: SourceSurface,
|
|
||||||
source: Rect<i32>,
|
|
||||||
destination: Point2D<i32>,
|
|
||||||
);
|
|
||||||
fn create_gradient_stops(&self, gradient_stops: Vec<GradientStop>) -> GradientStops;
|
|
||||||
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder>;
|
|
||||||
fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Box<dyn GenericDrawTarget>;
|
|
||||||
fn create_source_surface_from_data(&self, data: &[u8]) -> Option<SourceSurface>;
|
|
||||||
fn draw_surface(
|
|
||||||
&mut self,
|
|
||||||
surface: SourceSurface,
|
|
||||||
dest: Rect<f64>,
|
|
||||||
source: Rect<f64>,
|
|
||||||
filter: Filter,
|
|
||||||
draw_options: &DrawOptions,
|
|
||||||
);
|
|
||||||
fn draw_surface_with_shadow(
|
|
||||||
&self,
|
|
||||||
surface: SourceSurface,
|
|
||||||
dest: &Point2D<f32>,
|
|
||||||
color: &Color,
|
|
||||||
offset: &Vector2D<f32>,
|
|
||||||
sigma: f32,
|
|
||||||
operator: CompositionOp,
|
|
||||||
);
|
|
||||||
fn fill(&mut self, path: &Path, pattern: Pattern, draw_options: &DrawOptions);
|
|
||||||
fn fill_text(
|
|
||||||
&mut self,
|
|
||||||
text_runs: Vec<TextRun>,
|
|
||||||
start: Point2D<f32>,
|
|
||||||
pattern: &Pattern,
|
|
||||||
draw_options: &DrawOptions,
|
|
||||||
);
|
|
||||||
fn fill_rect(&mut self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>);
|
|
||||||
fn get_size(&self) -> Size2D<i32>;
|
|
||||||
fn get_transform(&self) -> Transform2D<f32>;
|
|
||||||
fn pop_clip(&mut self);
|
|
||||||
fn push_clip(&mut self, path: &Path);
|
|
||||||
fn set_transform(&mut self, matrix: &Transform2D<f32>);
|
|
||||||
fn snapshot(&self) -> SourceSurface;
|
|
||||||
fn stroke(
|
|
||||||
&mut self,
|
|
||||||
path: &Path,
|
|
||||||
pattern: Pattern,
|
|
||||||
stroke_options: &StrokeOptions,
|
|
||||||
draw_options: &DrawOptions,
|
|
||||||
);
|
|
||||||
fn stroke_line(
|
|
||||||
&mut self,
|
|
||||||
start: Point2D<f32>,
|
|
||||||
end: Point2D<f32>,
|
|
||||||
pattern: Pattern,
|
|
||||||
stroke_options: &StrokeOptions,
|
|
||||||
draw_options: &DrawOptions,
|
|
||||||
);
|
|
||||||
fn stroke_rect(
|
|
||||||
&mut self,
|
|
||||||
rect: &Rect<f32>,
|
|
||||||
pattern: Pattern,
|
|
||||||
stroke_options: &StrokeOptions,
|
|
||||||
draw_options: &DrawOptions,
|
|
||||||
);
|
|
||||||
fn snapshot_data(&self) -> &[u8];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum GradientStop {
|
|
||||||
Raqote(raqote::GradientStop),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum GradientStops {
|
|
||||||
Raqote(Vec<raqote::GradientStop>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum Color {
|
|
||||||
Raqote(raqote::SolidSource),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum CompositionOp {
|
|
||||||
Raqote(raqote::BlendMode),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum SourceSurface {
|
|
||||||
Raqote(Vec<u8>), // TODO: See if we can avoid the alloc (probably?)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum Path {
|
|
||||||
Raqote(raqote::Path),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum Pattern<'a> {
|
|
||||||
Raqote(crate::raqote_backend::Pattern<'a>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum DrawOptions {
|
|
||||||
Raqote(raqote::DrawOptions),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum StrokeOptions {
|
|
||||||
Raqote(raqote::StrokeStyle),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum Filter {
|
pub(crate) enum Filter {
|
||||||
Bilinear,
|
Bilinear,
|
||||||
Nearest,
|
Nearest,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CanvasData<'a> {
|
pub(crate) struct CanvasData<'a, B: Backend> {
|
||||||
backend: Box<dyn Backend>,
|
backend: B,
|
||||||
drawtarget: Box<dyn GenericDrawTarget>,
|
drawtarget: B::DrawTarget,
|
||||||
path_state: Option<PathState>,
|
path_state: Option<PathState<B>>,
|
||||||
state: CanvasPaintState<'a>,
|
state: CanvasPaintState<'a, B>,
|
||||||
saved_states: Vec<CanvasPaintState<'a>>,
|
saved_states: Vec<CanvasPaintState<'a, B>>,
|
||||||
compositor_api: CrossProcessCompositorApi,
|
compositor_api: CrossProcessCompositorApi,
|
||||||
image_key: ImageKey,
|
image_key: ImageKey,
|
||||||
font_context: Arc<FontContext>,
|
font_context: Arc<FontContext>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_backend() -> Box<dyn Backend> {
|
impl<'a, B: Backend> CanvasData<'a, B> {
|
||||||
Box::new(crate::raqote_backend::RaqoteBackend)
|
pub(crate) fn new(
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> CanvasData<'a> {
|
|
||||||
pub fn new(
|
|
||||||
size: Size2D<u64>,
|
size: Size2D<u64>,
|
||||||
compositor_api: CrossProcessCompositorApi,
|
compositor_api: CrossProcessCompositorApi,
|
||||||
font_context: Arc<FontContext>,
|
font_context: Arc<FontContext>,
|
||||||
) -> CanvasData<'a> {
|
backend: B,
|
||||||
|
) -> CanvasData<'a, B> {
|
||||||
let size = size.max(MIN_WR_IMAGE_SIZE);
|
let size = size.max(MIN_WR_IMAGE_SIZE);
|
||||||
let backend = create_backend();
|
|
||||||
let draw_target = backend.create_drawtarget(size);
|
let draw_target = backend.create_drawtarget(size);
|
||||||
let image_key = compositor_api.generate_image_key().unwrap();
|
let image_key = compositor_api.generate_image_key().unwrap();
|
||||||
let descriptor = ImageDescriptor {
|
let descriptor = ImageDescriptor {
|
||||||
|
@ -614,10 +429,10 @@ impl<'a> CanvasData<'a> {
|
||||||
SerializableImageData::Raw(IpcSharedMemory::from_bytes(draw_target.snapshot_data()));
|
SerializableImageData::Raw(IpcSharedMemory::from_bytes(draw_target.snapshot_data()));
|
||||||
compositor_api.update_images(vec![ImageUpdate::AddImage(image_key, descriptor, data)]);
|
compositor_api.update_images(vec![ImageUpdate::AddImage(image_key, descriptor, data)]);
|
||||||
CanvasData {
|
CanvasData {
|
||||||
|
state: backend.new_paint_state(),
|
||||||
backend,
|
backend,
|
||||||
drawtarget: draw_target,
|
drawtarget: draw_target,
|
||||||
path_state: None,
|
path_state: None,
|
||||||
state: CanvasPaintState::default(),
|
|
||||||
saved_states: vec![],
|
saved_states: vec![],
|
||||||
compositor_api,
|
compositor_api,
|
||||||
image_key,
|
image_key,
|
||||||
|
@ -625,11 +440,11 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn image_key(&self) -> ImageKey {
|
pub(crate) fn image_key(&self) -> ImageKey {
|
||||||
self.image_key
|
self.image_key
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_image(
|
pub(crate) fn draw_image(
|
||||||
&mut self,
|
&mut self,
|
||||||
image_data: &[u8],
|
image_data: &[u8],
|
||||||
image_size: Size2D<u64>,
|
image_size: Size2D<u64>,
|
||||||
|
@ -648,8 +463,8 @@ impl<'a> CanvasData<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let draw_options = self.state.draw_options.clone();
|
let draw_options = self.state.draw_options.clone();
|
||||||
let writer = |draw_target: &mut dyn GenericDrawTarget| {
|
let writer = |draw_target: &mut B::DrawTarget| {
|
||||||
write_image(
|
write_image::<B>(
|
||||||
draw_target,
|
draw_target,
|
||||||
image_data,
|
image_data,
|
||||||
source_rect.size,
|
source_rect.size,
|
||||||
|
@ -669,15 +484,15 @@ impl<'a> CanvasData<'a> {
|
||||||
// TODO(pylbrecht) pass another closure for raqote
|
// TODO(pylbrecht) pass another closure for raqote
|
||||||
self.draw_with_shadow(&rect, writer);
|
self.draw_with_shadow(&rect, writer);
|
||||||
} else {
|
} else {
|
||||||
writer(&mut *self.drawtarget);
|
writer(&mut self.drawtarget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_context_state(&mut self) {
|
pub(crate) fn save_context_state(&mut self) {
|
||||||
self.saved_states.push(self.state.clone());
|
self.saved_states.push(self.state.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn restore_context_state(&mut self) {
|
pub(crate) fn restore_context_state(&mut self) {
|
||||||
if let Some(state) = self.saved_states.pop() {
|
if let Some(state) = self.saved_states.pop() {
|
||||||
let _ = mem::replace(&mut self.state, state);
|
let _ = mem::replace(&mut self.state, state);
|
||||||
self.drawtarget.set_transform(&self.state.transform);
|
self.drawtarget.set_transform(&self.state.transform);
|
||||||
|
@ -685,7 +500,7 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill_text_with_size(
|
pub(crate) fn fill_text_with_size(
|
||||||
&mut self,
|
&mut self,
|
||||||
text: String,
|
text: String,
|
||||||
x: f64,
|
x: f64,
|
||||||
|
@ -764,7 +579,7 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#text-preparation-algorithm>
|
/// <https://html.spec.whatwg.org/multipage/#text-preparation-algorithm>
|
||||||
pub fn fill_text(
|
pub(crate) fn fill_text(
|
||||||
&mut self,
|
&mut self,
|
||||||
text: String,
|
text: String,
|
||||||
x: f64,
|
x: f64,
|
||||||
|
@ -782,7 +597,7 @@ impl<'a> CanvasData<'a> {
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#text-preparation-algorithm>
|
/// <https://html.spec.whatwg.org/multipage/#text-preparation-algorithm>
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-measuretext>
|
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-measuretext>
|
||||||
pub fn measure_text(&mut self, text: String) -> TextMetrics {
|
pub(crate) fn measure_text(&mut self, text: String) -> TextMetrics {
|
||||||
// > Step 2: Replace all ASCII whitespace in text with U+0020 SPACE characters.
|
// > Step 2: Replace all ASCII whitespace in text with U+0020 SPACE characters.
|
||||||
let text = replace_ascii_whitespace(text);
|
let text = replace_ascii_whitespace(text);
|
||||||
let Some(ref font_style) = self.state.font_style else {
|
let Some(ref font_style) = self.state.font_style else {
|
||||||
|
@ -934,49 +749,15 @@ impl<'a> CanvasData<'a> {
|
||||||
point2(x + anchor_x, y + anchor_y)
|
point2(x + anchor_x, y + anchor_y)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill_rect(&mut self, rect: &Rect<f32>) {
|
pub(crate) fn fill_rect(&mut self, rect: &Rect<f32>) {
|
||||||
if self.state.fill_style.is_zero_size_gradient() {
|
if self.state.fill_style.is_zero_size_gradient() {
|
||||||
return; // Paint nothing if gradient size is zero.
|
return; // Paint nothing if gradient size is zero.
|
||||||
}
|
}
|
||||||
|
|
||||||
let draw_rect = match &self.state.fill_style {
|
let draw_rect = self.state.fill_style.draw_rect(rect);
|
||||||
Pattern::Raqote(pattern) => match pattern {
|
|
||||||
crate::raqote_backend::Pattern::Surface(pattern) => {
|
|
||||||
let pattern_rect = Rect::new(Point2D::origin(), pattern.size());
|
|
||||||
let mut draw_rect = rect.intersection(&pattern_rect).unwrap_or(Rect::zero());
|
|
||||||
|
|
||||||
match pattern.repetition() {
|
|
||||||
Repetition::NoRepeat => {
|
|
||||||
draw_rect.size.width =
|
|
||||||
draw_rect.size.width.min(pattern_rect.size.width);
|
|
||||||
draw_rect.size.height =
|
|
||||||
draw_rect.size.height.min(pattern_rect.size.height);
|
|
||||||
},
|
|
||||||
Repetition::RepeatX => {
|
|
||||||
draw_rect.size.width = rect.size.width;
|
|
||||||
draw_rect.size.height =
|
|
||||||
draw_rect.size.height.min(pattern_rect.size.height);
|
|
||||||
},
|
|
||||||
Repetition::RepeatY => {
|
|
||||||
draw_rect.size.height = rect.size.height;
|
|
||||||
draw_rect.size.width =
|
|
||||||
draw_rect.size.width.min(pattern_rect.size.width);
|
|
||||||
},
|
|
||||||
Repetition::Repeat => {
|
|
||||||
draw_rect = *rect;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
draw_rect
|
|
||||||
},
|
|
||||||
crate::raqote_backend::Pattern::Color(..) |
|
|
||||||
crate::raqote_backend::Pattern::LinearGradient(..) |
|
|
||||||
crate::raqote_backend::Pattern::RadialGradient(..) => *rect,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.need_to_draw_shadow() {
|
if self.need_to_draw_shadow() {
|
||||||
self.draw_with_shadow(&draw_rect, |new_draw_target: &mut dyn GenericDrawTarget| {
|
self.draw_with_shadow(&draw_rect, |new_draw_target: &mut B::DrawTarget| {
|
||||||
new_draw_target.fill_rect(
|
new_draw_target.fill_rect(
|
||||||
&draw_rect,
|
&draw_rect,
|
||||||
self.state.fill_style.clone(),
|
self.state.fill_style.clone(),
|
||||||
|
@ -992,17 +773,17 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_rect(&mut self, rect: &Rect<f32>) {
|
pub(crate) fn clear_rect(&mut self, rect: &Rect<f32>) {
|
||||||
self.drawtarget.clear_rect(rect);
|
self.drawtarget.clear_rect(rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stroke_rect(&mut self, rect: &Rect<f32>) {
|
pub(crate) fn stroke_rect(&mut self, rect: &Rect<f32>) {
|
||||||
if self.state.stroke_style.is_zero_size_gradient() {
|
if self.state.stroke_style.is_zero_size_gradient() {
|
||||||
return; // Paint nothing if gradient size is zero.
|
return; // Paint nothing if gradient size is zero.
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.need_to_draw_shadow() {
|
if self.need_to_draw_shadow() {
|
||||||
self.draw_with_shadow(rect, |new_draw_target: &mut dyn GenericDrawTarget| {
|
self.draw_with_shadow(rect, |new_draw_target: &mut B::DrawTarget| {
|
||||||
new_draw_target.stroke_rect(
|
new_draw_target.stroke_rect(
|
||||||
rect,
|
rect,
|
||||||
self.state.stroke_style.clone(),
|
self.state.stroke_style.clone(),
|
||||||
|
@ -1030,12 +811,12 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn begin_path(&mut self) {
|
pub(crate) fn begin_path(&mut self) {
|
||||||
// Erase any traces of previous paths that existed before this.
|
// Erase any traces of previous paths that existed before this.
|
||||||
self.path_state = None;
|
self.path_state = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close_path(&mut self) {
|
pub(crate) fn close_path(&mut self) {
|
||||||
self.path_builder().close();
|
self.path_builder().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1097,14 +878,14 @@ impl<'a> CanvasData<'a> {
|
||||||
assert!(self.path_state.as_ref().unwrap().is_path())
|
assert!(self.path_state.as_ref().unwrap().is_path())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path(&self) -> &Path {
|
fn path(&self) -> &B::Path {
|
||||||
self.path_state
|
self.path_state
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("Should have called ensure_path()")
|
.expect("Should have called ensure_path()")
|
||||||
.path()
|
.path()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill(&mut self) {
|
pub(crate) fn fill(&mut self) {
|
||||||
if self.state.fill_style.is_zero_size_gradient() {
|
if self.state.fill_style.is_zero_size_gradient() {
|
||||||
return; // Paint nothing if gradient size is zero.
|
return; // Paint nothing if gradient size is zero.
|
||||||
}
|
}
|
||||||
|
@ -1113,16 +894,16 @@ impl<'a> CanvasData<'a> {
|
||||||
self.drawtarget.fill(
|
self.drawtarget.fill(
|
||||||
&self.path().clone(),
|
&self.path().clone(),
|
||||||
self.state.fill_style.clone(),
|
self.state.fill_style.clone(),
|
||||||
&self.state.draw_options,
|
&self.state.draw_options.clone(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill_path(&mut self, path: &[PathSegment]) {
|
pub(crate) fn fill_path(&mut self, path: &[PathSegment]) {
|
||||||
if self.state.fill_style.is_zero_size_gradient() {
|
if self.state.fill_style.is_zero_size_gradient() {
|
||||||
return; // Paint nothing if gradient size is zero.
|
return; // Paint nothing if gradient size is zero.
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = to_path(path, self.drawtarget.create_path_builder());
|
let path = to_path::<B>(path, self.drawtarget.create_path_builder());
|
||||||
|
|
||||||
self.drawtarget.fill(
|
self.drawtarget.fill(
|
||||||
&path,
|
&path,
|
||||||
|
@ -1131,7 +912,7 @@ impl<'a> CanvasData<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stroke(&mut self) {
|
pub(crate) fn stroke(&mut self) {
|
||||||
if self.state.stroke_style.is_zero_size_gradient() {
|
if self.state.stroke_style.is_zero_size_gradient() {
|
||||||
return; // Paint nothing if gradient size is zero.
|
return; // Paint nothing if gradient size is zero.
|
||||||
}
|
}
|
||||||
|
@ -1145,12 +926,12 @@ impl<'a> CanvasData<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stroke_path(&mut self, path: &[PathSegment]) {
|
pub(crate) fn stroke_path(&mut self, path: &[PathSegment]) {
|
||||||
if self.state.stroke_style.is_zero_size_gradient() {
|
if self.state.stroke_style.is_zero_size_gradient() {
|
||||||
return; // Paint nothing if gradient size is zero.
|
return; // Paint nothing if gradient size is zero.
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = to_path(path, self.drawtarget.create_path_builder());
|
let path = to_path::<B>(path, self.drawtarget.create_path_builder());
|
||||||
|
|
||||||
self.drawtarget.stroke(
|
self.drawtarget.stroke(
|
||||||
&path,
|
&path,
|
||||||
|
@ -1160,18 +941,18 @@ impl<'a> CanvasData<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clip(&mut self) {
|
pub(crate) fn clip(&mut self) {
|
||||||
self.ensure_path();
|
self.ensure_path();
|
||||||
let path = self.path().clone();
|
let path = self.path().clone();
|
||||||
self.drawtarget.push_clip(&path);
|
self.drawtarget.push_clip(&path);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clip_path(&mut self, path: &[PathSegment]) {
|
pub(crate) fn clip_path(&mut self, path: &[PathSegment]) {
|
||||||
let path = to_path(path, self.drawtarget.create_path_builder());
|
let path = to_path::<B>(path, self.drawtarget.create_path_builder());
|
||||||
self.drawtarget.push_clip(&path);
|
self.drawtarget.push_clip(&path);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_point_in_path(
|
pub(crate) fn is_point_in_path(
|
||||||
&mut self,
|
&mut self,
|
||||||
x: f64,
|
x: f64,
|
||||||
y: f64,
|
y: f64,
|
||||||
|
@ -1190,7 +971,7 @@ impl<'a> CanvasData<'a> {
|
||||||
chan.send(result).unwrap();
|
chan.send(result).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_point_in_path_(
|
pub(crate) fn is_point_in_path_(
|
||||||
&mut self,
|
&mut self,
|
||||||
path: &[PathSegment],
|
path: &[PathSegment],
|
||||||
x: f64,
|
x: f64,
|
||||||
|
@ -1202,7 +983,7 @@ impl<'a> CanvasData<'a> {
|
||||||
Some(PathState::UserSpacePath(_, Some(transform))) => transform,
|
Some(PathState::UserSpacePath(_, Some(transform))) => transform,
|
||||||
Some(_) | None => &self.drawtarget.get_transform(),
|
Some(_) | None => &self.drawtarget.get_transform(),
|
||||||
};
|
};
|
||||||
let result = to_path(path, self.drawtarget.create_path_builder()).contains_point(
|
let result = to_path::<B>(path, self.drawtarget.create_path_builder()).contains_point(
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
path_transform,
|
path_transform,
|
||||||
|
@ -1210,15 +991,15 @@ impl<'a> CanvasData<'a> {
|
||||||
chan.send(result).unwrap();
|
chan.send(result).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_to(&mut self, point: &Point2D<f32>) {
|
pub(crate) fn move_to(&mut self, point: &Point2D<f32>) {
|
||||||
self.path_builder().move_to(point);
|
self.path_builder().move_to(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_to(&mut self, point: &Point2D<f32>) {
|
pub(crate) fn line_to(&mut self, point: &Point2D<f32>) {
|
||||||
self.path_builder().line_to(point);
|
self.path_builder().line_to(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_builder(&mut self) -> PathBuilderRef {
|
fn path_builder(&mut self) -> PathBuilderRef<B> {
|
||||||
if self.path_state.is_none() {
|
if self.path_state.is_none() {
|
||||||
self.path_state = Some(PathState::UserSpacePathBuilder(
|
self.path_state = Some(PathState::UserSpacePathBuilder(
|
||||||
self.drawtarget.create_path_builder(),
|
self.drawtarget.create_path_builder(),
|
||||||
|
@ -1283,18 +1064,18 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rect(&mut self, rect: &Rect<f32>) {
|
pub(crate) fn rect(&mut self, rect: &Rect<f32>) {
|
||||||
self.path_builder().rect(rect);
|
self.path_builder().rect(rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn quadratic_curve_to(&mut self, cp: &Point2D<f32>, endpoint: &Point2D<f32>) {
|
pub(crate) fn quadratic_curve_to(&mut self, cp: &Point2D<f32>, endpoint: &Point2D<f32>) {
|
||||||
if self.path_state.is_none() {
|
if self.path_state.is_none() {
|
||||||
self.move_to(cp);
|
self.move_to(cp);
|
||||||
}
|
}
|
||||||
self.path_builder().quadratic_curve_to(cp, endpoint);
|
self.path_builder().quadratic_curve_to(cp, endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bezier_curve_to(
|
pub(crate) fn bezier_curve_to(
|
||||||
&mut self,
|
&mut self,
|
||||||
cp1: &Point2D<f32>,
|
cp1: &Point2D<f32>,
|
||||||
cp2: &Point2D<f32>,
|
cp2: &Point2D<f32>,
|
||||||
|
@ -1306,7 +1087,7 @@ impl<'a> CanvasData<'a> {
|
||||||
self.path_builder().bezier_curve_to(cp1, cp2, endpoint);
|
self.path_builder().bezier_curve_to(cp1, cp2, endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn arc(
|
pub(crate) fn arc(
|
||||||
&mut self,
|
&mut self,
|
||||||
center: &Point2D<f32>,
|
center: &Point2D<f32>,
|
||||||
radius: f32,
|
radius: f32,
|
||||||
|
@ -1318,12 +1099,12 @@ impl<'a> CanvasData<'a> {
|
||||||
.arc(center, radius, start_angle, end_angle, ccw);
|
.arc(center, radius, start_angle, end_angle, ccw);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn arc_to(&mut self, cp1: &Point2D<f32>, cp2: &Point2D<f32>, radius: f32) {
|
pub(crate) fn arc_to(&mut self, cp1: &Point2D<f32>, cp2: &Point2D<f32>, radius: f32) {
|
||||||
self.path_builder().arc_to(cp1, cp2, radius);
|
self.path_builder().arc_to(cp1, cp2, radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn ellipse(
|
pub(crate) fn ellipse(
|
||||||
&mut self,
|
&mut self,
|
||||||
center: &Point2D<f32>,
|
center: &Point2D<f32>,
|
||||||
radius_x: f32,
|
radius_x: f32,
|
||||||
|
@ -1344,45 +1125,45 @@ impl<'a> CanvasData<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_fill_style(&mut self, style: FillOrStrokeStyle) {
|
pub(crate) fn set_fill_style(&mut self, style: FillOrStrokeStyle) {
|
||||||
self.backend
|
self.backend
|
||||||
.set_fill_style(style, &mut self.state, &*self.drawtarget);
|
.set_fill_style(style, &mut self.state, &self.drawtarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_stroke_style(&mut self, style: FillOrStrokeStyle) {
|
pub(crate) fn set_stroke_style(&mut self, style: FillOrStrokeStyle) {
|
||||||
self.backend
|
self.backend
|
||||||
.set_stroke_style(style, &mut self.state, &*self.drawtarget);
|
.set_stroke_style(style, &mut self.state, &self.drawtarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_line_width(&mut self, width: f32) {
|
pub(crate) fn set_line_width(&mut self, width: f32) {
|
||||||
self.state.stroke_opts.set_line_width(width);
|
self.state.stroke_opts.set_line_width(width);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_line_cap(&mut self, cap: LineCapStyle) {
|
pub(crate) fn set_line_cap(&mut self, cap: LineCapStyle) {
|
||||||
self.state.stroke_opts.set_line_cap(cap);
|
self.state.stroke_opts.set_line_cap(cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_line_join(&mut self, join: LineJoinStyle) {
|
pub(crate) fn set_line_join(&mut self, join: LineJoinStyle) {
|
||||||
self.state.stroke_opts.set_line_join(join);
|
self.state.stroke_opts.set_line_join(join);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_miter_limit(&mut self, limit: f32) {
|
pub(crate) fn set_miter_limit(&mut self, limit: f32) {
|
||||||
self.state.stroke_opts.set_miter_limit(limit);
|
self.state.stroke_opts.set_miter_limit(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_line_dash(&mut self, items: Vec<f32>) {
|
pub(crate) fn set_line_dash(&mut self, items: Vec<f32>) {
|
||||||
self.state.stroke_opts.set_line_dash(items);
|
self.state.stroke_opts.set_line_dash(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_line_dash_offset(&mut self, offset: f32) {
|
pub(crate) fn set_line_dash_offset(&mut self, offset: f32) {
|
||||||
self.state.stroke_opts.set_line_dash_offset(offset);
|
self.state.stroke_opts.set_line_dash_offset(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_transform(&self) -> Transform2D<f32> {
|
pub(crate) fn get_transform(&self) -> Transform2D<f32> {
|
||||||
self.drawtarget.get_transform()
|
self.drawtarget.get_transform()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_transform(&mut self, transform: &Transform2D<f32>) {
|
pub(crate) fn set_transform(&mut self, transform: &Transform2D<f32>) {
|
||||||
// If there is an in-progress path, store the existing transformation required
|
// If there is an in-progress path, store the existing transformation required
|
||||||
// to move between device and user space.
|
// to move between device and user space.
|
||||||
match self.path_state.as_mut() {
|
match self.path_state.as_mut() {
|
||||||
|
@ -1398,32 +1179,28 @@ impl<'a> CanvasData<'a> {
|
||||||
self.drawtarget.set_transform(transform)
|
self.drawtarget.set_transform(transform)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_global_alpha(&mut self, alpha: f32) {
|
pub(crate) fn set_global_alpha(&mut self, alpha: f32) {
|
||||||
self.state.draw_options.set_alpha(alpha);
|
self.state.draw_options.set_alpha(alpha);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_global_composition(&mut self, op: CompositionOrBlending) {
|
pub(crate) fn set_global_composition(&mut self, op: B::CompositionOrBlending) {
|
||||||
self.backend.set_global_composition(op, &mut self.state);
|
self.backend.set_global_composition(op, &mut self.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recreate(&mut self, size: Option<Size2D<u64>>) {
|
pub(crate) fn recreate(&mut self, size: Option<Size2D<u64>>) {
|
||||||
let size = size
|
let size = size
|
||||||
.unwrap_or_else(|| self.drawtarget.get_size().to_u64())
|
.unwrap_or_else(|| self.drawtarget.get_size().to_u64())
|
||||||
.max(MIN_WR_IMAGE_SIZE);
|
.max(MIN_WR_IMAGE_SIZE);
|
||||||
self.drawtarget = self
|
self.drawtarget = self
|
||||||
.backend
|
.backend
|
||||||
.create_drawtarget(Size2D::new(size.width, size.height));
|
.create_drawtarget(Size2D::new(size.width, size.height));
|
||||||
self.state = self.backend.recreate_paint_state(&self.state);
|
self.state = self.backend.new_paint_state();
|
||||||
self.saved_states.clear();
|
self.saved_states.clear();
|
||||||
self.update_image_rendering();
|
self.update_image_rendering();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn snapshot(&self) {
|
|
||||||
self.drawtarget.snapshot_data();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Update image in WebRender
|
/// Update image in WebRender
|
||||||
pub fn update_image_rendering(&mut self) {
|
pub(crate) fn update_image_rendering(&mut self) {
|
||||||
let descriptor = ImageDescriptor {
|
let descriptor = ImageDescriptor {
|
||||||
size: self.drawtarget.get_size().cast_unit(),
|
size: self.drawtarget.get_size().cast_unit(),
|
||||||
stride: None,
|
stride: None,
|
||||||
|
@ -1444,7 +1221,7 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
|
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
|
||||||
pub fn put_image_data(&mut self, mut imagedata: Vec<u8>, rect: Rect<u64>) {
|
pub(crate) fn put_image_data(&mut self, mut imagedata: Vec<u8>, rect: Rect<u64>) {
|
||||||
assert_eq!(imagedata.len() % 4, 0);
|
assert_eq!(imagedata.len() % 4, 0);
|
||||||
assert_eq!(rect.size.area() as usize, imagedata.len() / 4);
|
assert_eq!(rect.size.area() as usize, imagedata.len() / 4);
|
||||||
pixels::rgba8_byte_swap_and_premultiply_inplace(&mut imagedata);
|
pixels::rgba8_byte_swap_and_premultiply_inplace(&mut imagedata);
|
||||||
|
@ -1459,31 +1236,31 @@ impl<'a> CanvasData<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_shadow_offset_x(&mut self, value: f64) {
|
pub(crate) fn set_shadow_offset_x(&mut self, value: f64) {
|
||||||
self.state.shadow_offset_x = value;
|
self.state.shadow_offset_x = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_shadow_offset_y(&mut self, value: f64) {
|
pub(crate) fn set_shadow_offset_y(&mut self, value: f64) {
|
||||||
self.state.shadow_offset_y = value;
|
self.state.shadow_offset_y = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_shadow_blur(&mut self, value: f64) {
|
pub(crate) fn set_shadow_blur(&mut self, value: f64) {
|
||||||
self.state.shadow_blur = value;
|
self.state.shadow_blur = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_shadow_color(&mut self, value: AbsoluteColor) {
|
pub(crate) fn set_shadow_color(&mut self, value: AbsoluteColor) {
|
||||||
self.backend.set_shadow_color(value, &mut self.state);
|
self.backend.set_shadow_color(value, &mut self.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_font(&mut self, font_style: FontStyleStruct) {
|
pub(crate) fn set_font(&mut self, font_style: FontStyleStruct) {
|
||||||
self.state.font_style = Some(ServoArc::new(font_style))
|
self.state.font_style = Some(ServoArc::new(font_style))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_text_align(&mut self, text_align: TextAlign) {
|
pub(crate) fn set_text_align(&mut self, text_align: TextAlign) {
|
||||||
self.state.text_align = text_align;
|
self.state.text_align = text_align;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_text_baseline(&mut self, text_baseline: TextBaseline) {
|
pub(crate) fn set_text_baseline(&mut self, text_baseline: TextBaseline) {
|
||||||
self.state.text_baseline = text_baseline;
|
self.state.text_baseline = text_baseline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1495,7 +1272,7 @@ impl<'a> CanvasData<'a> {
|
||||||
self.state.shadow_blur != 0.0f64)
|
self.state.shadow_blur != 0.0f64)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> Box<dyn GenericDrawTarget> {
|
fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> B::DrawTarget {
|
||||||
let mut draw_target = self.drawtarget.create_similar_draw_target(&Size2D::new(
|
let mut draw_target = self.drawtarget.create_similar_draw_target(&Size2D::new(
|
||||||
source_rect.size.width as i32,
|
source_rect.size.width as i32,
|
||||||
source_rect.size.height as i32,
|
source_rect.size.height as i32,
|
||||||
|
@ -1509,11 +1286,11 @@ impl<'a> CanvasData<'a> {
|
||||||
|
|
||||||
fn draw_with_shadow<F>(&self, rect: &Rect<f32>, draw_shadow_source: F)
|
fn draw_with_shadow<F>(&self, rect: &Rect<f32>, draw_shadow_source: F)
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut dyn GenericDrawTarget),
|
F: FnOnce(&mut B::DrawTarget),
|
||||||
{
|
{
|
||||||
let shadow_src_rect = self.state.transform.outer_transformed_rect(rect);
|
let shadow_src_rect = self.state.transform.outer_transformed_rect(rect);
|
||||||
let mut new_draw_target = self.create_draw_target_for_shadow(&shadow_src_rect);
|
let mut new_draw_target = self.create_draw_target_for_shadow(&shadow_src_rect);
|
||||||
draw_shadow_source(&mut *new_draw_target);
|
draw_shadow_source(&mut new_draw_target);
|
||||||
self.drawtarget.draw_surface_with_shadow(
|
self.drawtarget.draw_surface_with_shadow(
|
||||||
new_draw_target.snapshot(),
|
new_draw_target.snapshot(),
|
||||||
&Point2D::new(shadow_src_rect.origin.x, shadow_src_rect.origin.y),
|
&Point2D::new(shadow_src_rect.origin.x, shadow_src_rect.origin.y),
|
||||||
|
@ -1531,7 +1308,7 @@ impl<'a> CanvasData<'a> {
|
||||||
/// canvas_size: The size of the canvas we're reading from
|
/// canvas_size: The size of the canvas we're reading from
|
||||||
/// read_rect: The area of the canvas we want to read from
|
/// read_rect: The area of the canvas we want to read from
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn read_pixels(
|
pub(crate) fn read_pixels(
|
||||||
&self,
|
&self,
|
||||||
read_rect: Option<Rect<u64>>,
|
read_rect: Option<Rect<u64>>,
|
||||||
canvas_size: Option<Size2D<u64>>,
|
canvas_size: Option<Size2D<u64>>,
|
||||||
|
@ -1564,7 +1341,7 @@ impl<'a> CanvasData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for CanvasData<'_> {
|
impl<B: Backend> Drop for CanvasData<'_, B> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.compositor_api
|
self.compositor_api
|
||||||
.update_images(vec![ImageUpdate::DeleteImage(self.image_key)]);
|
.update_images(vec![ImageUpdate::DeleteImage(self.image_key)]);
|
||||||
|
@ -1575,20 +1352,21 @@ const HANGING_BASELINE_DEFAULT: f32 = 0.8;
|
||||||
const IDEOGRAPHIC_BASELINE_DEFAULT: f32 = 0.5;
|
const IDEOGRAPHIC_BASELINE_DEFAULT: f32 = 0.5;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CanvasPaintState<'a> {
|
pub(crate) struct CanvasPaintState<'a, B: Backend> {
|
||||||
pub draw_options: DrawOptions,
|
pub(crate) draw_options: B::DrawOptions,
|
||||||
pub fill_style: Pattern<'a>,
|
pub(crate) fill_style: B::Pattern<'a>,
|
||||||
pub stroke_style: Pattern<'a>,
|
pub(crate) stroke_style: B::Pattern<'a>,
|
||||||
pub stroke_opts: StrokeOptions,
|
pub(crate) stroke_opts: B::StrokeOptions,
|
||||||
/// The current 2D transform matrix.
|
/// The current 2D transform matrix.
|
||||||
pub transform: Transform2D<f32>,
|
pub(crate) transform: Transform2D<f32>,
|
||||||
pub shadow_offset_x: f64,
|
pub(crate) shadow_offset_x: f64,
|
||||||
pub shadow_offset_y: f64,
|
pub(crate) shadow_offset_y: f64,
|
||||||
pub shadow_blur: f64,
|
pub(crate) shadow_blur: f64,
|
||||||
pub shadow_color: Color,
|
pub(crate) shadow_color: B::Color,
|
||||||
pub font_style: Option<ServoArc<FontStyleStruct>>,
|
pub(crate) font_style: Option<ServoArc<FontStyleStruct>>,
|
||||||
pub text_align: TextAlign,
|
pub(crate) text_align: TextAlign,
|
||||||
pub text_baseline: TextBaseline,
|
pub(crate) text_baseline: TextBaseline,
|
||||||
|
pub(crate) _backend: PhantomData<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// It writes an image to the destination target
|
/// It writes an image to the destination target
|
||||||
|
@ -1598,14 +1376,14 @@ pub struct CanvasPaintState<'a> {
|
||||||
/// dest_rect: Area of the destination target where the pixels will be copied
|
/// dest_rect: Area of the destination target where the pixels will be copied
|
||||||
/// smoothing_enabled: It determines if smoothing is applied to the image result
|
/// smoothing_enabled: It determines if smoothing is applied to the image result
|
||||||
/// premultiply: Determines whenever the image data should be premultiplied or not
|
/// premultiply: Determines whenever the image data should be premultiplied or not
|
||||||
fn write_image(
|
fn write_image<B: Backend>(
|
||||||
draw_target: &mut dyn GenericDrawTarget,
|
draw_target: &mut B::DrawTarget,
|
||||||
mut image_data: Vec<u8>,
|
mut image_data: Vec<u8>,
|
||||||
image_size: Size2D<f64>,
|
image_size: Size2D<f64>,
|
||||||
dest_rect: Rect<f64>,
|
dest_rect: Rect<f64>,
|
||||||
smoothing_enabled: bool,
|
smoothing_enabled: bool,
|
||||||
premultiply: bool,
|
premultiply: bool,
|
||||||
draw_options: &DrawOptions,
|
draw_options: &B::DrawOptions,
|
||||||
) {
|
) {
|
||||||
if image_data.is_empty() {
|
if image_data.is_empty() {
|
||||||
return;
|
return;
|
||||||
|
@ -1634,25 +1412,11 @@ fn write_image(
|
||||||
draw_target.draw_surface(source_surface, dest_rect, image_rect, filter, draw_options);
|
draw_target.draw_surface(source_surface, dest_rect, image_rect, filter, draw_options);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait RectToi32 {
|
pub(crate) trait RectToi32 {
|
||||||
fn to_i32(&self) -> Rect<i32>;
|
|
||||||
fn ceil(&self) -> Rect<f64>;
|
fn ceil(&self) -> Rect<f64>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RectToi32 for Rect<f64> {
|
impl RectToi32 for Rect<f64> {
|
||||||
fn to_i32(&self) -> Rect<i32> {
|
|
||||||
Rect::new(
|
|
||||||
Point2D::new(
|
|
||||||
self.origin.x.to_i32().unwrap(),
|
|
||||||
self.origin.y.to_i32().unwrap(),
|
|
||||||
),
|
|
||||||
Size2D::new(
|
|
||||||
self.size.width.to_i32().unwrap(),
|
|
||||||
self.size.height.to_i32().unwrap(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ceil(&self) -> Rect<f64> {
|
fn ceil(&self) -> Rect<f64> {
|
||||||
Rect::new(
|
Rect::new(
|
||||||
Point2D::new(self.origin.x.ceil(), self.origin.y.ceil()),
|
Point2D::new(self.origin.x.ceil(), self.origin.y.ceil()),
|
||||||
|
@ -1661,22 +1425,6 @@ impl RectToi32 for Rect<f64> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait RectExt {
|
|
||||||
fn to_u64(&self) -> Rect<u64>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RectExt for Rect<f64> {
|
|
||||||
fn to_u64(&self) -> Rect<u64> {
|
|
||||||
self.cast()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RectExt for Rect<u32> {
|
|
||||||
fn to_u64(&self) -> Rect<u64> {
|
|
||||||
self.cast()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn replace_ascii_whitespace(text: String) -> String {
|
fn replace_ascii_whitespace(text: String) -> String {
|
||||||
text.chars()
|
text.chars()
|
||||||
.map(|c| match c {
|
.map(|c| match c {
|
||||||
|
|
|
@ -11,18 +11,21 @@ use canvas_traits::ConstellationCanvasMsg;
|
||||||
use canvas_traits::canvas::*;
|
use canvas_traits::canvas::*;
|
||||||
use compositing_traits::CrossProcessCompositorApi;
|
use compositing_traits::CrossProcessCompositorApi;
|
||||||
use crossbeam_channel::{Sender, select, unbounded};
|
use crossbeam_channel::{Sender, select, unbounded};
|
||||||
use euclid::default::Size2D;
|
use euclid::default::{Point2D, Rect, Size2D, Transform2D};
|
||||||
use fonts::{FontContext, SystemFontServiceProxy};
|
use fonts::{FontContext, SystemFontServiceProxy};
|
||||||
use ipc_channel::ipc::{self, IpcSender};
|
use ipc_channel::ipc::{self, IpcSender};
|
||||||
use ipc_channel::router::ROUTER;
|
use ipc_channel::router::ROUTER;
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use net_traits::ResourceThreads;
|
use net_traits::ResourceThreads;
|
||||||
|
use style::color::AbsoluteColor;
|
||||||
|
use style::properties::style_structs::Font as FontStyleStruct;
|
||||||
use webrender_api::ImageKey;
|
use webrender_api::ImageKey;
|
||||||
|
|
||||||
use crate::canvas_data::*;
|
use crate::canvas_data::*;
|
||||||
|
use crate::raqote_backend::RaqoteBackend;
|
||||||
|
|
||||||
pub struct CanvasPaintThread<'a> {
|
pub struct CanvasPaintThread<'a> {
|
||||||
canvases: HashMap<CanvasId, CanvasData<'a>>,
|
canvases: HashMap<CanvasId, Canvas<'a>>,
|
||||||
next_canvas_id: CanvasId,
|
next_canvas_id: CanvasId,
|
||||||
compositor_api: CrossProcessCompositorApi,
|
compositor_api: CrossProcessCompositorApi,
|
||||||
font_context: Arc<FontContext>,
|
font_context: Arc<FontContext>,
|
||||||
|
@ -113,10 +116,14 @@ impl<'a> CanvasPaintThread<'a> {
|
||||||
let canvas_id = self.next_canvas_id;
|
let canvas_id = self.next_canvas_id;
|
||||||
self.next_canvas_id.0 += 1;
|
self.next_canvas_id.0 += 1;
|
||||||
|
|
||||||
let canvas_data =
|
let canvas_data = CanvasData::new(
|
||||||
CanvasData::new(size, self.compositor_api.clone(), self.font_context.clone());
|
size,
|
||||||
|
self.compositor_api.clone(),
|
||||||
|
self.font_context.clone(),
|
||||||
|
RaqoteBackend,
|
||||||
|
);
|
||||||
let image_key = canvas_data.image_key();
|
let image_key = canvas_data.image_key();
|
||||||
self.canvases.insert(canvas_id, canvas_data);
|
self.canvases.insert(canvas_id, Canvas::Raqote(canvas_data));
|
||||||
|
|
||||||
(canvas_id, image_key)
|
(canvas_id, image_key)
|
||||||
}
|
}
|
||||||
|
@ -276,7 +283,347 @@ impl<'a> CanvasPaintThread<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn canvas(&mut self, canvas_id: CanvasId) -> &mut CanvasData<'a> {
|
fn canvas(&mut self, canvas_id: CanvasId) -> &mut Canvas<'a> {
|
||||||
self.canvases.get_mut(&canvas_id).expect("Bogus canvas id")
|
self.canvases.get_mut(&canvas_id).expect("Bogus canvas id")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Canvas<'a> {
|
||||||
|
Raqote(CanvasData<'a, RaqoteBackend>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Canvas<'_> {
|
||||||
|
fn set_fill_style(&mut self, style: FillOrStrokeStyle) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_fill_style(style),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.fill(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_text(&mut self, text: String, x: f64, y: f64, max_width: Option<f64>, is_rtl: bool) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.fill_text(text, x, y, max_width, is_rtl),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_rect(&mut self, rect: &Rect<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.fill_rect(rect),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_stroke_style(&mut self, style: FillOrStrokeStyle) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_stroke_style(style),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stroke_rect(&mut self, rect: &Rect<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.stroke_rect(rect),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn begin_path(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.begin_path(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close_path(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.close_path(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_path(&mut self, path: &[PathSegment]) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.fill_path(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stroke(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.stroke(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stroke_path(&mut self, path: &[PathSegment]) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.stroke_path(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clip(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.clip(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_point_in_path(&mut self, x: f64, y: f64, fill_rule: FillRule, chan: IpcSender<bool>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.is_point_in_path(x, y, fill_rule, chan),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_point_in_path_(
|
||||||
|
&mut self,
|
||||||
|
path: &[PathSegment],
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
fill_rule: FillRule,
|
||||||
|
chan: IpcSender<bool>,
|
||||||
|
) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => {
|
||||||
|
canvas_data.is_point_in_path_(path, x, y, fill_rule, chan)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_rect(&mut self, rect: &Rect<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.clear_rect(rect),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_image(
|
||||||
|
&mut self,
|
||||||
|
data: &[u8],
|
||||||
|
size: Size2D<u64>,
|
||||||
|
dest_rect: Rect<f64>,
|
||||||
|
source_rect: Rect<f64>,
|
||||||
|
smoothing_enabled: bool,
|
||||||
|
is_premultiplied: bool,
|
||||||
|
) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.draw_image(
|
||||||
|
data,
|
||||||
|
size,
|
||||||
|
dest_rect,
|
||||||
|
source_rect,
|
||||||
|
smoothing_enabled,
|
||||||
|
is_premultiplied,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_pixels(
|
||||||
|
&mut self,
|
||||||
|
read_rect: Option<Rect<u64>>,
|
||||||
|
canvas_size: Option<Size2D<u64>>,
|
||||||
|
) -> snapshot::Snapshot {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.read_pixels(read_rect, canvas_size),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_to(&mut self, point: &Point2D<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.move_to(point),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn line_to(&mut self, point: &Point2D<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.line_to(point),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rect(&mut self, rect: &Rect<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.rect(rect),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn quadratic_curve_to(&mut self, cp: &Point2D<f32>, pt: &Point2D<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.quadratic_curve_to(cp, pt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bezier_curve_to(&mut self, cp1: &Point2D<f32>, cp2: &Point2D<f32>, pt: &Point2D<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.bezier_curve_to(cp1, cp2, pt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn arc(&mut self, center: &Point2D<f32>, radius: f32, start: f32, end: f32, ccw: bool) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.arc(center, radius, start, end, ccw),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn arc_to(&mut self, cp1: &Point2D<f32>, cp2: &Point2D<f32>, radius: f32) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.arc_to(cp1, cp2, radius),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn ellipse(
|
||||||
|
&mut self,
|
||||||
|
center: &Point2D<f32>,
|
||||||
|
radius_x: f32,
|
||||||
|
radius_y: f32,
|
||||||
|
rotation: f32,
|
||||||
|
start: f32,
|
||||||
|
end: f32,
|
||||||
|
ccw: bool,
|
||||||
|
) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => {
|
||||||
|
canvas_data.ellipse(center, radius_x, radius_y, rotation, start, end, ccw)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn restore_context_state(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.restore_context_state(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_context_state(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.save_context_state(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_line_width(&mut self, width: f32) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_line_width(width),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_line_cap(&mut self, cap: LineCapStyle) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_line_cap(cap),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_line_join(&mut self, join: LineJoinStyle) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_line_join(join),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_miter_limit(&mut self, limit: f32) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_miter_limit(limit),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_line_dash(&mut self, items: Vec<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_line_dash(items),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_line_dash_offset(&mut self, offset: f32) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_line_dash_offset(offset),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_transform(&mut self, matrix: &Transform2D<f32>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_transform(matrix),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_global_alpha(&mut self, alpha: f32) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_global_alpha(alpha),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_global_composition(&mut self, op: CompositionOrBlending) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_global_composition(op),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_shadow_offset_x(&mut self, value: f64) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_shadow_offset_x(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_shadow_offset_y(&mut self, value: f64) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_shadow_offset_y(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_shadow_blur(&mut self, value: f64) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_shadow_blur(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_shadow_color(&mut self, color: AbsoluteColor) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_shadow_color(color),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_font(&mut self, font_style: FontStyleStruct) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_font(font_style),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_text_align(&mut self, text_align: TextAlign) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_text_align(text_align),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_text_baseline(&mut self, text_baseline: TextBaseline) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.set_text_baseline(text_baseline),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn measure_text(&mut self, text: String) -> TextMetrics {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.measure_text(text),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clip_path(&mut self, path: &[PathSegment]) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.clip_path(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_transform(&self) -> Transform2D<f32> {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.get_transform(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn put_image_data(&mut self, unwrap: Vec<u8>, rect: Rect<u64>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.put_image_data(unwrap, rect),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_image_rendering(&mut self) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.update_image_rendering(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recreate(&mut self, size: Option<Size2D<u64>>) {
|
||||||
|
match self {
|
||||||
|
Canvas::Raqote(canvas_data) => canvas_data.recreate(size),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
|
|
||||||
|
mod backend;
|
||||||
mod raqote_backend;
|
mod raqote_backend;
|
||||||
|
|
||||||
pub mod canvas_data;
|
pub mod canvas_data;
|
||||||
|
|
|
@ -17,10 +17,11 @@ use range::Range;
|
||||||
use raqote::PathOp;
|
use raqote::PathOp;
|
||||||
use style::color::AbsoluteColor;
|
use style::color::AbsoluteColor;
|
||||||
|
|
||||||
use crate::canvas_data::{
|
use crate::backend::{
|
||||||
self, Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, Filter, GenericDrawTarget,
|
Backend, DrawOptionsHelpers, GenericDrawTarget, GenericPathBuilder, PathHelpers,
|
||||||
GenericPathBuilder, GradientStop, GradientStops, Path, SourceSurface, StrokeOptions, TextRun,
|
PatternHelpers, StrokeOptionsHelpers,
|
||||||
};
|
};
|
||||||
|
use crate::canvas_data::{CanvasPaintState, Filter, TextRun};
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
/// The shared font cache used by all canvases that render on a thread. It would be nicer
|
/// The shared font cache used by all canvases that render on a thread. It would be nicer
|
||||||
|
@ -30,80 +31,85 @@ thread_local! {
|
||||||
static SHARED_FONT_CACHE: RefCell<HashMap<FontIdentifier, Font>> = RefCell::default();
|
static SHARED_FONT_CACHE: RefCell<HashMap<FontIdentifier, Font>> = RefCell::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct RaqoteBackend;
|
pub(crate) struct RaqoteBackend;
|
||||||
|
|
||||||
impl Backend for RaqoteBackend {
|
impl Backend for RaqoteBackend {
|
||||||
fn get_composition_op(&self, opts: &DrawOptions) -> CompositionOp {
|
type Pattern<'a> = Pattern<'a>;
|
||||||
CompositionOp::Raqote(opts.as_raqote().blend_mode)
|
type StrokeOptions = raqote::StrokeStyle;
|
||||||
|
type Color = raqote::SolidSource;
|
||||||
|
type DrawOptions = raqote::DrawOptions;
|
||||||
|
type CompositionOp = raqote::BlendMode;
|
||||||
|
type CompositionOrBlending = CompositionOrBlending;
|
||||||
|
type DrawTarget = raqote::DrawTarget;
|
||||||
|
type PathBuilder = PathBuilder;
|
||||||
|
type SourceSurface = Vec<u8>; // TODO: See if we can avoid the alloc (probably?)
|
||||||
|
type Path = raqote::Path;
|
||||||
|
type GradientStop = raqote::GradientStop;
|
||||||
|
type GradientStops = Vec<raqote::GradientStop>;
|
||||||
|
|
||||||
|
fn get_composition_op(&self, opts: &Self::DrawOptions) -> Self::CompositionOp {
|
||||||
|
opts.blend_mode
|
||||||
}
|
}
|
||||||
|
|
||||||
fn need_to_draw_shadow(&self, color: &Color) -> bool {
|
fn need_to_draw_shadow(&self, color: &Self::Color) -> bool {
|
||||||
color.as_raqote().a != 0
|
color.a != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_shadow_color(&mut self, color: AbsoluteColor, state: &mut CanvasPaintState<'_>) {
|
fn set_shadow_color(&mut self, color: AbsoluteColor, state: &mut CanvasPaintState<'_, Self>) {
|
||||||
state.shadow_color = Color::Raqote(color.to_raqote_style());
|
state.shadow_color = color.to_raqote_style();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_fill_style(
|
fn set_fill_style(
|
||||||
&mut self,
|
&mut self,
|
||||||
style: FillOrStrokeStyle,
|
style: FillOrStrokeStyle,
|
||||||
state: &mut CanvasPaintState<'_>,
|
state: &mut CanvasPaintState<'_, Self>,
|
||||||
_drawtarget: &dyn GenericDrawTarget,
|
_drawtarget: &Self::DrawTarget,
|
||||||
) {
|
) {
|
||||||
if let Some(pattern) = style.to_raqote_pattern() {
|
if let Some(pattern) = style.to_raqote_pattern() {
|
||||||
state.fill_style = canvas_data::Pattern::Raqote(pattern);
|
state.fill_style = pattern;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_stroke_style(
|
fn set_stroke_style(
|
||||||
&mut self,
|
&mut self,
|
||||||
style: FillOrStrokeStyle,
|
style: FillOrStrokeStyle,
|
||||||
state: &mut CanvasPaintState<'_>,
|
state: &mut CanvasPaintState<'_, Self>,
|
||||||
_drawtarget: &dyn GenericDrawTarget,
|
_drawtarget: &Self::DrawTarget,
|
||||||
) {
|
) {
|
||||||
if let Some(pattern) = style.to_raqote_pattern() {
|
if let Some(pattern) = style.to_raqote_pattern() {
|
||||||
state.stroke_style = canvas_data::Pattern::Raqote(pattern);
|
state.stroke_style = pattern;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_global_composition(
|
fn set_global_composition(
|
||||||
&mut self,
|
&mut self,
|
||||||
op: CompositionOrBlending,
|
op: Self::CompositionOrBlending,
|
||||||
state: &mut CanvasPaintState<'_>,
|
state: &mut CanvasPaintState<'_, Self>,
|
||||||
) {
|
) {
|
||||||
state.draw_options.as_raqote_mut().blend_mode = op.to_raqote_style();
|
state.draw_options.blend_mode = op.to_raqote_style();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_drawtarget(&self, size: Size2D<u64>) -> Box<dyn GenericDrawTarget> {
|
fn create_drawtarget(&self, size: Size2D<u64>) -> Self::DrawTarget {
|
||||||
Box::new(raqote::DrawTarget::new(
|
raqote::DrawTarget::new(size.width as i32, size.height as i32)
|
||||||
size.width as i32,
|
|
||||||
size.height as i32,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recreate_paint_state<'a>(&self, _state: &CanvasPaintState<'a>) -> CanvasPaintState<'a> {
|
fn new_paint_state<'a>(&self) -> CanvasPaintState<'a, Self> {
|
||||||
CanvasPaintState::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for CanvasPaintState<'_> {
|
|
||||||
fn default() -> Self {
|
|
||||||
let pattern = Pattern::Color(255, 0, 0, 0);
|
let pattern = Pattern::Color(255, 0, 0, 0);
|
||||||
CanvasPaintState {
|
CanvasPaintState {
|
||||||
draw_options: DrawOptions::Raqote(raqote::DrawOptions::new()),
|
draw_options: raqote::DrawOptions::new(),
|
||||||
fill_style: canvas_data::Pattern::Raqote(pattern.clone()),
|
fill_style: pattern.clone(),
|
||||||
stroke_style: canvas_data::Pattern::Raqote(pattern),
|
stroke_style: pattern,
|
||||||
stroke_opts: StrokeOptions::Raqote(Default::default()),
|
stroke_opts: Default::default(),
|
||||||
transform: Transform2D::identity(),
|
transform: Transform2D::identity(),
|
||||||
shadow_offset_x: 0.0,
|
shadow_offset_x: 0.0,
|
||||||
shadow_offset_y: 0.0,
|
shadow_offset_y: 0.0,
|
||||||
shadow_blur: 0.0,
|
shadow_blur: 0.0,
|
||||||
shadow_color: Color::Raqote(raqote::SolidSource::from_unpremultiplied_argb(0, 0, 0, 0)),
|
shadow_color: raqote::SolidSource::from_unpremultiplied_argb(0, 0, 0, 0),
|
||||||
font_style: None,
|
font_style: None,
|
||||||
text_align: TextAlign::default(),
|
text_align: TextAlign::default(),
|
||||||
text_baseline: TextBaseline::default(),
|
text_baseline: TextBaseline::default(),
|
||||||
|
_backend: std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,136 +236,122 @@ impl Repetition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl canvas_data::Pattern<'_> {
|
pub fn source<'a>(pattern: &Pattern<'a>) -> raqote::Source<'a> {
|
||||||
pub fn source(&self) -> raqote::Source {
|
match pattern {
|
||||||
|
Pattern::Color(a, r, g, b) => raqote::Source::Solid(
|
||||||
|
raqote::SolidSource::from_unpremultiplied_argb(*a, *r, *g, *b),
|
||||||
|
),
|
||||||
|
Pattern::LinearGradient(pattern) => raqote::Source::new_linear_gradient(
|
||||||
|
pattern.gradient.clone(),
|
||||||
|
pattern.start,
|
||||||
|
pattern.end,
|
||||||
|
raqote::Spread::Pad,
|
||||||
|
),
|
||||||
|
Pattern::RadialGradient(pattern) => raqote::Source::new_two_circle_radial_gradient(
|
||||||
|
pattern.gradient.clone(),
|
||||||
|
pattern.center1,
|
||||||
|
pattern.radius1,
|
||||||
|
pattern.center2,
|
||||||
|
pattern.radius2,
|
||||||
|
raqote::Spread::Pad,
|
||||||
|
),
|
||||||
|
Pattern::Surface(pattern) => raqote::Source::Image(
|
||||||
|
pattern.image,
|
||||||
|
pattern.extend,
|
||||||
|
pattern.filter,
|
||||||
|
pattern.transform,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PatternHelpers for Pattern<'_> {
|
||||||
|
fn is_zero_size_gradient(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
canvas_data::Pattern::Raqote(pattern) => match pattern {
|
Pattern::RadialGradient(pattern) => {
|
||||||
Pattern::Color(a, r, g, b) => raqote::Source::Solid(
|
let centers_equal = pattern.center1 == pattern.center2;
|
||||||
raqote::SolidSource::from_unpremultiplied_argb(*a, *r, *g, *b),
|
let radii_equal = pattern.radius1 == pattern.radius2;
|
||||||
),
|
(centers_equal && radii_equal) || pattern.gradient.stops.is_empty()
|
||||||
Pattern::LinearGradient(pattern) => raqote::Source::new_linear_gradient(
|
|
||||||
pattern.gradient.clone(),
|
|
||||||
pattern.start,
|
|
||||||
pattern.end,
|
|
||||||
raqote::Spread::Pad,
|
|
||||||
),
|
|
||||||
Pattern::RadialGradient(pattern) => raqote::Source::new_two_circle_radial_gradient(
|
|
||||||
pattern.gradient.clone(),
|
|
||||||
pattern.center1,
|
|
||||||
pattern.radius1,
|
|
||||||
pattern.center2,
|
|
||||||
pattern.radius2,
|
|
||||||
raqote::Spread::Pad,
|
|
||||||
),
|
|
||||||
Pattern::Surface(pattern) => raqote::Source::Image(
|
|
||||||
pattern.image,
|
|
||||||
pattern.extend,
|
|
||||||
pattern.filter,
|
|
||||||
pattern.transform,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
}
|
Pattern::LinearGradient(pattern) => {
|
||||||
}
|
(pattern.start == pattern.end) || pattern.gradient.stops.is_empty()
|
||||||
pub fn is_zero_size_gradient(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
canvas_data::Pattern::Raqote(pattern) => match pattern {
|
|
||||||
Pattern::RadialGradient(pattern) => {
|
|
||||||
let centers_equal = pattern.center1 == pattern.center2;
|
|
||||||
let radii_equal = pattern.radius1 == pattern.radius2;
|
|
||||||
(centers_equal && radii_equal) || pattern.gradient.stops.is_empty()
|
|
||||||
},
|
|
||||||
Pattern::LinearGradient(pattern) => {
|
|
||||||
(pattern.start == pattern.end) || pattern.gradient.stops.is_empty()
|
|
||||||
},
|
|
||||||
Pattern::Color(..) | Pattern::Surface(..) => false,
|
|
||||||
},
|
},
|
||||||
|
Pattern::Color(..) | Pattern::Surface(..) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_rect(&self, rect: &Rect<f32>) -> Rect<f32> {
|
||||||
|
match self {
|
||||||
|
Pattern::Surface(pattern) => {
|
||||||
|
let pattern_rect = Rect::new(Point2D::origin(), pattern.size());
|
||||||
|
let mut draw_rect = rect.intersection(&pattern_rect).unwrap_or(Rect::zero());
|
||||||
|
|
||||||
|
match pattern.repetition() {
|
||||||
|
Repetition::NoRepeat => {
|
||||||
|
draw_rect.size.width = draw_rect.size.width.min(pattern_rect.size.width);
|
||||||
|
draw_rect.size.height = draw_rect.size.height.min(pattern_rect.size.height);
|
||||||
|
},
|
||||||
|
Repetition::RepeatX => {
|
||||||
|
draw_rect.size.width = rect.size.width;
|
||||||
|
draw_rect.size.height = draw_rect.size.height.min(pattern_rect.size.height);
|
||||||
|
},
|
||||||
|
Repetition::RepeatY => {
|
||||||
|
draw_rect.size.height = rect.size.height;
|
||||||
|
draw_rect.size.width = draw_rect.size.width.min(pattern_rect.size.width);
|
||||||
|
},
|
||||||
|
Repetition::Repeat => {
|
||||||
|
draw_rect = *rect;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_rect
|
||||||
|
},
|
||||||
|
Pattern::Color(..) | Pattern::LinearGradient(..) | Pattern::RadialGradient(..) => *rect,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StrokeOptions {
|
impl StrokeOptionsHelpers for raqote::StrokeStyle {
|
||||||
pub fn set_line_width(&mut self, _val: f32) {
|
fn set_line_width(&mut self, _val: f32) {
|
||||||
match self {
|
self.width = _val;
|
||||||
StrokeOptions::Raqote(options) => options.width = _val,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn set_miter_limit(&mut self, _val: f32) {
|
fn set_miter_limit(&mut self, _val: f32) {
|
||||||
match self {
|
self.miter_limit = _val;
|
||||||
StrokeOptions::Raqote(options) => options.miter_limit = _val,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn set_line_join(&mut self, val: LineJoinStyle) {
|
fn set_line_join(&mut self, val: LineJoinStyle) {
|
||||||
match self {
|
self.join = val.to_raqote_style();
|
||||||
StrokeOptions::Raqote(options) => options.join = val.to_raqote_style(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn set_line_cap(&mut self, val: LineCapStyle) {
|
fn set_line_cap(&mut self, val: LineCapStyle) {
|
||||||
match self {
|
self.cap = val.to_raqote_style();
|
||||||
StrokeOptions::Raqote(options) => options.cap = val.to_raqote_style(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn set_line_dash(&mut self, items: Vec<f32>) {
|
fn set_line_dash(&mut self, items: Vec<f32>) {
|
||||||
match self {
|
self.dash_array = items;
|
||||||
StrokeOptions::Raqote(options) => options.dash_array = items,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn set_line_dash_offset(&mut self, offset: f32) {
|
fn set_line_dash_offset(&mut self, offset: f32) {
|
||||||
match self {
|
self.dash_offset = offset;
|
||||||
StrokeOptions::Raqote(options) => options.dash_offset = offset,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn as_raqote(&self) -> &raqote::StrokeStyle {
|
|
||||||
match self {
|
|
||||||
StrokeOptions::Raqote(options) => options,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DrawOptions {
|
impl DrawOptionsHelpers for raqote::DrawOptions {
|
||||||
pub fn set_alpha(&mut self, val: f32) {
|
fn set_alpha(&mut self, val: f32) {
|
||||||
match self {
|
self.alpha = val;
|
||||||
DrawOptions::Raqote(draw_options) => draw_options.alpha = val,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn as_raqote(&self) -> &raqote::DrawOptions {
|
|
||||||
match self {
|
|
||||||
DrawOptions::Raqote(options) => options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn as_raqote_mut(&mut self) -> &mut raqote::DrawOptions {
|
|
||||||
match self {
|
|
||||||
DrawOptions::Raqote(options) => options,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Path {
|
impl PathHelpers<RaqoteBackend> for raqote::Path {
|
||||||
pub fn transformed_copy_to_builder(
|
fn transformed_copy_to_builder(&self, transform: &Transform2D<f32>) -> PathBuilder {
|
||||||
&self,
|
PathBuilder(Some(raqote::PathBuilder::from(
|
||||||
transform: &Transform2D<f32>,
|
self.clone().transform(transform),
|
||||||
) -> Box<dyn GenericPathBuilder> {
|
)))
|
||||||
Box::new(PathBuilder(Some(raqote::PathBuilder::from(
|
|
||||||
self.as_raqote().clone().transform(transform),
|
|
||||||
))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn contains_point(&self, x: f64, y: f64, path_transform: &Transform2D<f32>) -> bool {
|
fn contains_point(&self, x: f64, y: f64, path_transform: &Transform2D<f32>) -> bool {
|
||||||
self.as_raqote()
|
self.clone()
|
||||||
.clone()
|
|
||||||
.transform(path_transform)
|
.transform(path_transform)
|
||||||
.contains_point(0.1, x as f32, y as f32)
|
.contains_point(0.1, x as f32, y as f32)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn copy_to_builder(&self) -> Box<dyn GenericPathBuilder> {
|
fn copy_to_builder(&self) -> PathBuilder {
|
||||||
Box::new(PathBuilder(Some(raqote::PathBuilder::from(
|
PathBuilder(Some(raqote::PathBuilder::from(self.clone())))
|
||||||
self.as_raqote().clone(),
|
|
||||||
))))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_raqote(&self) -> &raqote::Path {
|
|
||||||
match self {
|
|
||||||
Path::Raqote(p) => p,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,7 +365,7 @@ fn create_gradient_stops(gradient_stops: Vec<CanvasGradientStop>) -> Vec<raqote:
|
||||||
stops
|
stops
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GenericDrawTarget for raqote::DrawTarget {
|
impl GenericDrawTarget<RaqoteBackend> for raqote::DrawTarget {
|
||||||
fn clear_rect(&mut self, rect: &Rect<f32>) {
|
fn clear_rect(&mut self, rect: &Rect<f32>) {
|
||||||
let mut pb = raqote::PathBuilder::new();
|
let mut pb = raqote::PathBuilder::new();
|
||||||
pb.rect(
|
pb.rect(
|
||||||
|
@ -385,59 +377,47 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
let mut options = raqote::DrawOptions::new();
|
let mut options = raqote::DrawOptions::new();
|
||||||
options.blend_mode = raqote::BlendMode::Clear;
|
options.blend_mode = raqote::BlendMode::Clear;
|
||||||
let pattern = Pattern::Color(0, 0, 0, 0);
|
let pattern = Pattern::Color(0, 0, 0, 0);
|
||||||
GenericDrawTarget::fill(
|
<Self as GenericDrawTarget<RaqoteBackend>>::fill(self, &pb.finish(), pattern, &options);
|
||||||
self,
|
|
||||||
&Path::Raqote(pb.finish()),
|
|
||||||
canvas_data::Pattern::Raqote(pattern),
|
|
||||||
&DrawOptions::Raqote(options),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
fn copy_surface(
|
fn copy_surface(
|
||||||
&mut self,
|
&mut self,
|
||||||
surface: SourceSurface,
|
surface: <RaqoteBackend as Backend>::SourceSurface,
|
||||||
source: Rect<i32>,
|
source: Rect<i32>,
|
||||||
destination: Point2D<i32>,
|
destination: Point2D<i32>,
|
||||||
) {
|
) {
|
||||||
let mut dt = raqote::DrawTarget::new(source.size.width, source.size.height);
|
let mut dt = raqote::DrawTarget::new(source.size.width, source.size.height);
|
||||||
let data = surface.as_raqote();
|
let data = surface;
|
||||||
let s = unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u32, data.len() / 4) };
|
let s = unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u32, data.len() / 4) };
|
||||||
dt.get_data_mut().copy_from_slice(s);
|
dt.get_data_mut().copy_from_slice(s);
|
||||||
raqote::DrawTarget::copy_surface(self, &dt, source.to_box2d(), destination);
|
raqote::DrawTarget::copy_surface(self, &dt, source.to_box2d(), destination);
|
||||||
}
|
}
|
||||||
// TODO(pylbrecht)
|
|
||||||
// Somehow a duplicate of `create_gradient_stops()` with different types.
|
|
||||||
// It feels cumbersome to convert GradientStop back and forth just to use
|
|
||||||
// `create_gradient_stops()`, so I'll leave this here for now.
|
|
||||||
fn create_gradient_stops(&self, gradient_stops: Vec<GradientStop>) -> GradientStops {
|
|
||||||
let mut stops = gradient_stops
|
|
||||||
.into_iter()
|
|
||||||
.map(|item| *item.as_raqote())
|
|
||||||
.collect::<Vec<raqote::GradientStop>>();
|
|
||||||
// https://www.w3.org/html/test/results/2dcontext/annotated-spec/canvas.html#testrefs.2d.gradient.interpolate.overlap
|
|
||||||
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
|
|
||||||
GradientStops::Raqote(stops)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> {
|
fn create_path_builder(&self) -> <RaqoteBackend as Backend>::PathBuilder {
|
||||||
Box::new(PathBuilder::new())
|
PathBuilder::new()
|
||||||
}
|
}
|
||||||
fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Box<dyn GenericDrawTarget> {
|
fn create_similar_draw_target(
|
||||||
Box::new(raqote::DrawTarget::new(size.width, size.height))
|
&self,
|
||||||
|
size: &Size2D<i32>,
|
||||||
|
) -> <RaqoteBackend as Backend>::DrawTarget {
|
||||||
|
raqote::DrawTarget::new(size.width, size.height)
|
||||||
}
|
}
|
||||||
fn create_source_surface_from_data(&self, data: &[u8]) -> Option<SourceSurface> {
|
fn create_source_surface_from_data(
|
||||||
Some(SourceSurface::Raqote(data.to_vec()))
|
&self,
|
||||||
|
data: &[u8],
|
||||||
|
) -> Option<<RaqoteBackend as Backend>::SourceSurface> {
|
||||||
|
Some(data.to_vec())
|
||||||
}
|
}
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
fn draw_surface(
|
fn draw_surface(
|
||||||
&mut self,
|
&mut self,
|
||||||
surface: SourceSurface,
|
surface: <RaqoteBackend as Backend>::SourceSurface,
|
||||||
dest: Rect<f64>,
|
dest: Rect<f64>,
|
||||||
source: Rect<f64>,
|
source: Rect<f64>,
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
draw_options: &DrawOptions,
|
draw_options: &<RaqoteBackend as Backend>::DrawOptions,
|
||||||
) {
|
) {
|
||||||
let surface_data = surface.as_raqote();
|
let surface_data = surface;
|
||||||
let image = raqote::Image {
|
let image = raqote::Image {
|
||||||
width: source.size.width as i32,
|
width: source.size.width as i32,
|
||||||
height: source.size.height as i32,
|
height: source.size.height as i32,
|
||||||
|
@ -470,33 +450,29 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
dest.size.height as f32,
|
dest.size.height as f32,
|
||||||
);
|
);
|
||||||
|
|
||||||
GenericDrawTarget::fill(
|
<Self as GenericDrawTarget<RaqoteBackend>>::fill(self, &pb.finish(), pattern, draw_options);
|
||||||
self,
|
|
||||||
&Path::Raqote(pb.finish()),
|
|
||||||
canvas_data::Pattern::Raqote(pattern),
|
|
||||||
draw_options,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
fn draw_surface_with_shadow(
|
fn draw_surface_with_shadow(
|
||||||
&self,
|
&self,
|
||||||
_surface: SourceSurface,
|
_surface: <RaqoteBackend as Backend>::SourceSurface,
|
||||||
_dest: &Point2D<f32>,
|
_dest: &Point2D<f32>,
|
||||||
_color: &Color,
|
_color: &<RaqoteBackend as Backend>::Color,
|
||||||
_offset: &Vector2D<f32>,
|
_offset: &Vector2D<f32>,
|
||||||
_sigma: f32,
|
_sigma: f32,
|
||||||
_operator: CompositionOp,
|
_operator: <RaqoteBackend as Backend>::CompositionOp,
|
||||||
) {
|
) {
|
||||||
warn!("no support for drawing shadows");
|
warn!("no support for drawing shadows");
|
||||||
}
|
}
|
||||||
fn fill(&mut self, path: &Path, pattern: canvas_data::Pattern, draw_options: &DrawOptions) {
|
fn fill(
|
||||||
match draw_options.as_raqote().blend_mode {
|
&mut self,
|
||||||
|
path: &<RaqoteBackend as Backend>::Path,
|
||||||
|
pattern: <RaqoteBackend as Backend>::Pattern<'_>,
|
||||||
|
draw_options: &<RaqoteBackend as Backend>::DrawOptions,
|
||||||
|
) {
|
||||||
|
match draw_options.blend_mode {
|
||||||
raqote::BlendMode::Src => {
|
raqote::BlendMode::Src => {
|
||||||
self.clear(raqote::SolidSource::from_unpremultiplied_argb(0, 0, 0, 0));
|
self.clear(raqote::SolidSource::from_unpremultiplied_argb(0, 0, 0, 0));
|
||||||
self.fill(
|
self.fill(path, &source(&pattern), draw_options);
|
||||||
path.as_raqote(),
|
|
||||||
&pattern.source(),
|
|
||||||
draw_options.as_raqote(),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
raqote::BlendMode::Clear |
|
raqote::BlendMode::Clear |
|
||||||
raqote::BlendMode::SrcAtop |
|
raqote::BlendMode::SrcAtop |
|
||||||
|
@ -505,26 +481,19 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
raqote::BlendMode::Xor |
|
raqote::BlendMode::Xor |
|
||||||
raqote::BlendMode::DstOver |
|
raqote::BlendMode::DstOver |
|
||||||
raqote::BlendMode::SrcOver => {
|
raqote::BlendMode::SrcOver => {
|
||||||
self.fill(
|
self.fill(path, &source(&pattern), draw_options);
|
||||||
path.as_raqote(),
|
|
||||||
&pattern.source(),
|
|
||||||
draw_options.as_raqote(),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
raqote::BlendMode::SrcIn |
|
raqote::BlendMode::SrcIn |
|
||||||
raqote::BlendMode::SrcOut |
|
raqote::BlendMode::SrcOut |
|
||||||
raqote::BlendMode::DstIn |
|
raqote::BlendMode::DstIn |
|
||||||
raqote::BlendMode::DstAtop => {
|
raqote::BlendMode::DstAtop => {
|
||||||
let mut options = *draw_options.as_raqote();
|
let mut options = *draw_options;
|
||||||
self.push_layer_with_blend(1., options.blend_mode);
|
self.push_layer_with_blend(1., options.blend_mode);
|
||||||
options.blend_mode = raqote::BlendMode::SrcOver;
|
options.blend_mode = raqote::BlendMode::SrcOver;
|
||||||
self.fill(path.as_raqote(), &pattern.source(), &options);
|
self.fill(path, &source(&pattern), &options);
|
||||||
self.pop_layer();
|
self.pop_layer();
|
||||||
},
|
},
|
||||||
_ => warn!(
|
_ => warn!("unrecognized blend mode: {:?}", draw_options.blend_mode),
|
||||||
"unrecognized blend mode: {:?}",
|
|
||||||
draw_options.as_raqote().blend_mode
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -532,8 +501,8 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
&mut self,
|
&mut self,
|
||||||
text_runs: Vec<TextRun>,
|
text_runs: Vec<TextRun>,
|
||||||
start: Point2D<f32>,
|
start: Point2D<f32>,
|
||||||
pattern: &canvas_data::Pattern,
|
pattern: &<RaqoteBackend as Backend>::Pattern<'_>,
|
||||||
draw_options: &DrawOptions,
|
draw_options: &<RaqoteBackend as Backend>::DrawOptions,
|
||||||
) {
|
) {
|
||||||
let mut advance = 0.;
|
let mut advance = 0.;
|
||||||
for run in text_runs.iter() {
|
for run in text_runs.iter() {
|
||||||
|
@ -581,8 +550,8 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
run.font.descriptor.pt_size.to_f32_px(),
|
run.font.descriptor.pt_size.to_f32_px(),
|
||||||
&ids,
|
&ids,
|
||||||
&positions,
|
&positions,
|
||||||
&pattern.source(),
|
&source(pattern),
|
||||||
draw_options.as_raqote(),
|
draw_options,
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -591,8 +560,8 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
fn fill_rect(
|
fn fill_rect(
|
||||||
&mut self,
|
&mut self,
|
||||||
rect: &Rect<f32>,
|
rect: &Rect<f32>,
|
||||||
pattern: canvas_data::Pattern,
|
pattern: <RaqoteBackend as Backend>::Pattern<'_>,
|
||||||
draw_options: Option<&DrawOptions>,
|
draw_options: Option<&<RaqoteBackend as Backend>::DrawOptions>,
|
||||||
) {
|
) {
|
||||||
let mut pb = raqote::PathBuilder::new();
|
let mut pb = raqote::PathBuilder::new();
|
||||||
pb.rect(
|
pb.rect(
|
||||||
|
@ -602,16 +571,16 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
rect.size.height,
|
rect.size.height,
|
||||||
);
|
);
|
||||||
let draw_options = if let Some(options) = draw_options {
|
let draw_options = if let Some(options) = draw_options {
|
||||||
*options.as_raqote()
|
*options
|
||||||
} else {
|
} else {
|
||||||
raqote::DrawOptions::new()
|
raqote::DrawOptions::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
GenericDrawTarget::fill(
|
<Self as GenericDrawTarget<RaqoteBackend>>::fill(
|
||||||
self,
|
self,
|
||||||
&Path::Raqote(pb.finish()),
|
&pb.finish(),
|
||||||
pattern,
|
pattern,
|
||||||
&DrawOptions::Raqote(draw_options),
|
&draw_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
fn get_size(&self) -> Size2D<i32> {
|
fn get_size(&self) -> Size2D<i32> {
|
||||||
|
@ -623,41 +592,36 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
fn pop_clip(&mut self) {
|
fn pop_clip(&mut self) {
|
||||||
self.pop_clip();
|
self.pop_clip();
|
||||||
}
|
}
|
||||||
fn push_clip(&mut self, path: &Path) {
|
fn push_clip(&mut self, path: &<RaqoteBackend as Backend>::Path) {
|
||||||
self.push_clip(path.as_raqote());
|
self.push_clip(path);
|
||||||
}
|
}
|
||||||
fn set_transform(&mut self, matrix: &Transform2D<f32>) {
|
fn set_transform(&mut self, matrix: &Transform2D<f32>) {
|
||||||
self.set_transform(matrix);
|
self.set_transform(matrix);
|
||||||
}
|
}
|
||||||
fn snapshot(&self) -> SourceSurface {
|
fn snapshot(&self) -> <RaqoteBackend as Backend>::SourceSurface {
|
||||||
SourceSurface::Raqote(self.snapshot_data().to_vec())
|
self.snapshot_data().to_vec()
|
||||||
}
|
}
|
||||||
fn stroke(
|
fn stroke(
|
||||||
&mut self,
|
&mut self,
|
||||||
path: &Path,
|
path: &<RaqoteBackend as Backend>::Path,
|
||||||
pattern: canvas_data::Pattern,
|
pattern: Pattern<'_>,
|
||||||
stroke_options: &StrokeOptions,
|
stroke_options: &<RaqoteBackend as Backend>::StrokeOptions,
|
||||||
draw_options: &DrawOptions,
|
draw_options: &<RaqoteBackend as Backend>::DrawOptions,
|
||||||
) {
|
) {
|
||||||
self.stroke(
|
self.stroke(path, &source(&pattern), stroke_options, draw_options);
|
||||||
path.as_raqote(),
|
|
||||||
&pattern.source(),
|
|
||||||
stroke_options.as_raqote(),
|
|
||||||
draw_options.as_raqote(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
fn stroke_line(
|
fn stroke_line(
|
||||||
&mut self,
|
&mut self,
|
||||||
start: Point2D<f32>,
|
start: Point2D<f32>,
|
||||||
end: Point2D<f32>,
|
end: Point2D<f32>,
|
||||||
pattern: canvas_data::Pattern,
|
pattern: <RaqoteBackend as Backend>::Pattern<'_>,
|
||||||
stroke_options: &StrokeOptions,
|
stroke_options: &<RaqoteBackend as Backend>::StrokeOptions,
|
||||||
draw_options: &DrawOptions,
|
draw_options: &<RaqoteBackend as Backend>::DrawOptions,
|
||||||
) {
|
) {
|
||||||
let mut pb = raqote::PathBuilder::new();
|
let mut pb = raqote::PathBuilder::new();
|
||||||
pb.move_to(start.x, start.y);
|
pb.move_to(start.x, start.y);
|
||||||
pb.line_to(end.x, end.y);
|
pb.line_to(end.x, end.y);
|
||||||
let mut stroke_options = stroke_options.as_raqote().clone();
|
let mut stroke_options = stroke_options.clone();
|
||||||
let cap = match stroke_options.join {
|
let cap = match stroke_options.join {
|
||||||
raqote::LineJoin::Round => raqote::LineCap::Round,
|
raqote::LineJoin::Round => raqote::LineCap::Round,
|
||||||
_ => raqote::LineCap::Butt,
|
_ => raqote::LineCap::Butt,
|
||||||
|
@ -666,17 +630,17 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
|
|
||||||
self.stroke(
|
self.stroke(
|
||||||
&pb.finish(),
|
&pb.finish(),
|
||||||
&pattern.source(),
|
&source(&pattern),
|
||||||
&stroke_options,
|
&stroke_options,
|
||||||
draw_options.as_raqote(),
|
draw_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
fn stroke_rect(
|
fn stroke_rect(
|
||||||
&mut self,
|
&mut self,
|
||||||
rect: &Rect<f32>,
|
rect: &Rect<f32>,
|
||||||
pattern: canvas_data::Pattern,
|
pattern: <RaqoteBackend as Backend>::Pattern<'_>,
|
||||||
stroke_options: &StrokeOptions,
|
stroke_options: &<RaqoteBackend as Backend>::StrokeOptions,
|
||||||
draw_options: &DrawOptions,
|
draw_options: &<RaqoteBackend as Backend>::DrawOptions,
|
||||||
) {
|
) {
|
||||||
let mut pb = raqote::PathBuilder::new();
|
let mut pb = raqote::PathBuilder::new();
|
||||||
pb.rect(
|
pb.rect(
|
||||||
|
@ -688,9 +652,9 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||||
|
|
||||||
self.stroke(
|
self.stroke(
|
||||||
&pb.finish(),
|
&pb.finish(),
|
||||||
&pattern.source(),
|
&source(&pattern),
|
||||||
stroke_options.as_raqote(),
|
stroke_options,
|
||||||
draw_options.as_raqote(),
|
draw_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
|
@ -709,7 +673,7 @@ impl Filter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PathBuilder(Option<raqote::PathBuilder>);
|
pub(crate) struct PathBuilder(Option<raqote::PathBuilder>);
|
||||||
|
|
||||||
impl PathBuilder {
|
impl PathBuilder {
|
||||||
fn new() -> PathBuilder {
|
fn new() -> PathBuilder {
|
||||||
|
@ -717,7 +681,7 @@ impl PathBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GenericPathBuilder for PathBuilder {
|
impl GenericPathBuilder<RaqoteBackend> for PathBuilder {
|
||||||
fn arc(
|
fn arc(
|
||||||
&mut self,
|
&mut self,
|
||||||
origin: Point2D<f32>,
|
origin: Point2D<f32>,
|
||||||
|
@ -726,7 +690,8 @@ impl GenericPathBuilder for PathBuilder {
|
||||||
end_angle: f32,
|
end_angle: f32,
|
||||||
anticlockwise: bool,
|
anticlockwise: bool,
|
||||||
) {
|
) {
|
||||||
self.ellipse(
|
<PathBuilder as GenericPathBuilder<RaqoteBackend>>::ellipse(
|
||||||
|
self,
|
||||||
origin,
|
origin,
|
||||||
radius,
|
radius,
|
||||||
radius,
|
radius,
|
||||||
|
@ -840,9 +805,9 @@ impl GenericPathBuilder for PathBuilder {
|
||||||
|
|
||||||
fn get_current_point(&mut self) -> Option<Point2D<f32>> {
|
fn get_current_point(&mut self) -> Option<Point2D<f32>> {
|
||||||
let path = self.finish();
|
let path = self.finish();
|
||||||
self.0 = Some(path.as_raqote().clone().into());
|
self.0 = Some(path.clone().into());
|
||||||
|
|
||||||
path.as_raqote().ops.iter().last().and_then(|op| match op {
|
path.ops.iter().last().and_then(|op| match op {
|
||||||
PathOp::MoveTo(point) | PathOp::LineTo(point) => Some(Point2D::new(point.x, point.y)),
|
PathOp::MoveTo(point) | PathOp::LineTo(point) => Some(Point2D::new(point.x, point.y)),
|
||||||
PathOp::CubicTo(_, _, point) => Some(Point2D::new(point.x, point.y)),
|
PathOp::CubicTo(_, _, point) => Some(Point2D::new(point.x, point.y)),
|
||||||
PathOp::QuadTo(_, point) => Some(Point2D::new(point.x, point.y)),
|
PathOp::QuadTo(_, point) => Some(Point2D::new(point.x, point.y)),
|
||||||
|
@ -864,8 +829,8 @@ impl GenericPathBuilder for PathBuilder {
|
||||||
end_point.y,
|
end_point.y,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
fn finish(&mut self) -> Path {
|
fn finish(&mut self) -> raqote::Path {
|
||||||
Path::Raqote(self.0.take().unwrap().finish())
|
self.0.take().unwrap().finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -977,14 +942,6 @@ impl ToRaqotePattern<'_> for FillOrStrokeStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Color {
|
|
||||||
fn as_raqote(&self) -> &raqote::SolidSource {
|
|
||||||
match self {
|
|
||||||
Color::Raqote(s) => s,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ToRaqoteStyle for AbsoluteColor {
|
impl ToRaqoteStyle for AbsoluteColor {
|
||||||
type Target = raqote::SolidSource;
|
type Target = raqote::SolidSource;
|
||||||
|
|
||||||
|
@ -1054,19 +1011,3 @@ impl ToRaqoteStyle for CompositionStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceSurface {
|
|
||||||
fn as_raqote(&self) -> &Vec<u8> {
|
|
||||||
match self {
|
|
||||||
SourceSurface::Raqote(s) => s,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GradientStop {
|
|
||||||
fn as_raqote(&self) -> &raqote::GradientStop {
|
|
||||||
match self {
|
|
||||||
GradientStop::Raqote(s) => s,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue