字体文件Powershell / .NET扩展属性

问题描述

我正在尝试优化this以仅获取字体文件的Title和Type属性。我已经使用该函数来验证我的目标文件是否具有这些属性,并且我有这个

$shell = new-object -com shell.application
$folder = $shell.namespace("\\px\Rollouts\Misc\Fonts\Arial")
$item = $folder.Items().Item('arial.ttf')
$item.ExtendedProperty('Title')
$item.ExtendedProperty('Type')

可以正确返回类型,但不能返回标题。所以,我有两个问题。

1:这两个属性之间有什么区别,一个属性起作用而另一个不起作用?

2:是否有更好的本机PowerShell或.NET PS也可以获取此信息,而无需放到COM?我的搜索显示了即使在C#中也使用COM kudge的示例,这似乎很疯狂。

编辑:我可以使用

$folder.getDetailsOf($item,21)
$folder.getDetailsOf($item,191)

但是我不知道这些数字是否真的是通用的。我首先想到的是,我更喜欢使用属性名称,但是后来,尽管如此,但该名称在法语计算机上是否相同?啊,没什么容易的。 :)

编辑2: 不,不是普遍的。在Windows 7中,字体类型的191参考是182。

编辑3: 好的,我有,它可以在Windows 7和Windows 10上运行。

$shell = new-object -com shell.application
$folder = $shell.namespace("\\px\Rollouts\Misc\Fonts\Arial")
$item = $folder.Items().Item('arial.ttf')
foreach ($i in 0..266) {
    if ($folder.getDetailsOf($folder.items,$i) -eq 'Title') {
        $title = "Title: $($folder.getDetailsOf($item,$i))"
    }
    if ($folder.getDetailsOf($folder.items,$i) -eq 'Type') {
        $type = "Type: $($folder.getDetailsOf($item,$i))"
    }
    if ($title -and $type) {
        break
    }
}
$title 
$type

性能是可以接受的,所以如果有人可以验证它是否可以在使用其他语言的机器上运行,我想我有一个解决方案。但是仍然很想听到关于GOOD解决方案的信息。

编辑4:好的,这变得越来越复杂。 [Windows.Media.GlyphTypeface]方法对于Arial来说看起来不错。但是后来我看了看You Gothic(YuGothM.ttc),它在注册表中的名称Yu Gothic Medium & Yu Gothic UI Regular (TrueType),甚至无法从文件中的数据构造该字符串。对于从文件开始并最终为所有用户安装字体的方式,我有点茫然,这与Microsoft @ $%#$设置之前的方式完全相同。我的意思是,我现在可以用英语做到这一点,但我确实希望得到普遍支持。更奇怪的是,Yu Gothic在写字板中甚至无法测试正确设置注册属性名称是否很重要。但是其中一个是sitka.ttc,它使用Sitka Small & Sitka Text & Sitka Subheading & Sitka heading & Sitka display & Sitka Banner (TrueType)。我将其更改为仅Sitka Small,可以通过Win32FamilyNames属性使用它,并且似乎可行,其他诸如Sitka display和Sitka heading之类的内容仍可在写字板中使用。但这是否表明注册表中属性名称有些随意?而且它仍然引出一个问题,当用右键单击安装时,Windows如何确定该怎么称呼这些东西?它必须是文件中的信息,对吧?

编辑5:基于@filimonic答案,我现在有了这个,它太好了。奇怪的是,使用Segoe UI Light时,重量报告为350。我想我需要测试重量,如果它是整数,则不要使用它。可能我需要测试每种字体,看看它们都报告了什么,希望整数是一个可行的过滤器。

$file = '\\Localhost\c$\Windows\Fonts\seguisli.ttf'
$glyph = [System.Windows.Media.GlyphTypeface]::new([uri]::new($file))
if ($null -eq ($familyNames = $glyph.Win32FamilyNames['en-us'])) {
    $familyNames = $gi.Win32FamilyNames.Values.Item(0)
}
$weight = $glyph.Weight
$style = $glyph.Style

$familyNames
$weight
$style

解决方法

要从文件中获取字体名称:

$file = 'C:\...\File.ttc'
$fontCol = [System.Drawing.Text.PrivateFontCollection]::new()
$fontCol.AddFontFile($file)
$fontListInFile = [String[]]$fontCol.Families.Name
$fontCol.Dispose()

要获取安装的字体名称:

$fontCol = [System.Drawing.Text.InstalledFontCollection]::new()
$fontListInstalled = [String[]]$fontCol.Families.Name
$fontCol.Dispose()

或者具有扩展选项:

$files = @( Get-ChildItem -Path 'C:\Windows\Fonts' -File  ) | 
    Where-Object {$_.Name -notlike '*.fon'} |
    Where-Object {$_.Name -notlike '*.CompositeFont'} |
    Where-Object {$_.Name -notlike '*.ini'}

$fonts = @()
forEach ($f in $files)
{
    try 
    {
        $gi = [System.Windows.Media.GlyphTypeface]::new([uri]::new($f.FullName))

        $fam = $gi.Win32FamilyNames['en-us']
        if ($null -eq $fam) { $fam = $gi.Win32FamilyNames.Values.Item(0) }
        $w = $gi.Weight
        $s = $gi.Style


        $fonts += @([PSCustomObject]@{
            'File' = $f.FullName
            'FontFamily' = $fam
            'Weight' = $w
            'Style' = $s
            'FontFullName' = "$($fam)#$($s)#$($w)"})
    }
    catch
    {
    Write-Warning -Message "Error reading $($f.FullName) : $($_.Exception.Message)"
    }
}