Merge lines.love

This commit is contained in:
Kartik K. Agaram 2023-04-02 09:22:32 -07:00
commit 3547c18db6
18 changed files with 117 additions and 251 deletions

16
app.lua
View File

@ -137,7 +137,7 @@ function App.run_tests_and_initialize()
Test_errors = {}
App.run_tests()
if #Test_errors > 0 then
error('There were test failures:\n\n'..table.concat(Test_errors))
error(('There were %d test failures:\n\n%s'):format(#Test_errors, table.concat(Test_errors)))
end
App.disable_tests()
App.initialize_globals()
@ -210,16 +210,8 @@ function App.wait_fake_time(t)
App.time = App.time + t
end
-- LÖVE's Text primitive retains no trace of the string it was created from,
-- so we'll wrap it for our tests.
--
-- This implies that we need to hook any operations we need on Text objects.
function App.newText(font, s)
return {type='text', data=s, text=love.graphics.newText(font, s)}
end
function App.width(text)
return text.text:getWidth()
return love.graphics.getFont():getWidth(text)
end
function App.screen.draw(obj, x,y)
@ -381,7 +373,7 @@ end
-- prepend file/line/test
function prepend_debug_info_to_test_failure(test_name, err)
local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
local stack_trace = debug.traceback('', --[[stack frame]]4)
local stack_trace = debug.traceback('', --[[stack frame]]5)
local file_and_line_number = stack_trace:gsub('stack traceback:\n', ''):gsub(': .*', '')
local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number
--? local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number..'\t\t'..stack_trace:gsub('\n', '\n\t\t')
@ -423,9 +415,7 @@ function App.disable_tests()
App.screen.move = love.window.setPosition
App.screen.position = love.window.getPosition
App.screen.print = love.graphics.print
App.newText = love.graphics.newText
App.screen.draw = love.graphics.draw
App.width = function(text) return text:getWidth() end
if Current_app == nil or Current_app == 'run' then
App.open_for_reading = function(filename) return io.open(filename, 'r') end
App.open_for_writing = function(filename) return io.open(filename, 'w') end

View File

@ -43,21 +43,19 @@ function source.draw_menu_bar()
end
function add_hotkey_to_menu(s)
local s_text = to_text(s)
local width = App.width(s_text)
local width = App.width(s)
if Menu_cursor > App.screen.width - 30 then
return
end
App.color(Menu_command_color)
App.screen.draw(s_text, Menu_cursor,5)
App.screen.print(s, Menu_cursor,5)
Menu_cursor = Menu_cursor + width + 30
end
function source.draw_file_navigator()
App.color(Menu_command_color)
local filter_text = to_text(File_navigation.filter)
App.screen.draw(filter_text, 5, 5)
draw_cursor(5 + App.width(filter_text), 5)
App.screen.print(File_navigation.filter, 5, 5)
draw_cursor(5 + App.width(File_navigation.filter), 5)
if File_navigation.num_lines == nil then
File_navigation.num_lines = source.num_lines_for_file_navigator(File_navigation.candidates)
end
@ -97,7 +95,7 @@ function source.num_lines_for_file_navigator(candidates)
local result = 1
local x = 5
for i,filename in ipairs(candidates) do
local width = App.width(to_text(filename))
local width = App.width(filename)
if x + width > App.screen.width - 5 then
result = result+1
x = 5 + width
@ -109,8 +107,7 @@ function source.num_lines_for_file_navigator(candidates)
end
function add_file_to_menu(x,y, s, cursor_highlight)
local s_text = to_text(s)
local width = App.width(s_text)
local width = App.width(s)
if x + width > App.screen.width - 5 then
y = y + Editor_state.line_height
x = 5
@ -125,7 +122,7 @@ function add_file_to_menu(x,y, s, cursor_highlight)
end
})
App.color(Menu_command_color)
App.screen.draw(s_text, x,y)
App.screen.print(s, x,y)
x = x + width + 30
return x,y
end
@ -189,8 +186,7 @@ function log_render.file_navigator_state(o, x,y, w)
local x2,y2 = 0,0
local width = 0
for i,filename in ipairs(o.files) do
local filename_text = to_text(filename)
width = App.width(filename_text)
width = App.width(filename)
if x2 + width > App.screen.width - 5 then
y2 = y2 + Editor_state.line_height
x2 = 0
@ -207,8 +203,7 @@ function log_render.file_navigator_state(o, x,y, w)
local x3,y3 = 0,y -- x3 is relative, y3 is absolute
local width = 0
for i,filename in ipairs(o.files) do
local filename_text = to_text(filename)
width = App.width(filename_text)
width = App.width(filename)
if x3 + width > App.screen.width - 5 then
y3 = y3 + Editor_state.line_height
x3 = 0
@ -219,7 +214,7 @@ function log_render.file_navigator_state(o, x,y, w)
end
if x3 >= menu_xmin and x3 + width < menu_xmax then
App.color(Menu_command_color)
App.screen.draw(filename_text, x + x3-menu_xmin, y3)
App.screen.print(filename, x + x3-menu_xmin, y3)
end
x3 = x3 + width + 30
end
@ -246,7 +241,7 @@ end
function file_coord(index)
local y,x = Menu_status_bar_height, 5
for i,filename in ipairs(File_navigation.candidates) do
local width = App.width(to_text(filename))
local width = App.width(filename)
if x + width > App.screen.width - 5 then
y = y + Editor_state.line_height
x = 5
@ -264,7 +259,7 @@ function file_index(fy, fx, fwidth)
local y,x = Menu_status_bar_height, 5
local best_guess, best_guess_x, best_guess_width
for i,filename in ipairs(File_navigation.candidates) do
local width = App.width(to_text(filename))
local width = App.width(filename)
if x + width > App.screen.width - 5 then
y = y + Editor_state.line_height
x = 5

