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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Gradient specifications.
//!
//! We provide both linear and radial gradients; and for each flavor
//! we provide two representations, a 'generic' representation that uses
//! points in the [unit square], and a 'fixed' representation that uses
//! image-space coordinates.
//!
//! The generic representations ([`LinearGradient`] and [`RadialGradient`])
//! are useful for cases such as UI, when the same gradient may be reused
//! with different shapes. The fixed representations
//! ([`FixedLinearGradient`] and [`FixedRadialGradient`]) may be better suited
//! to working with content in existing formats such as SVG. A fixed gradient
//! can be generated from a generic gradient by mapping points from the unit
//! square onto any arbitrary rectangle.
//!
//! The fixed variants are provided because they more closely match the types
//! used by many 2d graphics APIs; but you can use the generic representations
//! anywhere you can use the fixed ones, and they will be automatically
//! resolved appropriately.
//!
//! [`LinearGradient`]: struct.LinearGradient.html
//! [`RadialGradient`]: struct.RadialGradient.html
//! [`FixedLinearGradient`]: struct.FixedLinearGradient.html
//! [`FixedRadialGradient`]: struct.FixedRadialGradient.html
//! [unit square]: https://en.wikipedia.org/wiki/Unit_square

use std::borrow::Cow;

use kurbo::{Point, Rect, Size, Vec2};

use crate::{IntoBrush, RenderContext};

use crate::Color;

/// Specification of a linear gradient.
///
/// This specification is in terms of image-space coordinates. In many
/// cases, it is better to specify coordinates relative to the `Rect`
/// of the item being drawn; for these, use [`LinearGradient`] instead.
///
/// [`LinearGradient`]: struct.LinearGradient.html
#[derive(Debug, Clone)]
pub struct FixedLinearGradient {
    /// The start point (corresponding to pos 0.0).
    pub start: Point,
    /// The end point (corresponding to pos 1.0).
    pub end: Point,
    /// The stops.
    ///
    /// There must be at least two for the gradient to be valid.
    pub stops: Vec<GradientStop>,
}

/// Specification of a radial gradient in image-space.
///
/// This specification is in terms of image-space coordinates. In many
/// cases, it is better to specify coordinates relative to the `Rect`
/// of the item being drawn; for these, use [`RadialGradient`] instead.
///
/// [`RadialGradient`]: struct.RadialGradient.html
#[derive(Debug, Clone)]
pub struct FixedRadialGradient {
    /// The center.
    pub center: Point,
    /// The offset of the origin relative to the center.
    pub origin_offset: Vec2,
    /// The radius.
    ///
    /// The circle with this radius from the center corresponds to pos 1.0.
    pub radius: f64,
    /// The stops (see similar field in [`LinearGradient`](struct.LinearGradient.html)).
    pub stops: Vec<GradientStop>,
}

/// Any fixed gradient.
///
/// This is provided as a convenience, so that we can provide API that
/// accept both [`FixedLinearGradient`] and [`FixedRadialGradient`].
/// You should not construct this type dirctly; rather construct one of those
/// types, both of which impl `Into<FixedGradient>`.
///
/// [`FixedLinearGradient`]: struct.FixedLinearGradient.html
/// [`FixedRadialGradient`]: struct.FixedRadialGradient.html
#[derive(Debug, Clone)]
pub enum FixedGradient {
    /// A linear gradient.
    Linear(FixedLinearGradient),
    /// A radial gradient.
    Radial(FixedRadialGradient),
}

/// Specification of a gradient stop.
#[derive(Debug, Clone)]
pub struct GradientStop {
    /// The coordinate of the stop.
    pub pos: f32,
    /// The color at that stop.
    pub color: Color,
}

/// A flexible, ergonomic way to describe gradient stops.
pub trait GradientStops {
    fn to_vec(self) -> Vec<GradientStop>;
}

/// A description of a linear gradient in the unit rect, which can be resolved
/// to a fixed gradient.
///
/// The start and end points in the gradient are given in [`UnitPoint`] coordinates,
/// which are then resolved to image-space coordinates for any given concrete `Rect`.
///
/// When the fixed coordinates are known, use [`FixedLinearGradient`] instead.
///
/// [`UnitPoint`]: struct.UnitPoint.html
/// [`FixedLinearGradient`]: struct.FixedLinearGradient.html
#[derive(Debug, Clone)]
pub struct LinearGradient {
    start: UnitPoint,
    end: UnitPoint,
    stops: Vec<GradientStop>,
}

