spyder-ide / qtawesome

Iconic fonts in PyQt and PySide applications
https://qtawesome.readthedocs.io/en/latest/index.html
MIT License
802 stars 105 forks source link

Util function to generate char unicode map #201

Open mhamid3d opened 2 years ago

mhamid3d commented 2 years ago

It would be nice to have a utility to generate the char map since it's required to load additional fonts. Although, if this adds too many dependencies, maybe a mention in the docs to what method was used to generate the precompiled ones.

dalthviz commented 2 years ago

Hi @mhamid3d thank you for the feedback! I think you are right, at least a mention in the docs should be done. The function handling this probably could use the code used in the UPDATE.md for the different fonts. So basically with the corresponding font CSS file, parse it and dump the parsing into a .json file. So something like this:


import re
import json
import urllib.request

def create_font_charmap(font_name, font_css_url):
    req = urllib.request.urlopen(font_css_url)
    if req.status != 200:
        raise Exception('Failed to download CSS Charmap')

    rawcss = req.read().decode()
    req.close()

    charmap = {}
    pattern = '^\.ph-(.+):before {\s*content: "(.+)";\s*}$'
    data = re.findall(pattern, rawcss, re.MULTILINE)
    for name, key in data:
        key = key.replace('\\', '0x')
        name = name.lower()
        charmap[name] = key

    with open(f'{font_name}-charmap.json', 'w') as fp:
        json.dump(charmap, fp, indent=4, sort_keys=True)

Let us know if you want to work on this!