-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MaterialIcons] Add script to parse codepoints and generate the QML file
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |