@@ -12,8 +12,10 @@ module fortplot_pdf_axes
1212 draw_mixed_font_text, draw_rotated_mixed_font_text, &
1313 draw_pdf_mathtext, estimate_pdf_text_width
1414 use fortplot_text_helpers, only: prepare_mathtext_if_needed
15- use fortplot_text_layout, only: has_mathtext
15+ use fortplot_text_layout, only: has_mathtext, preprocess_math_text
1616 use fortplot_latex_parser, only: process_latex_in_text
17+ use fortplot_mathtext, only: mathtext_element_t, parse_mathtext
18+ use fortplot_pdf_mathtext_render, only: render_mathtext_element_pdf
1719 use fortplot_axes, only: compute_scale_ticks, format_tick_label, MAX_TICKS
1820 use fortplot_tick_calculation, only: determine_decimals_from_ticks, &
1921 format_tick_value_consistent
@@ -558,18 +560,74 @@ end subroutine render_mixed_text
558560
559561 subroutine render_rotated_mixed_text (ctx , x , y , text )
560562 ! ! Helper: process LaTeX and render rotated mixed-font ylabel
561- ! ! Uses same logic as PNG: process LaTeX ONLY, no Unicode conversion
563+ ! ! Now supports mathtext rendering for ylabel with $...$ delimiters
562564 type (pdf_context_core), intent (inout ) :: ctx
563565 real (wp), intent (in ) :: x, y
564566 character (len=* ), intent (in ) :: text
565567 character (len= 512 ) :: processed
566568 integer :: plen
569+ character (len= 600 ) :: math_ready
570+ integer :: mlen
567571
568- ! Process LaTeX commands ONLY (same as PNG does)
572+ ! Process LaTeX commands
569573 call process_latex_in_text(text, processed, plen)
570- call draw_rotated_mixed_font_text(ctx, x, y, processed(1 :plen))
574+
575+ ! Check if mathtext is present ($...$ delimiters)
576+ call prepare_mathtext_if_needed(processed(1 :plen), math_ready, mlen)
577+
578+ if (has_mathtext(math_ready(1 :mlen))) then
579+ ! For mathtext, we need to use a rotated mathtext renderer
580+ ! Since draw_pdf_mathtext doesn't support rotation, we'll use
581+ ! the text matrix approach with mathtext rendering
582+ call draw_rotated_pdf_mathtext(ctx, x, y, math_ready(1 :mlen))
583+ else
584+ call draw_rotated_mixed_font_text(ctx, x, y, processed(1 :plen))
585+ end if
571586 end subroutine render_rotated_mixed_text
572587
588+ subroutine draw_rotated_pdf_mathtext (ctx , x , y , text )
589+ ! ! Draw rotated mathtext for ylabel
590+ ! ! Uses text matrix rotation with mathtext rendering
591+ type (pdf_context_core), intent (inout ) :: ctx
592+ real (wp), intent (in ) :: x, y
593+ character (len=* ), intent (in ) :: text
594+ character (len= 1024 ) :: matrix_cmd
595+ character (len= 2048 ) :: preprocessed_text
596+ integer :: processed_len
597+ character (len= 4096 ) :: math_ready
598+ integer :: mlen
599+ type (mathtext_element_t), allocatable :: elements(:)
600+ real (wp) :: x_pos
601+ integer :: i
602+
603+ ! Process text for mathtext
604+ call process_latex_in_text(text, preprocessed_text, processed_len)
605+ call preprocess_math_text(preprocessed_text(1 :processed_len), math_ready, mlen)
606+
607+ ! Parse mathtext elements
608+ elements = parse_mathtext(math_ready(1 :mlen))
609+
610+ ! Begin text object with rotation matrix (90 degrees counterclockwise)
611+ ctx% stream_data = ctx% stream_data // ' BT' // new_line(' a' )
612+
613+ ! Set font
614+ write (matrix_cmd, ' ("/F", I0, 1X, F0.1, " Tf")' ) &
615+ ctx% fonts% get_helvetica_obj(), PDF_LABEL_SIZE
616+ ctx% stream_data = ctx% stream_data // trim (adjustl (matrix_cmd)) // new_line(' a' )
617+
618+ ! Set rotation matrix: [0 1 -1 0 x y] for 90-degree rotation
619+ write (matrix_cmd, ' ("0 1 -1 0 ", F0.3, 1X, F0.3, " Tm")' ) x, y
620+ ctx% stream_data = ctx% stream_data // trim (adjustl (matrix_cmd)) // new_line(' a' )
621+
622+ ! Render mathtext elements in rotated context
623+ x_pos = 0.0_wp
624+ do i = 1 , size (elements)
625+ call render_mathtext_element_pdf(ctx, elements(i), x_pos, 0.0_wp , PDF_LABEL_SIZE)
626+ end do
627+
628+ ctx% stream_data = ctx% stream_data // ' ET' // new_line(' a' )
629+ end subroutine draw_rotated_pdf_mathtext
630+
573631 subroutine draw_pdf_y_labels_with_overlap_detection (ctx , y_positions , y_labels , num_y , plot_left , canvas_height )
574632 ! ! Draw Y-axis labels with overlap detection to prevent clustering
575633 type (pdf_context_core), intent (inout ) :: ctx
0 commit comments