-
Notifications
You must be signed in to change notification settings - Fork 11
/
emojis_font_component.py
174 lines (125 loc) · 5.82 KB
/
emojis_font_component.py
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
import re
import os
from enum import Enum
from textwrap import dedent
from typing import Dict, Final, List, NamedTuple
from emoji import unicode_codes
from PIL import Image, ImageDraw, ImageFont
from pil_image import ImageJuce
from juce_init import START_JUCE_COMPONENT
import popsicle as juce
language_pack: Dict[str, str] = unicode_codes.get_emoji_unicode_dict("en")
EMOJI_UNICODE_REGEX = "|".join(map(re.escape, sorted(language_pack.values(), key=len, reverse=True)))
EMOJI_REGEX: Final[re.Pattern[str]] = re.compile(f'({EMOJI_UNICODE_REGEX})')
class NodeType(Enum):
text = 0
emoji = 1
class Node(NamedTuple):
type: NodeType
content: str
class EmojiComponent(juce.Component):
font: juce.Font = juce.Font(juce.FontOptions(12.0))
colour: juce.Colour = juce.Colours.black
nodes: List[List[Node]]
unicode_font = None
unicode_size = 109
def __init__(self):
juce.Component.__init__(self)
font_file = juce.File(os.path.abspath(__file__)).getSiblingFile("NotoColorEmoji.ttf")
self.unicode_font = ImageFont.truetype(font_file.getFullPathName(), self.unicode_size)
self.im = Image.new("RGBA", (self.unicode_size + 20, self.unicode_size + 20))
self.draw = ImageDraw.Draw(self.im)
self.setOpaque(False)
def setFont(self, font: juce.Font):
self.font = font
self.repaint()
def setColour(self, colour: juce.Colour):
self.colour = colour
self.repaint()
def setText(self, text: str):
self.nodes = self.splitTextIntoNodes(text)
self.repaint()
def splitTextIntoNodes(self, text: str) -> List[List[Node]]:
lines = []
for line in text.splitlines():
nodes = []
for i, chunk in enumerate(EMOJI_REGEX.split(line)):
if not chunk:
continue
if not i % 2:
nodes.append(Node(NodeType.text, chunk))
continue
nodes.append(Node(NodeType.emoji, chunk))
lines.append(nodes)
return lines
def paint(self, g: juce.Graphics):
if not self.nodes:
return
font_height = self.font.getHeight()
emoji_size = int(font_height * 1.1)
g.setFont(self.font)
g.setColour(self.colour)
def new_line(x, y):
return 0, y + font_height + 4.0
x = 0
y = 0
for lines in self.nodes:
x, y = new_line(x, y)
current_text = None
for node in lines:
if node.type == NodeType.text:
current_text = node.content
while current_text:
remaining_text = []
text_width = self.font.getStringWidthFloat(current_text)
while current_text and text_width > self.getWidth() - int(x):
words = current_text.split(" ")
current_text = " ".join(words[:-1])
if words:
remaining_text.append(words[-1])
text_width = self.font.getStringWidthFloat(current_text)
if current_text:
g.drawText(current_text,
int(x), int(y), min(int(text_width), self.getWidth() - int(x)), int(font_height),
juce.Justification.centredLeft, useEllipsesIfTooBig=False)
x += text_width
current_text = None
if remaining_text:
x, y = new_line(x, y)
current_text = " ".join(remaining_text)
else:
self.draw.rectangle((0, 0, self.im.size[0], self.im.size[1]), fill=(0, 0, 0, 0))
self.draw.text((0, 0), node.content, embedded_color=True, font=self.unicode_font)
if emoji_size > self.getWidth() - int(x):
x, y = new_line(x, y)
g.drawImageWithin(ImageJuce(self.im), int(x), int(y), emoji_size, emoji_size,
juce.RectanglePlacement.centred | juce.RectanglePlacement.onlyReduceInSize)
x += emoji_size
class ExampleComponent(juce.Component):
def __init__(self):
juce.Component.__init__(self)
self.emoji_one = EmojiComponent()
self.emoji_one.setFont(juce.Font(juce.FontOptions(16.0)))
self.emoji_one.setColour(juce.Colours.white)
self.emoji_one.setText(dedent("""
I 🕴️ 100% 💶 agree 💯 that 👉💀🔕🐑 this automated 🏧 generator does 👩🦲 NOT 🚯🚯🚯 provide 👋 the same 😯
quality 👌 as hand 👊 crafted emoji 🤟 pasta. 🍝 But 😥 I 🤖 think 🤔 there's 🛒 something ❓❔ cool 🧊
about 🌈 being 😑 able 💪💪 to take 👏 a 10,000 word 📓 wikipedia 💻 article 📄 and instantly add 👈
emojis 🐅🐅🐢🦅🦅🦋🐒
""").strip())
self.addAndMakeVisible(self.emoji_one)
self.slider = juce.Slider()
self.slider.setRange(1.0, 100, 0.1)
self.slider.setValue(16.0)
self.slider.onValueChange = lambda: self.emoji_one.setFont(juce.Font(juce.FontOptions(self.slider.getValue())))
self.addAndMakeVisible(self.slider)
self.setOpaque(True)
self.setSize(600, 400)
def paint(self, g: juce.Graphics):
g.fillAll(self.findColour(juce.DocumentWindow.backgroundColourId, True))
def resized(self):
bounds = self.getLocalBounds()
self.emoji_one.setBounds(bounds)
self.slider.setBounds(bounds.removeFromBottom(20))
if __name__ == "__main__":
START_JUCE_COMPONENT(ExampleComponent, name="Emoji Example")