FileLink-PsiTransfer/resources/svg2img.ps1
2026-04-20 11:49:03 -05:00

149 lines
4 KiB
PowerShell

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$svgDir = Join-Path $scriptDir "svg"
$outDir = Join-Path $scriptDir "img"
$logoSvg = Join-Path $scriptDir "logo.svg"
$sizes = @(16, 32, 64)
$logoSizes = @(16, 32, 48, 64, 96, 128)
$themes = @{
"light" = "#000000"
"dark" = "#ffffff"
}
$inkscapeCmd = @()
$tmpRoot = if (Test-Path -Path "/tmp") { "/tmp" } else { [System.IO.Path]::GetTempPath() }
$tempDir = Join-Path $tmpRoot ("psitransfer-filelink-svg2img-" + [guid]::NewGuid().ToString())
$tempSvg = Join-Path $tempDir "themed-icon.svg"
if (!(Test-Path -Path $outDir)) {
New-Item -ItemType Directory -Path $outDir | Out-Null
}
if (!(Test-Path -Path $svgDir)) {
New-Item -ItemType Directory -Path $svgDir | Out-Null
}
if (!(Test-Path -Path $tempDir)) {
New-Item -ItemType Directory -Path $tempDir | Out-Null
}
function Remove-TempFiles {
if (Test-Path -Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force
}
}
function Resolve-InkscapeCommand {
$nativeInkscape = Get-Command "inkscape" -ErrorAction SilentlyContinue
if ($nativeInkscape) {
return @($nativeInkscape.Source)
}
$flatpak = Get-Command "flatpak" -ErrorAction SilentlyContinue
if ($flatpak) {
& $flatpak.Source info org.inkscape.Inkscape *> $null
if ($LASTEXITCODE -eq 0) {
return @(
$flatpak.Source,
"run",
"--filesystem=$scriptDir",
"--filesystem=/tmp",
"--command=inkscape",
"org.inkscape.Inkscape"
)
}
}
throw "Inkscape CLI is not installed or not in PATH, and Flatpak Inkscape (org.inkscape.Inkscape) was not found."
}
function Export-Png {
param (
[string] $InputSvg,
[string] $OutFile,
[int] $Size
)
Write-Host "Exporting $OutFile..."
Invoke-Inkscape @(
$InputSvg,
"--export-type=png",
"--export-filename=$OutFile",
"--export-width=$Size",
"--export-height=$Size",
"--actions=export-do"
)
}
function Invoke-Inkscape {
param (
[string[]] $Arguments
)
& $inkscapeCmd[0] @($inkscapeCmd | Select-Object -Skip 1) @Arguments
}
function Inject-Color {
param (
[string] $Original,
[string] $Color
)
$content = Get-Content $Original -Raw
if ($content -match '<svg[^>]*>') {
$patched = $content -replace '<svg([^>]*?)>', "<svg`$1 style=`"color: $Color`">"
Set-Content -Path $tempSvg -Value $patched
}
else {
throw "Couldn't find <svg> tag to patch."
}
}
try {
$inkscapeCmd = Resolve-InkscapeCommand
if (Test-Path -Path $logoSvg) {
foreach ($size in $logoSizes) {
Export-Png -InputSvg $logoSvg -OutFile (Join-Path $outDir "logo$size.png") -Size $size
}
Export-Png -InputSvg $logoSvg -OutFile (Join-Path $outDir "logo.png") -Size 512
}
else {
Write-Warning "Logo source not found: $logoSvg"
}
$svgFiles = Get-ChildItem -Path $svgDir -Filter *.svg -ErrorAction SilentlyContinue
if (-not $svgFiles) {
Write-Host "No themed SVG icons found in $svgDir. Logo exports completed."
exit 0
}
foreach ($svgFile in $svgFiles) {
$svgPath = $svgFile.FullName
$baseName = $svgFile.BaseName
foreach ($theme in $themes.Keys) {
$color = $themes[$theme]
Inject-Color -Original $svgPath -Color $color
foreach ($size in $sizes) {
$outFile = Join-Path $outDir "$baseName-$theme-$size.png"
Write-Host "Exporting $outFile (color $color)..."
Invoke-Inkscape @(
$tempSvg,
"--export-type=png",
"--export-filename=$outFile",
"--export-width=$size",
"--export-height=$size",
"--actions=export-do"
)
}
}
}
Write-Host "Done generating logo PNGs and light/dark themed icon PNGs."
}
finally {
Remove-TempFiles
}