从命令行获取 TTF 文件的实际字体名称

从命令行获取 TTF 文件的实际字体名称

这似乎是一个简单的问题,如何从命令行获取字体的实际名称。

以下是示例。字体文件名为segoesc.ttf。然而,字体菜单中显示的字体实际名称为Segoe Script

在此处输入图片描述

我已尝试过:

  1. 有一个 Python 脚本可以获取字体名称。但我不喜欢 Python 依赖。

  2. 右键单击 Fontfile 并转到“属性”>“详细信息”,显示属性列表。我尝试使用,wmic但它没有显示Title属性。

wmic 数据文件“c:\Windows\fonts\segoesc.ttf”获取标题

谢谢。

答案1

在 PowerShell 中:

(New-Object -TypeName Windows.Media.GlyphTypeface -ArgumentList 'C:\Windows\Fonts\segoesc.ttf').Win32FamilyNames.Values

返回Segoe Script

感谢 Samuel Leslie 和胶水

答案2

尝试使用以下 C 语言小程序进行编译微软 Visual C++

#include <stdlib.h>
#include <stdio.h>

#define QFR_DESCRIPTION 1

/* Link with GDI32 library */
int GetFontResourceInfoW(
    wchar_t* lpszFilename,
    unsigned long* cbBuffer,
    void* lpBuffer,
    unsigned long dwQueryType);

int wmain(int wargc, wchar_t** wargv) {
    int res = 0, size = 0;

    if (wargc == 2) {
        res = GetFontResourceInfoW(wargv[1], &size, NULL, QFR_DESCRIPTION);
        wchar_t* buff = malloc(size);
        res = GetFontResourceInfoW(wargv[1], &size, buff, QFR_DESCRIPTION);
        if (res)
            wprintf(L"Description: %ls\n", buff);
        free(buff);
    }

    return 0;
}
  • 命令行示例:ProgramName.exe C:\path\to\font.ttf

进一步阅读:

答案3

任何能够处理 COM 对象 (J-/vbscript/PowerShell) 的 (脚本) 语言都可以使用 Shell.Application 获取扩展文件属性

此 PowerShell 脚本列出了字体文件的典型扩展属性

$path        = 'A:\segoescb.ttf'
$folder      = Split-Path $path
$file        = Split-Path $path -Leaf

$shell       = New-Object -COMObject Shell.Application
$shellfolder = $shell.Namespace($folder)
$shellfile   = $shellfolder.ParseName($file)

## get (localized) description and value of 
##   specified extended attributes numbers
## (0,2,21,165,166,195) 

(0,1,2,3,4,5,6,9,10,19,21,25,33,34,58,62,165,166,167,170,191,192,193,195,197,203,255)| 
Foreach-Object { 
    '{0,3} {1,-30} = {2}' -f $_,
            $shellfolder.GetDetailsOf($null, $_), 
            $shellfolder.GetDetailsOf($shellfile, $_) 
}

样本(德语)输出:

  0 Name                           = segoescb.ttf
  1 Größe                          = 567 KB
  2 Elementtyp                     = IrfanView TTF File
  3 Änderungsdatum                 = 2018-04-12 01:34
  4 Erstelldatum                   = 2019-05-07 15:46
  5 Letzter Zugriff                = 2019-05-07 15:46
  6 Attribute                      = AC
  9 Erkannter Typ                  = Nicht angegeben
 10 Besitzer                       = xxxxxxxx\LotPings
 19 Bewertung                      = Nicht bewertet
 21 Titel                          = Segoe Script Bold
 25 Copyright                      = © 2016 Microsoft Corporation. All Rights Reserved.
 33 Firma                          = Microsoft Corporation
 34 Dateibeschreibung              =
 58 Gesamtgröße                    = 0,99 GB
 62 Computer                       = xxxxxxxx (dieser PC)
165 Dateierweiterung               = .ttf
166 Dateiname                      = segoescb.ttf
167 Dateiversion                   = 5.02
170 Freier Speicherplatz           = 998 MB
191 Ordnername                     = A:\
192 Ordnerpfad                     = A:\
193 Ordner                         = A:\
195 Pfad                           = A:\segoescb.ttf
197 Typ                            = IrfanView TTF File
203 Verknüpfungsstatus             = Nicht aufgelöst
255 Verwendeter Speicherplatz      = ‎2%

因此绝对最小值是 0 或 166 和 21。

答案4

以下方法检索所有已安装的字体并创建单独的字形字体文件名可以识别数据。之后,您可以使用文件名来定位相关数据并提取所需的字段(不限于 Win32FamilyNames)。

Add-Type -AssemblyName PresentationCore
$glyphTypefaceMap = @{}
foreach ($regPath in @(
    # You can find your font in these two locations if it is already installed.
    'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts', # Local Machine
    'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' # Current User
)) {
    $fontList = Get-ItemProperty $regPath
    foreach ($_ in ($fontList | Get-Member -MemberType NoteProperty)) {
        $propertyName = $_.Name
        if (!($propertyName.Contains("TrueType"))) {
            continue
        }
        $value = $fontList.$propertyName
        $fontPath = ""
        if (Test-Path $value) {
            $fontPath = $value
        } else {
            $fontPath = Join-Path $env:SystemRoot fonts/$value
        }

        if (!(Test-Path $fontPath)) {
            Write-Verbose "path not exits: $value" # Test-Path "C:\...\RobotoFlex[GRAD,wght].ttf" will get false even exists.
            continue
        }

        $glyphTypeface = New-Object -TypeName Windows.Media.GlyphTypeface -ArgumentList $fontPath
        # Wait-Debugger
        $name = (Get-Item ($glyphTypeface.FontUri.LocalPath)).Name # xxx.ttf
        $glyphTypefaceMap[$name] = $glyphTypeface
    }
}
# Now you can use the font file name to locate the data you need 
# and select the desired fields from the font.
# For example,
$glyphTypefaceMap["Arial.ttf"]
$glyphTypefaceMap["Arial.ttf"].Win32FamilyNames

在此处输入图片描述

$glyphTypefaceMap.GetEnumerator() | foreach { "$($_.Value.FontUri.LocalPath) $($_.Value.Win32FamilyNames)"}
<#
output:

C:\WINDOWS\fonts\msjhbd.ttc [en-US, Microsoft JhengHei] [zh-HK, 微軟正黑體] [zh-TW, 微軟正黑體]
C:\WINDOWS\fonts\mingliu.ttc [en-US, MingLiU] [zh-HK, 細明體] [zh-TW, 細明體]
...
#>

相关内容