Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
a623e85
1
Parent(s):
6fbee81
Update app.py
Browse files
app.py
CHANGED
@@ -125,8 +125,9 @@ class FontManager:
|
|
125 |
], check=True)
|
126 |
subprocess.run([
|
127 |
"apt-get", "install", "-y",
|
128 |
-
"fonts-liberation",
|
129 |
-
"fontconfig"
|
|
|
130 |
], check=True)
|
131 |
|
132 |
# Clear font cache
|
@@ -139,34 +140,63 @@ class FontManager:
|
|
139 |
|
140 |
@staticmethod
|
141 |
def get_font_paths() -> Dict[str, str]:
|
142 |
-
"""Get the paths to the required fonts"""
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
font_paths = {
|
145 |
-
'regular':
|
146 |
-
'bold':
|
147 |
}
|
148 |
|
149 |
-
#
|
150 |
-
|
151 |
'regular': [
|
152 |
-
'
|
153 |
-
'
|
154 |
-
'
|
155 |
],
|
156 |
'bold': [
|
157 |
-
'
|
158 |
-
'
|
159 |
-
'
|
160 |
]
|
161 |
}
|
162 |
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
for style in ['regular', 'bold']:
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
return font_paths
|
172 |
|
@@ -189,10 +219,12 @@ class CertificateGenerator:
|
|
189 |
"""Load fonts with fallbacks"""
|
190 |
fonts = {}
|
191 |
try:
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
196 |
except Exception as e:
|
197 |
print(f"Font loading error: {e}. Using default font.")
|
198 |
default = ImageFont.load_default()
|
|
|
125 |
], check=True)
|
126 |
subprocess.run([
|
127 |
"apt-get", "install", "-y",
|
128 |
+
"fonts-liberation", # Liberation Sans fonts
|
129 |
+
"fontconfig", # Font configuration
|
130 |
+
"fonts-dejavu-core" # DejaVu fonts as fallback
|
131 |
], check=True)
|
132 |
|
133 |
# Clear font cache
|
|
|
140 |
|
141 |
@staticmethod
|
142 |
def get_font_paths() -> Dict[str, str]:
|
143 |
+
"""Get the paths to the required fonts with multiple fallbacks"""
|
144 |
+
standard_paths = [
|
145 |
+
"/usr/share/fonts",
|
146 |
+
"/usr/local/share/fonts",
|
147 |
+
"/usr/share/fonts/truetype",
|
148 |
+
"~/.fonts"
|
149 |
+
]
|
150 |
+
|
151 |
font_paths = {
|
152 |
+
'regular': None,
|
153 |
+
'bold': None
|
154 |
}
|
155 |
|
156 |
+
# Common font filenames to try
|
157 |
+
fonts_to_try = {
|
158 |
'regular': [
|
159 |
+
'LiberationSans-Regular.ttf',
|
160 |
+
'DejaVuSans.ttf',
|
161 |
+
'FreeSans.ttf'
|
162 |
],
|
163 |
'bold': [
|
164 |
+
'LiberationSans-Bold.ttf',
|
165 |
+
'DejaVuSans-Bold.ttf',
|
166 |
+
'FreeSans-Bold.ttf'
|
167 |
]
|
168 |
}
|
169 |
|
170 |
+
def find_font(font_name: str) -> Optional[str]:
|
171 |
+
"""Search for a font file in standard locations"""
|
172 |
+
for base_path in standard_paths:
|
173 |
+
for root, _, files in os.walk(os.path.expanduser(base_path)):
|
174 |
+
if font_name in files:
|
175 |
+
return os.path.join(root, font_name)
|
176 |
+
return None
|
177 |
+
|
178 |
+
# Try to find each font
|
179 |
for style in ['regular', 'bold']:
|
180 |
+
for font_name in fonts_to_try[style]:
|
181 |
+
font_path = find_font(font_name)
|
182 |
+
if font_path:
|
183 |
+
font_paths[style] = font_path
|
184 |
+
break
|
185 |
+
|
186 |
+
# If no fonts found, try using fc-match as fallback
|
187 |
+
if not all(font_paths.values()):
|
188 |
+
try:
|
189 |
+
for style in ['regular', 'bold']:
|
190 |
+
if not font_paths[style]:
|
191 |
+
result = subprocess.run(
|
192 |
+
['fc-match', '-f', '%{file}', 'sans-serif:style=' + style],
|
193 |
+
capture_output=True,
|
194 |
+
text=True
|
195 |
+
)
|
196 |
+
if result.returncode == 0 and result.stdout.strip():
|
197 |
+
font_paths[style] = result.stdout.strip()
|
198 |
+
except Exception as e:
|
199 |
+
print(f"Warning: Could not use fc-match to find fonts: {e}")
|
200 |
|
201 |
return font_paths
|
202 |
|
|
|
219 |
"""Load fonts with fallbacks"""
|
220 |
fonts = {}
|
221 |
try:
|
222 |
+
if self.font_paths['regular'] and self.font_paths['bold']:
|
223 |
+
fonts['title'] = ImageFont.truetype(self.font_paths['bold'], 60)
|
224 |
+
fonts['text'] = ImageFont.truetype(self.font_paths['regular'], 40)
|
225 |
+
fonts['subtitle'] = ImageFont.truetype(self.font_paths['regular'], 30)
|
226 |
+
else:
|
227 |
+
raise ValueError("No suitable fonts found")
|
228 |
except Exception as e:
|
229 |
print(f"Font loading error: {e}. Using default font.")
|
230 |
default = ImageFont.load_default()
|