aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/fontpreview
blob: 831bc16efd0293b7a6d21958feb8c9558c14c5de (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env bash
# License: GPLv3
# Credits: Felipe Facundes

SCRIPT="${0##*/}"
TMPDIR="${TMPDIR:-/tmp}"
FONTPREVIEWTEMPDIR="${TMPDIR}/${SCRIPT%.*}"
TEMP_FILE="$FONTPREVIEWTEMPDIR/temp_prev.jpg"

[[ -d "${FONTPREVIEWTEMPDIR}" ]] && rm -rf "${FONTPREVIEWTEMPDIR}"
[[ ! -d "${FONTPREVIEWTEMPDIR}" ]] && mkdir -p "${FONTPREVIEWTEMPDIR}"


VERSION=1.0
FONT_SIZE="${FONT_SIZE:-24}"
SIZE="${SIZE:-1000x700}"

BG_COLOR=${BG_COLOR:-"#ffffff"}
FG_COLOR=${FG_COLOR:-"#000000"}
PREVIEW_TEXT="${PREVIEW_TEXT:-$(
	cat <<-EOF
	{}

	ABCDEFGHIJKLMNOPQRSTUVWXYZ

	abcdefghijklmnopqrstuvwxyz

	1234567890

	'$\!/\%|&*@+=#?()[]'

	A white sheep peacefully 
	grazes on the green pasture.
	EOF
)}"

# Check if fzf and ImageMagick are installed
if ! command -v fzf magick &> /dev/null; then
    echo "Error: This script requires 'fzf' and 'ImageMagick' to be installed."
    exit 1
fi

imv -s none -i imv-preview 1>&2 2>/dev/null &
IMV_PID=$!

cleanup() { [[ -d "${FONTPREVIEWTEMPDIR}" ]] && rm -rf "${FONTPREVIEWTEMPDIR}" && kill -9 "$IMV_PID"; }
trap cleanup INT EXIT

MODE="$TEMP_FILE && (imv-msg $IMV_PID close all && imv-msg $IMV_PID open $TEMP_FILE)"

read -ra cmd <<< "$MODE"

clipboard_copy() {
    echo "$1" | wl-copy
    echo "Font copied to clipboard!"
}

show_requirements() {
printf "%s" "\
usage: $SCRIPT [--requirements]

Essential dependencies and their purposes:

Core requirements:
   fzf                    Fuzzy finder for interactive font selection
   ImageMagick            Font preview generation and display (magick command)

Optional dependencies:
   wl-copy                (Wayland) Copy fonts to clipboard
   xclip                  (X11) Copy fonts to clipboard  
   termux-clipboard-set   (Termux) Copy fonts to clipboard
   feh                    Fallback image viewer for terminals without Sixel support
   xdg-open               Fallback image viewer for Desktops without feh
"
}

show_help() {
printf "%s" "\
usage: $SCRIPT [-h] [--size \"px\"] [--font-size \"FONT_SIZE\"] [--bg-color \"BG_COLOR\"] 
                      [--fg-color \"FG_COLOR\"] [--preview-text \"PREVIEW_TEXT\"] [-i font.otf] 
                      [-o fontpreview.png] [--dark] [--version]
 
┌─┐┌─┐┌┐┌┌┬┐┌─┐┬─┐┌─┐┬  ┬┬┌─┐┬ ┬
├┤ │ ││││ │ ├─┘├┬┘├┤ └┐┌┘│├┤ │││
└  └─┘┘└┘ ┴ ┴  ┴└─└─┘ └┘ ┴└─┘└┴┘
Very customizable and minimal font previewer written in bash
 
optional arguments:
   -h,  --help            show this help message and exit
   -r,  --requirements    show dependencies and their purposes
   -i,  --input           filename of the input font (.otf, .ttf, .woff are supported)
   -o,  --output          filename of the output preview image (input.png if not set)
   -s,  --size            size of the font preview window (default: $SIZE)
   -fs, --font-size       font size (default: $FONT_SIZE)
   -bg, --bg-color        background color of the font preview window (default: $BG_COLOR)
   -fg, --fg-color        foreground color of the font preview window (default: $FG_COLOR)
   -t,  --text            preview text that should be displayed in the font preview window
   -v,  --version         show the version of fontpreview you are using
   -d,  --dark            switch to dark mode

Environment Variables:
   FONT_SIZE              Set default font size (currently: $FONT_SIZE)
   SIZE                   Set default preview size (currently: $SIZE)
   BG_COLOR               Set default background color (currently: $BG_COLOR)
   FG_COLOR               Set default foreground color (currently: $FG_COLOR)
   PREVIEW_TEXT           Set default preview text
"
echo
show_requirements
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        -s|--size)
            shift
            SIZE="$1"
            ;;
        -mr|--margin-right)
            shift
            FZF_MARGIN_R="$1"
            ;;
        -mh|--margin-height)
            shift
            FZF_MARGIN_H="$1"
            ;;
        -h|--help)
            show_help
            exit
            ;;
        -r|--requirements)
            show_requirements
            exit
            ;;
        -v|--version)
            echo "$VERSION"
            exit
            ;;
        -i|--input)
            shift
            input_font="$1"
            ;;
        -in|--install)
            shift
            install_font
            ;;
        -o|--output)
            shift
            output_file="$1"
            ;;
        -fs|--font-size)
            shift
            FONT_SIZE="$1"
            ;;
        -bg|--bg-color)
            shift
            BG_COLOR="$1"
            ;;
        -fg|--fg-color)
            shift
            FG_COLOR="$1"
            ;;
        -t|--text)
            shift
            PREVIEW_TEXT="$1"
            ;;
        -d|--dark)
            shift
            DARK=1
            ;;
        --)
            shift
            break
            ;;
        *)
            shift
            break
            ;;
    esac
    shift
done

if [[ -n "$DARK" ]]; then
    _tmpcolor="$BG_COLOR"
    BG_COLOR="$FG_COLOR"
    FG_COLOR="$_TMPCOL"
    unset _tmpcolor
fi

# If an input file was specified, use only that
if [[ -f "$input_font" ]]; then
    font_list="$input_font"
else
    # Generate font list only if no input file was specified
    font_list=$(magick -list font | grep "Font:" | awk '{print $2}' | sort -u)
fi

if [[ -n "$output_file" ]]; then
	[[ ! -f "$font_list" ]] && echo "Use -i to specify the font file and -o to extract the image" && exit 1
	magick -size "$SIZE" -background "$BG_COLOR" -fill "$FG_COLOR" -font "$font_list" \
		-pointsize "$FONT_SIZE" label:"$PREVIEW_TEXT" -geometry "$SIZE" "$output_file"
	exit 0
elif [[ -z "$output_file" ]]; then
	# Use fzf for interactive selection with Sixel preview
	selected_font=$(echo "$font_list" | fzf \
		--prompt="Select a font: " \
		--preview="magick -size \"$SIZE\" -background \"$BG_COLOR\" -fill \"$FG_COLOR\" -font '{}' \
		-pointsize \"$FONT_SIZE\" label:\"$PREVIEW_TEXT\" -geometry \"$SIZE\" ${cmd[*]}")
	# If a font was selected, show confirmation
	if [[ -n "$selected_font" ]]; then
		echo "Selected font: $selected_font"
		clipboard_copy "$selected_font"
	else
		echo "No font selected."
	fi
fi