Skip to content

Question: How to use z-index for drawing text with background color above an image? #6061

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

Closed
markus-bauer opened this issue Feb 25, 2023 · 3 comments
Labels

Comments

@markus-bauer
Copy link

The documentation states:
"Negative z-index values below INT32_MIN/2 (-1,073,741,824) will be drawn under cells with non-default background colors."
(https://sw.kovidgoyal.net/kitty/graphics-protocol/#controlling-displayed-image-layout)
Here's is an example code that attempts to do this.

import sys
from base64 import standard_b64encode

# --- this is copied from https://sw.kovidgoyal.net/kitty/graphics-protocol/#a-minimal-example
def serialize_gr_command(**cmd):
    payload = cmd.pop('payload', None)
    cmd = ','.join(f'{k}={v}' for k, v in cmd.items())
    ans = []
    w = ans.append
    w(b'\033_G'), w(cmd.encode('ascii'))
    if payload:
        w(b';')
        w(payload)
    w(b'\033\\')
    return b''.join(ans)

def write_chunked(**cmd):
    data = standard_b64encode(cmd.pop('data'))
    while data:
        chunk, data = data[:4096], data[4096:]
        m = 1 if data else 0
        sys.stdout.buffer.write(serialize_gr_command(payload=chunk, m=m,
                                                    **cmd))
        sys.stdout.flush()
        cmd.clear()
# -----------------------------------------------------------------------------

def move_cursor(x,y):
    cmd = "\x1b[{};{}H".format(y,x)
    sys.stdout.write(cmd)
    sys.stdout.flush()

def set_bg_color():
    cmd = "\x1b[41m"
    sys.stdout.write(cmd)
    sys.stdout.flush()


def run_example(image_path):
    x = 0
    y = 0
    offset = 5

    # draw some text with red background
    for i in range(10):
        move_cursor(x, y+i)
        set_bg_color()
        print("this is some text on top of the image")

    # draw image at an offset
    move_cursor(x + offset, y + offset)

    # Negative z-index values below INT32_MIN/2 (-1,073,741,824) will be drawn under cells with non-default background colors.
    INT32_MIN_2 = -1_073_741_824
    z = INT32_MIN_2 - 100

    with open(image_path, 'rb') as f:
        write_chunked(a='T', f=100, data=f.read(), z=z)

# path to a png image:
image_path = "/path/to/image.png"

run_example(image_path)

Here's the result:
screenshot

As you see, the image is below the text, but not below the background color.
How do I use z-index correctly to draw cells with background color over the image?

@markus-bauer
Copy link
Author

So it was a bug. I thought I did something wrong. How is it that no one found this before?
Anyway, thanks for the fix. But I did not test your patch yet, because I don't want to build this from source.
I'm going to wait for the next release.

@page-down
Copy link
Contributor

@markus-bauer

... I don't want to build this from source.

You can download the nightly release to try it out.
https://github.com/kovidgoyal/kitty/releases/tag/nightly
https://sw.kovidgoyal.net/kitty/binary/#customizing-the-installation

@markus-bauer
Copy link
Author

@page-down Thanks for the info. That patch wasn't in nightly when I wrote it.
But now I did install the newest nightly and can confirm that this issue is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants