Skip to content

Text Format

TextFormat

src.drawpyo.diagram.text_format.TextFormat

Bases: DiagramBase

The TextFormat class handles all of the formatting specifically around a text box or label.

Source code in src/drawpyo/diagram/text_format.py
  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
 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class TextFormat(DiagramBase):
    """The TextFormat class handles all of the formatting specifically around a text box or label."""

    def __init__(self, **kwargs):
        """TextFormat objects can be initialized with no properties or any of what's listed below:

        Keyword Args:
            fontColor (int, optional): The color of the text in the object (#ffffff)
            fontFamily (str, optional): The typeface of the text in the object (see Draw.io for available fonts)
            fontSize (int, optional): The size of the text in the object in points
            align (str, optional): The horizontal alignment of the text in the object ('left', 'center', or 'right')
            verticalAlign (str, optional): The vertical alignment of the text in the object ('top', 'middle', 'bottom')
            textOpacity (int, optional): The opacity of the text in the object
            direction (str, optional): The direction to print the text ('vertical', 'horizontal')
            bold (bool, optional): Whether the text in the object should be bold
            italic (bool, optional): Whether the text in the object should be italic
            underline (bool, optional): Whether the text in the object should be underlined
            labelPosition (str, optional): The position of the object label ('left', 'center', or 'right')
            labelBackgroundColor (str, optional): The background color of the object label (#ffffff)
            labelBorderColor (str, optional): The border color of the object label (#ffffff)
            fomrattedText (bool, optional): Whether to render the text as HTML formatted or not

        """
        super().__init__(**kwargs)
        self.fontFamily = kwargs.get("fontFamily", None)
        self.fontSize = kwargs.get("fontSize", None)
        self.fontColor = kwargs.get("fontColor", None)
        self.labelBorderColor = kwargs.get("labelBorderColor", None)
        self.labelBackgroundColor = kwargs.get("labelBackgroundColor", None)
        self.labelPosition = kwargs.get("labelPosition", None)
        self.textShadow = kwargs.get("textShadow", None)
        self.textOpacity = kwargs.get("textOpacity", None)
        self.spacingTop = kwargs.get("spacingTop", None)
        self.spacingLeft = kwargs.get("spacingLeft", None)
        self.spacingBottom = kwargs.get("spacingBottom", None)
        self.spacingRight = kwargs.get("spacingRight", None)
        self.spacing = kwargs.get("spacing", None)
        self.align = kwargs.get("align", None)
        self.verticalAlign = kwargs.get("verticalAlign", None)
        # These need to be enumerated
        self.direction = kwargs.get("direction", None)
        # This is actually horizontal. 0 means vertical text, 1 or not present
        # means horizontal
        self.formattedText = kwargs.get(
            "formattedText", None
        )  # prints in the style string as html
        self.bold = kwargs.get("bold", False)
        self.italic = kwargs.get("italic", False)
        self.underline = kwargs.get("underline", False)

        self._style_attributes = [
            "html",
            "fontFamily",
            "fontStyle",
            "fontSize",
            "fontColor",
            "labelBorderColor",
            "labelBackgroundColor",
            "labelPosition",
            "textShadow",
            "textOpacity",
            "spacingTop",
            "spacingLeft",
            "spacingBottom",
            "spacingRight",
            "spacing",
            "align",
            "verticalAlign",
            "horizontal",
        ]

    @property
    def formattedText(self):
        """formattedText wraps the Draw.io style attribute 'html'. This controls whether the text is rendered with HTML attributes or as plain text."""
        return self.html

    @formattedText.setter
    def formattedText(self, value):
        self.html = value

    @formattedText.deleter
    def formattedText(self, value):
        self.html = None

    # The direction of the text is encoded as 'horizontal' in Draw.io. This is
    # unintuitive so I provided a direction alternate syntax.
    @property
    def horizontal(self):
        return directions[self._direction]

    @horizontal.setter
    def horizontal(self, value):
        if value in directions_inv.keys():
            self._direction = directions_inv[value]
        else:
            raise ValueError("{0} is not an allowed value of horizontal".format(value))

    @property
    def directions(self):
        """The direction controls the direction of the text and can be either horizontal or vertical."""
        return directions

    @property
    def direction(self):
        return self._direction

    @direction.setter
    def direction(self, value):
        if value in directions.keys():
            self._direction = value
        else:
            raise ValueError("{0} is not an allowed value of direction".format(value))

    @property
    def font_style(self):
        """The font_style is a numeric format that corresponds to a combination of three other attributes: bold, italic, and underline. Any combination of them can be true."""
        bld = self.bold
        ita = self.italic
        unl = self.underline

        # 0 = normal
        # 1 = bold
        # 2 = italic
        # 3 = bold and italic
        # 4 = underline
        # 5 = bold and underlined
        # 6 = italic and underlined
        # 7 = bolt, italic, and underlined

        if not bld and not ita and not unl:
            return 0
        elif bld and not ita and not unl:
            return 1
        elif not bld and ita and not unl:
            return 2
        elif bld and ita and not unl:
            return 3
        elif not bld and not ita and unl:
            return 4
        elif bld and not ita and unl:
            return 5
        elif not bld and ita and unl:
            return 6
        elif bld and ita and unl:
            return 7

    @property
    def fontStyle(self):
        if self.font_style != 0:
            return self.font_style
        return None

