classes.c12_glyph

  1from functools import cached_property
  2from loguru import logger
  3import drawBot
  4import colorama
  5from icecream import ic
  6
  7from lib import helpers, glyphs
  8
  9
 10class KGlyph:
 11    def __init__(self, source: str) -> None:
 12        """Initialize a KGlyph instance."""
 13        self.source: str = source
 14        """The glyph source identifier. Can be character, name, or unicode."""
 15
 16    @cached_property
 17    def name(self) -> str:
 18        """
 19        Returns:
 20            The glyph name, see `lib.glyphs.toName`.
 21
 22        Example:
 23            ccaron
 24        """
 25        return glyphs.toName(self.source)
 26
 27    @cached_property
 28    def char(self) -> str:
 29        """
 30        Returns:
 31            The corresponding character, see `lib.glyphs.toChar`.
 32
 33        Example:
 34            č
 35        """
 36        return glyphs.toChar(self.source)
 37
 38    @cached_property
 39    def unicode(self) -> str:
 40        """
 41        Returns:
 42            The corresponding unicode string, see `lib.glyphs.toUni`.
 43
 44        Example:
 45            uni010D
 46        """
 47        return glyphs.toUni(self.source, "full")
 48
 49    @cached_property
 50    def category(self) -> str:
 51        """Returns the glyph category, see `lib.glyphs.getCategory`."""
 52        return glyphs.getCategory(self.source)
 53
 54    def get(self, key: str, defaultValue=None):
 55        """
 56        Retrieve an attribute by key, emulating `dict.get()`.
 57
 58        Args:
 59            key: The attribute name to retrieve.
 60            defaultValue: Value to return if the attribute is not found.
 61
 62        Returns:
 63            The attribute value or defaultValue if not found.
 64        """
 65        return getattr(self, key, defaultValue)
 66
 67    def getSupported(self, font: str) -> str | None:
 68        """
 69        Find the supported glyph name in the given font.
 70
 71        Args:
 72            font: The font path to check for glyph support.
 73
 74        Returns:
 75            The name of the supported glyph in the font, or None if not found.
 76        """
 77        with drawBot.savedState():
 78            drawBot.font(font)
 79            glyphsInFont = drawBot.listFontGlyphNames()
 80
 81            possibleMatches = helpers.removeNone(
 82                [
 83                    *helpers.coerceList(self.name),
 84                    self.char,
 85                    self.unicode,
 86                    glyphs.toSnakeCase(self.source),
 87                    glyphs.toPartBase(self.source),
 88                ]
 89            )
 90
 91            for possibleMatch in possibleMatches:
 92                if possibleMatch in glyphsInFont:
 93                    return possibleMatch
 94
 95            logger.debug("Glyph not supported: {}", self.source)
 96
 97    def __str__(self) -> str:
 98        """Returns a human-friendly string representation of the KGlyph, including name, character, unicode, and category."""
 99        output = f"{colorama.Back.BLACK}KGlyph{colorama.Style.RESET_ALL}"
100
101        if self.name:
102            # Can be multiple names
103            isMultipleNames = isinstance(self.name, list)
104            names = helpers.coerceList(self.name)
105
106            output += " Name" + ("s" if isMultipleNames else " ")
107            for ni, name in enumerate(names):
108                isNotFirst = ni != 0
109                if isNotFirst:
110                    output += "\n" + " " * 12
111                output += f" {colorama.Back.LIGHTYELLOW_EX}{name.ljust(30)}{colorama.Style.RESET_ALL}"
112
113        if self.char:
114            output += f" Char {colorama.Back.BLUE}{self.char}{colorama.Style.RESET_ALL}"
115            # Justify text
116            charAllotedSpaces = 6
117            if self.name == "zerowidthspace":
118                charAllotedSpaces += 1
119            output += " " * (charAllotedSpaces - len(self.char))
120
121        unicodeToPrint = self.unicode or ""
122        output += (
123            f" Unicode {colorama.Back.CYAN}{unicodeToPrint}{colorama.Style.RESET_ALL}"
124        )
125        output += " " * (9 - len(unicodeToPrint))
126
127        output += f" Category {colorama.Back.LIGHTRED_EX}{self.category}{colorama.Style.RESET_ALL}"
128
129        return output
class KGlyph:
 11class KGlyph:
 12    def __init__(self, source: str) -> None:
 13        """Initialize a KGlyph instance."""
 14        self.source: str = source
 15        """The glyph source identifier. Can be character, name, or unicode."""
 16
 17    @cached_property
 18    def name(self) -> str:
 19        """
 20        Returns:
 21            The glyph name, see `lib.glyphs.toName`.
 22
 23        Example:
 24            ccaron
 25        """
 26        return glyphs.toName(self.source)
 27
 28    @cached_property
 29    def char(self) -> str:
 30        """
 31        Returns:
 32            The corresponding character, see `lib.glyphs.toChar`.
 33
 34        Example:
 35            č
 36        """
 37        return glyphs.toChar(self.source)
 38
 39    @cached_property
 40    def unicode(self) -> str:
 41        """
 42        Returns:
 43            The corresponding unicode string, see `lib.glyphs.toUni`.
 44
 45        Example:
 46            uni010D
 47        """
 48        return glyphs.toUni(self.source, "full")
 49
 50    @cached_property
 51    def category(self) -> str:
 52        """Returns the glyph category, see `lib.glyphs.getCategory`."""
 53        return glyphs.getCategory(self.source)
 54
 55    def get(self, key: str, defaultValue=None):
 56        """
 57        Retrieve an attribute by key, emulating `dict.get()`.
 58
 59        Args:
 60            key: The attribute name to retrieve.
 61            defaultValue: Value to return if the attribute is not found.
 62
 63        Returns:
 64            The attribute value or defaultValue if not found.
 65        """
 66        return getattr(self, key, defaultValue)
 67
 68    def getSupported(self, font: str) -> str | None:
 69        """
 70        Find the supported glyph name in the given font.
 71
 72        Args:
 73            font: The font path to check for glyph support.
 74
 75        Returns:
 76            The name of the supported glyph in the font, or None if not found.
 77        """
 78        with drawBot.savedState():
 79            drawBot.font(font)
 80            glyphsInFont = drawBot.listFontGlyphNames()
 81
 82            possibleMatches = helpers.removeNone(
 83                [
 84                    *helpers.coerceList(self.name),
 85                    self.char,
 86                    self.unicode,
 87                    glyphs.toSnakeCase(self.source),
 88                    glyphs.toPartBase(self.source),
 89                ]
 90            )
 91
 92            for possibleMatch in possibleMatches:
 93                if possibleMatch in glyphsInFont:
 94                    return possibleMatch
 95
 96            logger.debug("Glyph not supported: {}", self.source)
 97
 98    def __str__(self) -> str:
 99        """Returns a human-friendly string representation of the KGlyph, including name, character, unicode, and category."""
