Skip to content

text_fonts

TextFonts

Bases: Photoshop

An installed font.

Source code in photoshop/api/_text_fonts.py
 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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class TextFonts(Photoshop):
    """An installed font."""

    def __init__(self, parent=None):
        super().__init__(parent=parent)

    """
    MAGIC METHODS
    """

    def __len__(self):
        return self.length

    def __iter__(self):
        for font in self.app:
            yield TextFont(font)

    def __contains__(self, name: str):
        """Check if a font is installed. Lookup by font postScriptName (fastest) or name.

        Args:
            name: Name or postScriptName of the font to look for.

        Returns:
            bool: True if font is found, otherwise False.
        """
        # Look for postScriptName
        if self.get(name):
            return True
        # Look for name (slow)
        for font in self:
            try:
                if font.name == name:
                    return True
            except COMError:
                continue
        return False

    def __getitem__(self, key: str):
        """Access a given TextFont using dictionary key lookup, must provide the postScriptName.

        Args:
            key: The postScriptName of the font.

        Returns:
            TextFont instance.

        """
        try:
            return TextFont(self.app[key])
        except ArgumentError:
            raise PhotoshopPythonAPIError(f'Could not find a font with postScriptName "{key}"')

    """
    METHODS
    """

    def get(self, key: str, default: Any = None) -> Union[TextFont, Any]:
        """
        Accesses a given TextFont using dictionary key lookup of postScriptName, returns default if not found.

        Args:
            key: The postScriptName of the font.
            default: Value to return if font isn't found.

        Returns:
            TextFont instance.

        """
        try:
            return TextFont(self.app[key])
        except (KeyError, ArgumentError):
            return default

    def getByName(self, name: str) -> TextFont:
        """Gets the font by the font name.

        Args:
            name: The name of the font.


        Returns:
            font instance.

        """
        for font in self.app:
            if font.name == name:
                return TextFont(font)
        raise PhotoshopPythonAPIError('Could not find a TextFont named "{name}"')

    """
    PROPERTIES
    """

    @property
    def _fonts(self):
        return [a for a in self.app]

    @property
    def length(self):
        """The number pf elements in the collection."""
        return len(self._fonts)

length property

The number pf elements in the collection.

__contains__(name)

Check if a font is installed. Lookup by font postScriptName (fastest) or name.

Parameters:

Name Type Description Default
name str

Name or postScriptName of the font to look for.

required

Returns:

Name Type Description
bool

True if font is found, otherwise False.

Source code in photoshop/api/_text_fonts.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def __contains__(self, name: str):
    """Check if a font is installed. Lookup by font postScriptName (fastest) or name.

    Args:
        name: Name or postScriptName of the font to look for.

    Returns:
        bool: True if font is found, otherwise False.
    """
    # Look for postScriptName
    if self.get(name):
        return True
    # Look for name (slow)
    for font in self:
        try:
            if font.name == name:
                return True
        except COMError:
            continue
    return False

__getitem__(key)

Access a given TextFont using dictionary key lookup, must provide the postScriptName.

Parameters:

Name Type Description Default
key str

The postScriptName of the font.

required

Returns:

Type Description

TextFont instance.

Source code in photoshop/api/_text_fonts.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __getitem__(self, key: str):
    """Access a given TextFont using dictionary key lookup, must provide the postScriptName.

    Args:
        key: The postScriptName of the font.

    Returns:
        TextFont instance.

    """
    try:
        return TextFont(self.app[key])
    except ArgumentError:
        raise PhotoshopPythonAPIError(f'Could not find a font with postScriptName "{key}"')

get(key, default=None)

Accesses a given TextFont using dictionary key lookup of postScriptName, returns default if not found.

Parameters:

Name Type Description Default
key str

The postScriptName of the font.

required
default Any

Value to return if font isn't found.

None

Returns:

Type Description
Union[TextFont, Any]

TextFont instance.

Source code in photoshop/api/_text_fonts.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def get(self, key: str, default: Any = None) -> Union[TextFont, Any]:
    """
    Accesses a given TextFont using dictionary key lookup of postScriptName, returns default if not found.

    Args:
        key: The postScriptName of the font.
        default: Value to return if font isn't found.

    Returns:
        TextFont instance.

    """
    try:
        return TextFont(self.app[key])
    except (KeyError, ArgumentError):
        return default

getByName(name)

Gets the font by the font name.

Parameters:

Name Type Description Default
name str

The name of the font.

required

Returns:

Type Description
TextFont

font instance.

Source code in photoshop/api/_text_fonts.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def getByName(self, name: str) -> TextFont:
    """Gets the font by the font name.

    Args:
        name: The name of the font.


    Returns:
        font instance.

    """
    for font in self.app:
        if font.name == name:
            return TextFont(font)
    raise PhotoshopPythonAPIError('Could not find a TextFont named "{name}"')

Last update: 2024-11-17
Created: 2024-11-17