Skip to content

Fonts

Manage and load font choices.

add_font_choices_to_pdf(pdf, font_options)

Add font to choices to pdf.

Takes PDF instance and checks if the selected fonts are default fonts to FPDF2. If they are not default it takes the font location from the fonts.yaml file and adds the font under its name to the pdf.

Source code in report_generator/fonts.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def add_font_choices_to_pdf(pdf: object, font_options: dict) -> object:
    """Add font to choices to pdf.

    Takes PDF instance and checks if the selected fonts are
    default fonts to FPDF2. If they are not default it takes the font location from the fonts.yaml file and adds the
    font under its name to the pdf.

    Args:
        pdf ()              - FPDF2 PDF class instantiation.
        font_options (dict) - Selected fonts and settings
    """
    config = load_config()

    config["fonts"]
    font_dict = font_dict_loader()
    for font_name, font_types in font_dict["custom_font_types"].items():
        for font_type, file_name in font_types.items():
            logger.debug(font_name, font_type, file_name)
            font_path = os.path.join(config["dir_path"], "data", "fonts")
            font_loc = os.path.join(font_path, file_name)
            if font_type == "Normal":
                font_type = ""
            pdf.add_font(font_name, font_type, font_loc)

    return pdf

check_if_default_font(font_name)

Check if choice is a default font.

Checks if the font_name passed into method is on the list of the 14 default fonts built into the PDF engine.

Returns:

Type Description
bool

bool - Whether the font is a default font or not.

Source code in report_generator/fonts.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def check_if_default_font(font_name: str) -> bool:
    """Check if choice is a default font.

    Checks if the font_name passed into method is on
    the list of the 14 default fonts built into the
    PDF engine.

    Args:
        font_name (str) - The name of the font to be
                          checked.

    Returns:
        bool            - Whether the font is a default
                          font or not.
    """

    fonts_dict = font_dict_loader()

    if fonts_dict["font_types"][font_name] is True:
        return True
    else:
        return False

font_dict_loader()

Load fonts from yaml file.

Show dict of font-name, font-location pairings based on yaml file in projects data/fonts directory.

Returns:

Type Description
dict

font-dict (dict): dict of font-name, font-file-path str values. Used to select font choices or to add font to pdf.

Source code in report_generator/fonts.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def font_dict_loader() -> dict:
    """Load fonts from yaml file.

    Show dict of font-name, font-location pairings
    based on yaml file in projects data/fonts directory.

    Returns:
        font-dict (dict): dict of font-name, font-file-path str
                          values. Used to select font choices or
                          to add font to pdf.
    """
    fonts = {}
    config = load_config()
    font_path = os.path.join(config["dir_path"], "data", "fonts")
    font_yaml = os.path.join(font_path, "fonts.yaml")
    print(os.path.isfile(font_yaml))
    with open(font_yaml, "r") as file:
        fonts = yaml.load(file, Loader=yaml.loader.SafeLoader)

    return fonts

font_selection_updater(chosen_fonts)

Update config.yaml with default font selection.

Takes a dict of chosen fonts and updates the config.yaml with the selection. Fonts included for: - Title Font, colour, font-size - Paragraph Font, colour, font-size - Heading Font, colour, font-size

Parameters:

Name Type Description Default
chosen_fonts dict

Dict

required
Source code in report_generator/fonts.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def font_selection_updater(chosen_fonts: dict) -> None:
    """Update config.yaml with default font selection.

    Takes a dict of chosen fonts and updates the config.yaml
    with the selection. Fonts included for:
        - Title Font, colour, font-size
        - Paragraph Font, colour, font-size
        - Heading Font, colour, font-size

    Args:
        chosen_fonts (dict): Dict

    """
    config = load_config()
    dump_config(config)