Skip to content

Commit 3f98b2c

Browse files
committed
fix: center PNG title using measured text width (fixes #999)
1 parent fc70135 commit 3f98b2c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/backends/raster/fortplot_raster_axes.f90

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ subroutine render_title_centered(raster, width, height, plot_area, title_text)
273273
character(len=*), intent(in) :: title_text
274274

275275
real(wp) :: title_px, title_py
276+
integer :: text_width
276277
integer(1) :: r, g, b
277278
character(len=500) :: processed_text, escaped_text
278279
integer :: processed_len
@@ -282,11 +283,12 @@ subroutine render_title_centered(raster, width, height, plot_area, title_text)
282283
call escape_unicode_for_raster(processed_text(1:processed_len), escaped_text)
283284

284285
! Calculate title position centered over plot area
285-
! X position: center of plot area horizontally
286-
title_px = real(plot_area%left + plot_area%width / 2, wp)
286+
! Compute text width to center horizontally (left coordinate = center - width/2)
287+
text_width = calculate_text_width(trim(escaped_text))
288+
title_px = real(plot_area%left + plot_area%width / 2 - text_width / 2, wp)
287289

288-
! Y position: above plot area (like matplotlib)
289-
! Place title approximately 30 pixels above the plot area
290+
! Y position: above plot area (like matplotlib)
291+
! Place title approximately TITLE_VERTICAL_OFFSET pixels above the plot area
290292
title_py = real(plot_area%bottom - TITLE_VERTICAL_OFFSET, wp)
291293

292294
! Get current color and render title directly in pixel coordinates
@@ -295,4 +297,4 @@ subroutine render_title_centered(raster, width, height, plot_area, title_text)
295297
int(title_px), int(title_py), trim(escaped_text), r, g, b)
296298
end subroutine render_title_centered
297299

298-
end module fortplot_raster_axes
300+
end module fortplot_raster_axes

0 commit comments

Comments
 (0)