From c30349d54e67c1ee7e1ea759e8378fcf6a6c9ff4 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Sun, 21 Mar 2021 20:12:59 +0200 Subject: [PATCH] [hb-view] Support kitty inline images protocol https://github.com/harfbuzz/harfbuzz/issues/2758 --- util/helper-cairo.cc | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/util/helper-cairo.cc b/util/helper-cairo.cc index ec96e25a3..73c1c21b0 100644 --- a/util/helper-cairo.cc +++ b/util/helper-cairo.cc @@ -178,6 +178,7 @@ helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font) enum class image_protocol_t { NONE = 0, ITERM2, + KITTY, }; struct finalize_closure_t { @@ -298,6 +299,35 @@ finalize_png (finalize_closure_t *closure) g_string_printf (string, "\033]1337;File=inline=1;size=%zu:%s\a\n", base64_len, base64); } + else if (closure->protocol == image_protocol_t::KITTY) + { +#define CHUNK_SIZE 4096 + /* https://sw.kovidgoyal.net/kitty/graphics-protocol.html */ + for (size_t pos = 0; pos < base64_len; pos += CHUNK_SIZE) + { + size_t len = base64_len - pos; + + if (pos == 0) + g_string_append (string, "\033_Ga=T,f=100,m="); + else + g_string_append (string, "\033_Gm="); + + if (len > CHUNK_SIZE) + { + g_string_append (string, "1;"); + g_string_append_len (string, base64 + pos, CHUNK_SIZE); + } + else + { + g_string_append (string, "0;"); + g_string_append_len (string, base64 + pos, len); + } + + g_string_append (string, "\033\\"); + } + g_string_append (string, "\n"); +#undef CHUNK_SIZE + } closure->write_func (closure->closure, (unsigned char *) string->str, string->len); @@ -424,6 +454,12 @@ helper_cairo_create_context (double w, double h, extension = "png"; protocol = image_protocol_t::ITERM2; } + else if ((name = getenv ("TERM")) != nullptr && + 0 == g_ascii_strcasecmp (name, "xterm-kitty")) + { + extension = "png"; + protocol = image_protocol_t::KITTY; + } else extension = "ansi"; #else