Skip to content

Instantly share code, notes, and snippets.

@naoki-mizuno
Created January 15, 2020 07:33
Show Gist options
  • Save naoki-mizuno/c42ef26e944b8454d5dc8fae47143840 to your computer and use it in GitHub Desktop.
Save naoki-mizuno/c42ef26e944b8454d5dc8fae47143840 to your computer and use it in GitHub Desktop.
jsk_rviz_plugins Pictogram code generator
#!/bin/bash
# Path to metadata/icons.yml
FONTAWESOME_YML=$1
# Path to pictogram_font_mapping.h
PICTOGRAM_FONT_MAPPING=$2
echo "Generating C++ code from $FONTAWESOME_YML"
./to_cpp_code.py $FONTAWESOME_YML > ./new_code.cpp
echo 'Finding names only present in old C++ code'
diff \
<( egrep -o '"fa-[^"]+"' ./new_code.cpp ) \
<( egrep -o '"fa-[^"]+"' $PICTOGRAM_FONT_MAPPING ) \
| grep '^>' | sed -e 's/> //' > ./only_in_old.txt
echo 'Merging old names into new code'
cp new_code.cpp merged_code.cpp
echo >> merged_code.cpp
while read name; do
old_code_line="$( grep $name $PICTOGRAM_FONT_MAPPING )"
unicode="$( echo "$old_code_line" | grep -o '\\xF[0-9a-f][0-9a-f][0-9a-f]' )"
if [[ -z "$( grep "$unicode" ./new_code.cpp )" ]]; then
# Unicode character found in the old code, but not in new
echo "$old_code_line" >> ./no_alternative.cpp
else
echo "$old_code_line" >> ./merged_code.cpp
fi
done < ./only_in_old.txt
# Clean up
rm ./new_code.cpp ./only_in_old.txt
#!/usr/bin/env python
import sys
import yaml
import argparse
def parse_args():
description = 'convert fontawesome yaml into cpp code'
parser = argparse.ArgumentParser(description=description,
add_help=False)
parser.add_argument('-h', '--help', action='help',
help='show this help message and exit')
parser.add_argument('-v', '--var-name',
dest='var',
default='fontawesome_character_map',
nargs=1,
metavar='NAME',
help='variable name in the C++ code')
parser.add_argument('-p', '--prefix',
dest='prefix',
default='fa-',
nargs=1,
metavar='PREFIX',
help='prefix to use for the name')
parser.add_argument('-i', '--indent',
dest='indent',
default=6,
nargs=1,
metavar='DEPTH',
help='indentation depth in the C++ code')
parser.add_argument('yaml',
metavar='YAML',
help='yaml file to read from')
args = parser.parse_args()
return args
def main():
args = parse_args()
doc = yaml.load(open(args.yaml))
unicode_to_name = {}
for name in doc:
# Example: f586
code = doc[name]['unicode']
# Example: F586
code = 'F' + code[1:]
unicode_to_name[code] = name
fmt = '{indent}{var}["{prefix}{name}"] = QString::fromWCharArray(L"\\x{code}");'
indent = ' ' * args.indent
for code in sorted(unicode_to_name.keys()):
name = unicode_to_name[code]
cpp_code = fmt.format(indent=indent,
var=args.var,
prefix=args.prefix,
name=name, code=code)
print(cpp_code)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment