From 0f4aea6db747498467ab5b43a762ef3d5f39f56b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 29 Dec 2023 11:18:41 -0800 Subject: [PATCH 1/4] pull font into editor Now it adjusts the current font for itself. And it's up to the caller to adjust the current font after. --- edit.lua | 12 +++++++----- log_browser.lua | 2 +- run.lua | 10 ++++------ source.lua | 10 ++++------ source_edit.lua | 12 +++++++----- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/edit.lua b/edit.lua index 7b913fc..7f8a379 100644 --- a/edit.lua +++ b/edit.lua @@ -23,7 +23,7 @@ Same_point_distance = 4 -- pixel distance at which two points are considered th edit = {} -- run in both tests and a real run -function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen +function edit.initialize_state(top, left, right, font, line_height) -- currently always draws to bottom of screen local result = { -- a line is either text or a drawing -- a text is a table with: @@ -83,7 +83,8 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c current_drawing_mode = 'line', -- one of the available shape modes previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points - font_height = font_height, + font = font, + font_height = font:getHeight(), line_height = line_height, top = top, @@ -103,7 +104,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c search_backup = nil, -- stuff to restore when cancelling search } return result -end -- App.initialize_state +end -- edit.initialize_state function edit.check_locs(State) -- if State is inconsistent (i.e. file changed by some other program), @@ -156,6 +157,7 @@ end -- return y drawn until function edit.draw(State) State.button_handlers = {} + love.graphics.setFont(State.font) App.color(Text_color) assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines)) assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)) @@ -548,7 +550,7 @@ end function edit.update_font_settings(State, font_height) State.font_height = font_height - love.graphics.setFont(love.graphics.newFont(State.font_height)) + State.font = love.graphics.newFont(State.font_height) State.line_height = math.floor(font_height*1.3) end @@ -565,7 +567,7 @@ function edit.initialize_test_state() 15, -- top margin Test_margin_left, App.screen.width - Test_margin_right, - 14, -- font height assuming default LÖVE font + love.graphics.getFont(), 15) -- line height end diff --git a/log_browser.lua b/log_browser.lua index b6195b9..3a42394 100644 --- a/log_browser.lua +++ b/log_browser.lua @@ -6,7 +6,7 @@ -- functions to render them into the log_render namespace. function source.initialize_log_browser_side() - Log_browser_state = edit.initialize_state(Margin_top, Editor_state.right + Margin_right + Margin_left, (Editor_state.right+Margin_right)*2, Editor_state.font_height, Editor_state.line_height) + Log_browser_state = edit.initialize_state(Margin_top, Editor_state.right + Margin_right + Margin_left, (Editor_state.right+Margin_right)*2, Editor_state.font, Editor_state.line_height) Log_browser_state.filename = 'log' load_from_disk(Log_browser_state) -- TODO: pay no attention to Fold log_browser.parse(Log_browser_state) diff --git a/run.lua b/run.lua index 98a8345..fd01f46 100644 --- a/run.lua +++ b/run.lua @@ -54,14 +54,14 @@ function print_and_log(s) end function run.load_settings() - love.graphics.setFont(love.graphics.newFont(Settings.font_height)) + local font = love.graphics.newFont(Settings.font_height) -- set up desired window dimensions and make window resizable _, _, App.screen.flags = App.screen.size() App.screen.flags.resizable = true App.screen.width, App.screen.height = Settings.width, Settings.height App.screen.resize(App.screen.width, App.screen.height, App.screen.flags) run.set_window_position_from_settings(Settings) - Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, Settings.font_height, math.floor(Settings.font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, math.floor(Settings.font_height*1.3)) Editor_state.filename = Settings.filename Editor_state.screen_top1 = Settings.screen_top Editor_state.cursor1 = Settings.cursor @@ -79,11 +79,9 @@ end function run.initialize_default_settings() local font_height = 20 - love.graphics.setFont(love.graphics.newFont(font_height)) + local font = love.graphics.newFont(font_height) run.initialize_window_geometry() - 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 = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, math.floor(font_height*1.3)) Settings = run.settings() end diff --git a/source.lua b/source.lua index dafb1a7..d6ea40f 100644 --- a/source.lua +++ b/source.lua @@ -114,7 +114,7 @@ end function source.load_settings() local settings = Settings.source - love.graphics.setFont(love.graphics.newFont(settings.font_height)) + local font = love.graphics.newFont(settings.font_height) -- set up desired window dimensions and make window resizable _, _, App.screen.flags = App.screen.size() App.screen.flags.resizable = true @@ -126,7 +126,7 @@ function source.load_settings() if Show_log_browser_side then right = App.screen.width/2 - Margin_right end - Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, settings.font_height, math.floor(settings.font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, font, math.floor(settings.font_height*1.3)) Editor_state.filename = settings.filename Editor_state.filename = basename(Editor_state.filename) -- migrate settings that used full paths; we now support only relative paths within the app if settings.cursors then @@ -152,12 +152,10 @@ end function source.initialize_default_settings() local font_height = 20 - love.graphics.setFont(love.graphics.newFont(font_height)) + local font = love.graphics.newFont(font_height) source.initialize_window_geometry() - Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), App.screen.width-Margin_right) + Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), App.screen.width-Margin_right, font, math.floor(font_height*1.3)) Editor_state.filename = 'run.lua' - Editor_state.font_height = font_height - Editor_state.line_height = math.floor(font_height*1.3) end function source.initialize_window_geometry() diff --git a/source_edit.lua b/source_edit.lua index 0af9949..9414ee4 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -25,7 +25,7 @@ Same_point_distance = 4 -- pixel distance at which two points are considered th edit = {} -- run in both tests and a real run -function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen +function edit.initialize_state(top, left, right, font, line_height) -- currently always draws to bottom of screen local result = { -- a line is either text or a drawing -- a text is a table with: @@ -85,7 +85,8 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c current_drawing_mode = 'line', previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points - font_height = font_height, + font = font, + font_height = font:getHeight(), line_height = line_height, top = top, @@ -105,7 +106,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c search_backup = nil, -- stuff to restore when cancelling search } return result -end -- App.initialize_state +end -- edit.initialize_state function edit.check_locs(State) -- if State is inconsistent (i.e. file changed by some other program), @@ -157,6 +158,7 @@ end function edit.draw(State, hide_cursor, show_line_numbers) State.button_handlers = {} + love.graphics.setFont(State.font) App.color(Text_color) assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines)) assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)) @@ -552,7 +554,7 @@ end function edit.update_font_settings(State, font_height) State.font_height = font_height - love.graphics.setFont(love.graphics.newFont(State.font_height)) + State.font = love.graphics.newFont(State.font_height) State.line_height = math.floor(font_height*1.3) end @@ -569,7 +571,7 @@ function edit.initialize_test_state() 15, -- top margin Test_margin_left, App.screen.width - Test_margin_right, - 14, -- font height assuming default LÖVE font + love.graphics.getFont(), 15) -- line height end From 6601c9fad87fb8b06df442c964486c8050fec3ac Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 29 Dec 2023 11:26:24 -0800 Subject: [PATCH 2/4] update doc --- reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference.md b/reference.md index c158bef..84f6f0e 100644 --- a/reference.md +++ b/reference.md @@ -190,7 +190,7 @@ There's much more I could include here; check out [the LÖVE manual](https://lov The text-editor widget includes extremely thorough automated tests to give you early warning if you break something. -* `state = edit.initialize_state(top, left, right, font_height, line_height)` -- +* `state = edit.initialize_state(top, left, right, font, line_height)` -- returns an object that can be used to render an interactive editor widget for text and line drawings starting at `y=top` on the app window, between `x=left` and `x=right`. Wraps long lines at word boundaries where possible, From bd2179d8aa37e6088f20faae08fd6564a33e18c1 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 29 Dec 2023 11:52:28 -0800 Subject: [PATCH 3/4] bugfix scenario: run without config file, quit, run again expected: font size remains the same on second run Before this commit it was increasing on each run. It turns out the font height that you pass into love.graphics.newFont() is not the result of font:getHeight(). --- edit.lua | 5 +++-- log_browser.lua | 2 +- reference.md | 4 ++-- run.lua | 4 ++-- source.lua | 4 ++-- source_edit.lua | 3 ++- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/edit.lua b/edit.lua index 7f8a379..ba6acda 100644 --- a/edit.lua +++ b/edit.lua @@ -23,7 +23,7 @@ Same_point_distance = 4 -- pixel distance at which two points are considered th edit = {} -- run in both tests and a real run -function edit.initialize_state(top, left, right, font, line_height) -- currently always draws to bottom of screen +function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen local result = { -- a line is either text or a drawing -- a text is a table with: @@ -84,7 +84,7 @@ function edit.initialize_state(top, left, right, font, line_height) -- currentl previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points font = font, - font_height = font:getHeight(), + font_height = font_height, line_height = line_height, top = top, @@ -568,6 +568,7 @@ function edit.initialize_test_state() Test_margin_left, App.screen.width - Test_margin_right, love.graphics.getFont(), + 14, 15) -- line height end diff --git a/log_browser.lua b/log_browser.lua index 3a42394..dabec2b 100644 --- a/log_browser.lua +++ b/log_browser.lua @@ -6,7 +6,7 @@ -- functions to render them into the log_render namespace. function source.initialize_log_browser_side() - Log_browser_state = edit.initialize_state(Margin_top, Editor_state.right + Margin_right + Margin_left, (Editor_state.right+Margin_right)*2, Editor_state.font, Editor_state.line_height) + Log_browser_state = edit.initialize_state(Margin_top, Editor_state.right + Margin_right + Margin_left, (Editor_state.right+Margin_right)*2, Editor_state.font, Editor_state.font_height, Editor_state.line_height) Log_browser_state.filename = 'log' load_from_disk(Log_browser_state) -- TODO: pay no attention to Fold log_browser.parse(Log_browser_state) diff --git a/reference.md b/reference.md index 84f6f0e..df08f28 100644 --- a/reference.md +++ b/reference.md @@ -190,8 +190,8 @@ There's much more I could include here; check out [the LÖVE manual](https://lov The text-editor widget includes extremely thorough automated tests to give you early warning if you break something. -* `state = edit.initialize_state(top, left, right, font, line_height)` -- - returns an object that can be used to render an interactive editor widget +* `state = edit.initialize_state(top, left, right, font, font_height, line_height)` + -- returns an object that can be used to render an interactive editor widget for text and line drawings starting at `y=top` on the app window, between `x=left` and `x=right`. Wraps long lines at word boundaries where possible, or in the middle of words (no hyphenation yet) when it must. diff --git a/run.lua b/run.lua index fd01f46..2cdd892 100644 --- a/run.lua +++ b/run.lua @@ -61,7 +61,7 @@ function run.load_settings() App.screen.width, App.screen.height = Settings.width, Settings.height App.screen.resize(App.screen.width, App.screen.height, App.screen.flags) run.set_window_position_from_settings(Settings) - Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, math.floor(Settings.font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, Settings.font_height, math.floor(Settings.font_height*1.3)) Editor_state.filename = Settings.filename Editor_state.screen_top1 = Settings.screen_top Editor_state.cursor1 = Settings.cursor @@ -81,7 +81,7 @@ function run.initialize_default_settings() local font_height = 20 local font = love.graphics.newFont(font_height) run.initialize_window_geometry() - Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, math.floor(font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, font_height, math.floor(font_height*1.3)) Settings = run.settings() end diff --git a/source.lua b/source.lua index d6ea40f..14d7afc 100644 --- a/source.lua +++ b/source.lua @@ -126,7 +126,7 @@ function source.load_settings() if Show_log_browser_side then right = App.screen.width/2 - Margin_right end - Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, font, math.floor(settings.font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, font, font_height, math.floor(settings.font_height*1.3)) Editor_state.filename = settings.filename Editor_state.filename = basename(Editor_state.filename) -- migrate settings that used full paths; we now support only relative paths within the app if settings.cursors then @@ -154,7 +154,7 @@ function source.initialize_default_settings() local font_height = 20 local font = love.graphics.newFont(font_height) source.initialize_window_geometry() - Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), App.screen.width-Margin_right, font, math.floor(font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), App.screen.width-Margin_right, font, font_height, math.floor(font_height*1.3)) Editor_state.filename = 'run.lua' end diff --git a/source_edit.lua b/source_edit.lua index 9414ee4..8032278 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -25,7 +25,7 @@ Same_point_distance = 4 -- pixel distance at which two points are considered th edit = {} -- run in both tests and a real run -function edit.initialize_state(top, left, right, font, line_height) -- currently always draws to bottom of screen +function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen local result = { -- a line is either text or a drawing -- a text is a table with: @@ -572,6 +572,7 @@ function edit.initialize_test_state() Test_margin_left, App.screen.width - Test_margin_right, love.graphics.getFont(), + 14, 15) -- line height end From aa6bfb4b15ea75fa289b78cacc7de38f41daf69a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 29 Dec 2023 12:02:45 -0800 Subject: [PATCH 4/4] moar bugfix X-( --- source.lua | 2 +- source_edit.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source.lua b/source.lua index 14d7afc..1aa1f74 100644 --- a/source.lua +++ b/source.lua @@ -126,7 +126,7 @@ function source.load_settings() if Show_log_browser_side then right = App.screen.width/2 - Margin_right end - Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, font, font_height, math.floor(settings.font_height*1.3)) + Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, font, settings.font_height, math.floor(settings.font_height*1.3)) Editor_state.filename = settings.filename Editor_state.filename = basename(Editor_state.filename) -- migrate settings that used full paths; we now support only relative paths within the app if settings.cursors then diff --git a/source_edit.lua b/source_edit.lua index 8032278..f58d7e3 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -86,7 +86,7 @@ function edit.initialize_state(top, left, right, font, font_height, line_height) previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points font = font, - font_height = font:getHeight(), + font_height = font_height, line_height = line_height, top = top,