spyder-ide / qtawesome

Iconic fonts in PyQt and PySide applications

Home Page:https://qtawesome.readthedocs.io/en/latest/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Util function to generate char unicode map

mhamid3d opened this issue · comments

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.

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!