/// A description of a radial gradient in the unit rect, which can be resolved
/// to a fixed gradient.
///
/// The `center` and `origin` are given in [`UnitPoint`] coordinates.
///
/// The `center` parameter specifies the `center` of the circle, so that points
/// of distance radius from the `center` map to 1.0 in the gradient stops.
/// The `origin` parameter specifies the point that maps to 0.0 in the
/// gradient stops. Using the same `origin` and `center` gives a radially
/// symmetric gradient; using different points is useful for simulating
/// lighting effects. See [configuring a radial gradient][config] for a fuller
/// explanation.
///
/// By default, `origin` and `center` are both at the center (0.5, 0.5) point.
/// This can be changed during construction with the [`with_center`] and
/// [`with_origin`] builder methods.
///
/// The [`ScaleMode`] describes how the gradient is mapped to a non-square
/// rectangle; by default this will expand on the longest axis, but this can
/// be changed with the [`with_scale_mode`] builder method.
///
/// [config]: https://docs.microsoft.com/en-us/windows/win32/direct2d/direct2d-brushes-overview#configuring-a-radial-gradient
/// [`UnitPoint`]: struct.UnitPoint.html
/// [`ScaleMode`]: enum.ScaleMode.html
/// [`with_center`]: struct.RadialGradient.html#method.with_center
/// [`with_origin`]: struct.RadialGradient.html#method.with_origin
/// [`with_scale_mode`]: struct.RadialGradient.html#method.with_scale_mode
#[derive(Debug, Clone)]
pub struct RadialGradient {
    center: UnitPoint,
    origin: UnitPoint,
    radius: f64,
    stops: Vec<GradientStop>,
    scale_mode: ScaleMode,
}

/// Mappings from the unit square into a non-square rectangle.
#[derive(Debug, Clone)]
pub enum ScaleMode {
    /// The unit 1.0 is mapped to the smaller of width & height, but the mapped
    /// item may not cover the entire rectangle.
    Fit,
    /// The unit 1.0 is mapped to the larger of width & height; some
    /// values on the other axis may extend outside the target rectangle.
    Fill,
}

/// A representation of a point relative to a unit rectangle.
#[derive(Debug, Clone, Copy)]
pub struct UnitPoint {
    u: f64,
    v: f64,
}

impl GradientStops for Vec<GradientStop> {
    fn to_vec(self) -> Vec<GradientStop> {
        self
    }
}

impl<'a> GradientStops for &'a [GradientStop] {
    fn to_vec(self) -> Vec<GradientStop> {
        self.to_owned()
    }
}

// Generate equally-spaced stops.
impl<'a> GradientStops for &'a [Color] {
    fn to_vec(self) -> Vec<GradientStop> {
        if self.is_empty() {
            Vec::new()
        } else {
            let denom = (self.len() - 1).max(1) as f32;
            self.iter()
                .enumerate()
                .map(|(i, c)| GradientStop {
                    pos: (i as f32) / denom,
                    color: c.to_owned(),
                })
                .collect()
        }
    }
}

impl<'a> GradientStops for (Color, Color) {
    fn to_vec(self) -> Vec<GradientStop> {
        let stops: &[Color] = &[self.0, self.1];
        GradientStops::to_vec(stops)
    }
}

impl<'a> GradientStops for (Color, Color, Color) {
    fn to_vec(self) -> Vec<GradientStop> {
        let stops: &[Color] = &[self.0, self.1, self.2];
        GradientStops::to_vec(stops)
    }
}

impl<'a> GradientStops for (Color, Color, Color, Color) {
    fn to_vec(self) -> Vec<GradientStop> {
        let stops: &[Color] = &[self.0, self.1, self.2, self.3];
        GradientStops::to_vec(stops)
    }
}

