Skip to content

Commit 2b1ce82

Browse files
committed
refactor: add compute_title_position and use in render_title_centered
1 parent f8c6978 commit 2b1ce82

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/backends/raster/fortplot_raster_axes.f90

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,37 @@ subroutine render_title_centered(raster, width, height, plot_area, title_text)
274274
character(len=*), intent(in) :: title_text
275275

276276
real(wp) :: title_px, title_py
277-
integer :: text_width
278277
integer(1) :: r, g, b
279278
character(len=500) :: processed_text, escaped_text
280279
integer :: processed_len
281280

282-
! Process LaTeX commands and Unicode
283-
call process_latex_in_text(title_text, processed_text, processed_len)
284-
call escape_unicode_for_raster(processed_text(1:processed_len), escaped_text)
285-
286-
! Calculate title position centered over plot area
287-
! Compute text width to center horizontally (left coordinate = center - width/2)
288-
text_width = calculate_text_width(trim(escaped_text))
289-
title_px = real(plot_area%left + plot_area%width / 2 - text_width / 2, wp)
290-
291-
! Y position: above plot area (like matplotlib)
292-
! Place title approximately TITLE_VERTICAL_OFFSET pixels above the plot area
293-
title_py = real(plot_area%bottom - TITLE_VERTICAL_OFFSET, wp)
281+
! Compute title position (centered like matplotlib)
282+
call compute_title_position(plot_area, title_text, processed_text, processed_len, escaped_text, title_px, title_py)
294283

295284
! Get current color and render title directly in pixel coordinates
296285
call raster%get_color_bytes(r, g, b)
297286
call render_text_to_image(raster%image_data, width, height, &
298287
int(title_px), int(title_py), trim(escaped_text), r, g, b)
299288
end subroutine render_title_centered
300289

290+
subroutine compute_title_position(plot_area, title_text, processed_text, processed_len, escaped_text, title_px, title_py)
291+
!! Compute centered title position and return processed/escaped text
292+
!! Exposed for reuse and testing; keeps render routine small and focused
293+
type(plot_area_t), intent(in) :: plot_area
294+
character(len=*), intent(in) :: title_text
295+
character(len=*), intent(out) :: processed_text
296+
integer, intent(out) :: processed_len
297+
character(len=*), intent(out) :: escaped_text
298+
real(wp), intent(out) :: title_px, title_py
299+
300+
integer :: text_width
301+
302+
call process_latex_in_text(title_text, processed_text, processed_len)
303+
call escape_unicode_for_raster(processed_text(1:processed_len), escaped_text)
304+
305+
text_width = calculate_text_width(trim(escaped_text))
306+
title_px = real(plot_area%left + plot_area%width / 2 - text_width / 2, wp)
307+
title_py = real(plot_area%bottom - TITLE_VERTICAL_OFFSET, wp)
308+
end subroutine compute_title_position
309+
301310
end module fortplot_raster_axes

0 commit comments

Comments
 (0)