<commit_before>#!/usr/bin/env python
# coding: utf8

"""
Renderer for the keyboard and the text box used to display
the current text.
"""

import os.path as osp
import pygame  # pylint: disable=import-error

from . import vkeys


def fit_font(font_name, max_height):
    """Set the size of the font to fit the given height.

    This function uses the binary search algorithm to go faster
    than a one-by-one try.

    Parameters
    ----------
    font_name:
        Path to font file for rendering key.
    max_height:
        Height to fit.
    """
    font = pygame.font.Font(font_name, 1)

    # Ensure a large panel of characters heights
    text = "?/|!()§&@0123456789azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN"  # noqa

    start = 1
    end = max_height * 2

    while start < end:
        k = (start + end) // 2
        font = pygame.font.Font(font_name, k)
        height = font.size(text)[1]
        if height > max_height:
            end = k
        else:
            start = k + 1

    return font


def draw_round_rect(surface, color, rect, radius=0.1, width=0):
    """Draw a rounded rectangle.

    Parameters
    ----------
    surface:<commit_msg>Buffer to avoid reaching the limit of opened Font files<commit_after>#!/usr/bin/env python
# coding: utf8

"""
Renderer for the keyboard and the text box used to display
the current text.
"""

import os.path as osp
import pygame  # pylint: disable=import-error

from . import vkeys


def get_font(font_name, size):
    """Get font object from font name and size.

    This method is used to optimize the number of opened font files
    and avoid reaching the limit of the system (each call to Font()
    class will open the font file, and let it opened).
    """
    if osp.isfile(font_name):
        if not hasattr(pygame.font, 'loaded'):
            setattr(pygame.font, 'loaded', {})
        return pygame.font.loaded.setdefault((font_name, size), pygame.font.Font(font_name, size))
    else:
        return pygame.font.Font(font_name, size)


def fit_font(font_name, max_height):
    """Set the size of the font to fit the given height.

    This function uses the binary search algorithm to go faster
    than a one-by-one try.

    Parameters
    ----------
    font_name:
        Path to font file for rendering key.
    max_height:
        Height to fit.
    """
    font = get_font(font_name, 1)

    # Ensure a large panel of characters heights
    text = "?/|!()§&@0123456789azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN"  # noqa

    start = 1
    end = max_height * 2

    while start < end:
        k = (start + end) // 2
        font = get_font(font_name, k)
        height = font.size(text)[1]
        if height > max_height:
            end = k
        else:
            start = k + 1

    return font


def draw_round_rect(surface, color, rect, radius=0.1, width=0):
    """Draw a rounded rectangle.

    Parameters
    ----------
    surface:
{'fraction_non_alphanumeric': 0.10281642348150662, 'fraction_numerical': 0.013573125212080081, 'mean_word_length': 2.376861397479954, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1759262', 'n_tokens_mistral': 979, 'n_tokens_neox': 916, 'n_words': 316}