View File

@ -60,14 +60,13 @@ function Drawing.draw(State, line_index, y)
if State.current_drawing_mode == 'name' and i == line.pending.target_point then
-- create a faint red box for the name
App.color(Current_name_background_color)
local name_text
-- TODO: avoid computing name width on every repaint
local name_width
if p.name == '' then
name_text = State.em
name_width = App.width('m')
else
name_text = App.newText(love.graphics.getFont(), p.name)
name_width = App.width(p.name)
end
love.graphics.rectangle('fill', x,y, App.width(name_text), State.line_height)
love.graphics.rectangle('fill', x,y, name_width, State.line_height)
end
end
end

View File

@ -21,7 +21,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
-- rendering wrapped text lines needs some additional short-lived data per line:
-- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
-- starty, the y coord in pixels the line starts rendering from
-- fragments: snippets of rendered love.graphics.Text, guaranteed to not straddle screen lines
-- fragments: snippets of the line guaranteed to not straddle screen lines
-- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
line_cache = {},
@ -52,7 +52,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
font_height = font_height,
line_height = line_height,
em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width
top = top,
left = math.floor(left),
@ -68,7 +67,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
-- search
search_term = nil,
search_text = nil,
search_backup = nil, -- stuff to restore when cancelling search
}
return result
@ -220,7 +218,6 @@ function edit.text_input(State, t)
--? print('text input', t)
if State.search_term then
State.search_term = State.search_term..t
State.search_text = nil
Text.search_next(State)
else
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
@ -241,20 +238,17 @@ function edit.keychord_press(State, chord, key)
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
if chord == 'escape' then
State.search_term = nil
State.search_text = nil
State.cursor1 = State.search_backup.cursor
State.screen_top1 = State.search_backup.screen_top
State.search_backup = nil
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
elseif chord == 'return' then
State.search_term = nil
State.search_text = nil
State.search_backup = nil
elseif chord == 'backspace' then
local len = utf8.len(State.search_term)
local byte_offset = Text.offset(State.search_term, len)
State.search_term = string.sub(State.search_term, 1, byte_offset-1)
State.search_text = nil
elseif chord == 'down' then
State.cursor1.pos = State.cursor1.pos+1
Text.search_next(State)
@ -268,7 +262,6 @@ function edit.keychord_press(State, chord, key)
cursor={line=State.cursor1.line, pos=State.cursor1.pos},
screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
}
assert(State.search_text == nil)
-- zoom
elseif chord == 'C-=' then
edit.update_font_settings(State, State.font_height+2)
@ -359,7 +352,6 @@ function edit.update_font_settings(State, font_height)
State.font_height = font_height
love.graphics.setFont(love.graphics.newFont(State.font_height))
State.line_height = math.floor(font_height*1.3)
State.em = App.newText(love.graphics.getFont(), 'm')
Text_cache = {}
end

