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
use freya_engine::prelude::*;
use freya_node_state::{
    Border,
    BorderAlignment,
    CornerRadius,
};
use torin::prelude::Area;

pub enum BorderShape {
    DRRect(RRect, RRect),
    Path(Path),
}

pub fn render_border(
    canvas: &Canvas,
    rounded_rect: RRect,
    area: Area,
    border: &Border,
    corner_radius: CornerRadius,
) {
    // Create a new paint
    let mut border_paint = Paint::default();
    border_paint.set_style(PaintStyle::Fill);
    border_paint.set_anti_alias(true);

    border.fill.apply_to_paint(&mut border_paint, area);

    match border_shape(*rounded_rect.rect(), corner_radius, border) {
        BorderShape::DRRect(outer, inner) => {
            canvas.draw_drrect(outer, inner, &border_paint);
        }
        BorderShape::Path(path) => {
            canvas.draw_path(&path, &border_paint);
        }
    }
}

/// Returns a `Path` that will draw a [`Border`] around a base rectangle.
///
/// We don't use Skia's stroking API here, since we might need different widths for each side.
pub fn border_shape(
    base_rect: Rect,
    base_corner_radius: CornerRadius,
    border: &Border,
) -> BorderShape {
    let border_alignment = border.alignment;
    let border_width = border.width;

    // First we create a path that is outset from the rect by a certain amount on each side.
    //
    // Let's call this the outer border path.
    let (outer_rrect, outer_corner_radius) = {
        // Calculuate the outer corner radius for the border.
        let corner_radius = CornerRadius {
            top_left: outer_border_path_corner_radius(
                border_alignment,
                base_corner_radius.top_left,
                border_width.top,
                border_width.left,
            ),
            top_right: outer_border_path_corner_radius(
                border_alignment,
                base_corner_radius.top_right,
                border_width.top,
                border_width.right,
            ),
            bottom_left: outer_border_path_corner_radius(
                border_alignment,
                base_corner_radius.bottom_left,
                border_width.bottom,
                border_width.left,
            ),
            bottom_right: outer_border_path_corner_radius(
                border_alignment,
                base_corner_radius.bottom_right,
                border_width.bottom,
                border_width.right,
            ),
            smoothing: base_corner_radius.smoothing,
        };

        let rrect = RRect::new_rect_radii(
            {
                let mut rect = base_rect;
                let alignment_scale = match border_alignment {
                    BorderAlignment::Outer => 1.0,
                    BorderAlignment::Center => 0.5,
                    BorderAlignment::Inner => 0.0,
                };

                rect.left -= border_width.left * alignment_scale;
                rect.top -= border_width.top * alignment_scale;
                rect.right += border_width.right * alignment_scale;
                rect.bottom += border_width.bottom * alignment_scale;

                rect
            },
            &[
                (corner_radius.top_left, corner_radius.top_left).into(),
                (corner_radius.top_right, corner_radius.top_right).into(),
                (corner_radius.bottom_right, corner_radius.bottom_right).into(),
                (corner_radius.bottom_left, corner_radius.bottom_left).into(),
            ],
        );

        (rrect, corner_radius)
    };

    // After the outer path, we will then move to the inner bounds of the border.
    let (inner_rrect, inner_corner_radius) = {
        // Calculuate the inner corner radius for the border.
        let corner_radius = CornerRadius {
            top_left: inner_border_path_corner_radius(
                border_alignment,
                base_corner_radius.top_left,
                border_width.top,
                border_width.left,
            ),
            top_right: inner_border_path_corner_radius(
                border_alignment,
                base_corner_radius.top_right,
                border_width.top,
                border_width.right,
            ),
            bottom_left: inner_border_path_corner_radius(
                border_alignment,
                base_corner_radius.bottom_left,
                border_width.bottom,
                border_width.left,
            ),
            bottom_right: inner_border_path_corner_radius(
                border_alignment,
                base_corner_radius.bottom_right,
                border_width.bottom,
                border_width.right,
            ),
            smoothing: base_corner_radius.smoothing,
        };

        let rrect = RRect::new_rect_radii(
            {
                let mut rect = base_rect;
                let alignment_scale = match border_alignment {
                    BorderAlignment::Outer => 0.0,
                    BorderAlignment::Center => 0.5,
                    BorderAlignment::Inner => 1.0,
                };

                rect.left += border_width.left * alignment_scale;
                rect.top += border_width.top * alignment_scale;
                rect.right -= border_width.right * alignment_scale;
                rect.bottom -= border_width.bottom * alignment_scale;

                rect
            },
            &[
                (corner_radius.top_left, corner_radius.top_left).into(),
                (corner_radius.top_right, corner_radius.top_right).into(),
                (corner_radius.bottom_right, corner_radius.bottom_right).into(),
                (corner_radius.bottom_left, corner_radius.bottom_left).into(),
            ],
        );

        (rrect, corner_radius)
    };

    if base_corner_radius.smoothing > 0.0 {
        let mut path = Path::new();
        path.set_fill_type(PathFillType::EvenOdd);

        path.add_path(
            &outer_corner_radius.smoothed_path(outer_rrect),
            Point::new(outer_rrect.rect().x(), outer_rrect.rect().y()),
            None,
        );

        path.add_path(
            &inner_corner_radius.smoothed_path(inner_rrect),
            Point::new(inner_rrect.rect().x(), inner_rrect.rect().y()),
            None,
        );

        BorderShape::Path(path)
    } else {
        BorderShape::DRRect(outer_rrect, inner_rrect)
    }
}

fn outer_border_path_corner_radius(
    alignment: BorderAlignment,
    corner_radius: f32,
    width_1: f32,
    width_2: f32,
) -> f32 {
    if alignment == BorderAlignment::Inner || corner_radius == 0.0 {
        return corner_radius;
    }

    let mut offset = if width_1 == 0.0 {
        width_2
    } else if width_2 == 0.0 {
        width_1
    } else {
        width_1.min(width_2)
    };

    if alignment == BorderAlignment::Center {
        offset *= 0.5;
    }

    corner_radius + offset
}

fn inner_border_path_corner_radius(
    alignment: BorderAlignment,
    corner_radius: f32,
    width_1: f32,
    width_2: f32,
) -> f32 {
    if alignment == BorderAlignment::Outer || corner_radius == 0.0 {
        return corner_radius;
    }

    let mut offset = if width_1 == 0.0 {
        width_2
    } else if width_2 == 0.0 {
        width_1
    } else {
        width_1.min(width_2)
    };

    if alignment == BorderAlignment::Center {
        offset *= 0.5;
    }

    corner_radius - offset
}