From 50f31ec1c343b2c35c4ba0f5cbac7cc4363b238c Mon Sep 17 00:00:00 2001 From: Vangelis Kostalas Date: Tue, 7 May 2019 16:01:31 +0300 Subject: [PATCH] Added support for on-the-fly image creation --- .gitignore | 1 + generate_master_file.py | 1 + images/unicode.svg | 26 +++++++++++++++++ main.py | 63 ++++++++++++++++++++++++++++++++--------- manifest.json | 2 +- unicode_list.txt | 2 -- 6 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 images/unicode.svg diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a91187 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +images/cache/* diff --git a/generate_master_file.py b/generate_master_file.py index 028ed27..9583382 100644 --- a/generate_master_file.py +++ b/generate_master_file.py @@ -27,6 +27,7 @@ with open('unicode_list.txt', 'w') as target: num = int(code, 16) except ValueError: print('could not convert ' + code) + continue index = locate_block(num) if index is not None: target.write(name + '\t' + code + '\t' + blocks[index] + '\n') diff --git a/images/unicode.svg b/images/unicode.svg new file mode 100644 index 0000000..8395bc0 --- /dev/null +++ b/images/unicode.svg @@ -0,0 +1,26 @@ + + + + {symbol} + + diff --git a/main.py b/main.py index c706705..2c786ee 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,9 @@ import sys -sys.path.append('/home/zen/.local/lib/python2.7/site-packages/') + +sys.path.append("/home/zen/.local/lib/python2.7/site-packages/") import os +import codecs from fuzzywuzzy import process from ulauncher.api.client.Extension import Extension @@ -13,17 +15,18 @@ from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAct from ulauncher.api.shared.action.HideWindowAction import HideWindowAction -class DemoExtension(Extension): +FILE_PATH = os.path.dirname(sys.argv[0]) + +class UnicodeCharExtension(Extension): def __init__(self): - super(DemoExtension, self).__init__() + super(UnicodeCharExtension, self).__init__() self.subscribe(KeywordQueryEvent, KeywordQueryEventListener()) class KeywordQueryEventListener(EventListener): - def __init__(self): - f = open(os.path.dirname(sys.argv[0]) + '/unicode_list.txt', 'r') + f = open(os.path.dirname(sys.argv[0]) + "/unicode_list.txt", "r") data = f.readlines() self.data = {} # self.names = [] @@ -31,9 +34,9 @@ class KeywordQueryEventListener(EventListener): # self.blocks = [] for item in data: item = item.strip() - name, code, block = item.split('\t') - self.data[name + ' ' + block] = (name, block, code) - self.items.append(name + ' ' + block) + name, code, block = item.split("\t") + self.data[name + " " + block] = (name, block, code) + self.items.append(name + " " + block) # self.names.append(name) # self.blocks.append(block) @@ -42,14 +45,46 @@ class KeywordQueryEventListener(EventListener): arg = event.get_argument() if arg: matches = process.extract(arg, self.items, limit=15) + for m in matches: name, block, code = self.data[m[0]] - items.append(ExtensionResultItem(icon='images/bookmark.svg', - name=name + ' - ' + unichr(int(code, 16)), - description=block, - on_enter=CopyToClipboardAction(unichr(int(code, 16))))) + image_path = self.create_icon_image(code) + items.append( + ExtensionResultItem( + icon=image_path, + name=name + " - " + unichr(int(code, 16)), + description=block, + on_enter=CopyToClipboardAction(unichr(int(code, 16))), + ) + ) return RenderResultListAction(items) -if __name__ == '__main__': - DemoExtension().run() + def create_icon_image(self, code): + path = sys.argv[0] + "images/cache/icon_%s.svg" % code + if os.path.isfile(path): + return path + return create_character_icon(code) + + +def load_icon_template(): + with open(os.path.join(FILE_PATH, "images/unicode.svg"), "r") as i: + return i.read() + + +def is_icon_cached(code): + return os.path.isfile(os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code)) + + +def create_character_icon(code, font='sans-serif'): + template = load_icon_template() + icon = template.replace("{symbol}", unichr(int(code, 16))).replace('{font}', font) + with codecs.open( + os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code), "w", "utf-8" + ) as target: + target.write(icon) + return os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code) + + +if __name__ == "__main__": + UnicodeCharExtension().run() diff --git a/manifest.json b/manifest.json index 0f4c4a6..3152e48 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "developer_name": "John Doe", "icon": "images/bookmark.svg", "options": { - "query_debounce": 0.1 + "query_debounce": 0.5 }, "preferences": [ { diff --git a/unicode_list.txt b/unicode_list.txt index cdcb1e4..570e36b 100644 --- a/unicode_list.txt +++ b/unicode_list.txt @@ -531,7 +531,6 @@ bernoulli function 212C Letterlike Symbols BET SYMBOL 2136 Letterlike Symbols BETA SYMBOL, GREEK 03D0 Greek and Coptic beta, curled 03D0 Greek and Coptic -Betty BOOP Greek and Coptic BETWEEN 226C Mathematical Operators Beverage Symbols 1F375 Miscellaneous Symbols and Pictographs BEVERAGE, HOT 2615 Miscellaneous Symbols @@ -5449,7 +5448,6 @@ Thai Digits 0E50 Thai Thai Marks and Signs 0E48 Thai Thai Vowels 0E30 Thai THANTHAKHAT, THAI CHARACTER 0E4C Thai -the DOOD Thai theater masks 1F3AD Miscellaneous Symbols and Pictographs THERE DOES NOT EXIST 2204 Mathematical Operators THERE EXISTS 2203 Mathematical Operators