File size: 2,700 Bytes
17d49e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
from typing import Optional, Union
from PIL import Image, ImageDraw, ImageFont, ImageFilter

def text_draw_default(
    draw: ImageDraw.Draw,
    position: tuple,
    font: str,
    font_size: int,
    txt: str,
    txt_color: Optional[Union[str, tuple]] = None,
    variation: Optional[str] = None,
    only_size = False
):

    font = ImageFont.truetype(f"font/{font}", font_size)

    if variation: font.set_variation_by_name(variation)
    if not txt_color: txt_color = (255, 255, 255)  # Default to white
    if isinstance(txt_color, str): txt_color = hex2rgb(txt_color)

    txt_bbox = font.getbbox(txt)
    txt_size = (txt_bbox[2] - txt_bbox[0], txt_bbox[3] - txt_bbox[1])
    if not only_size:
        draw.text(position, txt, fill=txt_color, font=font)
    return txt_size

def text_draw_center(
        draw: ImageDraw.Draw,
        position: tuple,
        font: str,
        font_size: int,
        txt: str,
        txt_color: Optional[Union[str, tuple[int, int, int]]] = None,
        variation: Optional[str] = None,
        only_size = False
):
    font = ImageFont.truetype(f"font/{font}", font_size)

    # Apply variation if specified
    if variation: font.set_variation_by_name(variation)

    # Set default text color to white if not provided
    if not txt_color: txt_color = (255, 255, 255)  # Default to white
    if isinstance(txt_color, str): txt_color = hex2rgb(txt_color)

    # Calculate text size
    txt_bbox = font.getbbox(txt)
    txt_width = txt_bbox[2] - txt_bbox[0]
    txt_height = txt_bbox[3] - txt_bbox[1]

    # Calculate top-left position to center the text
    top_left_x = position[0] - txt_width / 2
    top_left_y = position[1] - txt_height / 2

    # Draw the text
    if not only_size:
        draw.text((top_left_x, top_left_y), txt, fill=txt_color, font=font)

    return int(txt_width), int(txt_height)

def text_draw_right(
    draw: ImageDraw.Draw,
    position: tuple,
    font: str,
    font_size: int,
    txt: str,
    txt_color: Optional[Union[str, tuple]] = None,
    variation: Optional[str] = None,
    only_size = False
):

    font = ImageFont.truetype(f"font/{font}", font_size)

    if variation: font.set_variation_by_name(variation)
    if not txt_color: txt_color = (255, 255, 255)  # Default to white
    if isinstance(txt_color, str): txt_color = hex2rgb(txt_color)

    txt_bbox = font.getbbox(txt)
    txt_size = (txt_bbox[2] - txt_bbox[0], txt_bbox[3] - txt_bbox[1])
    if not only_size:
        draw.text((position[0]-txt_size[0], position[1]), txt, fill=txt_color, font=font)
    return txt_size


def hex2rgb(hex_color):
    hex_color = hex_color.lstrip('#')
    return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))