@@ -14,6 +14,7 @@ module fortplot_raster_axes
1414
1515 private
1616 public :: raster_draw_axes_and_labels, raster_render_ylabel
17+ public :: compute_title_position
1718
1819contains
1920
@@ -277,22 +278,33 @@ subroutine render_title_centered(raster, width, height, plot_area, title_text)
277278 character (len= 500 ) :: processed_text, escaped_text
278279 integer :: processed_len
279280
280- ! Process LaTeX commands and Unicode
281- call process_latex_in_text(title_text, processed_text, processed_len)
282- call escape_unicode_for_raster(processed_text(1 :processed_len), escaped_text)
283-
284- ! 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)
287-
288- ! Y position: above plot area (like matplotlib)
289- ! Place title approximately 30 pixels above the plot area
290- 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)
291283
292284 ! Get current color and render title directly in pixel coordinates
293285 call raster% get_color_bytes(r, g, b)
294286 call render_text_to_image(raster% image_data, width, height, &
295287 int (title_px), int (title_py), trim (escaped_text), r, g, b)
296288 end subroutine render_title_centered
297289
298- end module fortplot_raster_axes
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+
310+ end module fortplot_raster_axes
0 commit comments