100        output = f"{colorama.Back.BLACK}KGlyph{colorama.Style.RESET_ALL}"
101
102        if self.name:
103            # Can be multiple names
104            isMultipleNames = isinstance(self.name, list)
105            names = helpers.coerceList(self.name)
106
107            output += " Name" + ("s" if isMultipleNames else " ")
108            for ni, name in enumerate(names):
109                isNotFirst = ni != 0
110                if isNotFirst:
111                    output += "\n" + " " * 12
112                output += f" {colorama.Back.LIGHTYELLOW_EX}{name.ljust(30)}{colorama.Style.RESET_ALL}"
113
114        if self.char:
115            output += f" Char {colorama.Back.BLUE}{self.char}{colorama.Style.RESET_ALL}"
116            # Justify text
117            charAllotedSpaces = 6
118            if self.name == "zerowidthspace":
119                charAllotedSpaces += 1
120            output += " " * (charAllotedSpaces - len(self.char))
121
122        unicodeToPrint = self.unicode or ""
123        output += (
124            f" Unicode {colorama.Back.CYAN}{unicodeToPrint}{colorama.Style.RESET_ALL}"
125        )
126        output += " " * (9 - len(unicodeToPrint))
127
128        output += f" Category {colorama.Back.LIGHTRED_EX}{self.category}{colorama.Style.RESET_ALL}"
129
130        return output
KGlyph(source: str)
12    def __init__(self, source: str) -> None:
13        """Initialize a KGlyph instance."""
14        self.source: str = source
15        """The glyph source identifier. Can be character, name, or unicode."""

Initialize a KGlyph instance.

source: str

The glyph source identifier. Can be character, name, or unicode.

name: str
17    @cached_property
18    def name(self) -> str:
19        """
20        Returns:
21            The glyph name, see `lib.glyphs.toName`.
22
23        Example:
24            ccaron
25        """
26        return glyphs.toName(self.source)
Returns:

The glyph name, see lib.glyphs.toName.

Example:

ccaron

char: str
28    @cached_property
29    def char(self) -> str:
30        """
31        Returns:
32            The corresponding character, see `lib.glyphs.toChar`.
33
34        Example:
35            č
36        """
37        return glyphs.toChar(self.source)
Returns:

The corresponding character, see lib.glyphs.toChar.

Example:

č

unicode: str
39    @cached_property
40    def unicode(self) -> str:
41        """
42        Returns:
43            The corresponding unicode string, see `lib.glyphs.toUni`.
44
45        Example:
46            uni010D
47        """
48        return glyphs.toUni(self.source, "full")
Returns:

The corresponding unicode string, see lib.glyphs.toUni.

Example:

uni010D

category: str
50    @cached_property
51    def category(self) -> str:
52        """Returns the glyph category, see `lib.glyphs.getCategory`."""
53        return glyphs.getCategory(self.source)

Returns the glyph category, see lib.glyphs.getCategory.

def get(self, key: str, defaultValue=None):
55    def get(self, key: str, defaultValue=None):
56        """
57        Retrieve an attribute by key, emulating `dict.get()`.
58
59        Args:
60            key: The attribute name to retrieve.
61            defaultValue: Value to return if the attribute is not found.
62
63        Returns:
64            The attribute value or defaultValue if not found.
65        """
66        return getattr(self, key, defaultValue)

Retrieve an attribute by key, emulating dict.get().

Arguments:
  • key: The attribute name to retrieve.
  • defaultValue: Value to return if the attribute is not found.
Returns:

The attribute value or defaultValue if not found.

def getSupported(self, font: str) -> str | None:
68    def getSupported(self, font: str) -> str | None:
69        """
70        Find the supported glyph name in the given font.
71
72        Args:
73            font: The font path to check for glyph support.
74
75        Returns:
76            The name of the supported glyph in the font, or None if not found.
77        """
78        with drawBot.savedState():
79            drawBot.font(font)
80            glyphsInFont = drawBot.listFontGlyphNames()
81
82            possibleMatches = helpers.removeNone(
83                [
84                    *helpers.coerceList(self.name),
85                    self.char,
86                    self.unicode,
87                    glyphs.toSnakeCase(self.source),
88                    glyphs.toPartBase(self.source),
89                ]
90            )
91
92            for possibleMatch in possibleMatches:
93                if possibleMatch in glyphsInFont:
94                    return possibleMatch
95
96            logger.debug("Glyph not supported: {}", self.source)

Find the supported glyph name in the given font.

Arguments:
  • font: The font path to check for glyph support.
Returns:

The name of the supported glyph in the font, or None if not found.