Merge text.love

This commit is contained in:
Kartik K. Agaram 2023-12-29 14:44:27 -08:00
commit 3b601dcbcf
3 changed files with 13 additions and 12 deletions

View File

@ -15,7 +15,7 @@ require 'text'
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, font_height, line_height) -- currently always draws to bottom of screen
local result = {
lines = {{data=''}}, -- array of strings
@ -52,6 +52,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
cursor_x = 0,
cursor_y = 0,
font = font,
font_height = font_height,
line_height = line_height,
@ -72,7 +73,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),
@ -104,6 +105,7 @@ end
-- return y drawn until
function edit.draw(State)
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))
@ -389,7 +391,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
@ -406,7 +408,8 @@ 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(),
14,
15) -- line height
end

View File

@ -96,14 +96,14 @@ end
function load_settings()
local settings = json.decode(love.filesystem.read('config'))
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)
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, 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
@ -121,11 +121,9 @@ end
function initialize_default_settings()
local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height))
local font = love.graphics.newFont(font_height)
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, font_height, math.floor(font_height*1.3))
end
function initialize_window_geometry()

View File

@ -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_height, 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 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.