View File

@ -147,5 +147,5 @@ function current_shape(State, shape)
end
function bullet_indent()
return App.width(to_text('* '))
return App.width('* ')
end

View File

@ -98,21 +98,20 @@ function log_browser.draw(State)
local xright = render_stack_right_margin(State, line_index, line, y)
if line.section_name then
App.color(Section_border_color)
local section_text = to_text(line.section_name)
if line.section_begin then
local sectiony = y+Section_border_padding_vertical
love.graphics.line(xleft,sectiony, xleft,y+State.line_height)
love.graphics.line(xright,sectiony, xright,y+State.line_height)
love.graphics.line(xleft,sectiony, xleft+50-2,sectiony)
love.graphics.draw(section_text, xleft+50,y)
love.graphics.line(xleft+50+App.width(section_text)+2,sectiony, xright,sectiony)
love.graphics.print(line.section_name, xleft+50,y)
love.graphics.line(xleft+50+App.width(line.section_name)+2,sectiony, xright,sectiony)
else assert(line.section_end)
local sectiony = y+State.line_height-Section_border_padding_vertical
love.graphics.line(xleft,y, xleft,sectiony)
love.graphics.line(xright,y, xright,sectiony)
love.graphics.line(xleft,sectiony, xleft+50-2,sectiony)
love.graphics.draw(section_text, xleft+50,y)
love.graphics.line(xleft+50+App.width(section_text)+2,sectiony, xright,sectiony)
love.graphics.print(line.section_name, xleft+50,y)
love.graphics.line(xleft+50+App.width(line.section_name)+2,sectiony, xright,sectiony)
end
else
if type(line.data) == 'string' then
@ -148,7 +147,7 @@ function render_stack_left_margin(State, line_index, line, y)
love.graphics.print(line.section_stack[i].name, x+State.font_height+5, y+5, --[[vertically]] math.pi/2)
end
if y > App.screen.height-log_browser.height(State, line_index) then
love.graphics.print(line.section_stack[i].name, x+State.font_height+5, App.screen.height-App.width(to_text(line.section_stack[i].name))-5, --[[vertically]] math.pi/2)
love.graphics.print(line.section_stack[i].name, x+State.font_height+5, App.screen.height-App.width(line.section_stack[i].name)-5, --[[vertically]] math.pi/2)
end
end
return log_browser.left_margin(State, line)
@ -163,7 +162,7 @@ function render_stack_right_margin(State, line_index, line, y)
love.graphics.print(line.section_stack[i].name, x, y+5, --[[vertically]] math.pi/2)
end
if y > App.screen.height-log_browser.height(State, line_index) then
love.graphics.print(line.section_stack[i].name, x, App.screen.height-App.width(to_text(line.section_stack[i].name))-5, --[[vertically]] math.pi/2)
love.graphics.print(line.section_stack[i].name, x, App.screen.height-App.width(line.section_stack[i].name)-5, --[[vertically]] math.pi/2)
end
end
return log_browser.right_margin(State, line)

12
run.lua
View File

