def get_objs_for_icon_rendering(font_path, char, dimension_name, dimension_value):
    """Return font and bounding rect for the given arguments.

    The dimension of the cropped char will be equal to the
    given one or at least as close as possible without
    surpassing it.

    The font object is a pygame.font.Font instance.

    This is achieved by trial and error, that is, by
    instantiating fonts and checking whether a cropped surf
    produced from them has the desired dimension. Such trial
    and error is quick enough as to being imperceptible.

    Parameters
    ==========
    check the docstring of the
    get_objs_for_icon_rendering function to know
    about the parameters in this function.
    """
    ### define an index to retrieve the appropriate dimension
    ### based on the dimension name

    if dimension_name == "height":
        index = 1
    elif dimension_name == "width":
        index = 0

    else:
        raise ValueError("'dimension_name' must be either 'width'" " or 'height'")

    ### define a foreground color (it can be any color)
    foreground_color = (0,) * 3

    ### utility function

    def get_size_results(font_size):

        ### create font and produce a surface with the
        ### given char, using its "render" method; the
        ### render method must not receive a background
        ### color, otherwise the bounding rect (the cropped
        ### area) would be the whole area of the surface

        font = Font(font_path, font_size)
        full_surf = font.render(char, True, foreground_color)

        ### obtain the bounding rect and the cropped char

        bounding_rect = full_surf.get_bounding_rect()

        cropped_surf = full_surf.subsurface(bounding_rect).copy()

        return (
            full_surf.get_size()[index],
            cropped_surf.get_size()[index],
            bounding_rect,
            font,
        )

    ### calculate a first size to be attempted; such size
    ### must be based on the ratio between the heights of
    ### a full surface and its cropped area

    full_dimension, cropped_dimension = get_size_results(dimension_value)[:2]

    ratio = full_dimension / cropped_dimension

    font_size = round(dimension_value * ratio)

    ### create a set to keep track of the attempted sizes
    attempted_sizes = set()

    ### create variable to store the chosen font
    chosen_font = None

    ### create variable to store highest dimension achieved
    ### which doesn't surpass the desired dimension (but
    ### can be equal)
    highest_dimension = 0

    while font_size not in attempted_sizes:

        ### grab dimension of crooped surface for given
        ### font size and also the bounding rect
        ### (the bounding rect will only be used outside
        ### the "while loop", but must be referenced here);
        ### also pick the font
        current_dimension, bounding_rect, font = get_size_results(font_size)[1:]

        ### store current font size as an attempted one since
        ### we just tried it
        attempted_sizes.add(font_size)

        ### if the dimension of the surf isn't the amount
        ### we desired, increment/decrement the font size
        ### so we can try it (if it wasn't before, since
        ### the condition to keep executing the while loop
        ### is using a size which wasn't attempted yet)

        if current_dimension != dimension_value:

            font_size += 1 if current_dimension < dimension_value else -1

        ### otherwise, since we reached a font which
        ### satisfies our dimension requirement, we can
        ### break out of the loop after storing the surf
        ### dimension as the highest achieved one and the
        ### font and bounding rect as the chosen ones

        else:

            highest_dimension = current_dimension
            chosen_font = font
            chosen_bounding_rect = bounding_rect

            break

        ### if the dimension of the text surface is higher
        ### than the ones achieved until now but still
        ### equal or below the desired amount, consider it
        ### the highest dimension achieved until now, and its
        ### font and bounding rect as the chosen ones

        if highest_dimension < current_dimension <= dimension_value:

            highest_dimension = current_dimension

            chosen_font = font
            chosen_bounding_rect = bounding_rect

    ### if the highest dimension achieved isn't the same as
    ### the desired one, raise an error to notify the user

    if dimension_value != highest_dimension:

        message = (
            f"couldn't get icon of {dimension_name}"
            f" {dimension_value} from {Path(font_path).name}"
            " font file"
        )

        raise ValueError(message)

    ### finally, return the chosen font and bounding rect
    return chosen_font, chosen_bounding_rect
Return font and bounding rect for the given arguments.

    The dimension of the cropped char will be equal to the
    given one or at least as close as possible without
    surpassing it.

    The font object is a pygame.font.Font instance.

    This is achieved by trial and error, that is, by
    instantiating fonts and checking whether a cropped surf
    produced from them has the desired dimension. Such trial
    and error is quick enough as to being imperceptible.

    Parameters
    ==========
    check the docstring of the
    get_objs_for_icon_rendering function to know
    about the parameters in this function.
    
nodezator/surfsman/icon.py