directions property

The direction controls the direction of the text and can be either horizontal or vertical.

font_style property

The font_style is a numeric format that corresponds to a combination of three other attributes: bold, italic, and underline. Any combination of them can be true.

formattedText deletable property writable

formattedText wraps the Draw.io style attribute 'html'. This controls whether the text is rendered with HTML attributes or as plain text.

__init__(**kwargs)

TextFormat objects can be initialized with no properties or any of what's listed below:

Other Parameters:

Name Type Description
fontColor int

The color of the text in the object (#ffffff)

fontFamily str

The typeface of the text in the object (see Draw.io for available fonts)

fontSize int

The size of the text in the object in points

align str

The horizontal alignment of the text in the object ('left', 'center', or 'right')

verticalAlign str

The vertical alignment of the text in the object ('top', 'middle', 'bottom')

textOpacity int

The opacity of the text in the object

direction str

The direction to print the text ('vertical', 'horizontal')

bold bool

Whether the text in the object should be bold

italic bool

Whether the text in the object should be italic

underline bool

Whether the text in the object should be underlined

labelPosition str

The position of the object label ('left', 'center', or 'right')

labelBackgroundColor str

The background color of the object label (#ffffff)

labelBorderColor str

The border color of the object label (#ffffff)

fomrattedText bool

Whether to render the text as HTML formatted or not

Source code in src/drawpyo/diagram/text_format.py
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
def __init__(self, **kwargs):
    """TextFormat objects can be initialized with no properties or any of what's listed below:

    Keyword Args:
        fontColor (int, optional): The color of the text in the object (#ffffff)
        fontFamily (str, optional): The typeface of the text in the object (see Draw.io for available fonts)
        fontSize (int, optional): The size of the text in the object in points
        align (str, optional): The horizontal alignment of the text in the object ('left', 'center', or 'right')
        verticalAlign (str, optional): The vertical alignment of the text in the object ('top', 'middle', 'bottom')
        textOpacity (int, optional): The opacity of the text in the object
        direction (str, optional): The direction to print the text ('vertical', 'horizontal')
        bold (bool, optional): Whether the text in the object should be bold
        italic (bool, optional): Whether the text in the object should be italic
        underline (bool, optional): Whether the text in the object should be underlined
        labelPosition (str, optional): The position of the object label ('left', 'center', or 'right')
        labelBackgroundColor (str, optional): The background color of the object label (#ffffff)
        labelBorderColor (str, optional): The border color of the object label (#ffffff)
        fomrattedText (bool, optional): Whether to render the text as HTML formatted or not

    """
    super().__init__(**kwargs)
    self.fontFamily = kwargs.get("fontFamily", None)
    self.fontSize = kwargs.get("fontSize", None)
    self.fontColor = kwargs.get("fontColor", None)
    self.labelBorderColor = kwargs.get("labelBorderColor", None)
    self.labelBackgroundColor = kwargs.get("labelBackgroundColor", None)
    self.labelPosition = kwargs.get("labelPosition", None)
    self.textShadow = kwargs.get("textShadow", None)
    self.textOpacity = kwargs.get("textOpacity", None)
    self.spacingTop = kwargs.get("spacingTop", None)
    self.spacingLeft = kwargs.get("spacingLeft", None)
    self.spacingBottom = kwargs.get("spacingBottom", None)
    self.spacingRight = kwargs.get("spacingRight", None)
    self.spacing = kwargs.get("spacing", None)
    self.align = kwargs.get("align", None)
    self.verticalAlign = kwargs.get("verticalAlign", None)
    # These need to be enumerated
    self.direction = kwargs.get("direction", None)
    # This is actually horizontal. 0 means vertical text, 1 or not present
    # means horizontal
    self.formattedText = kwargs.get(
        "formattedText", None
    )  # prints in the style string as html
    self.bold = kwargs.get("bold", False)
    self.italic = kwargs.get("italic", False)
    self.underline = kwargs.get("underline", False)

    self._style_attributes = [
        "html",
        "fontFamily",
        "fontStyle",
        "fontSize",
        "fontColor",
        "labelBorderColor",
        "labelBackgroundColor",
        "labelPosition",
        "textShadow",
        "textOpacity",
        "spacingTop",
        "spacingLeft",
        "spacingBottom",
        "spacingRight",
        "spacing",
        "align",
        "verticalAlign",
        "horizontal",
    ]