@ -71,12 +71,10 @@ end
function run.initialize_default_settings()
local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height))
local em = App.newText(love.graphics.getFont(), 'm')
run.initialize_window_geometry(App.width(em))
run.initialize_window_geometry(App.width('m'))
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
Editor_state.em = em
Settings = run.settings()
end
@ -197,10 +195,6 @@ function run.key_release(key, scancode)
return edit.key_release(Editor_state, key, scancode)
end
-- use this sparingly
function to_text(s)
if Text_cache[s] == nil then
Text_cache[s] = App.newText(love.graphics.getFont(), s)
end
return Text_cache[s]
function width(s)
return love.graphics.getFont():getWidth(s)
end

View File

@ -13,10 +13,7 @@ function Text.draw_search_bar(State)
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
App.color(Text_color)
App.screen.print(State.search_term, 25,y-5)
if State.search_text == nil then
State.search_text = App.newText(love.graphics.getFont(), State.search_term)
end
Text.draw_cursor(State, 25+App.width(State.search_text),y-5)
Text.draw_cursor(State, 25+App.width(State.search_term),y-5)
end
function Text.search_next(State)

View File

@ -58,15 +58,12 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
lo_px = 0
else
local before = line.data:sub(pos_offset, lo_offset-1)
local before_text = App.newText(love.graphics.getFont(), before)
lo_px = App.width(before_text)
lo_px = App.width(before)
end
--? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
local s = line.data:sub(lo_offset, hi_offset-1)
local text = App.newText(love.graphics.getFont(), s)
local text_width = App.width(text)
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height)
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
App.color(Text_color)
return lo_px
end

View File

@ -162,13 +162,11 @@ end
function source.initialize_default_settings()
local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height))
local em = App.newText(love.graphics.getFont(), 'm')
source.initialize_window_geometry(App.width(em))
source.initialize_window_geometry(App.width('m'))
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
Editor_state.filename = 'run.lua'
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
Editor_state.em = em
end
function source.initialize_window_geometry(em_width)
@ -418,11 +416,3 @@ function source.key_release(key, scancode)
return log_browser.keychord_press(Log_browser_state, chordkey, scancode)
end
end
-- use this sparingly
function to_text(s)
if Text_cache[s] == nil then
Text_cache[s] = App.newText(love.graphics.getFont(), s)
end
return Text_cache[s]
end

View File

@ -54,7 +54,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
-- rendering wrapped text lines needs some additional short-lived data per line:
-- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
-- starty, the y coord in pixels the line starts rendering from
-- fragments: snippets of rendered love.graphics.Text, guaranteed to not straddle screen lines
-- fragments: snippets of the line guaranteed to not straddle screen lines
-- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
line_cache = {},
@ -88,7 +88,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
font_height = font_height,
line_height = line_height,
em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width
top = top,
left = math.floor(left),
@ -104,7 +103,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
-- search
search_term = nil,
search_text = nil,
search_backup = nil, -- stuff to restore when cancelling search
}
return result
@ -325,7 +323,6 @@ function edit.text_input(State, t)
--? print('text input', t)
if State.search_term then
State.search_term = State.search_term..t
State.search_text = nil
Text.search_next(State)
elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
local before = snapshot(State, State.lines.current_drawing_index)
@ -356,20 +353,17 @@ function edit.keychord_press(State, chord, key)
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
if chord == 'escape' then
State.search_term = nil
State.search_text = nil
State.cursor1 = State.search_backup.cursor
State.screen_top1 = State.search_backup.screen_top
State.search_backup = nil
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
elseif chord == 'return' then
State.search_term = nil
State.search_text = nil
State.search_backup = nil
elseif chord == 'backspace' then
local len = utf8.len(State.search_term)
local byte_offset = Text.offset(State.search_term, len)
State.search_term = string.sub(State.search_term, 1, byte_offset-1)
State.search_text = nil
elseif chord == 'down' then
State.cursor1.pos = State.cursor1.pos+1
Text.search_next(State)
@ -383,7 +377,6 @@ function edit.keychord_press(State, chord, key)
cursor={line=State.cursor1.line, pos=State.cursor1.pos},
screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
}
assert(State.search_text == nil)
-- zoom
elseif chord == 'C-=' then
edit.update_font_settings(State, State.font_height+2)
@ -515,7 +508,6 @@ function edit.update_font_settings(State, font_height)
State.font_height = font_height
love.graphics.setFont(love.graphics.newFont(State.font_height))
State.line_height = math.floor(font_height*1.3)
State.em = App.newText(love.graphics.getFont(), 'm')
Text_cache = {}
end

