从字体的postscript名称中获取字体的文件名

时间:2014-03-19 10:30:17

标签: c++ windows macos fonts postscript

我正在尝试获取字体文件名,因为我所拥有的唯一信息是字体的'postscript'名称。 (重点:字体的名称是postscript而不是字体) 例如,我有以下postscript名称:TimesNewRomanPSMT 保存在注册表中的真实姓名是:Times New Roman(TrueType) 有没有办法从给定的postscript名称中获取该名称?

我在这里看到一篇类似的帖子没有得到答案: C# get font from postscript name

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

2 个答案:

答案 0 :(得分:0)

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

reg add" HKLM \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Fonts" / v" Arial Bold" / t REG_SZ / d arialbd.ttf

答案 1 :(得分:0)

我有一个类似的问题,但是对于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