Skip to content

Commit 50f31ec

Browse files
committed
Added support for on-the-fly image creation
1 parent fbfba8d commit 50f31ec

6 files changed

+78
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
images/cache/*

generate_master_file.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def locate_block(code):
2727
num = int(code, 16)
2828
except ValueError:
2929
print('could not convert ' + code)
30+
continue
3031
index = locate_block(num)
3132
if index is not None:
3233
target.write(name + '\t' + code + '\t' + blocks[index] + '\n')

images/unicode.svg

+26
Loading

main.py

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import sys
2-
sys.path.append('/home/zen/.local/lib/python2.7/site-packages/')
2+
3+
sys.path.append("/home/zen/.local/lib/python2.7/site-packages/")
34

45
import os
6+
import codecs
57
from fuzzywuzzy import process
68

79
from ulauncher.api.client.Extension import Extension
@@ -13,27 +15,28 @@
1315
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
1416

1517

16-
class DemoExtension(Extension):
18+
FILE_PATH = os.path.dirname(sys.argv[0])
19+
1720

21+
class UnicodeCharExtension(Extension):
1822
def __init__(self):
19-
super(DemoExtension, self).__init__()
23+
super(UnicodeCharExtension, self).__init__()
2024
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
2125

2226

2327
class KeywordQueryEventListener(EventListener):
24-
2528
def __init__(self):
26-
f = open(os.path.dirname(sys.argv[0]) + '/unicode_list.txt', 'r')
29+
f = open(os.path.dirname(sys.argv[0]) + "/unicode_list.txt", "r")
2730
data = f.readlines()
2831
self.data = {}
2932
# self.names = []
3033
self.items = []
3134
# self.blocks = []
3235
for item in data:
3336
item = item.strip()
34-
name, code, block = item.split('\t')
35-
self.data[name + ' ' + block] = (name, block, code)
36-
self.items.append(name + ' ' + block)
37+
name, code, block = item.split("\t")
38+
self.data[name + " " + block] = (name, block, code)
39+
self.items.append(name + " " + block)
3740
# self.names.append(name)
3841
# self.blocks.append(block)
3942

@@ -42,14 +45,46 @@ def on_event(self, event, extension):
4245
arg = event.get_argument()
4346
if arg:
4447
matches = process.extract(arg, self.items, limit=15)
48+
4549
for m in matches:
4650
name, block, code = self.data[m[0]]
47-
items.append(ExtensionResultItem(icon='images/bookmark.svg',
48-
name=name + ' - ' + unichr(int(code, 16)),
49-
description=block,
50-
on_enter=CopyToClipboardAction(unichr(int(code, 16)))))
51+
image_path = self.create_icon_image(code)
52+
items.append(
53+
ExtensionResultItem(
54+
icon=image_path,
55+
name=name + " - " + unichr(int(code, 16)),
56+
description=block,
57+
on_enter=CopyToClipboardAction(unichr(int(code, 16))),
58+
)
59+
)
5160

5261
return RenderResultListAction(items)
5362

54-
if __name__ == '__main__':
55-
DemoExtension().run()
63+
def create_icon_image(self, code):
64+
path = sys.argv[0] + "images/cache/icon_%s.svg" % code
65+
if os.path.isfile(path):
66+
return path
67+
return create_character_icon(code)
68+
69+
70+
def load_icon_template():
71+
with open(os.path.join(FILE_PATH, "images/unicode.svg"), "r") as i:
72+
return i.read()
73+
74+
75+
def is_icon_cached(code):
76+
return os.path.isfile(os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code))
77+
78+
79+
def create_character_icon(code, font='sans-serif'):
80+
template = load_icon_template()
81+
icon = template.replace("{symbol}", unichr(int(code, 16))).replace('{font}', font)
82+
with codecs.open(
83+
os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code), "w", "utf-8"
84+
) as target:
85+
target.write(icon)
86+
return os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code)
87+
88+
89+
if __name__ == "__main__":
90+
UnicodeCharExtension().run()

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"developer_name": "John Doe",
77
"icon": "images/bookmark.svg",
88
"options": {
9-
"query_debounce": 0.1
9+
"query_debounce": 0.5
1010
},
1111
"preferences": [
1212
{

unicode_list.txt

-2
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ bernoulli function 212C Letterlike Symbols
531531
BET SYMBOL 2136 Letterlike Symbols
532532
BETA SYMBOL, GREEK 03D0 Greek and Coptic
533533
beta, curled 03D0 Greek and Coptic
534-
Betty BOOP Greek and Coptic
535534
BETWEEN 226C Mathematical Operators
536535
Beverage Symbols 1F375 Miscellaneous Symbols and Pictographs
537536
BEVERAGE, HOT 2615 Miscellaneous Symbols
@@ -5449,7 +5448,6 @@ Thai Digits 0E50 Thai
54495448
Thai Marks and Signs 0E48 Thai
54505449
Thai Vowels 0E30 Thai
54515450
THANTHAKHAT, THAI CHARACTER 0E4C Thai
5452-
the DOOD Thai
54535451
theater masks 1F3AD Miscellaneous Symbols and Pictographs
54545452
THERE DOES NOT EXIST 2204 Mathematical Operators
54555453
THERE EXISTS 2203 Mathematical Operators

0 commit comments

Comments
 (0)