View File

@ -1,7 +1,5 @@
-- primitives for saving to file and loading from file
Fold = '\x1e' -- ASCII RS (record separator)
function file_exists(filename)
local infile = App.open_for_reading(filename)
if infile then
@ -28,13 +26,7 @@ function load_from_file(infile)
if line == '```lines' then -- inflexible with whitespace since these files are always autogenerated
table.insert(result, load_drawing(infile_next_line))
else
local line_info = {mode='text'}
if line:find(Fold) then
_, _, line_info.data, line_info.dataB = line:find('([^'..Fold..']*)'..Fold..'([^'..Fold..']*)')
else
line_info.data = line
end
table.insert(result, line_info)
table.insert(result, {mode='text', data=line})
end
end
end
@ -54,10 +46,6 @@ function save_to_disk(State)
store_drawing(outfile, line)
else
outfile:write(line.data)
if line.dataB and #line.dataB > 0 then
outfile:write(Fold)
outfile:write(line.dataB)
end
outfile:write('\n')
end
end
@ -147,11 +135,7 @@ function load_array(a)
else
--? print('inserting text')
local line_info = {mode='text'}
if line:find(Fold) then
_, _, line_info.data, line_info.dataB = line:find('([^'..Fold..']*)'..Fold..'([^'..Fold..']*)')
else
line_info.data = line
end
line_info.data = line
table.insert(result, line_info)
end
end

View File

@ -60,15 +60,12 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
lo_px = 0
else
local before = line.data:sub(pos_offset, lo_offset-1)
local before_text = App.newText(love.graphics.getFont(), before)
lo_px = App.width(before_text)
lo_px = App.width(before)
end
--? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
local s = line.data:sub(lo_offset, hi_offset-1)
local text = App.newText(love.graphics.getFont(), s)
local text_width = App.width(text)
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height)
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
App.color(Text_color)
return lo_px
end

View File

