# LVGL 9 Font-Patch für lv_font_conv Output

`lv_font_conv` (v1.5.3) generiert LVGL-8-kompatibles Format. Für LVGL 9 müssen 5 Änderungen an jeder generierten `.c`-Datei vorgenommen werden.

## Automatisierter Patch

### Python-Skript: `patch_lvgl9_font.py`

```python
import re, sys, pathlib

def patch_lvgl9_font(filepath):
    content = pathlib.Path(filepath).read_text()
    
    # 1. Remove #if LVGL_VERSION_MAJOR >= 8 guards
    content = re.sub(
        r'#if LVGL_VERSION_MAJOR >= 8\n(.+?)#endif\n',
        r'\1',
        content,
        flags=re.DOTALL
    )
    
    # 2. Remove .cache field from font_dsc
    content = re.sub(
        r'\.cache = NULL,\n',
        '',
        content
    )
    
    # 3. Add release_glyph, kerning, static_bitmap to font_dsc
    content = re.sub(
        r'(\.get_glyph_dsc = get_glyph_dsc,\n)(\.get_glyph_bitmap = get_glyph_bitmap,)',
        r'\1    .release_glyph = NULL,\n    .kerning = 0,\n    .static_bitmap = 0,\n    \2',
        content
    )
    
    # 4. Add fallback and user_data
    content = re.sub(
        r'(};\n\n)(const lv_font_t)',
        r'    .fallback = NULL,\n    .user_data = NULL,\n\1\2',
        content
    )
    
    pathlib.Path(filepath).write_text(content)
    print(f"Patched: {filepath}")

if __name__ == "__main__":
    for f in sys.argv[1:]:
        patch_lvgl9_font(f)
```

### Einzeiler für alle Font-Dateien

```bash
python3 patch_lvgl9_font.py firmware/src/font_*.c
```

## Manuelle Änderungen (Fallback)

Wenn das Skript fehlschlägt, hier die 5 manuellen Schritte:

### 1. `#if` Guards entfernen

**Vorher:**
```c
#if LVGL_VERSION_MAJOR >= 8
static lv_font_fmt_txt_glyph_cache_t cache;
#endif
```

**Nachher:** Alles zwischen `#if` und `#endif` entfernen, inklusive der Direktiven selbst.

### 2. `.cache` Feld entfernen

**Vorher:**
```c
static lv_font_fmt_txt_dsc_t font_dsc = {
    .glyph_bitmap = glyph_bitmap,
    .glyph_dsc = glyph_dsc,
    .cmaps = cmaps,
    .kern_dsc = NULL,
    .kern_scale = 0,
    .cmap_num = 1,
    .bpp = 4,
    .kern_classes = 0,
    .bitmap_format = 0,
    .cache = &cache  // <-- DIES ENTFERNEN
};
```

**Nachher:** Zeile `.cache = &cache,` löschen.

### 3. Neue Felder in font_dsc einfügen

**Nachher:**
```c
static lv_font_fmt_txt_dsc_t font_dsc = {
    .glyph_bitmap = glyph_bitmap,
    .glyph_dsc = glyph_dsc,
    .cmaps = cmaps,
    .kern_dsc = NULL,
    .kern_scale = 0,
    .cmap_num = 1,
    .bpp = 4,
    .kern_classes = 0,
    .bitmap_format = 0,
    .release_glyph = NULL,   // NEU
    .kerning = 0,            // NEU
    .static_bitmap = 0       // NEU
};
```

### 4. `fallback` und `user_data` zum Font-Struct hinzufügen

**Vorher:**
```c
const lv_font_t font_myfont_56 = {
    .dsc = &font_dsc,
    .fallback = NULL,  // <-- falls vorhanden
    .user_data = NULL  // <-- falls vorhanden
};
```

**Nachher:** Stelle sicher, dass beide Felder existieren:
```c
const lv_font_t font_myfont_56 = {
    .dsc = &font_dsc,
    .fallback = NULL,
    .user_data = NULL
};
```

## Wichtig: `--no-compress` Flag

Beim `lv_font_conv`-Aufruf muss `--no-compress` gesetzt sein, sonst funktioniert das Font mit LVGL 9 nicht:

```bash
lv_font_conv --font MyFont.ttf -r 0x20-0x7E \
  --size 56 --format lvgl --bpp 4 --no-compress \
  -o font_my_56.c --lv-include "lvgl.h"
```

## Fehler-Symptome wenn nicht gepatched

- Font compiliert ohne Fehler
- Aber rendert **unsichtbar** (keine Glyphen sichtbar)
- Oder kompiliert gar nicht (Missing fields in struct initializer)

## Testing

```bash
# Nach Patch kompilieren
pio run -d firmware

# Wenn Compilation fehlschlägt → Patch-Log prüfen
# Wenn Compilation ok aber unsichtbar → --no-compress vergessen?
```
