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