Skip to content

Commit

Permalink
[MaterialIcons] Add script to parse codepoints and generate the QML file
Browse files Browse the repository at this point in the history
  • Loading branch information
cbentejac committed Dec 11, 2023
1 parent d1c0fd2 commit 4668c77
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions meshroom/ui/qml/MaterialIcons/generate_material_icons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse
import os

parser = argparse.ArgumentParser(description='Generate a MaterialIcons.qml singleton from codepoints file.')
parser.add_argument('codepoints', type=str, help='Codepoints file.')
parser.add_argument('--output', type=str, default='.', help='')

args = parser.parse_args()

# Override icons with problematic names
mapping = {
'delete': 'delete_',
'class': 'class_',
'3d_rotation': '_3d_rotation',
'opacity': 'opacity_',
'transform': 'transform_',
'print': 'print_',
'public': 'public_',
'password': 'pwd',
'wifi_password': 'wifi_pwd',
'try': 'try_'
}

# Override icons that are numeric literals
numeric_literals = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

# List of existing name to override potential duplicates
names = []

with open(os.path.join(args.output, 'MaterialIcons.qml'), 'w') as qml_file:
qml_file.write('pragma Singleton\n')
qml_file.write('import QtQuick 2.15\n\n')
qml_file.write('QtObject {\n')
qml_file.write(' property FontLoader fl: FontLoader {\n')
qml_file.write(' source: "./MaterialIcons-Regular.ttf"\n')
qml_file.write(' }\n')
qml_file.write(' readonly property string fontFamily: fl.name\n\n')

with open(args.codepoints, 'r') as codepoints:
for line in codepoints.readlines():
name, code = line.strip().split(" ")
name = mapping.get(name, name)

# Add underscore to names that are numeric literals (e.g. "123" will become "_123")
if name[0] in numeric_literals:
name = "_" + name

# If the name already exists in the list, append an index
if name in names:
index = 2
while name + str(index) in names:
index = index + 1
name = name + str(index)

names.append(name)
qml_file.write(' readonly property string {}: "\\u{}"\n'.format(name, code))
qml_file.write('}\n')

0 comments on commit 4668c77

Please sign in to comment.