@ -12,21 +12,20 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
local x = State.left
local pos = 1
local screen_line_starting_pos = startpos
Text.compute_fragments(State, line_index)
Text.populate_screen_line_starting_pos(State, line_index)
local pos = 1
initialize_color()
for _, f in ipairs(line_cache.fragments) do
App.color(Text_color)
local frag, frag_text = f.data, f.text
select_color(frag)
local frag_len = utf8.len(frag)
--? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y)
select_color(f)
local frag_len = utf8.len(f)
--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y)
if pos < startpos then
-- render nothing
--? print('skipping', frag)
--? print('skipping', f)
else
-- render fragment
local frag_width = App.width(frag_text)
local frag_width = App.width(f)
if x + frag_width > State.right then
assert(x > State.left) -- no overfull lines
y = y + State.line_height
@ -41,12 +40,11 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
Text.draw_highlight(State, line, x,y, pos, lo,hi)
end
-- Make [[WikiWords]] (single word, all in one screen line) clickable.
local trimmed_word = rtrim(frag) -- compute_fragments puts whitespace at the end
local trimmed_word = rtrim(f) -- compute_fragments puts whitespace at the end
if starts_with(trimmed_word, '[[') and ends_with(trimmed_word, ']]') then
local filename = trimmed_word:gsub('^..(.*)..$', '%1')
if source.link_exists(State, filename) then
local filename_text = App.newText(love.graphics.getFont(), filename)
button(State, 'link', {x=x+App.width(to_text('[[')), y=y, w=App.width(filename_text), h=State.line_height, color={1,1,1},
button(State, 'link', {x=x+App.width('[['), y=y, w=App.width(filename), h=State.line_height, color={1,1,1},
icon = icon.hyperlink_decoration,
onpress1 = function()
source.switch_to_file(filename)
@ -54,7 +52,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
})
end
end
App.screen.draw(frag_text, x,y)
App.screen.print(f, x,y)
-- render cursor if necessary
if line_index == State.cursor1.line then
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
@ -65,7 +63,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
love.graphics.print(State.search_term, x+lo_px,y)
end
elseif Focus == 'edit' then
Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y)
Text.draw_cursor(State, x+Text.x(f, State.cursor1.pos-pos+1), y)
App.color(Text_color)
end
end
@ -105,21 +103,18 @@ function Text.populate_screen_line_starting_pos(State, line_index)
local x = State.left
local pos = 1
for _, f in ipairs(line_cache.fragments) do
local frag, frag_text = f.data, f.text
-- render fragment
local frag_width = App.width(frag_text)
local frag_width = App.width(f)
if x + frag_width > State.right then
x = State.left
table.insert(line_cache.screen_line_starting_pos, pos)
end
x = x + frag_width
local frag_len = utf8.len(frag)
pos = pos + frag_len
pos = pos + utf8.len(f)
end
end
function Text.compute_fragments(State, line_index)
--? print('compute_fragments', line_index, 'between', State.left, State.right)
local line = State.lines[line_index]
if line.mode ~= 'text' then return end
local line_cache = State.line_cache[line_index]
@ -130,35 +125,25 @@ function Text.compute_fragments(State, line_index)
local x = State.left
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_text = App.newText(love.graphics.getFont(), frag)
local frag_width = App.width(frag_text)
--? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go')
local frag_width = App.width(frag)
while x + frag_width > State.right do
--? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x))
if (x-State.left) < 0.8 * (State.right-State.left) then
--? print('splitting')
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.right - x)
--? print('bpos', bpos)
if bpos == 0 then break end -- avoid infinite loop when window is too narrow
local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
--? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes')
local frag1 = string.sub(frag, 1, boffset-1)
local frag1_text = App.newText(love.graphics.getFont(), frag1)
local frag1_width = App.width(frag1_text)
--? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px')
local frag1_width = App.width(frag1)
assert(x + frag1_width <= State.right)
table.insert(line_cache.fragments, {data=frag1, text=frag1_text})
table.insert(line_cache.fragments, frag1)
frag = string.sub(frag, boffset)
frag_text = App.newText(love.graphics.getFont(), frag)
frag_width = App.width(frag_text)
frag_width = App.width(frag)
end
x = State.left -- new line
end
if #frag > 0 then
--? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px')
table.insert(line_cache.fragments, {data=frag, text=frag_text})
table.insert(line_cache.fragments, frag)
end
x = x + frag_width
end
@ -776,8 +761,7 @@ function Text.screen_line_width(State, line_index, i)
else
screen_line = string.sub(line.data, start_pos)
end
local screen_line_text = App.newText(love.graphics.getFont(), screen_line)
return App.width(screen_line_text)
return App.width(screen_line)
end
function Text.screen_line_index(screen_line_starting_pos, pos)
@ -865,15 +849,13 @@ function Text.x_after(s, pos)
local offset = Text.offset(s, math.min(pos+1, #s+1))
local s_before = s:sub(1, offset-1)
--? print('^'..s_before..'$')
local text_before = App.newText(love.graphics.getFont(), s_before)
return App.width(text_before)
return App.width(s_before)
end
function Text.x(s, pos)
local offset = Text.offset(s, pos)
local s_before = s:sub(1, offset-1)
local text_before = App.newText(love.graphics.getFont(), s_before)
return App.width(text_before)
return App.width(s_before)
end
function Text.to2(State, loc1)

View File

@ -61,14 +61,9 @@ function snapshot(State, s,e)
for i=s,e do
local line = State.lines[i]
if line.mode == 'text' then
table.insert(event.lines, {mode='text', data=line.data, dataB=line.dataB})
table.insert(event.lines, {mode='text', data=line.data}) -- I've forgotten: should we deepcopy(line.data)?
elseif line.mode == 'drawing' then
local points=deepcopy(line.points)
--? print('copying', line.points, 'with', #line.points, 'points into', points)
local shapes=deepcopy(line.shapes)
--? print('copying', line.shapes, 'with', #line.shapes, 'shapes into', shapes)
table.insert(event.lines, {mode='drawing', h=line.h, points=points, shapes=shapes, pending={}})
--? table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}})
table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}})
else
print(line.mode)
assert(false)

