function SvgIconBuilder::calculateSvgValues

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php \Drupal\Core\Layout\Icon\SvgIconBuilder::calculateSvgValues()
  2. 8.9.x core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php \Drupal\Core\Layout\Icon\SvgIconBuilder::calculateSvgValues()
  3. 10 core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php \Drupal\Core\Layout\Icon\SvgIconBuilder::calculateSvgValues()

Calculates the dimensions and offsets of all regions.

Parameters

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().

int $width: The width of the SVG.

int $height: The height of the SVG.

int $stroke_width: The width of region borders.

int $padding: The padding between regions.

Return value

mixed[][] An array keyed by region name, with each element containing the 'height', 'width', and 'x' and 'y' offsets of each region.

1 call to SvgIconBuilder::calculateSvgValues()
SvgIconBuilder::build in core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php

File

core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php, line 159

Class

SvgIconBuilder
Builds SVG layout icons.

Namespace

Drupal\Core\Layout\Icon

Code

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.