├── example.gif ├── init.lua ├── license.txt ├── main.lua └── readme.md /example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/josefnpat/reflowprint/1bc510618697c278ebd0ce13c14a07bbbae0044e/example.gif -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | local trim5 = function(s) 2 | return s:match'^%s*(.*%S)' or '' 3 | end 4 | 5 | local reflow = function(progress,printf,text,x,y,w,a,sx,sy) 6 | assert(progress) 7 | assert(printf) 8 | assert(text) 9 | assert(x) 10 | assert(y) 11 | assert(sx) 12 | assert(sy) 13 | 14 | local font = love.graphics.getFont() 15 | local width,wrappedtext = font:getWrap(text,w) 16 | 17 | local target = math.floor( math.min(1,math.max(0,progress or 1)) * #text) 18 | local c_start = 0 19 | for i,line in pairs(wrappedtext) do 20 | local c_end = c_start + #line 21 | local linewidth = font:getWidth(trim5(line)) 22 | local offset = 0 -- a == nil or a == "left" 23 | 24 | if a == "center" then 25 | offset = (w-linewidth)/2 * sx 26 | elseif a == "right" then 27 | offset = (w-linewidth) * sx 28 | end 29 | -- Determine how much of the line is to be drawn 30 | local subline 31 | if c_end < target then -- all 32 | subline = line 33 | elseif c_start <= target and target <= c_end then -- partial 34 | subline = line:sub(1,target-c_start) 35 | else -- not at all 36 | break 37 | end 38 | printf( 39 | subline, 40 | x+offset, 41 | y+font:getHeight()*(i-1)*sy, 42 | 0, sx, sy 43 | ) 44 | c_start = c_end 45 | end 46 | end 47 | 48 | function reflowprint(i,text,x,y,w,a,sx,sy) 49 | sx = sx or 1 50 | sy = sy or 1 51 | if type(i) == "table" then 52 | reflow( 53 | i.progress, 54 | i.print or love.graphics.print, 55 | i.text,i.x,i.y,i.w,i.a,i.sx,i.sy 56 | ) 57 | else 58 | reflow(i,love.graphics.print,text,x,y,w,a,sx,sy) 59 | end 60 | end 61 | 62 | return reflowprint 63 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Josef Patoprsty 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /main.lua: -------------------------------------------------------------------------------- 1 | utf8 = require"utf8" 2 | 3 | reflowprint = require"init" 4 | 5 | scalex,scaley = 1,1 6 | alignment = "center" 7 | 8 | function badprint(progress,text,x,y,w,a,sx,sy) 9 | local t = text:sub(1, #text*progress) 10 | love.graphics.printf(t,x,y,w,a,0,sx,sy) 11 | end 12 | 13 | local letters_per_second = 25 14 | 15 | function love.load() 16 | defaultFont = love.graphics.newFont() 17 | newFont = love.graphics.newFont(48) 18 | dt = 0 19 | t = 1 20 | text="The quick brown fox jumped over the lazy dog." 21 | end 22 | 23 | function love.update(dt) 24 | dt = math.min(t,dt + 1) 25 | end 26 | 27 | function love.draw() 28 | dt = dt + love.timer.getDelta() 29 | love.window.setTitle("ScaleX: "..scalex.." ScaleY: "..scaley.." Alignment: "..alignment.." Text: "..text) 30 | 31 | local rpx,rpy,rpw,rph = 32,32,love.graphics.getWidth()/2-64,9000 32 | local bpx,bpy,bpw,bph = love.graphics.getWidth()/2+32,32,love.graphics.getWidth()/2-64,9000 33 | 34 | love.graphics.print("ReflowPrint",rpx,rpy-16) 35 | love.graphics.print("BadPrint",bpx,bpy-16) 36 | 37 | love.graphics.setFont(newFont) 38 | 39 | love.graphics.rectangle("line",rpx,rpy,rpw,rph) 40 | reflowprint(dt/t,text,rpx,rpy,rpw,alignment,scalex,scaley) 41 | 42 | love.graphics.rectangle("line",bpx,bpy,bpw,bph) 43 | badprint(dt/t,text,bpx,bpy,bpw,alignment,scalex,scaley) 44 | 45 | love.graphics.setFont(defaultFont) 46 | love.graphics.line(0,0,love.graphics.getWidth()*dt/t,0) 47 | end 48 | 49 | function love.textinput(t) 50 | text = text .. t 51 | end 52 | 53 | function love.keypressed(key) 54 | dt = 0 55 | if key == "backspace" then 56 | local byteoffset = utf8.offset(text, -1) 57 | if byteoffset then 58 | text = string.sub(text, 1, byteoffset - 1) 59 | end 60 | end 61 | if key == "left" then 62 | scalex = scalex/2 63 | elseif key == "right" then 64 | scalex = scalex*2 65 | elseif key == "up" then 66 | scaley = scaley/2 67 | elseif key == "down" then 68 | scaley = scaley*2 69 | elseif key == "space" then 70 | if alignment == "left" then 71 | alignment = "center" 72 | elseif alignment == "center" then 73 | alignment = "right" 74 | else--if alignment == "right" then 75 | alignment = "left" 76 | end 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ReflowPrint 2 | 3 | ReflowPrint is a a library designed for alignment of text that is shown one character at a time. 4 | 5 | This library is compatible with LÖVE 0.10.x and 11.0 6 | 7 | ![](example.gif) 8 | 9 | This library is released under the MIT License --------------------------------------------------------------------------------