-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont_catalog.py
More file actions
149 lines (116 loc) · 4.6 KB
/
font_catalog.py
File metadata and controls
149 lines (116 loc) · 4.6 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
#!/usr/bin/env python3
"""
Script to generate an ODT document listing all available system fonts
with their variants (bold, italic, etc.) and Lorem Ipsum sample text.
"""
import os
# import subprocess
from odf.opendocument import OpenDocumentText
# from odf.style import Style, TextProperties, ParagraphProperties
from odf.style import Style, TextProperties
# from odf.text import P, Span
from odf.text import P
import matplotlib.font_manager as fm
def get_system_fonts():
"""
Sammelt alle verfügbaren System-Fonts mit ihren Eigenschaften.
Gibt eine Liste von Dictionaries zurück mit Font-Informationen.
"""
fonts = []
seen = set() # Um Duplikate zu vermeiden
# Alle verfügbaren Fonts über matplotlib.font_manager abrufen
font_list = fm.fontManager.ttflist
for font in font_list:
# Eindeutiger Schlüssel für diese Font-Variante
key = (font.name, font.weight, font.style)
if key not in seen:
seen.add(key)
fonts.append({
'name': font.name,
'weight': font.weight, # z.B. 400 (normal), 700 (bold)
'style': font.style, # z.B. 'normal', 'italic', 'oblique'
'fname': font.fname # Pfad zur Font-Datei
})
# Sortieren nach Font-Name und dann nach Eigenschaften
fonts.sort(key=lambda x: (x['name'].lower(), x['weight'], x['style']))
return fonts
def get_variant_description(weight, style):
"""
Erstellt eine lesbare Beschreibung der Font-Variante.
"""
variants = []
# Weight beschreiben
if weight >= 700:
variants.append("Bold")
elif weight <= 300:
variants.append("Light")
elif weight != 400:
variants.append(f"Weight-{weight}")
# Style beschreiben
if style == 'italic':
variants.append("Italic")
elif style == 'oblique':
variants.append("Oblique")
if not variants:
return "Regular"
return " ".join(variants)
def create_odt_document(fonts, output_path):
"""
Erstellt ein ODT-Dokument mit allen Fonts und ihren Varianten.
"""
doc = OpenDocumentText()
# Lorem Ipsum Text (80 Zeichen)
lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempo"
print(f"Erzeuge ODT-Dokument mit {len(fonts)} Font-Varianten...")
for idx, font_info in enumerate(fonts):
font_name = font_info['name']
weight = font_info['weight']
style_name = font_info['style']
variant = get_variant_description(weight, style_name)
# Style für diesen Font erstellen
style_id = f"font_style_{idx}"
font_style = Style(name=style_id, family="paragraph")
# Text-Eigenschaften setzen
text_props = TextProperties()
text_props.setAttribute('fontfamily', font_name)
text_props.setAttribute('fontfamilygeneric', 'swiss')
text_props.setAttribute('fontpitch', 'variable')
# Weight setzen
if weight >= 700:
text_props.setAttribute('fontweight', 'bold')
elif weight <= 300:
text_props.setAttribute('fontweight', '300')
# Style setzen
if style_name == 'italic' or style_name == 'oblique':
text_props.setAttribute('fontstyle', 'italic')
text_props.setAttribute('fontsize', '11pt')
font_style.addElement(text_props)
doc.automaticstyles.addElement(font_style)
# Absatz mit Font-Name und Variante
p = P(stylename=font_style)
p.addText(f"{font_name} ({variant}): {lorem_ipsum}")
doc.text.addElement(p)
# Fortschrittsanzeige
if (idx + 1) % 50 == 0:
print(f" {idx + 1}/{len(fonts)} Fonts verarbeitet...")
# Dokument speichern
doc.save(output_path)
print(f"\nDokument erfolgreich gespeichert: {output_path}")
print(f"Absoluter Pfad: {os.path.abspath(output_path)}")
print(f"Anzahl der Font-Varianten: {len(fonts)}")
def main():
# Ausgabedatei
output_file = "/mnt/user-data/outputs/font_catalog.odt"
# Sicherstellen, dass das Ausgabeverzeichnis existiert
os.makedirs(os.path.dirname(output_file), exist_ok=True)
print("Sammle verfügbare System-Fonts...")
fonts = get_system_fonts()
if not fonts:
print("FEHLER: Keine Fonts gefunden!")
return 1
print(f"Gefunden: {len(fonts)} Font-Varianten")
# ODT-Dokument erstellen
create_odt_document(fonts, output_file)
return 0
if __name__ == "__main__":
exit(main())