通过PowerShell获取卸载的Windows字体的详细信息

时间:2020-04-19 15:23:25

标签: powershell fonts imagemagick

我正在使用PowerShell和ImageMagick创建我的字体集合的自定义图像,以确定要安装到系统中的字体。我想让图像包含一些字体信息,但是我很难提取该信息。我在[https://powershell.org/forums/topic/listing-font-details/#post-78006]中找到了一些有用的代码,但是我发现信息只能在系统字体的内部内访问。目录。我的收藏集位于%SystemRoot%\Fonts外部

我想访问类似的东西

  • 字体名称(Aparajita)
  • 字体样式(粗体,普通,斜体)
  • 字体类型(Raster,OpenType / TrueType)

这可能吗?

2 个答案:

答案 0 :(得分:1)

使用“ Shell.Application” Com对象获取文件的详细信息不仅限于%SystemRoot%\Fonts文件夹。

如果磁盘上有一个文件夹,用于保存字体集合,则可以使用下面的代码来获取每个字体文件的信息:

function Get-FontInfo {
    [CmdletBinding()]
    [OutputType([Psobject])]
    Param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)] 
        [string]$SourceFolder,
        [switch]$Recurse
    )
    # using a lookup hashtable to avoid localized field names
    $fontProperties = [ordered]@{
        0   = 'Name'
        1   = 'Size'
        2   = 'Type'
        20  = 'Author'
        21  = 'Title'
        25  = 'Copyright'
        33  = 'Company'
        34  = 'Description'
        164 = 'Extension'
        165 = 'FileName'
        166 = 'Version'
        194 = 'Path'
        196 = 'FileType'
        310 = 'Trademark'
    }
    $shell  = New-Object -ComObject "Shell.Application"
    $objDir = $shell.NameSpace($SourceFolder)
    $files  = Get-ChildItem -Path $SourceFolder -Filter '*.*' -File -Recurse:$Recurse

    foreach($file in $files) {
        $objFile   = $objDir.ParseName($file.Name)
        $mediaFile = $objDir.Items()
        $output    = [ordered]@{}
        $fontProperties.GetEnumerator() | ForEach-Object {
            $name  = $objDir.GetDetailsOf($mediaFile, $_.Name)
            if (![string]::IsNullOrWhiteSpace($name)) { 
                $output[$_.Value] = $objDir.GetDetailsOf($objFile, $_.Name)
            }
        }
        [PsCustomObject]$output
    }

    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objFile)
    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objDir)
    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}

Get-FontInfo -SourceFolder 'D:\Test'  # the path of the font folder

如果源文件夹中有子文件夹,请同时添加-Recurse开关。

输出类似:

Name        : aparaj.ttf
Size        : 194 kB
Type        : TrueType-lettertypebestand
Author      : 
Title       : Aparajita
Copyright   : Copyright (c) 2011, Modular Infotech, Pune, INDIA. - Licenced to Microsoft
Company     : 
Description : This font is primarily meant for use in displaying Hindi text in documents. It is an OpenType font, based on Unicode. 
Extension   : .ttf
FileName    : aparaj.ttf
Version     : 6.00
Path        : D:\Test\aparaj.ttf
FileType    : TrueType-lettertypebestand
Trademark   : 

答案 1 :(得分:0)

在查看Theo的答案并进行了一些代码修改之后,我发现根据是否安装了字体,存在不同的扩展属性。 以下输出的顶部是安装字体时的结果,而不是未安装字体时的结果。简而言之,如果要将“字体样式”和“设计为”嵌入到Im生成的图像中,则需要先安装字体。

Value                                           Attribute                      Index
-----                                           ---------                      -----
Regular                                         Font style                         1
Show                                            Show/hide                          2
Arabic                                          Designed for                       3
Text                                            Category                           4
Microsoft Corporation                           Designer/foundry                   5
Editable                                        Font embeddability                 6
OpenType                                        Font type                          7
Arabic Typesetting                              Family                             8
‎2013-‎Aug-‎22 ‏‎11:34 A                             Date modified                     10
505 KB                                          Size                              11
C:\Windows\Fonts\ARABTYPE.TTF                   Font file names                   13
6.00                                            Font version                      14
------------------------------------------------------------------------------------
609 KB                                          Size                               1
TrueType font file                              Item type                          2
2009-Jun-10 04:43 P                             Date modified                      3
2019-Dec-16 01:50 P                             Date created                       4
2019-Dec-16 01:50 P                             Date accessed                      5
A                                               Attributes                         6
Available offline                               Availability                       8
Unspecified                                     Perceived type                     9
Everyone                                        Owner                             10
Unrated                                         Rating                            19
Mamoun Sakkal, Paul C. Nelson and John Hudson   Authors                           20
Arabic Typesetting                              Title                             21
© 2008 Microsoft Corporation. All Rights Res... Copyright                         25
94.9 GB                                         Total size                        50
WINDOWS8.1 (this PC)                            Computer                          53
.ttf                                            File extension                   156
arabtype.ttf                                    Filename                         157
5.91                                            File version                     158
1.81 GB                                         Space free                       161
No                                              Shared                           178
Fonts                                           Folder name                      181
Y:\Documents\Fonts                              Folder path                      182
Fonts (Y:\Documents)                            Folder                           183
Y:\Documents\Fonts\arabtype.ttf                 Path                             185
TrueType font file                              Type                             187
Unresolved                                      Link status                      193
‎98%                                             Space used                       242
Not shared                                      Sharing status                   282
Available                                       <unknown>                        283
相关问题