Skip to content

Commit

Permalink
fix: scale aspect ratio correctly, fix tmux split (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Oct 24, 2023
1 parent 7d9151f commit 72bbf46
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
14 changes: 0 additions & 14 deletions lua/image/backends/kitty/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ backend.setup = function(state)

if state.options.kitty_method == "unicode-placeholders" then backend.features.crop = false end

vim.api.nvim_create_autocmd("VimResized", {
callback = function()
transmitted_images = {}
vim.defer_fn(function()
for _, image in pairs(backend.state.images) do
if image.is_rendered then
image.is_rendered = false
image:render()
end
end
end, 0)
end,
})

vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
backend.clear()
Expand Down
11 changes: 10 additions & 1 deletion lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ function Image:render(geometry)
-- utils.debug(("---------------- %s ----------------"):format(self.id))
local was_rendered = renderer.render(self)

-- utils.debug( ("[image] render: %s, success: %s x: %s, y: %s, width: %s, height: %s"):format( self.id, was_rendered, self.geometry.x, self.geometry.y, self.geometry.width, self.geometry.height))
-- utils.debug(
-- ("[image] render: %s, success: %s x: %s, y: %s, width: %s, height: %s"):format(
-- self.id,
-- was_rendered,
-- self.geometry.x,
-- self.geometry.y,
-- self.geometry.width,
-- self.geometry.height
-- )
-- )

-- clear if already rendered but rendering this should be prevented
if self.is_rendered and not was_rendered then
Expand Down
14 changes: 11 additions & 3 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ local render = function(image)
local image_rows = math.floor(image.image_height / term_size.cell_height)
local image_columns = math.floor(image.image_width / term_size.cell_width)

-- utils.debug(("renderer.render() %s %d"):format(image.id, image.internal_id))
-- utils.debug(("renderer.render() %s"):format(image.id), {
-- id = image.id,
-- x = image.geometry.x,
-- y = image.geometry.y,
-- width = image.geometry.width,
-- height = image.geometry.height,
-- })

local original_x = image.geometry.x or 0
local original_y = image.geometry.y or 0
Expand Down Expand Up @@ -115,10 +121,11 @@ local render = function(image)

-- global max window width/height percentage
if type(state.options.max_width_window_percentage) == "number" then
width = math.min(width, math.floor(window.width * state.options.max_width_window_percentage / 100))
width = math.min(width, math.floor((window.width - x_offset) * state.options.max_width_window_percentage / 100))
end
if type(state.options.max_height_window_percentage) == "number" then
height = math.min(height, math.floor(window.height * state.options.max_height_window_percentage / 100))
height =
math.min(height, math.floor((window.height - y_offset) * state.options.max_height_window_percentage / 100))
end
end

Expand Down Expand Up @@ -254,6 +261,7 @@ local render = function(image)

-- compute final geometry and prevent useless rerendering
local rendered_geometry = { x = absolute_x, y = absolute_y, width = width, height = height }
-- utils.debug("rendered_geometry", rendered_geometry)

-- handle crop/resize
local pixel_width = width * term_size.cell_width
Expand Down
4 changes: 2 additions & 2 deletions lua/image/utils/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ local adjust_to_aspect_ratio = function(term_size, image_width, image_height, wi
local percent_orig_height = pixel_height / image_height

if width == 0 and height ~= 0 then
width = math.floor(height * aspect_ratio)
width = math.floor(pixel_height / term_size.cell_width * aspect_ratio)
return width, height
end

if height == 0 and width ~= 0 then
height = math.floor(width / aspect_ratio)
height = math.floor(pixel_width / term_size.cell_height / aspect_ratio)
return width, height
end

Expand Down

0 comments on commit 72bbf46

Please sign in to comment.