Skip to content

Commit 8ed138d

Browse files
docs: Import-Font Help ( Fixes #4 )
1 parent cdfe48d commit 8ed138d

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

Commands/Import-Font.ps1

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
function Import-Font {
2+
<#
3+
.SYNOPSIS
4+
Imports Fonts
5+
.DESCRIPTION
6+
Imports a Font as a SVG font. This allows us to easily modify the glyphs.
7+
#>
28
param(
9+
# The path to the font file.
310
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
411
[Alias('FullName')]
512
[string]
613
$FontPath
714
)
815

916
begin {
17+
# Find font forge
1018
$fontForgeCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand('fontforge','Application')
19+
# and then find where we should put the local fonts
1120
$myModuleName = $MyInvocation.MyCommand.ScriptBlock.Module.Name
1221
if (-not $myModuleName) {
1322
$myModuleName = @($MyInvocation.MyCommand.Name -split '-')[-1]
1423
}
1524
$myFontsDirectory = Join-Path ([Environment]::GetFolderPath("LocalApplicationData")) $myModuleName
1625

26+
# Create a queue to store imports
1727
$importQueue = [Collections.Queue]::new()
28+
# and outputs
1829
$outputQueue = [Collections.Queue]::new()
1930

31+
# The fontForge script will always be the same
2032
$fontForgeScript = @(
2133
'Open($1)'
2234
'SelectWorthOutputting()'
2335
'Generate($2)'
2436
)
2537

38+
# And so will the first set of arguments to FontForge
2639
$fontForgeArgs = @(
27-
'-lang=ff'
28-
40+
'-lang=ff'
2941
'-c'
30-
3142
$fontForgeScript -join ';'
3243
)
3344
filter showProgress {
@@ -39,10 +50,24 @@ function Import-Font {
3950
}
4051

4152
process {
53+
# Add each font path to the queue
4254
$importQueue.Enqueue($FontPath)
4355
}
4456

4557
end {
58+
# Make sure font forge is installed
59+
if (-not $fontForgeCommand) {
60+
Write-Error "FontForge is not installed or in the Path"
61+
return
62+
}
63+
64+
# and that the fonts directory exists
65+
if (-not (Test-Path $myFontsDirectory)) {
66+
$null = New-Item -ItemType Directory -Path $myFontsDirectory
67+
if (-not $?) { return }
68+
}
69+
70+
# Write progress as we import fonts.
4671
$progressId = Get-Random
4772
$counter = 0
4873
$total = $importQueue.Count
@@ -53,32 +78,33 @@ function Import-Font {
5378
)
5479
$counter++
5580

81+
# Make sure we can get the font file
5682
$fontFileInfo = Get-Item -Path $fontPath -ErrorAction Ignore
57-
if (! $fontFileInfo) { continue }
58-
59-
if (-not (Test-Path $myFontsDirectory)) {
60-
$null = New-Item -ItemType Directory -Path $myFontsDirectory
61-
if (-not $?) { continue }
62-
}
83+
if (! $fontFileInfo) { continue }
6384

85+
# Replace it's extension with svg
6486
$destinationPath = $fontFileInfo.Name.Substring(0, $fontFileInfo.Name.Length - $fontFileInfo.Extension.Length) + '.svg'
87+
# and determine the absolute destination path
6588
$destinationPath = Join-Path $myFontsDirectory $destinationPath
6689

90+
# Create an array with our paths
6791
$pathArgs = @(
6892
$FontPath
6993
$DestinationPath
7094
)
7195

96+
# Call font forge with our script and paths
7297
& $fontForgeCommand @fontForgeArgs @pathArgs *>&1 | showProgress
98+
# If that worked
7399
if ($?) {
100+
# get the file
74101
$fontFile = Get-Item -Path $DestinationPath
102+
# and decorate it as a `Font.svg`
75103
$fontFile.pstypenames.add('Font.svg')
76104
$fontFile
77-
}
78-
105+
}
79106
}
80107

81108
Write-Progress "Importing fonts" "Complete!" -Completed -Id $progressId
82-
83109
}
84110
}

0 commit comments

Comments
 (0)