impl<'a> GradientStops for (Color, Color, Color, Color, Color) {
    fn to_vec(self) -> Vec<GradientStop> {
        let stops: &[Color] = &[self.0, self.1, self.2, self.3, self.4];
        GradientStops::to_vec(stops)
    }
}

impl<'a> GradientStops for (Color, Color, Color, Color, Color, Color) {
    fn to_vec(self) -> Vec<GradientStop> {
        let stops: &[Color] = &[self.0, self.1, self.2, self.3, self.4, self.5];
        GradientStops::to_vec(stops)
    }
}

impl UnitPoint {
    pub const TOP_LEFT: UnitPoint = UnitPoint::new(0.0, 0.0);
    pub const TOP: UnitPoint = UnitPoint::new(0.5, 0.0);
    pub const TOP_RIGHT: UnitPoint = UnitPoint::new(1.0, 0.0);
    pub const LEFT: UnitPoint = UnitPoint::new(0.0, 0.5);
    pub const CENTER: UnitPoint = UnitPoint::new(0.5, 0.5);
    pub const RIGHT: UnitPoint = UnitPoint::new(1.0, 0.5);
    pub const BOTTOM_LEFT: UnitPoint = UnitPoint::new(0.0, 1.0);
    pub const BOTTOM: UnitPoint = UnitPoint::new(0.5, 1.0);
    pub const BOTTOM_RIGHT: UnitPoint = UnitPoint::new(1.0, 1.0);

    /// Create a new UnitPoint.
    ///
    /// The `u` and `v` coordinates describe the point, with (0.0, 0.0) being
    /// the top-left, and (1.0, 1.0) being the bottom-right.
    pub const fn new(u: f64, v: f64) -> UnitPoint {
        UnitPoint { u, v }
    }

    /// Given a rectangle, resolve the point within the rectangle.
    pub fn resolve(self, rect: Rect) -> Point {
        Point::new(
            rect.x0 + self.u * (rect.x1 - rect.x0),
            rect.y0 + self.v * (rect.y1 - rect.y0),
        )
    }
}

impl LinearGradient {
    /// Create a new linear gradient.
    ///
    /// The `start` and `end` coordinates are [`UnitPoint`] coordinates, relative
    /// to the geometry of the shape being drawn.
    ///
    /// # Examples
    ///
    /// ```
    /// use piet::{Color, RenderContext, LinearGradient, UnitPoint};
    /// use piet::kurbo::{Circle, Point};
    ///
    /// # let mut render_ctx = piet::NullRenderContext::new();
    /// let circle = Circle::new(Point::new(100.0, 100.0), 50.0);
    /// let gradient = LinearGradient::new(
    ///     UnitPoint::TOP,
    ///     UnitPoint::BOTTOM,
    ///     (Color::WHITE, Color::BLACK)
    /// );
    /// render_ctx.fill(circle, &gradient);
    /// ```
    ///
    /// [`UnitPoint`]: struct.UnitPoint.html
    pub fn new(start: UnitPoint, end: UnitPoint, stops: impl GradientStops) -> LinearGradient {
        LinearGradient {
            start,
            end,
            stops: stops.to_vec(),
        }
    }

    // maybe these should be public API? that was my original intention but I'm not
    // sure there's a clear use, so keeping them private for now.
    /// Generate a [`FixedLinearGradient`] by mapping points in the unit square
    /// onto points in `rect`.
    ///
    /// [`FixedLinearGradient`]: struct.FixedLinearGradient.html
    fn resolve(&self, rect: Rect) -> FixedLinearGradient {
        FixedLinearGradient {
            start: self.start.resolve(rect),
            end: self.end.resolve(rect),
            stops: self.stops.clone(),
        }
    }
}

impl RadialGradient {
    /// Creates a simple `RadialGradient`. This gradient has `origin` and `center`
    /// set to `(0.5, 0.5)`, and uses the `Fill` [`ScaleMode`]. These attributes can be
    /// modified with the [`with_center`], [`with_origin`],
    /// and [`with_scale_mode`] builder methods.
    ///
    /// [`ScaleMode`]: enum.ScaleMode.html
    /// [`with_center`]: struct.RadialGradient.html#method.with_center
    /// [`with_origin`]: struct.RadialGradient.html#method.with_origin
    /// [`with_scale_mode`]: struct.RadialGradient.html#method.with_scale_mode
    pub fn new(radius: f64, stops: impl GradientStops) -> Self {
        RadialGradient {
            center: UnitPoint::CENTER,
            origin: UnitPoint::CENTER,
            radius,
            stops: stops.to_vec(),
            scale_mode: ScaleMode::Fill,
        }
    }

