Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hb-view] Use Chafa for terminal graphics if available #2959

Merged
merged 2 commits into from
Apr 26, 2021
Merged

[hb-view] Use Chafa for terminal graphics if available #2959

merged 2 commits into from
Apr 26, 2021

Conversation

hpjansson
Copy link
Contributor

Link with and use Chafa if available. This produces high-quality terminal graphics using symbols or sixels according to the detected terminal capabilities.

Fixes #2430.

@behdad
Copy link
Member

behdad commented Apr 24, 2021

Lovely!

One minor suggestion. Can we keep including and call the ansi-print code even when Chafa is available, such that we compile-check them, and let the compiler not use it?

@hpjansson
Copy link
Contributor Author

One minor suggestion. Can we keep including and call the ansi-print code even when Chafa is available, such that we compile-check them, and let the compiler not use it?

Sure thing, it's done.

meson.build Outdated Show resolved Hide resolved
hpjansson and others added 2 commits April 25, 2021 14:24
This produces high-quality terminal graphics using symbols or sixels
according to the detected terminal capabilities.

Fixes #2430.
No suitable versions are available on these setups.
config = chafa_canvas_config_new ();
chafa_canvas_config_set_canvas_mode (config, mode);
chafa_canvas_config_set_pixel_mode (config, pixel_mode);
chafa_canvas_config_set_cell_geometry (config, 10, 20);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious. How are these numbers used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cell geometry is the pixel size of a single character cell, and the canvas geometry is the size of the canvas in character cells. The cell size is only used in sixel mode: Let's say you have a canvas that's 10 cells wide, and we supply that character cells are 10 pixels wide. Chafa will then produce a sixel image that's 100 pixels wide, scaling the input image if necessary.

I decided to use 10x20 because that's the fixed cell size of VT240 and VT340 terminals (and possibly Windows Terminal too when/if it gets sixel support).

This will lead to sixel output being a bit bigger than expected on modern Linux terminals (character cells tend to be bigger than 10x20, but the aspect is usually about the same). It would be possible to get the cell size with the TIOCGWINSZ ioctl (.ws_xpixel and .ws_ypixel fields divided by .ws_col and .ws_row). The chafa command-line tool does this, but I avoided it in this patch because it seemed minor and might make Harfbuzz less portable.

See https://github.com/hpjansson/chafa/blob/2af7edec1adfdf1825cfb0dba46e8aaed7504918/tools/chafa/chafa.c#L726-L753 for how I did it in chafa. I can add something similar to hb-view if you think it's worth it.

Also see here for a long and somewhat opinionated discussion of sixel/cell dimensions arcana :)

@behdad behdad merged commit c571a96 into harfbuzz:master Apr 26, 2021
@behdad
Copy link
Member

behdad commented Apr 26, 2021

Humm. On my Mac terminal I'm getting bad rendering. This seems to be same as the chafa cmdline's default 24bit color which renders wrong as well:

Screen Shot 2021-04-26 at 12 30 49 PM

@hpjansson any ideas?

behdad added a commit that referenced this pull request Apr 26, 2021
@behdad
Copy link
Member

behdad commented Apr 26, 2021

I disabled the truecolor mode for now.

@behdad
Copy link
Member

behdad commented Apr 26, 2021

I disabled the truecolor mode for now.

After:

Screen Shot 2021-04-26 at 12 35 09 PM

@behdad
Copy link
Member

behdad commented Apr 26, 2021

Oh. It might be my bad $TERM:

$ echo $TERM
xterm-256color

@behdad
Copy link
Member

behdad commented Apr 26, 2021

Humm. I'm not setting it.

@hpjansson
Copy link
Contributor Author

Disabling the truecolor mode seems fine as a stopgap. Does your terminal set anything else in the environment that would make it possible for me to identify it? E.g. VTE sets COLORTERM and VTE_VERSION. The issue is that there's a whole bunch of terminals out there that support truecolor/directcolor but identify themselves as xterm-256color.

Worst case, I'll have to bite the bullet and make Chafa more conservative when identifying the terminal.

@behdad
Copy link
Member

behdad commented Apr 26, 2021

Here:

TERM=xterm-256color
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=440

@hpjansson
Copy link
Contributor Author

Great, thanks! I'll make an exception for it in the 1.6.1 release.

behdad added a commit that referenced this pull request Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[hb-view] Use libchafa if available...
4 participants