alexmyczko / fnt

apt for fonts, the missing font manager for macOS/linux
Other
489 stars 20 forks source link

fnt preview should use sixel for higher resolution images #36

Closed hackerb9 closed 1 month ago

hackerb9 commented 8 months ago

The font preview is a nifty idea, but the resolution is too low. I don't know much about chafa, but I thought it handled sixels which would allow for a crisp bitmap display in the terminal. Perhaps there is something fnt needs to do to enable it?

Here is an comparison with using sixels, via the ImageMagick convert command, to view a font on the command line in a terminal that handles graphics. (Xterm).

Comparison of fnt preview and ImageMagick font display

convert -list font | grep Font | grep -v Noto
convert -size 800x -font Iosevka label:'ABC xyz' sixel:- ; echo
convert -size 800x -background orange -fill white -font Sudo-Regular label:$'\U2022 ABC xyz \U1F5D1' sixel:- ; echo

But that only works for fonts that are already installed. My hope is that fnt will be able to do the same thing for any font.

crpb commented 2 months ago

I only use urxvt so thats hard for me to write. @alexmyczko add "help needed/wanted" as a tag.

crpb commented 2 months ago

So i tried in kitty which looks like the right part of your screenshot. But i couldn't get that what you did there with convert working here in either kitty or urxvt.

But i created a list with all fonts currently missing a preview so we will let @alexmyczko generate them at least.

Maybe you could provide some examples for convert that could actually work with fonts installed via fnt? I can't find iosevka or sudo so that already would fails here. And with other fonts i get convert-error messages in kitty and xterm+urxvt don't care at all and the output is blank. And fyi, imagemagick can be really buggy. So that might not be the best tool to integrate at all. I could just a option to let you open up sensible-browser or xdg-open to the picture or put it underneath the picture so people could click on it to see that in the browser instead.

crpb commented 2 months ago

btw, kitty does it like a charm in comparisment to the best terminal emulator 🙉 maim_20240908-173655

crpb commented 2 months ago

Ahh kitty doesn't do sixels.. yakuake looks as good as this.

alexmyczko commented 2 months ago

what would it do on terminals that do not support sixel rendering?

crpb commented 2 months ago

what would it do on terminals that do not support sixel rendering?

i don't fooding know how it does what it does but i only checked at https://github.com/smasher164/arewesixelyet / https://www.arewesixelyet.com/ for info if it's sixel or some other magic :P.

fremgang commented 1 month ago

I modified preview() to use timg. works not just in kitty

preview() {
    PREVIEW="${FNTDATA}/preview.png"
    PREVIEWURI="https://screenshots.debian.net/screenshot/fonts-$1"
    if download "$PREVIEWURI" "${PREVIEW}"; then
        # Check if the preview is a "not available" placeholder
        if [ "$($md5 "$PREVIEW" | awk '{print$1}')" = "b5765b390157e36eaf721c8848a4b04d" ]; then
            PREVIEWURI="https://sid.ethz.ch/fonts/$1/preview.png"
            if download "$PREVIEWURI" "$PREVIEW"; then
                if ! file --brief --mime-type "$PREVIEW" | grep -q 'image/png'; then
                    echo "Couldn't retrieve a preview for ${1}."
                    exit 69
                fi
            fi
        fi
        if ! command -v timg &>/dev/null; then
            echo "Warning: timg not found. Use $i install timg to get rid of me." >&2
            echo -e "\v\t${PREVIEWURI}\v"
            exit 0
        else
            if file --brief --mime-type "$PREVIEW" | grep -q 'image/png'; then
                timg "$PREVIEW" && PRINTED=1
                echo "$1: $PREVIEWURI"
            fi
        fi
    fi
    if [ "$PRINTED" != "1" ]; then
        echo "Couldn't display a preview for $1." >&2
        return 69
    fi
}
crpb commented 1 month ago

I modified preview() to use timg. works not just in kitty

Wow, this works so much better.

image

I think we will add that as a preferred previewer where avaialble. Thank you!

crpb commented 1 month ago

murphys law..

2610-012152 < cb> TIL timg - much better than chaffa https://0x0.st/s/JKjDBPuD5XCrix_XjgM1mA/XUp9.png 2610-071611 < _jwilk> TIL timg(1) uses quadrants by default. So, try "chafa --symbols quad". 2610-071624 < _jwilk> For me, the latter is faster and looks better.

For my urxvt session timg looks slightly better but that might even depend on the font and whatnot :P.

/me gives @jwilk a head scratcher

AnonymouX47 commented 1 month ago

@crpb, what makes the output on the right hand side of your screenshot look the way it does is a combination of:

Side Note `chafa` has the widest and most flexible Unicode [symbol range selection] support (see the "SYMBOLS" section of `chafa(1)`) and is faster than most (if not all) other alternatives.

As regards the actual topic of this issue, chafa already provides sixel support (and uses it in some terminal emulators) amongst other protocols, with reasonably good format/protocol selection heuristics. Though, in this case (on XTerm, I suppose), the heuristics seem to be wrong (chafa currently uses terminfo majorly, I believe).

@hpjansson, any thoughts about this? :thinking:

AnonymouX47 commented 1 month ago

@crpb

Here is what it should look like with a better font (timg on the left, chafa (with all symbol maps; the way fnt uses it) on the right):

Screenshot_2024-10-26_01-07-01

and with the default symbol maps I suggested earlier, i.e without --symbols:

Screenshot_2024-10-26_01-06-54

I believe chafa's output in this case is still more crisp and the text in the image has a better definition.

hpjansson commented 1 month ago

As regards the actual topic of this issue, chafa already provides sixel support (and uses it in some terminal emulators) amongst other protocols, with reasonably good format/protocol selection heuristics. Though, in this case (on XTerm, I suppose), the heuristics seem to be wrong (chafa currently uses terminfo majorly, I believe).

@hpjansson, any thoughts about this? :thinking:

We're using TERM and a handful of supplementary env vars to inform our heuristics. We detect XTerm this way, but there's not enough information to determine if it's in an emulation mode with sixel support (e.g. vt340). I think we'd have to actively probe the terminal, and support for that is coming.

By the way, if you need the best possible quality, I recommend adding '-w 9' to the command line. It comes at a price in processing power, and is thus not suitable for animation.

crpb commented 1 month ago

@AnonymouX47 Uh even better .. if only i would have played with it 🙈 .. I actually never tried to change the chafa call as you can see and just trusted @alexmyczko as he was trying that in various systems.

on XTerm, I suppose

rxvt-unicode actually which after comparing looks much better than XTerm... image

% appres XTerm URxvt |grep -E 'font:|Termname:'
*VT100.utf8Fonts.font:  -misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-iso10646-1
*SimpleMenu*menuLabel.font:     -adobe-helvetica-bold-r-normal--*-120-*-*-*-*-iso8859-*
URxvt*termName: rxvt-unicode-256color
XTerm*font:     -*-terminus-medium-r-*-*-12-*-*-*-*-*-*-*
*IconFont:      nil2

@AnonymouX47 @hpjansson

If i read it correctly best to just drop everything other than maybe the -w 9 and call it a day? (Yeah we won't do any animation here *sniggers*)

After scanning through the manpage i at least see that i probably shouldn't add this -f symbols to not force sixel shell user to use symbols 🤣. Will probably do so.

Thanks you both for the input.

hpjansson commented 1 month ago

If i read it correctly best to just drop everything other than maybe the -w 9 and call it a day?

Yes! Ideally you should be able to leave all the rendering decisions to us. If the adaptive defaults do a poor job, I'll happily look at any issues you file.