2022-10-16 21:13:19 +03:00
|
|
|
from tkinter.font import Font
|
|
|
|
import copy
|
2022-10-21 22:28:31 +03:00
|
|
|
from typing import List, Callable, Tuple
|
2022-10-16 21:13:19 +03:00
|
|
|
|
2022-10-29 14:11:55 +03:00
|
|
|
from ..theme.theme_manager import ThemeManager
|
2022-10-16 21:13:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTkFont(Font):
|
|
|
|
"""
|
2022-10-21 22:28:31 +03:00
|
|
|
Font object with size in pixel, independent of scaling.
|
|
|
|
To get scaled tuple representation use create_scaled_tuple() method.
|
|
|
|
|
|
|
|
family The font family name as a string.
|
|
|
|
size The font height as an integer in pixel.
|
|
|
|
weight 'bold' for boldface, 'normal' for regular weight.
|
|
|
|
slant 'italic' for italic, 'roman' for unslanted.
|
|
|
|
underline 1 for underlined text, 0 for normal.
|
|
|
|
overstrike 1 for overstruck text, 0 for normal.
|
|
|
|
|
|
|
|
Tkinter Font: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/fonts.html
|
2022-10-16 21:13:19 +03:00
|
|
|
"""
|
2022-10-21 22:28:31 +03:00
|
|
|
|
2022-10-16 21:13:19 +03:00
|
|
|
def __init__(self,
|
2022-11-11 01:27:55 +03:00
|
|
|
family: str = "default",
|
|
|
|
size: int = "default",
|
2022-10-16 21:13:19 +03:00
|
|
|
weight: str = "normal",
|
|
|
|
slant: str = "roman",
|
|
|
|
underline: bool = False,
|
|
|
|
overstrike: bool = False):
|
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
self._size_configure_callback_list: List[Callable] = []
|
2022-10-16 21:13:19 +03:00
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
self._family = family
|
2022-11-11 01:27:55 +03:00
|
|
|
self._size = ThemeManager.theme["text"]["size"] if size == "default" else size
|
2022-10-21 22:28:31 +03:00
|
|
|
self._tuple_style_string = f"{weight} {slant} {'underline' if underline else ''} {'overstrike' if overstrike else ''}"
|
2022-10-16 21:13:19 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
super().__init__(family=ThemeManager.theme["text"]["font"] if family == "default" else family,
|
2022-10-21 22:28:31 +03:00
|
|
|
size=-abs(self._size),
|
2022-10-16 21:13:19 +03:00
|
|
|
weight=weight,
|
|
|
|
slant=slant,
|
|
|
|
underline=underline,
|
|
|
|
overstrike=overstrike)
|
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
def add_size_configure_callback(self, callback: Callable):
|
|
|
|
""" add function, that gets called when font got configured """
|
|
|
|
self._size_configure_callback_list.append(callback)
|
2022-10-16 21:13:19 +03:00
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
def remove_size_configure_callback(self, callback: Callable):
|
|
|
|
""" remove function, that gets called when font got configured """
|
|
|
|
self._size_configure_callback_list.remove(callback)
|
2022-10-16 21:13:19 +03:00
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
def create_scaled_tuple(self, font_scaling: float) -> Tuple[str, int, str]:
|
|
|
|
""" return scaled tuple representation of font in the form (family: str, size: int, style: str)"""
|
|
|
|
return self._family, round(self._size * font_scaling), self._tuple_style_string
|
2022-10-16 21:13:19 +03:00
|
|
|
|
|
|
|
def config(self, *args, **kwargs):
|
|
|
|
raise AttributeError("'config' is not implemented for CTk widgets. For consistency, always use 'configure' instead.")
|
|
|
|
|
|
|
|
def configure(self, **kwargs):
|
|
|
|
if "size" in kwargs:
|
|
|
|
self._size = kwargs.pop("size")
|
2022-10-21 22:28:31 +03:00
|
|
|
super().configure(size=-abs(self._size))
|
2022-10-16 21:13:19 +03:00
|
|
|
|
|
|
|
super().configure(**kwargs)
|
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
# update style string for create_scaled_tuple() method
|
|
|
|
self._tuple_style_string = f"{super().cget('weight')} {super().cget('slant')} {'underline' if super().cget('underline') else ''} {'overstrike' if super().cget('overstrike') else ''}"
|
|
|
|
|
|
|
|
# call all functions registered with add_size_configure_callback()
|
|
|
|
for callback in self._size_configure_callback_list:
|
|
|
|
callback()
|
|
|
|
|
|
|
|
def cget(self, attribute_name: str) -> any:
|
2022-10-16 21:13:19 +03:00
|
|
|
if attribute_name == "size":
|
|
|
|
return self._size
|
|
|
|
else:
|
2022-10-21 22:28:31 +03:00
|
|
|
return super().cget(attribute_name)
|
2022-10-16 21:13:19 +03:00
|
|
|
|
|
|
|
def copy(self) -> "CTkFont":
|
|
|
|
return copy.deepcopy(self)
|