    /// A builder-style method for changing the center of the gradient. This
    /// does not change the `origin`; in the common case, you will want to change
    /// both values.
    ///
    /// See the main [`RadialGradient`] docs for an explanation of center vs. origin.
    ///
    /// [`RadialGradient`]: struct.RadialGradient.html
    pub fn with_center(mut self, center: UnitPoint) -> Self {
        self.center = center;
        self
    }

    /// A builder-style method for changing the origin of the gradient.
    ///
    /// See the main [`RadialGradient`] docs for an explanation of center vs. origin.
    ///
    /// [`RadialGradient`]: struct.RadialGradient.html
    pub fn with_origin(mut self, origin: UnitPoint) -> Self {
        self.origin = origin;
        self
    }

    /// A builder-style method for changing the [`ScaleMode`] of the gradient.
    ///
    /// [`ScaleMode`]: enum.ScaleMode.html
    pub fn with_scale_mode(mut self, scale_mode: ScaleMode) -> Self {
        self.scale_mode = scale_mode;
        self
    }

    /// Generate a [`FixedRadialGradient`] by mapping points in the unit square
    /// onto points in `rect`.
    ///
    /// [`FixedRadialGradient`]: struct.FixedRadialGradient.html
    fn resolve(&self, rect: Rect) -> FixedRadialGradient {
        let scale_len = match self.scale_mode {
            ScaleMode::Fill => rect.width().max(rect.height()),
            ScaleMode::Fit => rect.width().min(rect.height()),
        };

        let rect = equalize_sides_preserving_center(rect, scale_len);
        let center = self.center.resolve(rect);
        let origin = self.origin.resolve(rect);
        let origin_offset = origin - center;
        let radius = self.radius * scale_len;
        FixedRadialGradient {
            center,
            origin_offset,
            radius,
            stops: self.stops.clone(),
        }
    }
}

impl From<FixedLinearGradient> for FixedGradient {
    fn from(src: FixedLinearGradient) -> FixedGradient {
        FixedGradient::Linear(src)
    }
}

impl From<FixedRadialGradient> for FixedGradient {
    fn from(src: FixedRadialGradient) -> FixedGradient {
        FixedGradient::Radial(src)
    }
}

impl<P: RenderContext> IntoBrush<P> for FixedGradient {
    fn make_brush<'a>(&'a self, piet: &mut P, _bbox: impl FnOnce() -> Rect) -> Cow<'a, P::Brush> {
        // Also, at some point we might want to be smarter about the extra clone here.
        Cow::Owned(
            piet.gradient(self.to_owned())
                .expect("error creating gradient"),
        )
    }
}

impl<P: RenderContext> IntoBrush<P> for LinearGradient {
    fn make_brush<'a>(&'a self, piet: &mut P, bbox: impl FnOnce() -> Rect) -> Cow<'a, P::Brush> {
        let rect = bbox();
        let gradient = self.resolve(rect);
        // Perhaps the make_brush method should be fallible instead of panicking.
        Cow::Owned(piet.gradient(gradient).expect("error creating gradient"))
    }
}

impl<P: RenderContext> IntoBrush<P> for RadialGradient {
    fn make_brush<'a>(&'a self, piet: &mut P, bbox: impl FnOnce() -> Rect) -> Cow<'a, P::Brush> {
        let rect = bbox();
        let gradient = self.resolve(rect);
        // Perhaps the make_brush method should be fallible instead of panicking.
        Cow::Owned(piet.gradient(gradient).expect("error creating gradient"))
    }
}

fn equalize_sides_preserving_center(rect: Rect, new_len: f64) -> Rect {
    let size = Size::new(new_len, new_len);
    let origin = rect.center() - size.to_vec2() / 2.;
    Rect::from_origin_size(origin, size)
}