#!/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