繁体   English   中英

从字体的脚本名称中获取字体的文件名

[英]Get a font's file name from the font's postscript name

当我仅有的信息是字体的“ postscript”名称时,我试图获取字体文件名。 (强调:字体名称是脚本,而不是字体)。
例如,我有以下后记名称:TimesNewRomanPSMT。
保存在注册表中的真实名称是:Times New Roman(TrueType)。
有没有办法从给定的附言名称中获取该名称?

我在这里看到了类似的帖子,并没有保留: C#从脚本名称中获取字体

我正在用C ++编写代码,因此我不受编码语言的限制。 目前,我正在为Windows编写此代码,但它应该兼容,或者至少具有针对MacOS的备用代码

我有C ++代码从给定的fontfile中获取字体名称,检查标题……但是对于我测试过的某些字体却失败了(例如可以工作90%)。 我认为最简单的方法是使用十六进制编辑器打开字体文件并在其中搜索字体名称。 如果您担心注册表,则可以重新注册字体名称,如下例所示:

reg添加“ HKLM \\ SOFTWARE \\ Microsoft \\ Windows NT \\ CurrentVersion \\ Fonts” / v“ Arial Bold” / t REG_SZ / d arialbd.ttf

我有一个类似的问题,但对于Photoshop。 所以我写了下面的代码。 它输出包含您的系统中安装的所有字体的CSV文件,以及它们的文件名,Windows名称和Postscript名称。

您需要安装Photoshop和Python才能运行它。 在运行它之前,还要保持打开Photoshop窗口的状态,以便它可以从中获取字体列表。

SHORTNAME功能是从这里开始- https://gist.github.com/pklaus/dce37521579513c574d0

# This program lists all installed fonts on the computer with their font file name, Windows name and Postscript name.

import os
from fontTools import ttLib
from win32com.client import GetActiveObject
import pandas as pd

FONT_SPECIFIER_NAME_ID = 4
FONT_SPECIFIER_FAMILY_ID = 1
list = []
app = GetActiveObject("Photoshop.Application") # Get instance of open Photoshop window
df = pd.DataFrame(columns=['Font File Name', 'Windows Name', 'Postscript Name'])

def shortName(font):
    """Get the short name from the font's names table"""
    name = ""
    family = ""
    for record in font['name'].names:
        if b'\x00' in record.string:
            name_str = record.string.decode('utf-16-be')
        else:
            name_str = record.string.decode('utf-8')
        if record.nameID == FONT_SPECIFIER_NAME_ID and not name:
            name = name_str
        elif record.nameID == FONT_SPECIFIER_FAMILY_ID and not family:
            family = name_str
        if name and family: break
    return name, family

def getPostScriptName(winName):
    for i in range(0, len(app.fonts)):
        if(app.fonts[i].name == winName):
            return app.fonts[i].postScriptName

x = 0
for file in os.listdir(r'C:\Windows\Fonts'):
    if (file.endswith(".ttf") or file.endswith(".otf")):
        # list.append(file)
        try:
            fontfile = file
            file = "C:\\Windows\\Fonts\\" + file
            tt = ttLib.TTFont(file)
            psName = getPostScriptName(shortName(tt)[0])
            print(fontfile, shortName(tt)[0], psName)
            df.at[x, 'Font File Name'] = fontfile
            df.at[x, 'Windows Name'] = shortName(tt)[0]
            df.at[x, 'Postscript Name'] = psName
            x = x + 1
            df.to_csv("installed-fonts.csv",index=False)
        except Exception as e:
            print (e)
            continue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM