Newer
Older
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
<?php
namespace Drupal\Core\Layout\Icon;
use Drupal\Component\Utility\Html;
/**
* Builds SVG layout icons.
*/
class SvgIconBuilder implements IconBuilderInterface {
/**
* The machine name of the layout.
*
* @var string
*/
protected $id;
/**
* The label of the layout.
*
* @var string
*/
protected $label;
/**
* The width of the SVG.
*
* @var int
*/
protected $width = 125;
/**
* The height of the SVG.
*
* @var int
*/
protected $height = 150;
/**
* The padding between regions.
*
* @var int
*/
protected $padding = 4;
/**
* The width of region borders.
*
* @var int|null
*/
protected $strokeWidth = 1;
/**
* {@inheritdoc}
*/
public function build(array $icon_map) {
$regions = $this->calculateSvgValues($icon_map, $this->width, $this->height, $this->strokeWidth, $this->padding);
return $this->buildRenderArray($regions, $this->width, $this->height, $this->strokeWidth);
}
/**
* Builds a render array representation of an SVG.
*
* @param mixed[] $regions
* An array keyed by region name, with each element containing the 'height',
* 'width', and 'x' and 'y' offsets of each region.
* @param int $width
* The width of the SVG.
* @param int $height
* The height of the SVG.
* @param int|null $stroke_width
* The width of region borders.
*
* @return array
* A render array representing a SVG icon.
*/
protected function buildRenderArray(array $regions, $width, $height, $stroke_width) {
$build = [
'#type' => 'html_tag',
'#tag' => 'svg',
'#attributes' => [
'width' => $width,
'height' => $height,
'class' => [
'layout-icon',
],
],
];
if ($this->id) {
$build['#attributes']['class'][] = Html::getClass('layout-icon--' . $this->id);
}
if ($this->label) {
$build['title'] = [
'#type' => 'html_tag',
'#tag' => 'title',
'#value' => $this->label,
];
}
// Append each polygon to the SVG.
foreach ($regions as $region => $attributes) {
// Wrapping with a <g> element allows for metadata to exist alongside the
// rectangle.
$build['region'][$region] = [
'#type' => 'html_tag',
'#tag' => 'g',
];
$build['region'][$region]['title'] = [
'#type' => 'html_tag',
'#tag' => 'title',
'#value' => $region,
];
// Assemble the rectangle SVG element.
$build['region'][$region]['rect'] = [
'#type' => 'html_tag',
'#tag' => 'rect',
'#attributes' => [
'x' => $attributes['x'],
'y' => $attributes['y'],
'width' => $attributes['width'],
'height' => $attributes['height'],
'stroke-width' => $stroke_width,
'class' => [
'layout-icon__region',
Html::getClass('layout-icon__region--' . $region),
],
],
];
}
return $build;
}
/**
* Calculates the dimensions and offsets of all regions.
*
* @param string[][] $rows
* A two-dimensional array representing the visual output of the layout. See
* the documentation for the $icon_map parameter of
* \Drupal\Core\Layout\Icon\IconBuilderInterface::build().
* @param int $width
* The width of the SVG.
* @param int $height
* The height of the SVG.
* @param int $stroke_width
* The width of region borders.
* @param int $padding
* The padding between regions.
*
* @return mixed[][]
* An array keyed by region name, with each element containing the 'height',
* 'width', and 'x' and 'y' offsets of each region.
*/
protected function calculateSvgValues(array $rows, $width, $height, $stroke_width, $padding) {
$region_rects = [];
$row_height = $this->getLength(count($rows), $height, $stroke_width, $padding);
foreach ($rows as $row => $cols) {
$column_width = $this->getLength(count($cols), $width, $stroke_width, $padding);
$vertical_offset = $this->getOffset($row, $row_height, $stroke_width, $padding);
foreach ($cols as $col => $region) {
$horizontal_offset = $this->getOffset($col, $column_width, $stroke_width, $padding);
// Check if this region is new, or already exists in the rectangle.
if (!isset($region_rects[$region])) {
$region_rects[$region] = [
'x' => $horizontal_offset,
'y' => $vertical_offset,
'width' => $column_width,
'height' => $row_height,
];
}
else {
// In order to include the area of the previous region and any padding
// or border, subtract the calculated offset from the original offset.
$region_rects[$region]['width'] = $column_width + ($horizontal_offset - $region_rects[$region]['x']);
$region_rects[$region]['height'] = $row_height + ($vertical_offset - $region_rects[$region]['y']);
}
}
}
return $region_rects;
}
/**
* Gets the offset for this region.
*
* @param int $delta
* The zero-based delta of the region.
* @param int $length
* The height or width of the region.
* @param int $stroke_width
* The width of the region borders.
* @param int $padding
* The padding between regions.
*
* @return int
* The offset for this region.
*/
protected function getOffset($delta, $length, $stroke_width, $padding) {
// Half of the stroke width is drawn outside the dimensions.
$stroke_width /= 2;
// For every region in front of this add two strokes, as well as one
// directly in front.
$num_of_strokes = 2 * $delta + 1;
return ($num_of_strokes * $stroke_width) + ($delta * ($length + $padding));
}
/**
* Gets the height or width of a region.
*
* @param int $number_of_regions
* The total number of regions.
* @param int $length
* The total height or width of the icon.
* @param int $stroke_width
* The width of the region borders.
* @param int $padding
* The padding between regions.
*
* @return float|int
* The height or width of a region.
*/
protected function getLength($number_of_regions, $length, $stroke_width, $padding) {
if ($number_of_regions === 0) {
return 0;
}
// Half of the stroke width is drawn outside the dimensions.
$total_stroke = $number_of_regions * $stroke_width;
// Padding does not precede the first region.
$total_padding = ($number_of_regions - 1) * $padding;
// Divide the remaining length by the number of regions.
return ($length - $total_padding - $total_stroke) / $number_of_regions;
}
/**
* {@inheritdoc}
*/
public function setId($id) {
$this->id = $id;
return $this;
}
/**
* {@inheritdoc}
*/
public function setLabel($label) {
$this->label = $label;
return $this;
}
/**
* {@inheritdoc}
*/
public function setWidth($width) {
$this->width = $width;
return $this;
}
/**
* {@inheritdoc}
*/
public function setHeight($height) {
$this->height = $height;
return $this;
}
/**
* {@inheritdoc}
*/
public function setPadding($padding) {
$this->padding = $padding;
return $this;
}
/**
* {@inheritdoc}
*/
public function setStrokeWidth($stroke_width) {
$this->strokeWidth = $stroke_width;
return $this;
}
}