Skip to content

Commit

Permalink
fix: Fix jagged edges with LINES render method
Browse files Browse the repository at this point in the history
- Fix: Disable payload size optimization for `LINES` render method of
  kitty and iterm2 render styles.

The optimization results in slant/curved edges not lining up across
lines (amongst other artifacts observed on Konsole) supposedly
because the terminal emulator resizes each line separately.
  • Loading branch information
AnonymouX47 committed Jun 1, 2023
1 parent 514d283 commit 4d27055
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/term_image/image/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ def __new__(
)
return super().__new__(cls)

def _get_minimal_render_size(self, *, adjust: bool) -> Tuple[int, int]:
def _get_minimal_render_size(self, *, adjust: bool = False) -> Tuple[int, int]:
render_size = self._get_render_size()
r_height = self.rendered_height
width, height = (
Expand Down
19 changes: 17 additions & 2 deletions src/term_image/image/iterm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,22 @@ def _render_image(
mix: bool = False,
compress: int = 4,
) -> str:
# NOTE: It's more efficient to write separate strings to the buffer separately
# than concatenate and write together.

# Using `width=<columns>`, `height=<lines>` and `preserveAspectRatio=0` ensures
# that an image always occupies the correct amount of columns and lines even if
# the cell size has changed when it's drawn.
# Since we use `width` and `height` control data keys, there's no need
# upscaling an image on this end; ensures minimal payload.
# upscaling the image on this end to reduce payload.
# Anyways, this also implies that the image(s) have to be resized by the
# terminal emulator, thereby leaving various details of resizing in the hands
# of the terminal emulator such as the resampling method, etc.
# This particularly affects the LINES render method negatively, resulting in
# slant/curved edges not lining up across lines (amongst other artifacts
# observed on Konsole) supposedly because the terminal emulator resizes each
# line separately.
# Hence, this optimization is only used for the WHOLE render method.

r_width, r_height = self.rendered_size
render_method = (method or self._render_method).lower()
Expand Down Expand Up @@ -632,7 +643,11 @@ def _render_image(
)
)

width, height = self._get_minimal_render_size(adjust=render_method == LINES)
width, height = (
self._get_minimal_render_size()
if render_method == WHOLE
else self._get_render_size()
)

if ( # Read directly from file when possible and reasonable
self.read_from_file
Expand Down
16 changes: 14 additions & 2 deletions src/term_image/image/kitty.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,23 @@ def _render_image(
# Using `c` and `r` ensures that an image always occupies the correct amount
# of columns and lines even if the cell size has changed when it's drawn.
# Since we use `c` and `r` control data keys, there's no need upscaling the
# image on this end; ensures minimal payload.
# image on this end to reduce payload.
# Anyways, this also implies that the image(s) have to be resized by the
# terminal emulator, thereby leaving various details of resizing in the hands
# of the terminal emulator such as the resampling method, etc.
# This particularly affects the LINES render method negatively, resulting in
# slant/curved edges not lining up across lines (amongst other artifacts
# observed on Konsole) supposedly because the terminal emulator resizes each
# line separately.
# Hence, this optimization is only used for the WHOLE render method.

render_method = (method or self._render_method).lower()
r_width, r_height = self.rendered_size
width, height = self._get_minimal_render_size(adjust=render_method == LINES)
width, height = (
self._get_minimal_render_size()
if render_method == WHOLE
else self._get_render_size()
)

frame_img = img if frame else None
img = self._get_render_data(
Expand Down

0 comments on commit 4d27055

Please sign in to comment.