145
text.lua
View File

@ -4,64 +4,64 @@ Text = {}
-- draw a line starting from startpos to screen at y between State.left and State.right
-- return the final y, and position of start of final screen line drawn
function Text.draw(State, line_index, y, startpos)
--? print('text.draw', line_index, y)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
line_cache.starty = y
line_cache.startpos = startpos
-- wrap long lines
local x = State.left
local pos = 1
local screen_line_starting_pos = startpos
Text.compute_fragments(State, line_index)
for _, f in ipairs(line_cache.fragments) do
App.color(Text_color)
local frag, frag_text = f.data, f.text
local frag_len = utf8.len(frag)
--? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y)
local final_screen_line_starting_pos = startpos -- track value to return
Text.populate_screen_line_starting_pos(State, line_index)
assert(#line_cache.screen_line_starting_pos >= 1)
for i=1,#line_cache.screen_line_starting_pos do
local pos = line_cache.screen_line_starting_pos[i]
if pos < startpos then
-- render nothing
--? print('skipping', frag)
--? print('skipping', f)
else
final_screen_line_starting_pos = pos
local f = Text.screen_line(line, line_cache, i)
--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y)
local frag_len = utf8.len(f)
-- render fragment
local frag_width = App.width(frag_text)
if x + frag_width > State.right then
assert(x > State.left) -- no overfull lines
y = y + State.line_height
if y + State.line_height > App.screen.height then
return y, screen_line_starting_pos
end
screen_line_starting_pos = pos
x = State.left
end
if State.selection1.line then
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
Text.draw_highlight(State, line, x,y, pos, lo,hi)
Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
end
App.screen.draw(frag_text, x,y)
App.color(Text_color)
App.screen.print(f, State.left,y)
-- render cursor if necessary
if line_index == State.cursor1.line then
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
if State.search_term then
if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then
local lo_px = Text.draw_highlight(State, line, x,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
App.color(Text_color)
love.graphics.print(State.search_term, x+lo_px,y)
love.graphics.print(State.search_term, State.left+lo_px,y)
end
else
Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y)
Text.draw_cursor(State, State.left+Text.x(f, State.cursor1.pos-pos+1), y)
end
end
end
x = x + frag_width
end
pos = pos + frag_len
end
if State.search_term == nil then
if line_index == State.cursor1.line and State.cursor1.pos == pos then
Text.draw_cursor(State, x, y)
y = y + State.line_height
if y >= App.screen.height then
break
end
end
end
return y, screen_line_starting_pos
return y - State.line_height, final_screen_line_starting_pos
end
function Text.screen_line(line, line_cache, i)
local pos = line_cache.screen_line_starting_pos[i]
local offset = Text.offset(line.data, pos)
if i >= #line_cache.screen_line_starting_pos then
return line.data:sub(offset)
end
local endpos = line_cache.screen_line_starting_pos[i+1]-1
local end_offset = Text.offset(line.data, endpos)
return line.data:sub(offset, end_offset)
end
function Text.draw_cursor(State, x, y)
@ -81,67 +81,34 @@ function Text.populate_screen_line_starting_pos(State, line_index)
if line_cache.screen_line_starting_pos then
return
end
-- duplicate some logic from Text.draw
Text.compute_fragments(State, line_index)
line_cache.screen_line_starting_pos = {1}
local x = State.left
local x = 0
local pos = 1
for _, f in ipairs(line_cache.fragments) do
local frag, frag_text = f.data, f.text
-- render fragment
local frag_width = App.width(frag_text)
if x + frag_width > State.right then
x = State.left
table.insert(line_cache.screen_line_starting_pos, pos)
end
x = x + frag_width
local frag_len = utf8.len(frag)
pos = pos + frag_len
end
end
function Text.compute_fragments(State, line_index)
--? print('compute_fragments', line_index, 'between', State.left, State.right)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
if line_cache.fragments then
return
end
line_cache.fragments = {}
local x = State.left
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_text = App.newText(love.graphics.getFont(), frag)
local frag_width = App.width(frag_text)
--? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go')
while x + frag_width > State.right do
--? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x))
if (x-State.left) < 0.8 * (State.right-State.left) then
--? print('splitting')
local frag_width = App.width(frag)
--? print('-- frag:', frag, pos, x, frag_width, State.width)
while x + frag_width > State.width do
--? print('frag:', frag, pos, x, frag_width, State.width)
if x < 0.8 * State.width then
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.right - x)
--? print('bpos', bpos)
if bpos == 0 then break end -- avoid infinite loop when window is too narrow
local bpos = Text.nearest_pos_less_than(frag, State.width - x)
-- everything works if bpos == 0, but is a little inefficient
pos = pos + bpos
local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
--? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes')
local frag1 = string.sub(frag, 1, boffset-1)
local frag1_text = App.newText(love.graphics.getFont(), frag1)
local frag1_width = App.width(frag1_text)
--? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px')
assert(x + frag1_width <= State.right)
table.insert(line_cache.fragments, {data=frag1, text=frag1_text})
frag = string.sub(frag, boffset)
frag_text = App.newText(love.graphics.getFont(), frag)
frag_width = App.width(frag_text)
--? if bpos > 0 then
--? print('after chop:', frag)
--? end
frag_width = App.width(frag)
end
x = State.left -- new line
end
if #frag > 0 then
--? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px')
table.insert(line_cache.fragments, {data=frag, text=frag_text})
--? print('screen line:', pos)
table.insert(line_cache.screen_line_starting_pos, pos)
x = 0 -- new screen line
end
x = x + frag_width
pos = pos + utf8.len(frag)
end
end
@ -687,8 +654,7 @@ function Text.screen_line_width(State, line_index, i)
else
screen_line = string.sub(line.data, start_pos)
end
local screen_line_text = App.newText(love.graphics.getFont(), screen_line)
return App.width(screen_line_text)
return App.width(screen_line)
end
function Text.screen_line_index(screen_line_starting_pos, pos)
@ -776,15 +742,13 @@ function Text.x_after(s, pos)
local offset = Text.offset(s, math.min(pos+1, #s+1))
local s_before = s:sub(1, offset-1)
--? print('^'..s_before..'$')
local text_before = App.newText(love.graphics.getFont(), s_before)
return App.width(text_before)
return App.width(s_before)
end
function Text.x(s, pos)
local offset = Text.offset(s, pos)
local s_before = s:sub(1, offset-1)
local text_before = App.newText(love.graphics.getFont(), s_before)
return App.width(text_before)
return App.width(s_before)
end
function Text.to2(State, loc1)
@ -914,7 +878,6 @@ function Text.redraw_all(State)
end
function Text.clear_screen_line_cache(State, line_index)
State.line_cache[line_index].fragments = nil
State.line_cache[line_index].screen_line_starting_pos = nil
end

View File

@ -952,7 +952,7 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line()
y = y + Editor_state.line_height
App.screen.check(y, 'jkl ', 'screen:2')
y = y + Editor_state.line_height
App.screen.check(y, 'mno ', 'screen:3')
App.screen.check(y, 'mn', 'screen:3')
end
function test_pagedown_never_moves_up()

View File

@ -58,7 +58,7 @@ function snapshot(State, s,e)
-- deep copy lines without cached stuff like text fragments
for i=s,e do
local line = State.lines[i]
table.insert(event.lines, {data=line.data})
table.insert(event.lines, {data=line.data}) -- I've forgotten: should we deepcopy(line.data)?
end
return event
end