-
Notifications
You must be signed in to change notification settings - Fork 11
/
emojis_component.py
175 lines (128 loc) · 5.54 KB
/
emojis_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
175
import re
import os
import hashlib
from enum import Enum
from io import BytesIO
from textwrap import dedent
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from urllib.parse import quote_plus
from typing import Any, Dict, Final, List, Optional, NamedTuple
from emoji import unicode_codes
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})')
EMOJI_REQUEST_KWARGS: Dict[str, Any] = { "headers": {"User-Agent": "Mozilla/5.0"} }
EMOJI_BASE_CDN_URL: str = "https://emojicdn.elk.sh/"
EMOJI_STYLE: str = "google"
def get_emoji(emoji: str) -> Optional[BytesIO]:
emoji_cache = juce.File(os.path.abspath(__file__)).getParentDirectory().getChildFile("emoji_cache")
if not emoji_cache.isDirectory():
emoji_cache.createDirectory()
url = EMOJI_BASE_CDN_URL + quote_plus(emoji) + "?style=" + quote_plus(EMOJI_STYLE)
url_hash = hashlib.md5()
url_hash.update(url.encode("utf8"))
emoji_file = emoji_cache.getChildFile(url_hash.hexdigest())
if emoji_file.existsAsFile():
with open(emoji_file.getFullPathName(), "rb") as f:
return BytesIO(f.read())
try:
req = Request(url, **EMOJI_REQUEST_KWARGS)
with urlopen(req) as response:
response = response.read()
with open(emoji_file.getFullPathName(), "wb") as f:
f.write(response)
return BytesIO(response)
except HTTPError:
pass
return None
class NodeType(Enum):
text = 0
emoji = 1
class Node(NamedTuple):
type: NodeType
content: str
emoji: Optional[BytesIO]
class EmojiComponent(juce.Component):
font: juce.Font = juce.Font(juce.FontOptions(12.0))
colour: juce.Colour = juce.Colours.black
nodes: List[List[Node]]
def __init__(self):
juce.Component.__init__(self)
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, None))
continue
nodes.append(Node(NodeType.emoji, chunk, get_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)
y = 0
for lines in self.nodes:
x = 0
y += font_height + 2.0
height = int(font_height)
for node in lines:
if node.type == NodeType.text:
text_width = self.font.getStringWidthFloat(node.content)
g.drawText(node.content,
int(x), int(y), min(int(text_width), self.getWidth() - int(x)), height,
juce.Justification.centredLeft, useEllipsesIfTooBig=False)
x += text_width
elif node.emoji:
emoji = juce.ImageCache.getFromMemory(node.emoji.getbuffer())
if emoji and emoji.isValid():
g.drawImageWithin(emoji, int(x), int(y), emoji_size, emoji_size, juce.RectanglePlacement.centred)
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")