fortrangoingonforty/fortty / a7f901b

Browse files

Normalize fallback font baseline to fix glyph drift

Store fallback font's ascender and adjust bearing_y when rendering
glyphs from fallback font. This compensates for different font metrics
between primary and fallback fonts, keeping all glyphs on the same
baseline.
Authored by espadonne
SHA
a7f901bdd4772e0479f9be7062f43ab2e787423f
Parents
6d71706
Tree
9040dfd

1 changed file

StatusFile+-
M src/text/font.f90 11 0
src/text/font.f90modified
@@ -19,6 +19,7 @@ module font_mod
1919
     integer :: cell_height = 0
2020
     integer :: ascender = 0
2121
     integer :: descender = 0
22
+    integer :: fallback_ascender = 0  ! For baseline alignment with fallback font
2223
     logical :: loaded = .false.
2324
     logical :: has_fallback = .false.
2425
   end type font_t
@@ -194,6 +195,7 @@ contains
194195
     character(len=*), intent(in) :: path
195196
     character(len=256) :: c_path
196197
     integer(c_int) :: err
198
+    integer(c_int) :: fb_cell_w, fb_cell_h, fb_asc, fb_desc
197199
 
198200
     if (.not. font%loaded) return
199201
     if (len_trim(path) == 0) return
@@ -209,6 +211,10 @@ contains
209211
     end if
210212
 
211213
     font%has_fallback = .true.
214
+
215
+    ! Get fallback font metrics for baseline alignment
216
+    call fortty_ft_get_metrics(font%ft_face_fallback, fb_cell_w, fb_cell_h, fb_asc, fb_desc)
217
+    font%fallback_ascender = fb_asc
212218
   end subroutine font_load_fallback
213219
 
214220
   ! Check if a glyph exists in the primary font
@@ -278,6 +284,11 @@ contains
278284
     glyph%advance = adv
279285
     glyph%valid = .true.
280286
 
287
+    ! Normalize baseline: adjust bearing_y for ascender difference between fonts
288
+    if (used_fallback) then
289
+      glyph%bearing_y = glyph%bearing_y - (font%fallback_ascender - font%ascender)
290
+    end if
291
+
281292
   end function font_render_glyph_with_fallback
282293
 
283294
   ! Find a font that supports a specific codepoint using fontconfig