Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -77,6 +77,14 @@ def create_zip(svg_files):
|
|
| 77 |
# ------------------------
|
| 78 |
# UFO / FONTMAKE
|
| 79 |
# ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def build_ufo_from_glyphs(glyphs):
|
| 81 |
font = Font()
|
| 82 |
font.info.familyName = "TipTopType"
|
|
@@ -88,28 +96,25 @@ def build_ufo_from_glyphs(glyphs):
|
|
| 88 |
font.info.descender = -200
|
| 89 |
font.info.capHeight = 700
|
| 90 |
font.info.xHeight = 500
|
| 91 |
-
|
| 92 |
for letter, path_data in glyphs:
|
|
|
|
|
|
|
|
|
|
| 93 |
glyph = Glyph()
|
| 94 |
glyph.name = letter
|
| 95 |
glyph.unicode = ord(letter)
|
| 96 |
pen = glyph.getPen()
|
| 97 |
-
|
| 98 |
try:
|
| 99 |
# Analyser le chemin SVG
|
| 100 |
path = parse_path(path_data)
|
| 101 |
-
|
| 102 |
# Créer un nouveau chemin SVG pour normaliser et simplifier
|
| 103 |
normalized_path = SvgPath()
|
| 104 |
-
|
| 105 |
for segment in path:
|
| 106 |
if segment.length() == 0:
|
| 107 |
continue
|
| 108 |
-
|
| 109 |
# Commencer un nouveau sous-chemin
|
| 110 |
if not normalized_path or not normalized_path[-1].end == segment.start:
|
| 111 |
normalized_path.append(Line(start=segment.start, end=segment.start))
|
| 112 |
-
|
| 113 |
# Ajouter le segment au chemin normalisé
|
| 114 |
if isinstance(segment, Line):
|
| 115 |
normalized_path.append(Line(start=normalized_path[-1].end, end=segment.end))
|
|
@@ -138,7 +143,6 @@ def build_ufo_from_glyphs(glyphs):
|
|
| 138 |
# Convertir l'arc en une série de courbes cubiques
|
| 139 |
# Pour simplifier, nous allons approximer l'arc avec une ligne droite
|
| 140 |
normalized_path.append(Line(start=normalized_path[-1].end, end=segment.end))
|
| 141 |
-
|
| 142 |
# Dessiner le chemin normalisé dans le glyphe
|
| 143 |
if normalized_path:
|
| 144 |
# Calculer les limites du chemin pour centrer et mettre à l'échelle
|
|
@@ -147,13 +151,11 @@ def build_ufo_from_glyphs(glyphs):
|
|
| 147 |
# Définir la largeur du glyphe (par exemple, 80% de 1000 unités)
|
| 148 |
glyph_width = 800
|
| 149 |
glyph_height = 1000 # unités par em
|
| 150 |
-
|
| 151 |
# Calculer le facteur d'échelle pour que le glyphe tienne dans la hauteur
|
| 152 |
scale_factor = glyph_height * 0.8 / bounds.height
|
| 153 |
# Calculer le décalage pour centrer le glyphe horizontalement
|
| 154 |
offset_x = (glyph_width - bounds.width * scale_factor) / 2
|
| 155 |
offset_y = -bounds.bottom * scale_factor # décalage pour positionner la base sur la ligne de base
|
| 156 |
-
|
| 157 |
# Appliquer la transformation aux points du chemin
|
| 158 |
for segment in normalized_path:
|
| 159 |
# Appliquer la transformation aux points du segment
|
|
@@ -164,11 +166,9 @@ def build_ufo_from_glyphs(glyphs):
|
|
| 164 |
new_x = x * scale_factor + offset_x
|
| 165 |
new_y = y * scale_factor + offset_y
|
| 166 |
return (new_x, -new_y) # Inverser Y car les systèmes de coordonnées sont différents
|
| 167 |
-
|
| 168 |
start_point = transform_point(segment.start)
|
| 169 |
if len(normalized_path) == 1 or segment == normalized_path[0]:
|
| 170 |
pen.moveTo(start_point)
|
| 171 |
-
|
| 172 |
if isinstance(segment, Line):
|
| 173 |
end_point = transform_point(segment.end)
|
| 174 |
pen.lineTo(end_point)
|
|
@@ -177,11 +177,9 @@ def build_ufo_from_glyphs(glyphs):
|
|
| 177 |
control2 = transform_point(segment.control2)
|
| 178 |
end_point = transform_point(segment.end)
|
| 179 |
pen.curveTo(control1, control2, end_point)
|
| 180 |
-
|
| 181 |
# Fermer le chemin si nécessaire
|
| 182 |
if normalized_path.isclosed():
|
| 183 |
pen.closePath()
|
| 184 |
-
|
| 185 |
# Définir la largeur du glyphe
|
| 186 |
glyph.width = glyph_width
|
| 187 |
print(f"Glyph {letter} created with SVG path.")
|
|
@@ -202,30 +200,36 @@ def save_otf_font(glyphs, font_name="TipTopType-Regular.otf"):
|
|
| 202 |
if not glyphs:
|
| 203 |
print("No glyphs provided to generate OTF font.")
|
| 204 |
return None
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
| 214 |
project.run_from_ufos([ufo_path], output=["otf"], output_dir=output_dir)
|
| 215 |
print("Fontmake ran successfully.")
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
# ------------------------
|
| 231 |
# GENERATION
|
|
|
|
| 77 |
# ------------------------
|
| 78 |
# UFO / FONTMAKE
|
| 79 |
# ------------------------
|
| 80 |
+
def validate_svg_path(path_data):
|
| 81 |
+
try:
|
| 82 |
+
parse_path(path_data)
|
| 83 |
+
return True
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Invalid SVG path: {e}")
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
def build_ufo_from_glyphs(glyphs):
|
| 89 |
font = Font()
|
| 90 |
font.info.familyName = "TipTopType"
|
|
|
|
| 96 |
font.info.descender = -200
|
| 97 |
font.info.capHeight = 700
|
| 98 |
font.info.xHeight = 500
|
|
|
|
| 99 |
for letter, path_data in glyphs:
|
| 100 |
+
if not validate_svg_path(path_data):
|
| 101 |
+
print(f"Skipping glyph {letter} due to invalid SVG path.")
|
| 102 |
+
continue
|
| 103 |
glyph = Glyph()
|
| 104 |
glyph.name = letter
|
| 105 |
glyph.unicode = ord(letter)
|
| 106 |
pen = glyph.getPen()
|
|
|
|
| 107 |
try:
|
| 108 |
# Analyser le chemin SVG
|
| 109 |
path = parse_path(path_data)
|
|
|
|
| 110 |
# Créer un nouveau chemin SVG pour normaliser et simplifier
|
| 111 |
normalized_path = SvgPath()
|
|
|
|
| 112 |
for segment in path:
|
| 113 |
if segment.length() == 0:
|
| 114 |
continue
|
|
|
|
| 115 |
# Commencer un nouveau sous-chemin
|
| 116 |
if not normalized_path or not normalized_path[-1].end == segment.start:
|
| 117 |
normalized_path.append(Line(start=segment.start, end=segment.start))
|
|
|
|
| 118 |
# Ajouter le segment au chemin normalisé
|
| 119 |
if isinstance(segment, Line):
|
| 120 |
normalized_path.append(Line(start=normalized_path[-1].end, end=segment.end))
|
|
|
|
| 143 |
# Convertir l'arc en une série de courbes cubiques
|
| 144 |
# Pour simplifier, nous allons approximer l'arc avec une ligne droite
|
| 145 |
normalized_path.append(Line(start=normalized_path[-1].end, end=segment.end))
|
|
|
|
| 146 |
# Dessiner le chemin normalisé dans le glyphe
|
| 147 |
if normalized_path:
|
| 148 |
# Calculer les limites du chemin pour centrer et mettre à l'échelle
|
|
|
|
| 151 |
# Définir la largeur du glyphe (par exemple, 80% de 1000 unités)
|
| 152 |
glyph_width = 800
|
| 153 |
glyph_height = 1000 # unités par em
|
|
|
|
| 154 |
# Calculer le facteur d'échelle pour que le glyphe tienne dans la hauteur
|
| 155 |
scale_factor = glyph_height * 0.8 / bounds.height
|
| 156 |
# Calculer le décalage pour centrer le glyphe horizontalement
|
| 157 |
offset_x = (glyph_width - bounds.width * scale_factor) / 2
|
| 158 |
offset_y = -bounds.bottom * scale_factor # décalage pour positionner la base sur la ligne de base
|
|
|
|
| 159 |
# Appliquer la transformation aux points du chemin
|
| 160 |
for segment in normalized_path:
|
| 161 |
# Appliquer la transformation aux points du segment
|
|
|
|
| 166 |
new_x = x * scale_factor + offset_x
|
| 167 |
new_y = y * scale_factor + offset_y
|
| 168 |
return (new_x, -new_y) # Inverser Y car les systèmes de coordonnées sont différents
|
|
|
|
| 169 |
start_point = transform_point(segment.start)
|
| 170 |
if len(normalized_path) == 1 or segment == normalized_path[0]:
|
| 171 |
pen.moveTo(start_point)
|
|
|
|
| 172 |
if isinstance(segment, Line):
|
| 173 |
end_point = transform_point(segment.end)
|
| 174 |
pen.lineTo(end_point)
|
|
|
|
| 177 |
control2 = transform_point(segment.control2)
|
| 178 |
end_point = transform_point(segment.end)
|
| 179 |
pen.curveTo(control1, control2, end_point)
|
|
|
|
| 180 |
# Fermer le chemin si nécessaire
|
| 181 |
if normalized_path.isclosed():
|
| 182 |
pen.closePath()
|
|
|
|
| 183 |
# Définir la largeur du glyphe
|
| 184 |
glyph.width = glyph_width
|
| 185 |
print(f"Glyph {letter} created with SVG path.")
|
|
|
|
| 200 |
if not glyphs:
|
| 201 |
print("No glyphs provided to generate OTF font.")
|
| 202 |
return None
|
| 203 |
+
try:
|
| 204 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 205 |
+
ufo_path = os.path.join(tmpdir, "font.ufo")
|
| 206 |
+
font = build_ufo_from_glyphs(glyphs)
|
| 207 |
+
font.save(ufo_path)
|
| 208 |
+
print(f"UFO file saved at: {ufo_path}")
|
| 209 |
+
|
| 210 |
+
output_dir = os.path.join(tmpdir, "out")
|
| 211 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 212 |
+
|
| 213 |
+
project = FontProject()
|
| 214 |
project.run_from_ufos([ufo_path], output=["otf"], output_dir=output_dir)
|
| 215 |
print("Fontmake ran successfully.")
|
| 216 |
+
|
| 217 |
+
otf_files = glob.glob(os.path.join(output_dir, "**/*.otf"), recursive=True)
|
| 218 |
+
if not otf_files:
|
| 219 |
+
print("No OTF files generated by fontmake.")
|
| 220 |
+
return None
|
| 221 |
+
|
| 222 |
+
generated_path = otf_files[0]
|
| 223 |
+
print(f"OTF file generated at: {generated_path}")
|
| 224 |
+
|
| 225 |
+
final_path = os.path.join(tempfile.gettempdir(), font_name)
|
| 226 |
+
os.replace(generated_path, final_path)
|
| 227 |
+
print(f"OTF file moved to: {final_path}")
|
| 228 |
+
|
| 229 |
+
return final_path
|
| 230 |
+
except Exception as e:
|
| 231 |
+
print(f"Error in save_otf_font: {e}")
|
| 232 |
+
return None
|
| 233 |
|
| 234 |
# ------------------------
|
| 235 |
# GENERATION
|