+132
scritps/prefon
+132
scritps/prefon
···
1
+
#!/bin/sh
2
+
# Script to preview fonts
3
+
4
+
usage() {
5
+
printf '%b\n' "${0##*/} [-F FILE | -S | -t STRING] [OPTIONS]" \
6
+
"actions:" \
7
+
"\t-F FILE - read from file" \
8
+
"\t-S - read from stdin" \
9
+
"\t-t STRING - use a string" \
10
+
"\noptions:" \
11
+
"\t-b SIZE - border size" \
12
+
"\t-c COLORS - set the color of the text and background" \
13
+
"\t-f FONT - which font to preview" \
14
+
"\t-h - display this message" \
15
+
"\t-i - do not generate an inverse image" \
16
+
"\t-o FILE - output file for the generated image" \
17
+
"\t-s SIZE - size of the font" \
18
+
"\ndefault values:" \
19
+
"These values can be set before execution to change default values" \
20
+
"\tPREFON_ACTION=text (file, stdin, or text)" \
21
+
"\tPREFON_BORDER_SIZE=16" \
22
+
"\tPREFON_COLORS=\"000000,ffffff\"" \
23
+
"\tPREFON_FONT=\"monospace\"" \
24
+
"\tPREFON_OUTPUT=\"/tmp/prefon.png\"" \
25
+
"\tPREFON_SIZE=16" \
26
+
"\tPREFON_TEXT=\"The quick brown fox jumps\\\n over the lazy dog}\"" \
27
+
"\tPREFON_INVERSE=vertical (horizontal, vertical, none)"
28
+
}
29
+
30
+
info() {
31
+
case $1 in
32
+
i ) prefix=INFO ;;
33
+
w ) prefix=WARNING ;;
34
+
e ) prefix=ERROR ;;
35
+
* ) printf '%s\n' "sus!" 1>&2; exit 1 ;;
36
+
esac
37
+
shift
38
+
39
+
printf "${0##*/}: $prefix: %b\n" "$*" 1>&2
40
+
}
41
+
42
+
# Default values
43
+
PREFON_ACTION=${PREFON_ACTION:-text}
44
+
PREFON_BORDER_SIZE=${PREFON_BORDER_SIZE:-16}
45
+
PREFON_COLORS=${PREFON_COLORS:-000000,ffffff}
46
+
PREFON_FONT="${PREFON_FONT:-monospace}"
47
+
PREFON_OUTPUT="${PREFON_OUTPUT:-/tmp/prefon.png}"
48
+
PREFON_SIZE=${PREFON_SIZE:-16}
49
+
PREFON_TEXT="${PREFON_TEXT:-The quick brown fox jumps\n over the lazy dog}"
50
+
PREFON_INVERSE=${PREFON_WITHOUT_INVERSE:-vertical}
51
+
52
+
# Handle arguments
53
+
while [ "$*" ]
54
+
do
55
+
case $1 in
56
+
- ) shift; continue ;;
57
+
-- ) shift; break ;;
58
+
-* ) flag=${1#-}; shift ;;
59
+
* ) shift; continue ;;
60
+
esac
61
+
while [ "$flag" ]
62
+
do
63
+
arg=${flag%${flag#?}}
64
+
case $arg in
65
+
F ) PREFON_ACTION="file"; PREFON_FILE=$1; shift ;;
66
+
S ) PREFON_ACTION=stdin; PREFON_FILE=/dev/stdin ;;
67
+
b ) PREFON_BORDER_SIZE=$1; shift ;;
68
+
c ) PREFON_COLORS=$1; shift ;;
69
+
f ) PREFON_FONT=$1; shift ;;
70
+
h ) usage; exit 0 ;;
71
+
i ) PREFON_INVERSE=$1; shift ;;
72
+
o ) PREFON_OUTPUT=$1; shift ;;
73
+
s ) PREFON_SIZE=$1; shift ;;
74
+
t ) PREFON_ACTION=text; PREFON_TEXT=$1; shift ;;
75
+
* ) printf '%s\n' "${0##*/}: -$arg: invalid argument" 1>&2; usage 1>&2; exit 1 ;;
76
+
esac
77
+
flag=${flag#?}
78
+
done
79
+
done
80
+
81
+
generate_image() {
82
+
_gen_img() {
83
+
convert -background "#$2" -bordercolor "#$2" -border "$PREFON_BORDER_SIZE" \
84
+
pango:"<span foreground=\"#$1\" font_desc=\"$PREFON_FONT $PREFON_SIZE\">$4</span>" "$3"
85
+
}
86
+
87
+
fgcolor=${PREFON_COLORS%,*}
88
+
bgcolor=${PREFON_COLORS#*,}
89
+
90
+
_gen_img "$fgcolor" "$bgcolor" "$PREFON_OUTPUT" "$1" || { info e failed to generate image; exit 1; }
91
+
92
+
[ "$PREFON_INVERSE" != "none" ] && {
93
+
inverse="${PREFON_OUTPUT%.*}"
94
+
inverse="${inverse}-inverse.${PREFON_OUTPUT##*.}"
95
+
96
+
_gen_img "$bgcolor" "$fgcolor" "$inverse" "$1" || { info e failed to generate image; exit 1; }
97
+
98
+
case $PREFON_INVERSE in
99
+
horizontal ) convert "$PREFON_OUTPUT" "$inverse" +append "$PREFON_OUTPUT" \
100
+
|| { info e failed to generate image; exit 1; } ;;
101
+
vertical ) convert "$PREFON_OUTPUT" "$inverse" -append "$PREFON_OUTPUT" \
102
+
|| { info e failed to generate image; exit 1; } ;;
103
+
* ) info e "${PREFON_INVERSE}: is not a valid value for PREFON_INVERSE"; exit 1 ;;
104
+
esac
105
+
106
+
rm "$inverse" || info w failed to remove temporary file: "${inverse}"
107
+
}
108
+
109
+
printf '%s\n' "$PREFON_OUTPUT"
110
+
}
111
+
112
+
read_input() {
113
+
_output_file() {
114
+
while IFS= read -r line
115
+
do
116
+
printf '%s\n' "$line"
117
+
done < "$PREFON_FILE"
118
+
}
119
+
120
+
input="$(_output_file)"
121
+
generate_image "$input"
122
+
}
123
+
124
+
case $PREFON_ACTION in
125
+
file ) [ -e "$PREFON_FILE" ] || { info e "'$PREFON_FILE' does not exist"; exit 1; }
126
+
wa ;;
127
+
stdin ) [ -e /dev/stdin ] || { info e "/dev/stdin does not exist"; exit 1; }
128
+
wa ;;
129
+
text ) [ "$PREFON_TEXT" ] || { info e "string not provided"; exit 1; }
130
+
generate_image "$PREFON_TEXT" ;;
131
+
* ) info e invalid action: $PREFON_ACTION; exit 1 ;;
132
+
esac