forked from bobafetthotmail/refind-theme-regular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont2png.sh
executable file
·142 lines (130 loc) · 4.05 KB
/
font2png.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
139
140
141
142
#!/bin/bash
#
# Apply from mkfont.sh (by Roderick W. Smith)
#
# This program is licensed under the terms of the GNU GPL, version 3,
# or (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
CONVERT=`which convert 2> /dev/null`
FONT_NAME=""
FONT_SIZE=""
Y_POS=0
function print_help(){
echo "Generate a PNG file suitable for use as a rEFInd font"
echo "Usage:"
echo "$0 [[-f|--font] <font_name>] [[-s|--size] <number>] <outfile.png>"
echo "or"
echo "$0 [options]... <outfile.png>"
echo ""
echo "Options:"
echo "-f,--font: <font_name> Name of font"
echo "-s,--size: <number> Font size in points"
echo "-y,--y-pos: <number> or <number%> Y offset position in pixels"
echo " or in percentage by font height"
echo ""
echo ""
echo "-l,--list-font Display fonts list and exit"
echo "-h,--help Display this help message and exit"
echo ""
exit 1
}
if [ $# -ne 0 ]
then
ARGS=`getopt -a -o hlf:s:y: -l help,list-font,font:,size:,y-pos: -n "$0" -- "$@"`
eval set -- "$ARGS"
while [ $# -ne 0 ]
do
case "$1" in
-h|--help)
print_help
exit 1
;;
-l|--list-font)
$CONVERT -list font
exit 1
;;
-f|--font)
FONT_NAME=$2
shift 2
;;
-s|--size)
FONT_SIZE=$2
shift 2
;;
-y|--y-pos)
Y_POS=$2
shift 2
;;
--)
shift
break
;;
esac
done
else
echo "Try \`$0 --help' for more information." 1>&2
exit 1
fi
#font-name
if [[ $FONT_NAME == "" ]]
then
echo "$0 --font must be specified." 1>&2
exit 1
fi
#font-size
if [[ $FONT_SIZE == "" ]]
then
echo "$0 --size must be specified." 1>&2
exit 1
elif [[ $FONT_SIZE =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ $FONT_SIZE =~ ^-?[0-9]+([.][0-9]+)?$ ]]
then
font_size=`echo $FONT_SIZE | sed 's/^-//g'` #convert to positive
FONT_SIZE=${font_size%.*}
else
echo "$0 --size \`$FONT_SIZE' wrong numerical." 1>&2
exit 1
fi
#y-pos default=0
if [[ $Y_POS =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ $Y_POS =~ ^-?[0-9]+([.][0-9]+)?$ ]] #y-pos in pixels
then
v=${Y_POS%.*}
Y_POS=$v
elif [[ $Y_POS =~ ^[0-9]+([.][0-9]+)?%$ ]] #y-pos in percentage
then
v=`echo $Y_POS | sed 's/^-//g; s/%$//g'`
let y_position=(${FONT_SIZE}*${v%.*})/100
Y_POS=$y_position
elif [[ $Y_POS =~ ^-?[0-9]+([.][0-9]+)?%$ ]] #y-pos in percentage(negative)
then
v=`echo $Y_POS | sed 's/^-//g; s/%$//g'`
let y_position=(${FONT_SIZE}*${v%.*})/100
Y_POS=`echo "-$y_position"`
else
echo "$0 --y-pos \`$Y_POS' wrong numerical." 1>&2
exit 1
fi
#output_file
if [ $# -gt 0 ]
then
OUTPUT_PNG=$1
shift $#
else
echo "$0 Output file must be specified." 1>&2
exit 1
fi
if [[ ! $OUTPUT_PNG =~ .png$ ]]
then
echo "$0 \`$OUTPUT_PNG', output file must be PNG." 1>&2
exit 1
fi
HEIGHT=$FONT_SIZE
let CEllWIDTH=(${HEIGHT}*6+5)/10
let WIDTH=${CEllWIDTH}*96
echo "Creating ${WIDTH}x${HEIGHT} font bitmap...."
$CONVERT -size ${WIDTH}x${HEIGHT} xc:transparent \
-gravity SouthWest \
-font $FONT_NAME \
-pointsize $FONT_SIZE \
-draw "text 0,$Y_POS ' !\"#\$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_\`abcdefghijklmnopqrstuvwxyz{|}~?'" \
$OUTPUT_PNG