aboutsummaryrefslogtreecommitdiff
path: root/src/avp/components/text.py
blob: bee117e50e51d5acf0873c8c7011dfe5908f6e3d (plain)
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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from PIL import ImageEnhance, ImageFilter, ImageChops
from PyQt6.QtGui import QColor, QFont
from PyQt6 import QtGui, QtCore, QtWidgets
import os
import logging

from ..component import Component
from ..toolkit.frame import FramePainter, addShadow

log = logging.getLogger("AVP.Components.Text")


class Component(Component):
    name = "Title Text"
    version = "1.0.1"

    def widget(self, *args):
        super().widget(*args)
        self.title = "Text"
        self.alignment = 1
        self.titleFont = QFont()
        self.fontSize = self.height / 13.5

        self.page.comboBox_textAlign.addItem("Left")
        self.page.comboBox_textAlign.addItem("Middle")
        self.page.comboBox_textAlign.addItem("Right")
        self.page.comboBox_textAlign.setCurrentIndex(int(self.alignment))
        self.page.spinBox_fontSize.setValue(int(self.fontSize))
        self.page.pushButton_center.clicked.connect(self.centerXY)

        self.page.fontComboBox_titleFont.currentFontChanged.connect(
            self._sendUpdateSignal
        )
        # The QFontComboBox must be connected directly to the Qt Signal
        # which triggers the preview to update.
        # This unfortunately makes changing the font into a non-undoable action.
        # Fix requires updating ComponentAction to handle fonts

        self.trackWidgets(
            {
                "textColor": self.page.lineEdit_textColor,
                "title": self.page.lineEdit_title,
                "alignment": self.page.comboBox_textAlign,
                "fontSize": self.page.spinBox_fontSize,
                "xPosition": self.page.spinBox_xTextAlign,
                "yPosition": self.page.spinBox_yTextAlign,
                "fontStyle": self.page.comboBox_fontStyle,
                "stroke": self.page.spinBox_stroke,
                "strokeColor": self.page.lineEdit_strokeColor,
                "shadow": self.page.checkBox_shadow,
                "shadX": self.page.spinBox_shadX,
                "shadY": self.page.spinBox_shadY,
                "shadBlur": self.page.spinBox_shadBlur,
            },
            colorWidgets={
                "textColor": self.page.pushButton_textColor,
                "strokeColor": self.page.pushButton_strokeColor,
            },
            relativeWidgets=[
                "xPosition",
                "yPosition",
                "fontSize",
                "stroke",
                "shadX",
                "shadY",
                "shadBlur",
            ],
        )
        self.centerXY()

    def update(self):
        self.titleFont = self.page.fontComboBox_titleFont.currentFont()
        if self.page.checkBox_shadow.isChecked():
            self.page.label_shadX.setHidden(False)
            self.page.spinBox_shadX.setHidden(False)
            self.page.spinBox_shadY.setHidden(False)
            self.page.label_shadBlur.setHidden(False)
            self.page.spinBox_shadBlur.setHidden(False)
        else:
            self.page.label_shadX.setHidden(True)
            self.page.spinBox_shadX.setHidden(True)
            self.page.spinBox_shadY.setHidden(True)
            self.page.label_shadBlur.setHidden(True)
            self.page.spinBox_shadBlur.setHidden(True)

    def centerXY(self):
        self.setRelativeWidget("xPosition", 0.5)
        self.setRelativeWidget("yPosition", 0.521)

    def getXY(self):
        """Returns true x, y after considering alignment settings"""
        fm = QtGui.QFontMetrics(self.titleFont)
        text_width = fm.boundingRect(self.title).width()
        x = self.pixelValForAttr("xPosition")

        if self.alignment == 1:  # Middle
            offset = int(text_width / 2)
        elif self.alignment == 2:  # Right
            offset = text_width
        else:
            raise ValueError(f"Alignment value {self.alignment} unknown")

        x -= offset

        return x, self.yPosition

    def loadPreset(self, pr, *args):
        super().loadPreset(pr, *args)

        font = QFont()
        font.fromString(pr["titleFont"])
        self.page.fontComboBox_titleFont.setCurrentFont(font)

    def savePreset(self):
        saveValueStore = super().savePreset()
        saveValueStore["titleFont"] = self.titleFont.toString()
        return saveValueStore

    def previewRender(self):
        return self.addText(self.width, self.height)

    def properties(self):
        props = ["static"]
        if not self.title:
            props.append("error")
        return props

    def error(self):
        return "No text provided."

    def frameRender(self, frameNo):
        return self.addText(self.width, self.height)

    def addText(self, width, height):
        font = self.titleFont
        font.setPixelSize(self.fontSize)
        font.setStyle(QFont.Style.StyleNormal)
        font.setWeight(QFont.Weight.Normal)
        font.setCapitalization(QFont.Capitalization.MixedCase)
        if self.fontStyle == 1:
            font.setWeight(QFont.Weight.DemiBold)
        if self.fontStyle == 2:
            font.setWeight(QFont.Weight.Bold)
        elif self.fontStyle == 3:
            font.setStyle(QFont.Style.StyleItalic)
        elif self.fontStyle == 4:
            font.setWeight(QFont.Weight.Bold)
            font.setStyle(QFont.Style.StyleItalic)
        elif self.fontStyle == 5:
            font.setStyle(QFont.Style.StyleOblique)
        elif self.fontStyle == 6:
            font.setCapitalization(QFont.Capitalization.SmallCaps)

        image = FramePainter(width, height)
        x, y = self.getXY()
        log.debug("Text position translates to %s, %s", x, y)
        if self.stroke > 0:
            outliner = QtGui.QPainterPathStroker()
            outliner.setWidth(self.stroke)
            path = QtGui.QPainterPath()
            if self.fontStyle == 6:
                # PathStroker ignores smallcaps so we need this weird hack
                path.addText(x, y, font, self.title[0])
                fm = QtGui.QFontMetrics(font)
                newX = x + fm.boundingRect(self.title[0]).width()
                strokeFont = self.page.fontComboBox_titleFont.currentFont()
                strokeFont.setCapitalization(QFont.Capitalization.SmallCaps)
                strokeFont.setPixelSize(int((self.fontSize / 7) * 5))
                strokeFont.setLetterSpacing(QFont.SpacingType.PercentageSpacing, 139)
                path.addText(newX, y, strokeFont, self.title[1:])
            else:
                path.addText(x, y, font, self.title)
            path = outliner.createStroke(path)
            image.setPen(QtCore.Qt.PenStyle.NoPen)
            image.setBrush(QtGui.QColor(*self.strokeColor))
            image.drawPath(path)

        image.setFont(font)
        image.setPen(self.textColor)
        image.drawText(x, y, self.title)

        # turn QImage into Pillow frame
        frame = image.finalize()
        if self.shadow:
            frame = addShadow(frame, self.shadBlur / 10, self.shadX, self.shadY)

        return frame

    def commandHelp(self):
        print("Enter a string to use as centred white text:")
        print('    "title=User Error"')
        print("Specify a text color:\n    color=255,255,255")
        print("Set custom x, y position:\n    x=500 y=500")

    def command(self, arg):
        if "=" in arg:
            key, arg = arg.split("=", 1)
            if key == "color":
                self.page.lineEdit_textColor.setText(arg)
                return
            elif key == "size":
                self.page.spinBox_fontSize.setValue(int(arg))
                return
            elif key == "x":
                self.page.spinBox_xTextAlign.setValue(int(arg))
                return
            elif key == "y":
                self.page.spinBox_yTextAlign.setValue(int(arg))
                return
            elif key == "title":
                self.page.lineEdit_title.setText(arg)
                return
        super().command(arg)