forked from rfbezerra/svg-to-ttf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-icons.sh
executable file
·138 lines (125 loc) · 3.61 KB
/
convert-icons.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
CMD_LINE="docker run --rm -v "\""\${PWD}"\"":/fonts rfbezerra/svg-to-ttf"
helpFunction()
{
echo ""
echo "Usage: ${CMD_LINE} [OPTIONS]"
echo -e "\t-g, --gid [GID] GroupId owner of the destination folder; default is equal to uid"
echo -e "\t-i, --input [FOLDER] Folder where svg glyphs lives; default is ./input"
echo -e "\t-n, --name FONT_NAME Name of the generated font"
echo -e "\t-c, --codepoint [CODEPOINT] Starting point for the character map, hexadecimal."
echo -e "\t-o, --output [FOLDER] Destination folder for generated fonts; default is ./FONT_NAME"
echo -e "\t-s, --skip_conversion Skip the conversion from stroke to path"
echo -e "\t-u, --uid [UID] UserId owner of the destination folder; default is 0 (root)"
echo -e "\t-h, --help Show this help message"
exit 1 # Exit script after printing help
}
prepareFunction()
{
mkdir -p "${OUTPUT_FOLDER}"
TEMP_FOLDER=$(mktemp -d temp.XXXXXX)
cp "${INPUT_FOLDER}/"*.svg "${TEMP_FOLDER}"
}
convertFunction()
{
echo "Converting icons to paths"
CMD="/usr/bin/parallel --bar /usr/bin/inkscape {} \
--actions="\""select-all;object-to-path;export-overwrite;export-do"\"" ::: ${TEMP_FOLDER}/*.svg"
AUTHFILE=$(mktemp -p /tmp Xauthority.XXXXXX)
XAUTHORITY=$AUTHFILE /usr/bin/Xvfb :2020 -screen 0 640x480x24 -nolisten tcp >> /dev/null 2>&1 &
XVFBPID=$! && DISPLAY=:2020 ${CMD} && kill ${XVFBPID}
}
_convert_hex_to_dec() {
printf "%d\n" "0x${1}"
}
_compileFont() {
/usr/bin/fontcustom compile "${TEMP_FOLDER}" \
--font-name="${FONT_NAME}" \
--output="${OUTPUT_FOLDER}" \
--quiet \
--force \
--no-hash 2> /dev/null
}
defineCharacterMapFunction()
{
_compileFont
echo "Overriding manifesto with custom codepoints"
target_manifest=".fontcustom-manifest.json"
temp_manifest="/tmp/manifest.json"
export DECIMAL_CODEPOINT=$(_convert_hex_to_dec "${CODEPOINT}")
awk '{
for(line=1; line<=NF; line++) {
if ($line~/\"codepoint":/) {
sub(/[0-9]+/,ENVIRON["DECIMAL_CODEPOINT"]+i++)
}
}
}1' "${target_manifest}" > "${temp_manifest}" && \
mv "${temp_manifest}" "${target_manifest}"
}
buildFunction()
{
echo "Generating font"
_compileFont
}
cleanUpFunction()
{
chown ${UID}:${GID} -R "${OUTPUT_FOLDER}"
rm -rf "${TEMP_FOLDER}"
rm .fontcustom-manifest.json
}
while [ $# -gt 0 ]; do
case "$1" in
--input*|-i*)
if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=`
INPUT_FOLDER="${1#*=}"
;;
--output*|-o*)
if [[ "$1" != *=* ]]; then shift; fi
OUTPUT_FOLDER="${1#*=}"
;;
--name*|-n*)
if [[ "$1" != *=* ]]; then shift; fi
FONT_NAME="${1#*=}"
;;
--codepoint*|-c)
if [[ "$1" != *=* ]]; then shift; fi
CODEPOINT="${1#*=}"
;;
--uid*|-u*)
if [[ "$1" != *=* ]]; then shift; fi
UID="${1#*=}"
;;
--gid*|-g*)
if [[ "$1" != *=* ]]; then shift; fi
GID="${1#*=}"
;;
--skip_conversion|-s)
SKIP_CONVERSION="1"
;;
--help|-h)
helpFunction
exit 0
;;
*)
>&2 printf "Error: Invalid argument\n"
exit 1
;;
esac
shift
done
if [ -z "$FONT_NAME" ]
then
echo "Font name is obrigatory";
helpFunction
fi
INPUT_FOLDER="${INPUT_FOLDER:-./input}"
OUTPUT_FOLDER="${OUTPUT_FOLDER:-${FONT_NAME}}"
UID="${UID:-0}"
GID="${GID:-${UID}}"
CODEPOINT="${CODEPOINT:-}"
SKIP_CONVERSION="${SKIP_CONVERSION:-0}"
prepareFunction
[ "${SKIP_CONVERSION}" != "1" ] && convertFunction
[ "${CODEPOINT}" ] && defineCharacterMapFunction
buildFunction
cleanUpFunction