fortplot_text_layout Module

Shared text layout utilities (width/height calculations, mathtext helpers)



Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: DEFAULT_FONT_SIZE = 16
integer, public, parameter :: TITLE_FONT_SIZE = 20
real(kind=wp), public, parameter :: TITLE_FONT_SIZE_PT = 14.0_wp

Title font size in points. fortplot's chosen default; matplotlib's axes.titlesize / figure.titlesize default is "large" which resolves to 12pt at the default font.size of 10pt. 14pt was chosen here to give the title a more visible weight than the axis labels rendered at DEFAULT_FONT_SIZE.

integer, public, parameter :: LABEL_FONT_SIZE = 16
integer, public, parameter :: TICK_FONT_SIZE = 13

Functions

public pure function has_mathtext(text) result(is_mathtext)

Check if text needs math layout. A matched '$...$' segment always enables math. Braced script markup ('^{...}' or '{...}') also enables math even without dollars so grouped exponents and subscripts render as scripts instead of leaking literal grouping braces. Bare unbraced '^'/'' outside dollars still render literally and are not math.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

Return Value logical

public function calculate_text_width(text) result(width)

Calculate the pixel width of text using STB TrueType with UTF-8 support Supports mathematical notation with superscripts and subscripts

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

Return Value integer

public function calculate_text_width_with_size(text, pixel_height) result(width)

Calculate text width using a specific font size Supports mathematical notation with superscripts and subscripts

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text
real(kind=wp), intent(in) :: pixel_height

Return Value integer

public function calculate_text_height(text) result(height)

Calculate the pixel height of text using STB TrueType Supports mathematical notation with superscripts and subscripts

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

Return Value integer

public function calculate_text_height_with_size(pixel_height) result(height)

Calculate text height for a given font pixel size

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: pixel_height

Return Value integer

public function calculate_text_descent(text) result(descent_pixels)

Calculate the descent (below baseline) portion of text in pixels

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

Return Value integer

public recursive function calculate_mathtext_width_internal(elements, base_font_size) result(total_width)

Calculate total width of mathematical text elements

Arguments

Type IntentOptional Attributes Name
type(mathtext_element_t), intent(in) :: elements(:)
real(kind=wp), intent(in) :: base_font_size

Return Value integer

public function calculate_text_width_with_size_internal(text, pixel_height) result(width)

Internal text width calculation to avoid circular dependencies Note: Uses len(text) not len_trim to preserve trailing spaces in mathtext

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text
real(kind=wp), intent(in) :: pixel_height

Return Value integer

public function calculate_text_height_with_size_internal(pixel_height) result(height)

Internal text height calculation based on font metrics

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: pixel_height

Return Value integer

public function calculate_mathtext_height_internal(elements, base_font_size) result(total_height)

Calculate total height of mathematical text elements

Arguments

Type IntentOptional Attributes Name
type(mathtext_element_t), intent(in) :: elements(:)
real(kind=wp), intent(in) :: base_font_size

Return Value integer


Subroutines

public subroutine preprocess_math_text(input_text, result_text, result_len)

Escape '^'/'' outside math so they render literally, and pass the '$' delimiters through so the parser can toggle math mode (and tag math runs italic). Only content between '$...$' is treated as math, matching matplotlib: bare '^'/'' outside dollars stay literal. UTF-8 aware: multi-byte characters are copied as intact sequences

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input_text
character(len=*), intent(out) :: result_text
integer, intent(out) :: result_len