diff --git a/LICENSE b/LICENSE index 94535ad..42a486c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ MIT License Copyright (c) 2020 Siddharth Dushantha +Copyright (c) 2025 Paul Fox Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile deleted file mode 100644 index 2e453d9..0000000 --- a/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PREFIX?= /usr -BINDIR= ${PREFIX}/bin - -all: - @echo Run \'make install\' to install fontpreview on your device - -install: - @mkdir -p $(DESTDIR)$(BINDIR) - @cp fontpreview $(DESTDIR)$(BINDIR)/fontpreview - @chmod 755 $(DESTDIR)$(BINDIR)/fontpreview - @echo fontpreview has been installed on your device - -uninstall: - @rm -rf $(DESTDIR)$(BINDIR)/fontpreview - @rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(BINDIR) - @echo fontpreview has been removed from your device diff --git a/README.md b/README.md index fac96bf..b084dec 100644 --- a/README.md +++ b/README.md @@ -1,113 +1,74 @@ -


✨ Very customizable and minimal font previewer written in bash ✨

+ -`fontpreview` is a commandline tool that lets you **quickly search** for fonts -that are installed on your machine and preview them. The **fuzzy search** feature -is provided by `fzf` and the preview is generated with `imagemagick` and then -displayed using `nsxiv`. This tool is **highly customizable**, almost all of the -variables in this tool can be changed using the commandline flags or you can -configure them using environment variables. +# fontpreview -![](extra/demo.gif) +This previewer started as a clone of +, +written by github user sdushantha. They created a clever mechanism +for quickly getting a look at the contents of a font, in an easy to +navigate way. It's not clear that the original project is still being +maintained, so I've kind of had my way with the code, and it's diverged +quite a bit. The rest of the README describes my version, though from +a high level it's still pretty similar to sdushantha's. + +__fontpreview__ lets you quickly search through installed fonts using +the __fzf__ "fuzzy matcher", and preview them. The previewer (any of +__sxiv__, __nsxiv__, or __feh__ will work) is normally reused for each +new font, but it's also possible to bring up a separate preview for +every selection. + +![Animated screen showing fontpreview in use](extra/demo.gif) ## Dependencies -- `xdotool` -- `fzf` -- `imagemagick` -- `nsxiv` +Things you might need to fetch from your package manager: + +- xdotool +- fzf +- imagemagick +- nsxiv, sxiv, or feh + + +## Usage Summary + + usage: + + fontpreview [options] + to preview a font from its file (.otf, .ttf) + + fontpreview [options] + to create an image from the preview + + fontpreview [options] + interactive use + + Options: + -text text to be previewed + -font-size font size for the preview window + -no-preview-header don't include the font name in the preview + -multi-viewer spawn new viewer per font. (clean up manually.) + -bg-color background color for the preview window + -fg-color foreground color for the preview window + -size WxH width and height in pixels (overrides autosizing) + -position +X+Y/-X-Y position, as in X geometry (see "man X") + + All options can be shortened to the first character of each word in + the option: e.g., '-fs' for '-font-size', or '-t' for '-text'. + (Or, reduced to the the shortest unique prefix.) + Either '-' or '--' can introduce any option. + ## Installation -### Install using `make` -```bash -# Clone the repo -$ git clone https://github.com/sdushantha/fontpreview - -# Change your current directory to fontpreview -$ cd fontpreview - -# Install it -$ sudo make install -``` - -### Install it locally -```bash -# Download the fontpreview source code, save as fontpreview -# and make it executeable -$ curl -L https://git.io/raw_fontpreview > fontpreview && chmod +x fontpreview - -# Then move fontpreview to somewhere in your $PATH -# Here is an example -$ mv fontpreview ~/scripts/ -``` - -## Usage -``` -$ fontpreview --help -usage: fontpreview [-h] [--size "px"] [--position "+x+y"] [--search-prompt SEARCH_PROMPT] - [--font-size "FONT_SIZE"] [--bg-color "BG_COLOR"] [--fg-color "FG_COLOR"] - [--preview-text "PREVIEW_TEXT"] [-i font.otf] [-o preview.png] [--version] - -┌─┐┌─┐┌┐┌┌┬┐┌─┐┬─┐┌─┐┬ ┬┬┌─┐┬ ┬ -├┤ │ ││││ │ ├─┘├┬┘├┤ └┐┌┘│├┤ │││ -└ └─┘┘└┘ ┴ ┴ ┴└─└─┘ └┘ ┴└─┘└┴┘ -Very customizable and minimal font previewer written in bash - -optional arguments: - -h, --help show this help message and exit - -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) - --size size of the font preview window - --position the position where the font preview window should be displayed - --search-prompt input prompt of fuzzy searcher - --font-size font size - --bg-color background color of the font preview window - --fg-color foreground color of the font preview window - --preview-text preview text that should be displayed in the font preview window - --version show the version of fontpreview you are using -``` - -If you want to generate a preview image for a **single** font file -(.otf, .ttf, and .woff are supported), use the `-i` and `-o` option -to indicate the filename of the input font and the output preview -image. - -``` -$ fontpreview -i font.otf -o preview.png -``` - -This can be used with [überzug](https://github.com/seebye/ueberzug) -to implement font preview within terminal file managers such as -[vifm](https://vifm.info/). - -![](extra/vifm.png) - -A detailed setup instructions can be found [here](https://krasjet.com/scribbles/font-preview.html) - - -## Configure -You can configure `fontpreview` through environment variables. - -This can be in your `.bashrc`, `.zshrc`, etc - -```bash -# Input prompt of fuzzy searcher -export FONTPREVIEW_SEARCH_PROMPT="❯ " - -# Size of the font preview window -export FONTPREVIEW_SIZE=532x365 - -# The position where the font preview window should be displayed -export FONTPREVIEW_POSITION="+0+0" - -# Font size -export FONTPREVIEW_FONT_SIZE=38 - -# Background color of the font preview window -export FONTPREVIEW_BG_COLOR="#ffffff" - -# Foreground color of the font preview window -export FONTPREVIEW_FG_COLOR="#000000" - -# Preview text that should be displayed in the font preview window -export FONTPREVIEW_PREVIEW_TEXT="ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n1234567890\n!@$\%(){}[]" -``` +Simply download the script, make it executable, and put it in your +PATH somewhere. To download from github, click on the script "fontpreview", +then use the "hamburger" menu or ctrl-shift-s to download it. You'll need +to "```mv fontpreview.txt fontpreview```" after it's downloaded. + +## Customization + +Hey, it's a shell script -- customize all you want. ;-) If you +just want to change some of the predefined defaults for color, size, +etc, there's provision for putting those new definitions into +`$HOME/.fontpreview`. See the script for the names of the variables +that can be changed in that way. diff --git a/extra/demo.gif b/extra/demo.gif index 614dd2f..1a09c9c 100644 Binary files a/extra/demo.gif and b/extra/demo.gif differ diff --git a/fontpreview b/fontpreview index c4febae..ed77592 100755 --- a/fontpreview +++ b/fontpreview @@ -1,244 +1,465 @@ -#!/usr/bin/env bash +#!/bin/bash # # Siddharth Dushantha 2020 +# Paul Fox 2025 +# See MIT license, below. # -# Dependencies: nsxiv, imagemagick, xdotool, fzf +# Dependencies: nsxiv, sxiv, or feh, and imagemagick, xdotool, fzf -VERSION=1.0.7 +# debug=true -# Default values -SEARCH_PROMPT="❯ " -SIZE=1000x1000 -POSITION="+0+0" -FONT_SIZE=38 -BG_COLOR="#ffffff" -FG_COLOR="#000000" -PREVIEW_TEXT="ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n1234567890\n!@$\%(){}[]\nالسلام عليكم" - -show_help() { +usage() +{ printf "%s" "\ -usage: fontpreview [-h] [--size \"px\"] [--position \"+x+y\"] [--search-prompt SEARCH_PROMPT] - [--font-size \"FONT_SIZE\"] [--bg-color \"BG_COLOR\"] [--fg-color \"FG_COLOR\"] - [--preview-text \"PREVIEW_TEXT\"] [-i font.otf] [-o preview.png] [--version] - -┌─┐┌─┐┌┐┌┌┬┐┌─┐┬─┐┌─┐┬ ┬┬┌─┐┬ ┬ -├┤ │ ││││ │ ├─┘├┬┘├┤ └┐┌┘│├┤ │││ -└ └─┘┘└┘ ┴ ┴ ┴└─└─┘ └┘ ┴└─┘└┴┘ -Very customizable and minimal font previewer written in bash - -optional arguments: - -h, --help show this help message and exit - -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) - --size size of the font preview window - --position the position where the font preview window should be displayed - --search-prompt input prompt of fuzzy searcher - --font-size font size - --bg-color background color of the font preview window - --fg-color foreground color of the font preview window - --preview-text preview text that should be displayed in the font preview window - --version show the version of fontpreview you are using + +usage: + + fontpreview [options] + to preview a font from its file (.otf, .ttf) + + fontpreview [options] + to create an image from the preview + + fontpreview [options] + interactive use + + Options: + -text text to be previewed + -font-size font size for the preview window + -no-preview-header don't include the font name in the preview + -multi-viewer spawn new viewer per font. (clean up manually.) + -bg-color background color for the preview window + -fg-color foreground color for the preview window + -size WxH width and height in pixels (overrides autosizing) + -position +X+Y/-X-Y position, as in X geometry (see \"man X\") + + All options can be shortened to the first character of each word in + the option: e.g., '-fs' for '-font-size', or '-t' for '-text'. + (Or, reduced to the the shortest unique prefix.) + Either '-' or '--' can introduce any option. " } -pre_exit() { - # Get the proccess ID of this script and kill it. - # We are dumping the output of kill to /dev/null - # because if the user quits nsxiv before they - # exit this script, an error will be shown - # from kill and we dont want that - kill -9 "$(cat "$PIDFILE" 2>/dev/null)" &> /dev/null +# Debug support: +# Save a copy of original stderr on fd 3 for fzf to use. +# fd 4 will point at either /dev/null or stderr, depending on whether +# we're tracing. +exec 3>&2 +exec 4>/dev/null # dump fd 4, except when debugging (see below) +[[ $debug ]] && { exec 2>/tmp/fontpreview.log ; set -x; exec 4>&2; } + + +# Use mktemp to create a temporary directory that won't +# collide with temporary files of other application. +fp_dir="$(mktemp -d "${TMPDIR:-/tmp}/fontpreview_dir.XXXXXXXX")" || exit + +preview_image_name="$fp_dir/fontpreview" # .png suffix added later + +# Default values, mostly changeable from command line, and +# certainly from user config file +autosize=true # we autosize the window unless its overridden with --size +preview_size=530x730 +preview_pos=; # window manager chooses a position by default +font_size=38 # points +fontname_size=30 # points +bg_color="#ffffff" +fg_color="#000000" +preview_text=\ +'ABCDEFGHIJKLM\n\ + NOPQRSTUVWXYZ\n\ + abcdefghijklm\n\ + nopqrstuvwxyz\n\ + 1234567890\n\ + !@$\\/%%(){}[]\n\ + .,:;=+>--<__\n\ + ({[ -~\n\ + DO08B 1Ili\n\ + 098756938217\n\ + AR 9gq oa\n\ + السلام عليكم' +preview_include_fontname=true +fontname_wrap=30 # width in characters to which we wrap the name in the header + +# let user override the defaults above +source ~/.fontpreview 2>&4 + +on_exit() +{ + # kill the image viewer + test "$viewerpid" && kill -TERM $viewerpid 2>/dev/null # Delete tempfiles, so we don't leave useless files behind. - rm -rf "$FONTPREVIEW_DIR" + rm -rf "$fp_dir" + + echo + exit } -generate_preview(){ +wrapfontname() +{ + if [[ -f "$1" ]] + then # if it's a filename, just fold the basename + fold -w $fontname_wrap <<<"${1##*/}" + else # otherwise, change '-' to space and wrap on spaces + sed -e 's/-/ /g' <<<"$1" | fold -s -w $fontname_wrap + fi +} + +generate_preview() +{ + + fontspec="$1" + image="$2" + + autosize_preview + + declare -a headerargs + if [[ $preview_include_fontname ]] + then + wrapped_fontname="$(wrapfontname $fontspec)" + headerargs=( + -gravity north -pointsize $fontname_size + -font "$fontspec" + -annotate +0+0 "$wrapped_fontname" + ) + fi + + tempimage=$fp_dir/tmp-$(basename "$image") + # Credits: https://bit.ly/2UvLVhM - magick -size $SIZE xc:"$BG_COLOR" \ - -gravity center \ - -pointsize $FONT_SIZE \ - -font "$1" \ - -fill "$FG_COLOR" \ - -annotate +0+0 "$PREVIEW_TEXT" \ - -flatten "$2" + $magick -size $preview_size xc:"$bg_color" \ + "${headerargs[@]}" \ + -gravity south \ + -pointsize $font_size \ + -font "$fontspec" \ + -fill "$fg_color" \ + -annotate +0+0 \ + "$preview_text" \ + $tempimage && + mv "$tempimage" "$image" } -main(){ - # Checkig if needed dependencies are installed - dependencies=(xdotool nsxiv magick fzf) - for dependency in "${dependencies[@]}"; do - type -p "$dependency" &>/dev/null || { - echo "error: Could not find '${dependency}', is it installed?" >&2 - exit 1 - } - done +error() +{ + echo fontpreview: "$@" >&2 + [[ $debug ]] && echo fontpreview: "$@" >&3 + exit 1 +} + +exists() +{ + type -p "$1" &>/dev/null +} + +find_tools() +{ + # Checking if needed dependencies are installed + if exists nsxiv + then + viewer=nsxiv + # -N sets the X11 resource class name, not the title + vieweropts=( -N "fontpreview" -b ) + elif exists sxiv + then + viewer=sxiv + vieweropts=( -N "fontpreview" -b ) + elif exists feh + then + viewer=feh + vieweropts=( --title "fontpreview" ) + fi + test "$viewer" || error None of nsxiv, sxiv, or feh is available. + + exists convert && magick=convert + exists magick && magick=magick + test "$magick" || error Neither magick nor convert is available. + + exists xdotool || error xdotool not available + exists fzf || error fzf not available + +} + +start_viewer() +{ + # put the viewer in its own session, so it will keep running if + # we suspend. otherwise it will suspend too, and not be quittable + vieweropts+=( -g "$preview_size$preview_pos" ) + : ${vieweropts[@]} + setsid $viewer ${vieweropts[@]} $preview_image_name$newviewer.png & +} + +one_shot_commands() +{ + fontfile="$1" + imagefile="$2" + + test "$fontfile" || return + + # Point a font file to fontpreview and it will preview it. + # $ fontpreview fontFile.ttf + # Or, put the preview into an image file: + # $ fontpreview fontFile.ttf /tmp/fontimage.png + + test -r "$fontfile" || error "No such file font file $fontfile" + + if [ -f "$fontfile" ]; then + + if [[ "$imagefile" ]] + then + # user-specified image name + generate_preview "$fontfile" "$imagefile" + exit + fi - # Checking for enviornment variables which the user might have set. - # This config file for fontpreview is pretty much the bashrc, zshrc, etc - # Majority of the variables in fontpreview can changed using the enviornment variables - # and this makes fontpreview very customizable - [[ $FONTPREVIEW_SEARCH_PROMPT != "" ]] && SEARCH_PROMPT=$FONTPREVIEW_SEARCH_PROMPT - [[ $FONTPREVIEW_SIZE != "" ]] && SIZE=$FONTPREVIEW_SIZE - [[ $FONTPREVIEW_POSITION != "" ]] && POSITION=$FONTPREVIEW_POSITION - [[ $FONTPREVIEW_FONT_SIZE != "" ]] && FONT_SIZE=$FONTPREVIEW_FONT_SIZE - [[ $FONTPREVIEW_BG_COLOR != "" ]] && BG_COLOR=$FONTPREVIEW_BG_COLOR - [[ $FONTPREVIEW_FG_COLOR != "" ]] && FG_COLOR=$FONTPREVIEW_FG_COLOR - [[ $FONTPREVIEW_PREVIEW_TEXT != "" ]] && PREVIEW_TEXT=$FONTPREVIEW_PREVIEW_TEXT - - # Save the window ID of the terminal window fontpreview is executed in. - # This is so that when we open up nsxiv, we can change the focus back to - # the terminal window, so that the user can search for the fonts without - # having to manualy change the focus back to the terminal. - xdotool getactivewindow > "$TERMWIN_IDFILE" - - # Flag to run some commands only once in the loop - FIRST_RUN=true - - while true; do - # List out all the fonts which imagemagick is able to find, extract - # the font names and then pass them to fzf - font=$(magick -list font | awk -F: '/^[ ]*Font: /{print substr($NF,2)}' | fzf --prompt="$SEARCH_PROMPT") - - # Exit if nothing is returned by fzf, which also means that the user - # has pressed [ESCAPE] - [[ -z $font ]] && return - - generate_preview "$font" "$FONT_PREVIEW" - - if [[ $FIRST_RUN == true ]]; then - FIRST_RUN=false - - # Display the font preview using nsxiv - #nsxiv -g "$SIZE$POSITION" "$FONT_PREVIEW" -N "fontpreview" -b & - nsxiv -N "fontpreview" -b -g "$SIZE$POSITION" "$FONT_PREVIEW" & + # image name in our temp dir + generate_preview "$fontfile" "$preview_image_name.png" + + start_viewer + # Don't set viewerpid, else it will be killed when we exit. (The + # image will be removed when we exit, but the viewer will already + # have read it.) + + # exit will remove the file, so give viewer time to read it first + sleep 0.2 + exit + fi +} + +percent() +{ + [[ $# == 2 ]] || error internal error + echo $(( $1 * $2 / 100 )) +} + +autosize_preview() +{ + [[ $autosize ]] || return + + # this is a pain, because fonts vary greatly in height, even + # for a given point size. and their names can be + + # get the "dimensions" of the preview text + dims=( $(echo "$preview_text" | wc -l -L) ) + hgt=${dims[0]} # height (number of lines) + wid=${dims[1]} # width (length of longest line) + + prev_h=$(( $hgt * $font_size )) + prev_w=$(( $wid * $font_size )) + + # some fairly arbitrary padding -- point size apparently doesn't + # really mean anything, and fonts can vary by at least 30% in + # inter-line spacing, in my experience. try Courier vs + # IBM-Plex-Sans-Condensed-ExtraLight-Italic for example. + prev_h=$(percent 130 $prev_h) + prev_w=$(percent 110 $prev_w) + + # so far we've assumed that characters are square. fix that: + # assume that char width is 2/3 of height (western langs only, of + # course) + (( prev_w = 2 * prev_w / 3 )) + + # need to add extra for the fontname header height and width + if [[ $preview_include_fontname ]] + then + # we forcibly wrap the fontname, so we know the longest possible line + hdr_w=$(( fontname_wrap * fontname_size )) + hdr_w=$(( 2 * hdr_w / 3 )) + + (( prev_w < hdr_w )) && prev_w=$hdr_w + + # assume at most 3 lines of wrapped font name in the header + hdr_h=$(( 3 * $fontname_size )) + prev_h=$(($prev_h + $hdr_h)) + fi + + preview_size=${prev_w}x${prev_h} +} + +# for some reason using convert to size the text really doesn't work +# well -- it exaggerates like crazy. which probably means i'm doing +# something wrong. but also, it's much much slower, and causes a +# noticeable delay, so I'm happy not to use it. +autosize_preview_using_convert() +{ + [[ $autosize ]] || return + + dims=( $(convert -background none -fill black -font $fontspec \ + -pointsize $font_size -size 300x caption:"$preview_text" \ + -trim -format "%h %w\n" info:) ) + hgt=${dims[0]} # height + wid=${dims[1]} # width + + prev_h=$(percent 110 $hgt) + prev_w=$(percent 110 $wid) + + # need to add extra for the fontname header height and width + if [[ $preview_include_fontname ]] + then + # we forcibly wrap the fontname, so we know the longest possible line + hdr_w=$(( fontname_wrap * fontname_size )) + hdr_w=$(( 2 * hdr_w / 3 )) + + (( prev_w < hdr_w )) && prev_w=$hdr_w + + # assume at most 3 lines of wrapped font name in the header + hdr_h=$(( 3 * $fontname_size )) + prev_h=$(($prev_h + $hdr_h)) + fi + + preview_size=${prev_w}x${prev_h} +} + +main() +{ + # Save the window ID of our terminal window. We want to be able to + # bring focus back here, after spawning the viewer. + xdotool getactivewindow > "$fp_dir/term_win_id" + + qstring=; + + while : loop; do + # List all fonts imagemagick can find, and pass them to fzf. + # fzf's result file will have the query as the first line, and + # the result as the second. Or just the query if not found, + # or neither on failure. + $magick -list font | + sed -n -e '/^ *Font: /s///p' | + fzf --query="$qstring" \ + -i \ + --history=$fp_dir/history \ + --bind 'esc:ignore' \ + --print-query \ + --no-color \ + --prompt="> " 2>&3 \ + > $fp_dir/result + fzf_ret=$? + + qstring=$(head -n 1 $fp_dir/result) # might be empty + + # fzf exit codes: + # 0 Normal exit + # 1 No match + # 2 Error + # 130 Interrupted with CTRL-C or ESC + + case $fzf_ret in + 130|2) return ;; # error or interrupt + 1) continue ;; # no match -- reuse the previous query string + esac + + font=$(tail -n 1 $fp_dir/result) + + # make sure there's only one entry for this font in history, + # and make sure it's at the end. + grep -iv "^$font$" $fp_dir/history > $fp_dir/history.new + echo "$font" >> $fp_dir/history.new + mv $fp_dir/history.new $fp_dir/history + + # use found result as next query + qstring="$font" + + generate_preview "$font" "$preview_image_name$newviewer.png" + + if [[ ! $viewerpid ]] || ! kill -0 $viewerpid 2>/dev/null; then + start_viewer + if [[ $multi_viewer ]] + then + # by not setting $viewerpid, a new viewer will be + # started for every font selected. incrementing + # newviewer will give every viewer a unique filename, + (( newviewer++ )) + else + # this will let us reuse the existing viewer, since + # we'll have a record that it's alive. and by not + # setting $newviewer, the viewer will keep re-reading + # the same preview file. + viewerpid=$! + fi # Change focus from nsxiv, back to the terminal window # so that user can continue to search for fonts without # having to manually change focus back to the terminal window - xdotool windowfocus "$(cat "$TERMWIN_IDFILE")" - - # Save the process ID so that we can kill - # nsxiv when the user exits the script - echo $! >"$PIDFILE" - - # Check for crashes of nsxiv - elif [[ -f $PIDFILE ]] ; then - if ! pgrep -F "$PIDFILE" >/dev/null 2>&1; then - echo "Restart nsxiv - You maybe using a obsolete version. " >&2 - # Display the font preview using nsxiv - nsxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" & - - # Change focus from nsxiv, back to the terminal window - # so that user can continue to search for fonts without - # having to manually change focus back to the terminal window - xdotool windowfocus "$(cat "$TERMWIN_IDFILE")" - - # Save the process ID so that we can kill - # nsxiv when the user exits the script - echo $! >"$PIDFILE" - fi - - fi + xdotool windowfocus "$(cat "$fp_dir/term_win_id")" + fi done } -# Disable CTRL-Z because if we allowed this key press, -# then the script would exit but, nsxiv would still be -# running -trap "" SIGTSTP +process_args() +{ + options=$(getopt \ + --name fontpreview \ + --alternative \ + --options "hm" \ + --longoptions "position:,p:,\ + size:,s:,\ + font-size:,fs:,\ + bg-color:,bg:,bc:,\ + fg-color:,fg:,fc:,\ + text:,t:,\ + no-preview-header,nph,\ + multi-viewer,mv,m,\ + help,h" \ + -- "$@") || error "use -h for help" + + eval set -- "$options" + + while : option loop + do + : case "$1" + case "$1" in + --position|--p) preview_pos=$2; shift ;; + --size|--s) preview_size=$2; autosize=; shift ;; + --font-size|--fs) font_size=$2; shift ;; + --bg-color|--bc|--bg) bg_color=$2; shift ;; + --fg-color|--fc|--fg) fg_color=$2; shift ;; + --text|--t) preview_text=$2; shift ;; + --no-preview-header|--nph) preview_include_fontname=; ;; + --multi-viewer|--mv|--m|-m) multi_viewer=true ;; + --help|--h|-h) usage; exit ;; + --) + shift + break + ;; + esac + shift + done + fileargs=( "$@" ) -trap pre_exit EXIT +} -# Use mktemp to create a temporary directory that won't -# collide with temporary files of other application. -FONTPREVIEW_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fontpreview_dir.XXXXXXXX")" || exit -PIDFILE="$FONTPREVIEW_DIR/fontpreview.pid" -touch "$PIDFILE" || exit -FONT_PREVIEW="$FONTPREVIEW_DIR/fontpreview.png" -touch "$FONT_PREVIEW" || exit -TERMWIN_IDFILE="$FONTPREVIEW_DIR/fontpreview.termpid" -touch "$TERMWIN_IDFILE" || exit - -font=$1 - - -# Parse the arguments -options=$(getopt -o hi:o: --long position:,size:,version,search-prompt:,font-size:,bg-color:,fg-color:,preview-text:,input:,output:,help -- "$@") -eval set -- "$options" - -while true; do - case "$1" in - --size) - shift - FONTPREVIEW_SIZE=$2 - ;; - --position) - shift - FONTPREVIEW_POSITION=$2 - ;; - -h|--help) - show_help - exit - ;; - --version) - echo $VERSION - exit - ;; - -i|--input) - input_file="$2" - ;; - -o|--output) - output_file="$2" - ;; - --search-prompt) - FONTPREVIEW_SEARCH_PROMPT=$2 - ;; - --font-size) - FONTPREVIEW_FONT_SIZE=$2 - ;; - --bg-color) - FONTPREVIEW_BG_COLOR=$2 - ;; - --fg-color) - FONTPREVIEW_FG_COLOR=$2 - ;; - --preview-text) - FONTPREVIEW_PREVIEW_TEXT=$2 - ;; - --) - shift - break - ;; - esac - shift -done - - -# Point a font file to fontpreview and it will preview it. -# Example: -# $ fontpreview /path/to/fontFile.ttf -# -# This is useful because people can preview fonts which they have not -# installed onto their system. So if they want to preview a font file that -# is in their Downloads directory, then they can easily preview it. -if [ -f "$font" ]; then - generate_preview "$font" "$FONT_PREVIEW" +trap on_exit EXIT - # Display the font preview using nsxiv - nsxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" & +find_tools - # For some strange reason, nsxiv just doesnt have time to read the file - sleep 0.1 - exit -fi +process_args "$@" -# Check if the user gave an input file if they did, then create a preview -# and then save the preview to the current working directory -if [ "$input_file" != "" ] ; then - [ -z "$output_file" ] && output_file="${input_file}.png" - generate_preview "$input_file" "$output_file" - exit -fi +one_shot_commands ${fileargs[0]} ${fileargs[1]} # might not return main + +exit + +# +# MIT License +# +# Copyright (c) 2020 Siddharth Dushantha +# Copyright (c) 2025 Paul Fox +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +#