-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd_psnames.py
More file actions
288 lines (230 loc) · 8.57 KB
/
d_psnames.py
File metadata and controls
288 lines (230 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
"""
Extract PostScript Names from TTF Files
This script parses TTF files directly to extract PostScript names, including
variable font instances from fvar tables. It outputs a single text file
primarily used for understanding naming patterns.
Usage:
python d_psnames.py [--fonts-dir FONTS_DIR] [--output OUTPUT_FILE] [--verbose]
Features:
- Direct TTF file parsing (no reliance on metadata)
- Variable font instance support
- Progress tracking with tqdm
- Click-based CLI interface
"""
import os
import sys
import click
from pathlib import Path
from typing import List, Dict, Set, Optional
from tqdm import tqdm
try:
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._f_v_a_r import table__f_v_a_r
except ImportError:
print("Error: fonttools is required. Install with: pip install fonttools")
sys.exit(1)
def extract_postscript_name(font_path: str) -> Optional[str]:
"""
Extract PostScript name from a TTF file.
Args:
font_path: Path to the TTF file
Returns:
PostScript name or None if not found
"""
try:
with TTFont(font_path) as font:
# Get PostScript name from name table
name_table = font.get('name')
if name_table:
# Look for PostScript name (nameID 6)
ps_name = name_table.getDebugName(6)
if ps_name:
return ps_name
# For variable fonts, also check nameID 25 (Variations PostScript Name Prefix)
if 'fvar' in font:
vf_ps_name = name_table.getDebugName(25)
if vf_ps_name:
return vf_ps_name
return None
except Exception as e:
print(f"Error reading {font_path}: {e}")
return None
def extract_variable_font_instances(font_path: str) -> List[str]:
"""
Extract PostScript names for variable font instances from fvar table.
Args:
font_path: Path to the TTF file
Returns:
List of PostScript names for variable font instances
"""
instance_names = []
try:
with TTFont(font_path) as font:
# Check if this is a variable font
if 'fvar' not in font:
return instance_names
fvar_table = font['fvar']
name_table = font.get('name')
if not name_table:
return instance_names
# Extract instance names
for instance in fvar_table.instances:
# Get the PostScript name for this instance
instance_name = name_table.getDebugName(
instance.subfamilyNameID)
if instance_name:
instance_names.append(instance_name)
except Exception as e:
print(f"Error reading variable font instances from {font_path}: {e}")
return instance_names
def find_ttf_files(fonts_dir: str) -> List[str]:
"""
Find all TTF files in the given directory.
Args:
fonts_dir: Directory to search for TTF files
Returns:
List of TTF file paths
"""
ttf_files = []
fonts_path = Path(fonts_dir)
if not fonts_path.exists():
print(f"Error: Directory {fonts_dir} does not exist")
return ttf_files
# Recursively find all .ttf files
for ttf_file in fonts_path.rglob("*.ttf"):
ttf_files.append(str(ttf_file))
return sorted(ttf_files)
def analyze_naming_patterns(ps_names: List[str], instance_names: List[str]) -> Dict[str, int]:
"""
Analyze naming patterns in PostScript names.
Args:
ps_names: List of PostScript names
instance_names: List of variable font instance names
Returns:
Dictionary of pattern counts
"""
patterns = {}
# Analyze main PostScript names
for name in ps_names:
if not name:
continue
# Common patterns to look for
if '-' in name:
patterns['contains-hyphen'] = patterns.get(
'contains-hyphen', 0) + 1
if 'Regular' in name:
patterns['contains-regular'] = patterns.get(
'contains-regular', 0) + 1
if 'Bold' in name:
patterns['contains-bold'] = patterns.get('contains-bold', 0) + 1
if 'Italic' in name:
patterns['contains-italic'] = patterns.get(
'contains-italic', 0) + 1
if '[' in name and ']' in name:
patterns['contains-brackets'] = patterns.get(
'contains-brackets', 0) + 1
# Analyze variable font instance names
for name in instance_names:
if not name:
continue
if '-' in name:
patterns['instance-contains-hyphen'] = patterns.get(
'instance-contains-hyphen', 0) + 1
if 'Regular' in name:
patterns['instance-contains-regular'] = patterns.get(
'instance-contains-regular', 0) + 1
if 'Bold' in name:
patterns['instance-contains-bold'] = patterns.get(
'instance-contains-bold', 0) + 1
if 'Italic' in name:
patterns['instance-contains-italic'] = patterns.get(
'instance-contains-italic', 0) + 1
return patterns
@click.command()
@click.option('--fonts-dir',
default='./vendor/google',
help='Directory containing TTF files (default: ./vendor/google)')
@click.option('--output',
default='psnames.txt',
help='Output file for PostScript names (default: psnames.txt)')
@click.option('--verbose', '-v',
is_flag=True,
help='Enable verbose output')
def main(fonts_dir: str, output: str, verbose: bool):
"""
Extract PostScript names from TTF files.
This script parses TTF files directly to extract PostScript names,
including variable font instances from fvar tables.
"""
if verbose:
print(f"Searching for TTF files in: {fonts_dir}")
# Find all TTF files
ttf_files = find_ttf_files(fonts_dir)
if not ttf_files:
print("No TTF files found!")
return
if verbose:
print(f"Found {len(ttf_files)} TTF files")
# Extract PostScript names
ps_names = []
instance_names = []
variable_fonts = 0
failed_files = []
print("Extracting PostScript names...")
for ttf_file in tqdm(ttf_files, desc="Processing fonts"):
# Extract main PostScript name
ps_name = extract_postscript_name(ttf_file)
if ps_name:
ps_names.append(ps_name)
else:
failed_files.append(ttf_file)
# Check for variable font instances
instances = extract_variable_font_instances(ttf_file)
if instances:
variable_fonts += 1
instance_names.extend(instances)
if verbose and ps_name:
print(f"{os.path.basename(ttf_file)}: {ps_name}")
if instances:
for instance in instances:
print(f" Instance: {instance}")
# Analyze naming patterns
patterns = analyze_naming_patterns(ps_names, instance_names)
# Write output file
with open(output, 'w', encoding='utf-8') as f:
f.write("PostScript Names Analysis\n")
f.write("=" * 50 + "\n\n")
f.write(f"Total TTF files processed: {len(ttf_files)}\n")
f.write(f"Successful extractions: {len(ps_names)}\n")
f.write(f"Failed extractions: {len(failed_files)}\n")
f.write(f"Variable fonts found: {variable_fonts}\n")
f.write(f"Total variable font instances: {len(instance_names)}\n\n")
f.write("Naming Patterns:\n")
f.write("-" * 20 + "\n")
for pattern, count in sorted(patterns.items()):
f.write(f"{pattern}: {count}\n")
f.write("\n")
f.write("All PostScript Names:\n")
f.write("-" * 25 + "\n")
for name in sorted(set(ps_names)):
f.write(f"{name}\n")
f.write("\n")
f.write("Variable Font Instance Names:\n")
f.write("-" * 35 + "\n")
for name in sorted(set(instance_names)):
f.write(f"{name}\n")
f.write("\n")
if failed_files:
f.write("Failed Files:\n")
f.write("-" * 15 + "\n")
for file_path in failed_files:
f.write(f"{file_path}\n")
print(f"\nResults written to: {output}")
print(f"Processed {len(ttf_files)} files")
print(f"Found {len(set(ps_names))} unique PostScript names")
print(
f"Found {len(set(instance_names))} unique variable font instance names")
print(f"Variable fonts: {variable_fonts}")
if __name__ == '__main__':
main()