afegit fallback a la font_gen
afegida deteccio de caracters no definits a font_gen
This commit is contained in:
@@ -40,6 +40,19 @@ _ASCII_CHARS = [chr(i) for i in range(32, 127)] # 95 chars: espacio
|
||||
_EXTENDED_CHARS = list("ÀÁÈÉÍÏÒÓÚÜÑÇàáèéíïòóúüñç¡¿«»·") # 29 chars ES/CA/VA
|
||||
ALL_CHARS = _ASCII_CHARS + _EXTENDED_CHARS # 124 total
|
||||
|
||||
# Caracteres de fallback para TTFs sin soporte de acentos/especiales.
|
||||
# Si el TTF no tiene el char, se dibuja el fallback en su celda del bitmap.
|
||||
# El .fnt sigue registrando el codepoint original → texto localizado funciona.
|
||||
CHAR_FALLBACKS: dict[str, str] = {
|
||||
"À": "A", "Á": "A", "È": "E", "É": "E",
|
||||
"Í": "I", "Ï": "I", "Ò": "O", "Ó": "O",
|
||||
"Ú": "U", "Ü": "U", "Ñ": "N", "Ç": "C",
|
||||
"à": "a", "á": "a", "è": "e", "é": "e",
|
||||
"í": "i", "ï": "i", "ò": "o", "ó": "o",
|
||||
"ú": "u", "ü": "u", "ñ": "n", "ç": "c",
|
||||
"¡": "!", "¿": "?", "«": "<", "»": ">", "·": ".",
|
||||
}
|
||||
|
||||
|
||||
def _write_fnt(
|
||||
output_fnt: str,
|
||||
@@ -190,32 +203,69 @@ def render_font(
|
||||
ascent, descent = font.getmetrics()
|
||||
box_height = box_height_override if box_height_override is not None else (ascent + abs(descent))
|
||||
|
||||
# Filtrar chars: solo incluir los que el TTF soporta realmente.
|
||||
# Un char se descarta si su advance es 0 (el TTF no lo tiene) y no es un
|
||||
# espacio. Evita que chars sin glifo aparezcan con width=1 en el .fnt,
|
||||
# lo que causaría solapamiento masivo al renderizar texto localizado.
|
||||
chars_to_render = [
|
||||
ch for ch in ALL_CHARS
|
||||
if ch.isspace() or font.getlength(ch) >= 1.0
|
||||
]
|
||||
skipped = [ch for ch in ALL_CHARS if ch not in chars_to_render]
|
||||
if skipped:
|
||||
print(f"Omitidos : {len(skipped)} chars sin soporte en este TTF: {''.join(skipped)}")
|
||||
# Calcular y_offset antes de la clasificación (necesario para detectar .notdef)
|
||||
cap_tops = [font.getbbox(ch)[1] for ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if font.getbbox(ch)]
|
||||
y_offset = -round(sum(cap_tops) / len(cap_tops)) if cap_tops else 0
|
||||
|
||||
# Detectar el glifo .notdef: algunos TTF devuelven un glifo sustituto (con
|
||||
# advance > 0 y bbox válido) para chars que no tienen en su cmap. Esto provoca
|
||||
# falsos positivos en el test getlength >= 1. Se renderiza U+FFFE (garantizado
|
||||
# ausente en cualquier fuente de uso normal) y se guardan sus píxeles como
|
||||
# referencia. Cualquier char con píxeles idénticos se considera no soportado.
|
||||
_tmp_w = box_width_override or 32
|
||||
_nd_bbox = font.getbbox(chr(0xFFFE))
|
||||
if _nd_bbox:
|
||||
_nd_img = Image.new("RGBA", (_tmp_w, box_height), (0, 0, 0, 0))
|
||||
ImageDraw.Draw(_nd_img).text((-_nd_bbox[0], y_offset), chr(0xFFFE),
|
||||
font=font, fill=(255, 255, 255, 255))
|
||||
_notdef_bytes = _nd_img.tobytes()
|
||||
else:
|
||||
_notdef_bytes = None
|
||||
|
||||
def _is_notdef(ch: str) -> bool:
|
||||
"""True si el char renderiza el glifo .notdef en lugar de un glifo real."""
|
||||
if _notdef_bytes is None:
|
||||
return False
|
||||
bbox = font.getbbox(ch)
|
||||
if not bbox:
|
||||
return True
|
||||
img = Image.new("RGBA", (_tmp_w, box_height), (0, 0, 0, 0))
|
||||
ImageDraw.Draw(img).text((-bbox[0], y_offset), ch, font=font, fill=(255, 255, 255, 255))
|
||||
return img.tobytes() == _notdef_bytes
|
||||
|
||||
# Clasificar chars: soportados nativamente, con fallback, o sin soporte.
|
||||
# Un char tiene soporte nativo si su advance >= 1 Y no renderiza .notdef.
|
||||
# Si no, se busca en CHAR_FALLBACKS. Sin soporte y sin fallback, se omite.
|
||||
chars_to_render: list[str] = []
|
||||
char_render_as: dict[str, str] = {} # char → qué char dibujar realmente
|
||||
truly_skipped: list[str] = []
|
||||
|
||||
for ch in ALL_CHARS:
|
||||
if ch.isspace():
|
||||
chars_to_render.append(ch)
|
||||
char_render_as[ch] = ch
|
||||
elif font.getlength(ch) >= 1.0 and not _is_notdef(ch):
|
||||
chars_to_render.append(ch)
|
||||
char_render_as[ch] = ch
|
||||
elif ch in CHAR_FALLBACKS and font.getlength(CHAR_FALLBACKS[ch]) >= 1.0:
|
||||
chars_to_render.append(ch)
|
||||
char_render_as[ch] = CHAR_FALLBACKS[ch]
|
||||
else:
|
||||
truly_skipped.append(ch)
|
||||
|
||||
if truly_skipped:
|
||||
print(f"Omitidos : {len(truly_skipped)} chars sin soporte ni fallback: {''.join(truly_skipped)}")
|
||||
fallback_used = [ch for ch, r in char_render_as.items() if r != ch]
|
||||
if fallback_used:
|
||||
print(f"Fallback : {len(fallback_used)} chars con fallback: " +
|
||||
" ".join(f"{ch}→{char_render_as[ch]}" for ch in fallback_used))
|
||||
|
||||
# Medir advance width tipográfico: solo se usa para calcular box_width del canvas
|
||||
# cuando el usuario no lo especifica. El ancho real del .fnt se mide desde píxeles.
|
||||
char_widths: dict[str, int] = {}
|
||||
for ch in chars_to_render:
|
||||
char_widths[ch] = max(1, int(font.getlength(ch)))
|
||||
|
||||
# Calcular el offset vertical para eliminar el espacio en blanco en la parte
|
||||
# superior de la celda. Muchas fuentes bitmap tienen un em-box más grande que
|
||||
# los píxeles visibles (ascent incluye espacio interno). Usamos las letras
|
||||
# mayúsculas como referencia de "cap height": su bbox[1] indica cuántos
|
||||
# píxeles en blanco hay sobre los caracteres más altos, y restamos ese valor
|
||||
# para que las mayúsculas queden alineadas al borde superior de la celda.
|
||||
cap_tops = [font.getbbox(ch)[1] for ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if font.getbbox(ch)]
|
||||
y_offset = -round(sum(cap_tops) / len(cap_tops)) if cap_tops else 0
|
||||
render_ch = char_render_as[ch]
|
||||
char_widths[ch] = max(1, int(font.getlength(render_ch)))
|
||||
|
||||
# box_width: anchura de cada celda en el bitmap.
|
||||
# Si el usuario la especifica, se usa tal cual. Si no, se calcula como el
|
||||
@@ -244,7 +294,8 @@ def render_font(
|
||||
cell_x = col * box_width
|
||||
cell_y = row * box_height
|
||||
|
||||
bbox = font.getbbox(ch)
|
||||
draw_ch = char_render_as[ch] # char que realmente se dibuja (puede ser fallback)
|
||||
bbox = font.getbbox(draw_ch)
|
||||
if not bbox:
|
||||
# Sin glifos visibles (ej. espacio): celda vacía, correcto.
|
||||
continue
|
||||
@@ -261,7 +312,7 @@ def render_font(
|
||||
# glifo al inicio de la celda, compensando el bearing izquierdo.
|
||||
char_img = Image.new("RGBA", (box_width, box_height), (0, 0, 0, 0))
|
||||
draw = ImageDraw.Draw(char_img)
|
||||
draw.text((-bbox[0], y_offset), ch, font=font, fill=(255, 255, 255, 255))
|
||||
draw.text((-bbox[0], y_offset), draw_ch, font=font, fill=(255, 255, 255, 255))
|
||||
|
||||
# Umbralizar alpha y volcar al buffer de índices
|
||||
char_bytes = char_img.tobytes()
|
||||
|
||||
Reference in New Issue
Block a user