File size: 8,885 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use crate::renderer::{RenderParams, format_transform_matrix};
use glam::{DAffine2, DVec2};
use graphene_core::consts::{LAYER_OUTLINE_STROKE_COLOR, LAYER_OUTLINE_STROKE_WEIGHT};
use graphene_core::gradient::{Gradient, GradientType};
use graphene_core::uuid::generate_uuid;
use graphene_core::vector::style::{Fill, PaintOrder, PathStyle, Stroke, StrokeAlign, StrokeCap, StrokeJoin, ViewMode};
use std::fmt::Write;

pub trait RenderExt {
	type Output;
	fn render(
		&self,
		svg_defs: &mut String,
		element_transform: DAffine2,
		stroke_transform: DAffine2,
		bounds: [DVec2; 2],
		transformed_bounds: [DVec2; 2],
		aligned_strokes: bool,
		override_paint_order: bool,
		render_params: &RenderParams,
	) -> Self::Output;
}

impl RenderExt for Gradient {
	type Output = u64;

	// /// Adds the gradient def through mutating the first argument, returning the gradient ID.
	fn render(
		&self,
		svg_defs: &mut String,
		element_transform: DAffine2,
		stroke_transform: DAffine2,
		bounds: [DVec2; 2],
		transformed_bounds: [DVec2; 2],
		_aligned_strokes: bool,
		_override_paint_order: bool,
		_render_params: &RenderParams,
	) -> Self::Output {
		// TODO: Figure out how to use `self.transform` as part of the gradient transform, since that field (`Gradient::transform`) is currently never read from, it's only written to.

		let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
		let transformed_bound_transform = element_transform * DAffine2::from_scale_angle_translation(transformed_bounds[1] - transformed_bounds[0], 0., transformed_bounds[0]);

		let mut stop = String::new();
		for (position, color) in self.stops.0.iter() {
			stop.push_str("<stop");
			if *position != 0. {
				let _ = write!(stop, r#" offset="{}""#, (position * 1_000_000.).round() / 1_000_000.);
			}
			let _ = write!(stop, r##" stop-color="#{}""##, color.to_rgb_hex_srgb_from_gamma());
			if color.a() < 1. {
				let _ = write!(stop, r#" stop-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
			}
			stop.push_str(" />")
		}

		let mod_gradient = if transformed_bound_transform.matrix2.determinant() != 0. {
			transformed_bound_transform.inverse()
		} else {
			DAffine2::IDENTITY // Ignore if the transform cannot be inverted (the bounds are zero). See issue #1944.
		};
		let mod_points = element_transform * stroke_transform * bound_transform;

		let start = mod_points.transform_point2(self.start);
		let end = mod_points.transform_point2(self.end);

		let gradient_id = generate_uuid();

		let matrix = format_transform_matrix(mod_gradient);
		let gradient_transform = if matrix.is_empty() { String::new() } else { format!(r#" gradientTransform="{}""#, matrix) };

		match self.gradient_type {
			GradientType::Linear => {
				let _ = write!(
					svg_defs,
					r#"<linearGradient id="{}" x1="{}" x2="{}" y1="{}" y2="{}"{gradient_transform}>{}</linearGradient>"#,
					gradient_id, start.x, end.x, start.y, end.y, stop
				);
			}
			GradientType::Radial => {
				let radius = (f64::powi(start.x - end.x, 2) + f64::powi(start.y - end.y, 2)).sqrt();
				let _ = write!(
					svg_defs,
					r#"<radialGradient id="{}" cx="{}" cy="{}" r="{}"{gradient_transform}>{}</radialGradient>"#,
					gradient_id, start.x, start.y, radius, stop
				);
			}
		}

		gradient_id
	}
}

impl RenderExt for Fill {
	type Output = String;

	/// Renders the fill, adding necessary defs through mutating the first argument.
	fn render(
		&self,
		svg_defs: &mut String,
		element_transform: DAffine2,
		stroke_transform: DAffine2,
		bounds: [DVec2; 2],
		transformed_bounds: [DVec2; 2],
		aligned_strokes: bool,
		override_paint_order: bool,
		render_params: &RenderParams,
	) -> Self::Output {
		match self {
			Self::None => r#" fill="none""#.to_string(),
			Self::Solid(color) => {
				let mut result = format!(r##" fill="#{}""##, color.to_rgb_hex_srgb_from_gamma());
				if color.a() < 1. {
					let _ = write!(result, r#" fill-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
				}
				result
			}
			Self::Gradient(gradient) => {
				let gradient_id = gradient.render(
					svg_defs,
					element_transform,
					stroke_transform,
					bounds,
					transformed_bounds,
					aligned_strokes,
					override_paint_order,
					render_params,
				);
				format!(r##" fill="url('#{gradient_id}')""##)
			}
		}
	}
}

impl RenderExt for Stroke {
	type Output = String;

	/// Provide the SVG attributes for the stroke.
	fn render(
		&self,
		_svg_defs: &mut String,
		_element_transform: DAffine2,
		_stroke_transform: DAffine2,
		_bounds: [DVec2; 2],
		_transformed_bounds: [DVec2; 2],
		aligned_strokes: bool,
		override_paint_order: bool,
		_render_params: &RenderParams,
	) -> Self::Output {
		// Don't render a stroke at all if it would be invisible
		let Some(color) = self.color else { return String::new() };
		if !self.has_renderable_stroke() {
			return String::new();
		}

		// Set to None if the value is the SVG default
		let weight = (self.weight != 1.).then_some(self.weight);
		let dash_array = (!self.dash_lengths.is_empty()).then_some(self.dash_lengths());
		let dash_offset = (self.dash_offset != 0.).then_some(self.dash_offset);
		let stroke_cap = (self.cap != StrokeCap::Butt).then_some(self.cap);
		let stroke_join = (self.join != StrokeJoin::Miter).then_some(self.join);
		let stroke_join_miter_limit = (self.join_miter_limit != 4.).then_some(self.join_miter_limit);
		let stroke_align = (self.align != StrokeAlign::Center).then_some(self.align);
		let paint_order = (self.paint_order != PaintOrder::StrokeAbove || override_paint_order).then_some(PaintOrder::StrokeBelow);

		// Render the needed stroke attributes
		let mut attributes = format!(r##" stroke="#{}""##, color.to_rgb_hex_srgb_from_gamma());
		if color.a() < 1. {
			let _ = write!(&mut attributes, r#" stroke-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
		}
		if let Some(mut weight) = weight {
			if stroke_align.is_some() && aligned_strokes {
				weight *= 2.;
			}
			let _ = write!(&mut attributes, r#" stroke-width="{}""#, weight);
		}
		if let Some(dash_array) = dash_array {
			let _ = write!(&mut attributes, r#" stroke-dasharray="{}""#, dash_array);
		}
		if let Some(dash_offset) = dash_offset {
			let _ = write!(&mut attributes, r#" stroke-dashoffset="{}""#, dash_offset);
		}
		if let Some(stroke_cap) = stroke_cap {
			let _ = write!(&mut attributes, r#" stroke-linecap="{}""#, stroke_cap.svg_name());
		}
		if let Some(stroke_join) = stroke_join {
			let _ = write!(&mut attributes, r#" stroke-linejoin="{}""#, stroke_join.svg_name());
		}
		if let Some(stroke_join_miter_limit) = stroke_join_miter_limit {
			let _ = write!(&mut attributes, r#" stroke-miterlimit="{}""#, stroke_join_miter_limit);
		}
		// Add vector-effect attribute to make strokes non-scaling
		if self.non_scaling {
			let _ = write!(&mut attributes, r#" vector-effect="non-scaling-stroke""#);
		}
		if paint_order.is_some() {
			let _ = write!(&mut attributes, r#" style="paint-order: stroke;" "#);
		}
		attributes
	}
}

impl RenderExt for PathStyle {
	type Output = String;

	/// Renders the shape's fill and stroke attributes as a string with them concatenated together.
	#[allow(clippy::too_many_arguments)]
	fn render(
		&self,
		svg_defs: &mut String,
		element_transform: DAffine2,
		stroke_transform: DAffine2,
		bounds: [DVec2; 2],
		transformed_bounds: [DVec2; 2],
		aligned_strokes: bool,
		override_paint_order: bool,
		render_params: &RenderParams,
	) -> String {
		let view_mode = render_params.view_mode;
		match view_mode {
			ViewMode::Outline => {
				let fill_attribute = Fill::None.render(
					svg_defs,
					element_transform,
					stroke_transform,
					bounds,
					transformed_bounds,
					aligned_strokes,
					override_paint_order,
					render_params,
				);
				let mut outline_stroke = Stroke::new(Some(LAYER_OUTLINE_STROKE_COLOR), LAYER_OUTLINE_STROKE_WEIGHT);
				// Outline strokes should be non-scaling by default
				outline_stroke.non_scaling = true;
				let stroke_attribute = outline_stroke.render(
					svg_defs,
					element_transform,
					stroke_transform,
					bounds,
					transformed_bounds,
					aligned_strokes,
					override_paint_order,
					render_params,
				);
				format!("{fill_attribute}{stroke_attribute}")
			}
			_ => {
				let fill_attribute = self.fill.render(
					svg_defs,
					element_transform,
					stroke_transform,
					bounds,
					transformed_bounds,
					aligned_strokes,
					override_paint_order,
					render_params,
				);
				let stroke_attribute = self
					.stroke
					.as_ref()
					.map(|stroke| {
						stroke.render(
							svg_defs,
							element_transform,
							stroke_transform,
							bounds,
							transformed_bounds,
							aligned_strokes,
							override_paint_order,
							render_params,
						)
					})
					.unwrap_or_default();
				format!("{fill_attribute}{stroke_attribute}")
			}
		}
	}
}