From 876d6298b40fc8b00bf559d4ec2d909ab1e6bc80 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 16:29:17 -0700 Subject: App.width can no longer take a Text In the process I discovered the horrible fact that Text.x allocates a new Text. And it gets called (just once, thank goodness) on every single frame. --- text.lua | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'text.lua') diff --git a/text.lua b/text.lua index 589e4fd..5045570 100644 --- a/text.lua +++ b/text.lua @@ -23,7 +23,7 @@ function Text.draw(State, line_index, y, startpos) --? print('skipping', frag) else -- render fragment - local frag_width = App.width(frag_text) + local frag_width = App.width(f.data) if x + frag_width > State.right then assert(x > State.left) -- no overfull lines y = y + State.line_height @@ -90,7 +90,7 @@ function Text.populate_screen_line_starting_pos(State, line_index) 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.data) if x + frag_width > State.right then x = State.left table.insert(line_cache.screen_line_starting_pos, pos) @@ -114,7 +114,7 @@ function Text.compute_fragments(State, line_index) -- 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) + local frag_width = App.width(frag) --? 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)) @@ -129,13 +129,13 @@ function Text.compute_fragments(State, line_index) --? 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) + local frag1_width = App.width(frag1) --? 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) + frag_width = App.width(frag) end x = State.left -- new line end @@ -759,8 +759,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) @@ -848,15 +847,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) -- cgit 1.4.1-2-gfad0 /a> 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302