Files
ExecuteR/scripts/Generate-Icons.ps1
2026-11-06 17:39:46 -05:00

222 lines
10 KiB
PowerShell

<#
.SYNOPSIS
Regenerates the committed product icon, assets/icons/app.ico, and the server
MSI wizard's artwork, assets/installer/banner.bmp and dialog.bmp, from
assets/icon-source.png.
.DESCRIPTION
The .ico is committed: every build (local, CI, release) embeds the same file,
and nothing generates it at build time. Run this only after changing the
source artwork, then commit both files.
Writes 16, 20, 24, 32, 40, 48, 64 and 128 px frames as 32-bit DIBs (with an
AND mask, for the oldest readers) and the 256 px frame PNG-compressed, each
downscaled from the source with System.Drawing (GDI+): no installs, Windows
PowerShell 5.1 or PowerShell 7 on Windows.
.EXAMPLE
./scripts/Generate-Icons.ps1
.EXAMPLE
# The Being Viewed banner's icon (the product tile with an eye; the PNG is rendered from banner-icon-source.svg).
./scripts/Generate-Icons.ps1 -Source assets/banner-icon-source.png -Output assets/icons/banner.ico
#>
[CmdletBinding()]
param(
[string]$Source,
[string]$Output
)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
if (-not $Source) { $Source = Join-Path $repoRoot 'assets/icon-source.png' }
if (-not $Output) { $Output = Join-Path $repoRoot 'assets/icons/app.ico' }
$sizes = 256, 128, 64, 48, 40, 32, 24, 20, 16
if (-not (Test-Path $Source)) { throw "Icon source not found: $Source" }
Add-Type -AssemblyName System.Drawing
# One square frame, downscaled in a single high-quality pass (GDI+'s
# HighQualityBicubic prefilters large reductions). TileFlipXY keeps the
# transparent rounded corners from bleeding a dark fringe in at the edges.
function New-Frame([System.Drawing.Image]$image, [int]$size) {
$frame = New-Object System.Drawing.Bitmap($size, $size, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
$graphics = [System.Drawing.Graphics]::FromImage($frame)
$attributes = New-Object System.Drawing.Imaging.ImageAttributes
try {
$graphics.Clear([System.Drawing.Color]::Transparent)
$graphics.CompositingMode = [System.Drawing.Drawing2D.CompositingMode]::SourceCopy
$graphics.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$attributes.SetWrapMode([System.Drawing.Drawing2D.WrapMode]::TileFlipXY)
$graphics.DrawImage($image, (New-Object System.Drawing.Rectangle(0, 0, $size, $size)),
0, 0, $image.Width, $image.Height, [System.Drawing.GraphicsUnit]::Pixel, $attributes)
}
finally {
$attributes.Dispose()
$graphics.Dispose()
}
$frame
}
# The frame's pixels as top-down BGRA rows (Format32bppArgb's memory layout).
function Get-Pixels([System.Drawing.Bitmap]$frame) {
$rect = New-Object System.Drawing.Rectangle(0, 0, $frame.Width, $frame.Height)
$data = $frame.LockBits($rect, [System.Drawing.Imaging.ImageLockMode]::ReadOnly,
[System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
try {
$stride = $frame.Width * 4
$pixels = New-Object byte[] ($stride * $frame.Height)
for ($y = 0; $y -lt $frame.Height; $y++) {
[System.Runtime.InteropServices.Marshal]::Copy([IntPtr]($data.Scan0.ToInt64() + $y * $data.Stride),
$pixels, $y * $stride, $stride)
}
,$pixels
}
finally {
$frame.UnlockBits($data)
}
}
# An icon-resource DIB: BITMAPINFOHEADER (height doubled for the mask), the
# BGRA rows bottom-up, then a 1-bpp AND mask (1 = transparent) on 32-bit rows.
function ConvertTo-IconDib([System.Drawing.Bitmap]$frame) {
$size = $frame.Width
$pixels = Get-Pixels $frame
$maskStride = [int][Math]::Ceiling($size / 32.0) * 4
$mask = New-Object byte[] ($maskStride * $size)
$stream = New-Object System.IO.MemoryStream
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.Write([int]40); $writer.Write([int]$size); $writer.Write([int]($size * 2))
$writer.Write([int16]1); $writer.Write([int16]32); $writer.Write([int]0)
$writer.Write([int]($pixels.Length + $mask.Length))
$writer.Write([int]0); $writer.Write([int]0); $writer.Write([int]0); $writer.Write([int]0)
for ($y = $size - 1; $y -ge 0; $y--) {
$writer.Write($pixels, $y * $size * 4, $size * 4)
$maskRow = ($size - 1 - $y) * $maskStride
for ($x = 0; $x -lt $size; $x++) {
if ($pixels[($y * $size + $x) * 4 + 3] -eq 0) {
$mask[$maskRow + [int][Math]::Floor($x / 8)] = $mask[$maskRow + [int][Math]::Floor($x / 8)] -bor (0x80 -shr ($x % 8))
}
}
}
$writer.Write($mask)
$writer.Flush()
,$stream.ToArray()
}
function ConvertTo-Png([System.Drawing.Bitmap]$frame) {
$stream = New-Object System.IO.MemoryStream
$frame.Save($stream, [System.Drawing.Imaging.ImageFormat]::Png)
,$stream.ToArray()
}
$image = [System.Drawing.Image]::FromFile((Resolve-Path $Source).Path)
try {
if ($image.Width -ne $image.Height -or $image.Width -lt 256) {
throw "Icon source must be square and at least 256 px (got $($image.Width)x$($image.Height))."
}
$images = foreach ($size in $sizes) {
$frame = New-Frame $image $size
try {
$bytes = if ($size -ge 256) { ConvertTo-Png $frame } else { ConvertTo-IconDib $frame }
[pscustomobject]@{ Size = $size; Bytes = $bytes }
}
finally {
$frame.Dispose()
}
}
}
finally {
$image.Dispose()
}
# ICONDIR, one ICONDIRENTRY per frame, then the frames.
$stream = New-Object System.IO.MemoryStream
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.Write([int16]0); $writer.Write([int16]1); $writer.Write([int16]$images.Count)
$offset = 6 + 16 * $images.Count
foreach ($entry in $images) {
$dimension = if ($entry.Size -ge 256) { 0 } else { $entry.Size }
$writer.Write([byte]$dimension); $writer.Write([byte]$dimension)
$writer.Write([byte]0); $writer.Write([byte]0)
$writer.Write([int16]1); $writer.Write([int16]32)
$writer.Write([int]$entry.Bytes.Length); $writer.Write([int]$offset)
$offset += $entry.Bytes.Length
}
foreach ($entry in $images) { $writer.Write([byte[]]$entry.Bytes) }
$writer.Flush()
New-Item -ItemType Directory -Force (Split-Path -Parent $Output) | Out-Null
$Output = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Output)
[System.IO.File]::WriteAllBytes($Output, $stream.ToArray())
Write-Host "Generated $Output ($($sizes -join ', ') px, $($stream.Length) bytes)"
# ── Server MSI artwork (WixUIBannerBmp, WixUIDialogBmp) ─────────────────────
# The WiX UI's sizes: the banner across the top of every page (493x58; the page
# title is drawn over its left, so the icon sits at the right on white) and the
# Welcome/Finish artwork (493x312; the text is drawn over the right, so the
# product's panel, in the icon's own navy and blue, fills the left 164 px).
# 24-bit BMPs, as Windows Installer's Bitmap controls take them.
$navyTop = [System.Drawing.Color]::FromArgb(0x1E, 0x29, 0x3B)
$navyBottom = [System.Drawing.Color]::FromArgb(0x0F, 0x17, 0x2A)
$accent = [System.Drawing.Color]::FromArgb(0x38, 0xBD, 0xF8)
function New-Canvas([int]$width, [int]$height) {
$bitmap = New-Object System.Drawing.Bitmap($width, $height, [System.Drawing.Imaging.PixelFormat]::Format24bppRgb)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
$graphics.Clear([System.Drawing.Color]::White)
[pscustomobject]@{ Bitmap = $bitmap; Graphics = $graphics }
}
function Save-Canvas($canvas, [string]$path) {
$canvas.Graphics.Dispose()
$canvas.Bitmap.Save($path, [System.Drawing.Imaging.ImageFormat]::Bmp)
$canvas.Bitmap.Dispose()
Write-Host "Generated $path ($((Get-Item $path).Length) bytes)"
}
$artDir = Join-Path $repoRoot 'assets/installer'
New-Item -ItemType Directory -Force $artDir | Out-Null
$image = [System.Drawing.Image]::FromFile((Resolve-Path $Source).Path)
try {
# Banner: white, the icon at the right, a hairline of the accent along the bottom.
$banner = New-Canvas 493 58
$icon = New-Frame $image 44
try { $banner.Graphics.DrawImage($icon, 493 - 44 - 10, 7, 44, 44) } finally { $icon.Dispose() }
$line = New-Object System.Drawing.SolidBrush($accent)
try { $banner.Graphics.FillRectangle($line, 0, 56, 493, 2) } finally { $line.Dispose() }
Save-Canvas $banner (Join-Path $artDir 'banner.bmp')
# Dialog: the navy panel on the left with the icon and the product's name.
$dialog = New-Canvas 493 312
$panel = New-Object System.Drawing.Rectangle(0, 0, 164, 312)
$gradient = New-Object System.Drawing.Drawing2D.LinearGradientBrush($panel, $navyTop, $navyBottom, 90.0)
try { $dialog.Graphics.FillRectangle($gradient, $panel) } finally { $gradient.Dispose() }
$icon = New-Frame $image 96
try { $dialog.Graphics.DrawImage($icon, 34, 64, 96, 96) } finally { $icon.Dispose() }
$format = New-Object System.Drawing.StringFormat
$format.Alignment = [System.Drawing.StringAlignment]::Center
$nameFont = New-Object System.Drawing.Font('Segoe UI Semibold', 17, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
$roleFont = New-Object System.Drawing.Font('Segoe UI', 13, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
$white = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
$blue = New-Object System.Drawing.SolidBrush($accent)
try {
$dialog.Graphics.DrawString('ExecuteR', $nameFont, $white, (New-Object System.Drawing.RectangleF(0, 172, 164, 26)), $format)
$dialog.Graphics.DrawString('Server', $roleFont, $blue, (New-Object System.Drawing.RectangleF(0, 198, 164, 20)), $format)
}
finally {
$nameFont.Dispose(); $roleFont.Dispose(); $white.Dispose(); $blue.Dispose(); $format.Dispose()
}
Save-Canvas $dialog (Join-Path $artDir 'dialog.bmp')
}
finally {
$image.Dispose()
}