Don't calculate widths per-uft8-char when not needed (#1409)

This commit is contained in:
Guldoman 2023-03-28 01:17:11 +02:00 committed by George Sokianos
parent 350131dabc
commit 146dca9188
1 changed files with 28 additions and 11 deletions

View File

@ -175,12 +175,21 @@ function DocView:get_col_x_offset(line, col)
for _, type, text in self.doc.highlighter:each_token(line) do for _, type, text in self.doc.highlighter:each_token(line) do
local font = style.syntax_fonts[type] or default_font local font = style.syntax_fonts[type] or default_font
if font ~= default_font then font:set_tab_size(indent_size) end if font ~= default_font then font:set_tab_size(indent_size) end
for char in common.utf8_chars(text) do local length = #text
if column == col then if column + length <= col then
xoffset = xoffset + font:get_width(text)
column = column + length
if column >= col then
return xoffset return xoffset
end end
xoffset = xoffset + font:get_width(char) else
column = column + #char for char in common.utf8_chars(text) do
if column >= col then
return xoffset
end
xoffset = xoffset + font:get_width(char)
column = column + #char
end
end end
end end
@ -198,14 +207,22 @@ function DocView:get_x_offset_col(line, x)
for _, type, text in self.doc.highlighter:each_token(line) do for _, type, text in self.doc.highlighter:each_token(line) do
local font = style.syntax_fonts[type] or default_font local font = style.syntax_fonts[type] or default_font
if font ~= default_font then font:set_tab_size(indent_size) end if font ~= default_font then font:set_tab_size(indent_size) end
for char in common.utf8_chars(text) do local width = font:get_width(text)
local w = font:get_width(char) -- Don't take the shortcut if the width matches x,
if xoffset >= x then -- because we need last_i which should be calculated using utf-8.
return (xoffset - x > w / 2) and last_i or i if xoffset + width < x then
xoffset = xoffset + width
i = i + #text
else
for char in common.utf8_chars(text) do
local w = font:get_width(char)
if xoffset >= x then
return (xoffset - x > w / 2) and last_i or i
end
xoffset = xoffset + w
last_i = i
i = i + #char
end end
xoffset = xoffset + w
last_i = i
i = i + #char
end end
end end