I need to enumerate the postsript names of all the installed fonts.
for example:
foreach (FontFamily item in FontFamily.Families)
{
listBox1.Items.Add(item.Name);
}
This will give only the actual font names. But I need to find the postscript names.
Eg: For the font "Arial Black" - 1. Actual font name is "Arial Black" 2. PostScript name is "Arial-Black"
Thanks in advance, James
EDIT:
- @ plinth Actually, I read the font name from the PDF and load the corresponding system font. In this case, the PDF has a font name "Arial-Black" (Post script name).. how can I load the font from the sytem (Arial Black) accordingly....ideas??
So, the ideal method should be reading the postscript names from the 开发者_Go百科installed fonts
- Substituting '-' with '' is not a suitable solution because, there are possibilities of other font names such as "Arial-Bold", "Time New Roman - PSMT" etc..
You may do it directly in PostScript. Execute this:
%!PS
/FS { findfont exch scalefont setfont } bind def
% gets page boundaries
clippath pathbbox newpath
/var_TopOfPage exch def
/var_RightOfPage exch def
/var_BottomOfPage exch def
/var_LeftOfPage exch def
% helvetica is almost always there
12 /Helvetica FS
0 setgray
% set start position
var_LeftOfPage var_TopOfPage 30 sub moveto
/pos var_TopOfPage 20 sub def
GlobalFontDirectory {
var_LeftOfPage pos moveto
/FontName get 70 string cvs show
/pos pos 20 sub def
pos 0 le {
showpage
/pos var_TopOfPage 20 sub def
} if
} forall
showpage
%%EOF
I have been able to find out the available fonts of a printer with this code.
I hope I've helped you.
I have done something very similar to that. You should be aware the FontFamily.Families may not be the entire set of available fonts.
Why not just substitute '-' for ' '?
In my case, I needed to go to the PDF font name, which for Times New Roman in bold style had to be "TimesNewRoman,Bold".
private static string ToPdfFontName(Font f)
{
StringBuilder sb = new StringBuilder();
StripSpaces(sb, f.Name);
if ((f.Style & FontStyle.Bold)!= 0 && (f.Style & FontStyle.Italic)!= 0)
{
sb.Append(",BoldItalic");
}
else if ((f.Style & FontStyle.Bold)!= 0)
{
sb.Append(",Bold");
}
else if ((f.Style & FontStyle.Italic)!= 0)
{
sb.Append(",Italic");
}
return sb.ToString();
}
This code outputs a CSV of all the fonts installed in your system with their filename, Windows name and Postscript name.
You need to have Photoshop and Python installed to run it. Before running it, also keep a Photoshop window open so it can grab the list of fonts from there.
Shortname function was from here - 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")):
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
精彩评论