Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util function to generate char unicode map #201

Open
mhamid3d opened this issue Jan 12, 2022 · 1 comment
Open

Util function to generate char unicode map #201

mhamid3d opened this issue Jan 12, 2022 · 1 comment

Comments

@mhamid3d
Copy link

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
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants