Import SVG rendering tool

This commit is contained in:
Lagg1ng committed 2026-11-28 16:39:40 +00:00
1 parent 6bf57afc73
commit ac4c010c71
1 file changed
+118
+118
View File
@@ -0,0 +1,118 @@
param(
[string]$InputPath,
[string]$OutputPath
)
$ErrorActionPreference = 'Stop'
$projectRoot = Split-Path -Parent $PSScriptRoot
if (-not $InputPath) { $InputPath = Join-Path $projectRoot 'build\workload.svg' }
if (-not $OutputPath) { $OutputPath = Join-Path $projectRoot 'build\workload.png' }
if (-not [System.IO.Path]::IsPathRooted($InputPath)) { $InputPath = Join-Path $projectRoot $InputPath }
if (-not [System.IO.Path]::IsPathRooted($OutputPath)) { $OutputPath = Join-Path $projectRoot $OutputPath }
Add-Type -AssemblyName System.Drawing
[xml]$document = Get-Content -LiteralPath $InputPath -Raw -Encoding UTF8
$svg = $document.DocumentElement
$viewBox = $svg.GetAttribute('viewBox').Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
$imageWidth = [int]$viewBox[2]
$imageHeight = [int]$viewBox[3]
$bitmap = [System.Drawing.Bitmap]::new($imageWidth, $imageHeight)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
$graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAlias
function Get-AttrNumber {
param([System.Xml.XmlElement]$Element, [string]$Name, [float]$Default = 0)
$value = $Element.GetAttribute($Name)
if (-not $value) { return $Default }
return [float]::Parse($value, [System.Globalization.CultureInfo]::InvariantCulture)
}
function Get-AttrColor {
param([System.Xml.XmlElement]$Element, [string]$Name, [string]$Default = '#000000')
$value = $Element.GetAttribute($Name)
if (-not $value -or $value -eq 'none') { $value = $Default }
if ($value -match '^hsl\((\d+),\s*(\d+)%,\s*(\d+)%\)$') {
$hue = ([double]$Matches[1]) % 360.0
$saturation = [double]$Matches[2] / 100.0
$lightness = [double]$Matches[3] / 100.0
$chroma = (1.0 - [Math]::Abs(2.0 * $lightness - 1.0)) * $saturation
$x = $chroma * (1.0 - [Math]::Abs((($hue / 60.0) % 2.0) - 1.0))
$match = $lightness - $chroma / 2.0
if ($hue -lt 60) { $rgb = @($chroma, $x, 0.0) }
elseif ($hue -lt 120) { $rgb = @($x, $chroma, 0.0) }
elseif ($hue -lt 180) { $rgb = @(0.0, $chroma, $x) }
elseif ($hue -lt 240) { $rgb = @(0.0, $x, $chroma) }
elseif ($hue -lt 300) { $rgb = @($x, 0.0, $chroma) }
else { $rgb = @($chroma, 0.0, $x) }
$red = [int][Math]::Round(($rgb[0] + $match) * 255.0)
$green = [int][Math]::Round(($rgb[1] + $match) * 255.0)
$blue = [int][Math]::Round(($rgb[2] + $match) * 255.0)
return [System.Drawing.Color]::FromArgb($red, $green, $blue)
}
return [System.Drawing.ColorTranslator]::FromHtml($value)
}
try {
$graphics.Clear([System.Drawing.ColorTranslator]::FromHtml('#f5f1e8'))
$rectangles = @($svg.ChildNodes | Where-Object { $_.LocalName -eq 'rect' })
$mapRect = $rectangles[1]
$mapWidth = Get-AttrNumber $mapRect 'width'
$mapHeight = Get-AttrNumber $mapRect 'height'
$mapBackground = [System.Drawing.SolidBrush]::new((Get-AttrColor $mapRect 'fill' '#e8e0cf'))
$graphics.FillRectangle($mapBackground, 0, 0, $mapWidth, $mapHeight)
$mapBackground.Dispose()
$gridPen = [System.Drawing.Pen]::new([System.Drawing.ColorTranslator]::FromHtml('#d2c8b4'), 0.55)
for ($x = 0; $x -le $mapWidth; $x += 10) { $graphics.DrawLine($gridPen, $x, 0, $x, $mapHeight) }
for ($y = 0; $y -le $mapHeight; $y += 10) { $graphics.DrawLine($gridPen, 0, $y, $mapWidth, $y) }
$gridPen.Dispose()
foreach ($element in $svg.ChildNodes) {
if ($element -isnot [System.Xml.XmlElement]) { continue }
switch ($element.LocalName) {
'rect' {
$fill = $element.GetAttribute('fill')
if ($fill -eq '#5b6470' -or $fill -eq '#4b9b67' -or $fill -eq '#e6a23c' -or $fill -eq '#a9adaf') {
$brush = [System.Drawing.SolidBrush]::new((Get-AttrColor $element 'fill'))
$graphics.FillRectangle($brush, (Get-AttrNumber $element 'x'), (Get-AttrNumber $element 'y'), (Get-AttrNumber $element 'width'), (Get-AttrNumber $element 'height'))
$brush.Dispose()
}
}
'polyline' {
$parts = $element.GetAttribute('points').Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
if ($parts.Count -lt 2) { continue }
$points = [System.Drawing.PointF[]]::new($parts.Count)
for ($i = 0; $i -lt $parts.Count; $i++) {
$xy = $parts[$i].Split(',')
$points[$i] = [System.Drawing.PointF]::new(
[float]::Parse($xy[0], [System.Globalization.CultureInfo]::InvariantCulture),
[float]::Parse($xy[1], [System.Globalization.CultureInfo]::InvariantCulture))
}
$pen = [System.Drawing.Pen]::new((Get-AttrColor $element 'stroke'), (Get-AttrNumber $element 'stroke-width' 1))
$pen.LineJoin = [System.Drawing.Drawing2D.LineJoin]::Round
$graphics.DrawLines($pen, $points)
$pen.Dispose()
}
'circle' {
$radius = Get-AttrNumber $element 'r'
$brush = [System.Drawing.SolidBrush]::new((Get-AttrColor $element 'fill'))
$graphics.FillEllipse($brush, (Get-AttrNumber $element 'cx') - $radius, (Get-AttrNumber $element 'cy') - $radius, 2 * $radius, 2 * $radius)
$brush.Dispose()
}
'text' {
$font = [System.Drawing.Font]::new('Arial', (Get-AttrNumber $element 'font-size' 12))
$brush = [System.Drawing.SolidBrush]::new((Get-AttrColor $element 'fill'))
$graphics.DrawString($element.InnerText, $font, $brush, (Get-AttrNumber $element 'x'), (Get-AttrNumber $element 'y') - $font.GetHeight($graphics))
$font.Dispose()
$brush.Dispose()
}
}
}
$bitmap.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Png)
Write-Output "PNG written: $OutputPath"
} finally {
$graphics.Dispose()
$bitmap.Dispose()
}