Skip to content

Commit 8cdfcf3

Browse files
feat: Import-Font ( Fixes #4 )
1 parent 0a26c39 commit 8cdfcf3

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Commands/Import-Font.ps1

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
function Import-Font {
2+
param(
3+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
4+
[Alias('FullName')]
5+
[string]
6+
$FontPath
7+
)
8+
9+
begin {
10+
$fontForgeCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand('fontforge','Application')
11+
$myModuleName = $MyInvocation.MyCommand.ScriptBlock.Module.Name
12+
if (-not $myModuleName) {
13+
$myModuleName = @($MyInvocation.MyCommand.Name -split '-')[-1]
14+
}
15+
$myFontsDirectory = Join-Path ([Environment]::GetFolderPath("LocalApplicationData")) $myModuleName
16+
17+
$importQueue = [Collections.Queue]::new()
18+
$outputQueue = [Collections.Queue]::new()
19+
20+
$fontForgeScript = @(
21+
'Open($1)'
22+
'SelectWorthOutputting()'
23+
'Generate($2)'
24+
)
25+
26+
$fontForgeArgs = @(
27+
'-lang=ff'
28+
29+
'-c'
30+
31+
$fontForgeScript -join ';'
32+
)
33+
filter showProgress {
34+
$output = $_
35+
Write-Progress -Id $progressId "Importing Fonts $fontPath" "$line " -PercentComplete $percentComplete
36+
Write-Verbose $output
37+
$outputQueue.Enqueue($output)
38+
}
39+
}
40+
41+
process {
42+
$importQueue.Enqueue($FontPath)
43+
}
44+
45+
end {
46+
$progressId = Get-Random
47+
$counter = 0
48+
$total = $importQueue.Count
49+
while ($importQueue.Count) {
50+
$fontPath = $importQueue.Dequeue()
51+
$percentComplete = (
52+
$counter * 100 / $total
53+
)
54+
$counter++
55+
56+
$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+
}
63+
64+
$destinationPath = $fontFileInfo.Name.Substring(0, $fontFileInfo.Name.Length - $fontFileInfo.Extension.Length) + '.svg'
65+
$destinationPath = Join-Path $myFontsDirectory $destinationPath
66+
67+
$pathArgs = @(
68+
$FontPath
69+
$DestinationPath
70+
)
71+
72+
& $fontForgeCommand @fontForgeArgs @pathArgs *>&1 | showProgress
73+
if ($?) {
74+
$fontFile = Get-Item -Path $DestinationPath
75+
$fontFile.pstypenames.add('Font.svg')
76+
$fontFile
77+
}
78+
79+
}
80+
81+
Write-Progress "Importing fonts" "Complete!" -Completed -Id $progressId
82+
83+
}
84+
}

0 commit comments

Comments
 (0)