I’ve made my textwrap module open-source, hoping we can make it better, together!
https://github.com/mimetic/corona-styled-textwrap.git
A pure-Lua text rendering module for Corona SDK which can handle basic HTML, fonts, and even basic font metrics.
I’ve made this library public in the hopes that we can fix the bugs and improve it. The documentation is poor, I know…feel free to help, or ask me questions!!!
It has a major flaw right now – it cannot render styled text that is right/center justified. So sad. Hard to fix.
However, for everything else, it is fantastic. I use it for my ebook app, and it is fast and flexible.
There is a ton of stuff here, including a my library of useful functions.
Understanding the parts:
- textwrap.lua : the module that renders a piece of text. The text can have basic HTML coding (p, br, i, em, b, li, ol), as well as my built-in paragraph formatting. It will also read the ‘class’ attribute of HTML to figure out the style, then apply the style from the textstyles.txt file!
- HTML support: entities.lua, html.lua : these are open source modules I found and modified to handle HTML
- fontmetrics.lua, fontmetrics.txt, fontvariations.txt : this module and files let the textwrap module position type correctly on the screen. Normally, you can’t position with baseline, but these modules let us do that.
- funx.lua : a large collection of useful functions
Here’s an example of how it works, from the main.lua in the github rep.
local params = { text = mytext, font = "AvenirNext-Regular", size = "12", lineHeight = "16", color = {0, 0, 0, 255}, width = w, alignment = "Left", opacity = "100%", minCharCount = 5, -- Minimum number of characters per line. Start low. targetDeviceScreenSize = screenW..","..screenH, -- Target screen size, may be different from current screen size letterspacing = 0, maxHeight = screenH - 50, minWordLen = 2, textstyles = textStyles, defaultStyle = "Normal", cacheDir = cacheDir, } local t = textwrap.autoWrappedText(params)