-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generate-TerminalAssets.ps1
207 lines (184 loc) · 7.9 KB
/
Generate-TerminalAssets.ps1
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
#
# Generate-TerminalAssets.ps1
#
# Typical usage:
# .\Generate-TerminalAssets.ps1 -Path .\Terminal.svg -HighContrastPath .\Terminal_HC.svg -Destination .\images
# .\Generate-TerminalAssets.ps1 -Path .\Terminal_Pre.svg -HighContrastPath .\Terminal_Pre_HC.svg -Destination .\images-Pre
# .\Generate-TerminalAssets.ps1 -Path .\Terminal_Dev.svg -HighContrastPath .\Terminal_Dev_HC.svg -Destination .\images-Dev
#
# Some icons benefit from manual hints. The most efficient way to do that is to run the script twice:
#
# 1. Run .\Generate-TerminalAssets.ps1 ...args... -Destination .\images -KeepIntermediates
# 2. Manually hint the intermediate images under .\images\_intermediate*.png
# 3. Run .\Generate-TerminalAssets.ps1 ...args... -Destination .\images -UseExistingIntermediates
#
# Hinting the intermediate files minimizes the number of times you'll have to
# hint the same image. You may want to hint just the _intermediate.*.png and
# _intermediate.black.*.png files, and delete _intermediate.white.*.png. The
# script will then automatically derive _intermediate.white.*.png from
# _intermediate.black.*.png.
#
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$Path,
[string]$Destination,
[int[]]$Altforms = (16, 20, 24, 30, 32, 36, 40, 48, 60, 64, 72, 80, 96, 256),
[int[]]$Win32IconSizes = (16, 20, 24, 32, 48, 64, 256),
[switch]$Unplated = $true,
[float[]]$Scales = (1.0, 1.25, 1.5, 2.0, 4.0),
[string]$HighContrastPath = "",
[switch]$UseExistingIntermediates = $false,
[switch]$KeepIntermediates = $false
)
$assetTypes = @(
[pscustomobject]@{Name="LargeTile"; W=310; H=310; IconSize=96}
[pscustomobject]@{Name="LockScreenLogo"; W=24; H=24; IconSize=24}
[pscustomobject]@{Name="SmallTile"; W=71; H=71; IconSize=36}
[pscustomobject]@{Name="SplashScreen"; W=620; H=300; IconSize=96}
[pscustomobject]@{Name="Square44x44Logo"; W=44; H=44; IconSize=32}
[pscustomobject]@{Name="Square150x150Logo"; W=150; H=150; IconSize=48}
[pscustomobject]@{Name="StoreLogo"; W=50; H=50; IconSize=36}
[pscustomobject]@{Name="Wide310x150Logo"; W=310; H=150; IconSize=48}
)
function CeilToEven ([int]$i) { if ($i % 2 -eq 0) { [int]($i) } else { [int]($i + 1) } }
$inflatedAssetSizes = $assetTypes | ForEach-Object {
$as = $_;
$scales | ForEach-Object {
[pscustomobject]@{
Name = $as.Name + ".scale-$($_*100)"
W = [math]::Round($as.W * $_, [System.MidpointRounding]::ToPositiveInfinity)
H = [math]::Round($as.H * $_, [System.MidpointRounding]::ToPositiveInfinity)
IconSize = CeilToEven ($as.IconSize * $_)
}
}
}
$allAssetSizes = $inflatedAssetSizes + ($Altforms | ForEach-Object {
[pscustomobject]@{
Name = "Square44x44Logo.targetsize-${_}"
W = [int]$_
H = [int]$_
IconSize = [int]$_
}
If ($Unplated) {
[pscustomobject]@{
Name = "Square44x44Logo.targetsize-${_}_altform-unplated"
W = [int]$_
H = [int]$_
IconSize = [int]$_
}
}
})
# Cross product with the 3 high contrast modes
$allAssetSizes = $allAssetSizes | ForEach-Object {
$asset = $_
("standard", "black", "white") | ForEach-Object {
$contrast = $_
$name = $asset.Name
If ($contrast -Ne "standard") {
If ($HighContrastPath -Eq "") {
# "standard" is the default, so we can omit it in filenames
return
}
$name += "_contrast-" + $contrast
}
[pscustomobject]@{
Name = $name
W = $asset.W
H = $asset.H
IconSize = $asset.IconSize
Contrast = $_
}
}
}
$allSizes = $allAssetSizes.IconSize | Group-Object | Select-Object -Expand Name
$TranslatedSVGPath = & wsl wslpath -u ((Get-Item $Path -ErrorAction:Stop).FullName -Replace "\\","/")
$TranslatedSVGContrastPath = $null
If ($HighContrastPath -Ne "") {
$TranslatedSVGContrastPath = & wsl wslpath -u ((Get-Item $HighContrastPath -ErrorAction:Stop).FullName -Replace "\\","/")
}
& wsl which inkscape | Out-Null
If ($LASTEXITCODE -Ne 0) { throw "Inkscape is not installed in WSL" }
& wsl which convert | Out-Null
If ($LASTEXITCODE -Ne 0) { throw "imagemagick is not installed in WSL" }
If (-Not [string]::IsNullOrEmpty($Destination)) {
New-Item -Type Directory $Destination -EA:Ignore
$TranslatedOutDir = & wsl wslpath -u ((Get-Item $Destination -EA:Stop).FullName -Replace "\\","/")
} Else {
$TranslatedOutDir = "."
}
$intermediates = [System.Collections.Concurrent.ConcurrentBag[PSCustomObject]]::new()
$intermediateFiles = [System.Collections.Concurrent.ConcurrentBag[string]]::new()
# Generate the base icons
$allSizes | ForEach-Object -Parallel {
$sz = $_;
$destinationNt = $using:Destination
$destinationWsl = $using:TranslatedOutDir
$svgStandardWsl = $using:TranslatedSVGPath
$svgContrastWsl = $using:TranslatedSVGContrastPath
$intermediateStandardNt = "$destinationNt\_intermediate.standard.$($sz).png"
$intermediateStandardWsl = "$destinationWsl/_intermediate.standard.$($sz).png"
If (($using:UseExistingIntermediates -Eq $false) -Or (-Not (Test-Path $intermediateStandardNt))) {
wsl inkscape -o "$intermediateStandardWsl" -w $sz -h $sz $svgStandardWsl
} Else {
Write-Host "Using existing $intermediateStandardNt"
}
($using:intermediateFiles).Add($intermediateStandardNt)
($using:intermediates).Add([PSCustomObject]@{
Contrast = "standard"
Size = $sz
PathWSL = $intermediateStandardWsl
})
If ($svgContrastWsl -Ne $null) {
$intermediateBlackNt = "$destinationNt\_intermediate.black.$($sz).png"
$intermediateWhiteNt = "$destinationNt\_intermediate.white.$($sz).png"
$intermediateBlackWsl = "$destinationWsl/_intermediate.black.$($sz).png"
$intermediateWhiteWsl = "$destinationWsl/_intermediate.white.$($sz).png"
If (($using:UseExistingIntermediates -Eq $false) -Or (-Not (Test-Path $intermediateBlackNt))) {
wsl inkscape -o "$intermediateBlackWsl" -w $sz -h $sz $svgContrastWsl
} Else {
Write-Host "Using existing $intermediateBlackNt"
}
If (($using:UseExistingIntermediates -Eq $false) -Or (-Not (Test-Path $intermediateWhiteNt))) {
# The HC white icon is just a negative image of the HC black one
wsl convert "$intermediateBlackWsl" -channel RGB -negate "$intermediateWhiteWsl"
} Else {
Write-Host "Using existing $intermediateWhiteNt"
}
($using:intermediateFiles).Add($intermediateBlackNt)
($using:intermediateFiles).Add($intermediateWhiteNt)
($using:intermediates).Add([PSCustomObject]@{
Contrast = "black"
Size = $sz
PathWSL = $intermediateBlackWsl
})
($using:intermediates).Add([PSCustomObject]@{
Contrast = "white"
Size = $sz
PathWSL = $intermediateWhiteWsl
})
}
}
$intermediates | ? { $_.Size -In $Win32IconSizes } | Group-Object Contrast | ForEach-Object -Parallel {
$assetName = "fluentinfo.ico"
If ($_.Name -Ne "standard") {
$assetName = "fluentinfo_contrast-$($_.Name).ico"
}
Write-Host "Producing win32 .ico for contrast=$($_.Name) as $assetName"
wsl convert $_.Group.PathWSL "$($using:TranslatedOutDir)/$assetName"
}
# Once the base icons are done, splat them into the middles of larger canvases.
$allAssetSizes | ForEach-Object -Parallel {
$asset = $_
If ($asset.W -Eq $asset.H -And $asset.IconSize -eq $asset.W) {
Write-Host "Copying base icon for size=$($asset.IconSize), contrast=$($asset.Contrast) to $($asset.Name)"
Copy-Item "${using:Destination}\_intermediate.$($asset.Contrast).$($asset.IconSize).png" "${using:Destination}\$($asset.Name).png" -Force
} Else {
wsl convert "$($using:TranslatedOutDir)/_intermediate.$($asset.Contrast).$($asset.IconSize).png" -gravity center -background transparent -extent "$($asset.W)x$($asset.H)" "$($using:TranslatedOutDir)/$($asset.Name).png"
}
}
If ($KeepIntermediates -Eq $false) {
$intermediateFiles | ForEach-Object {
Write-Host "Cleaning up intermediate file $_"
Remove-Item $_
}
}