Here is my styled-text, HTML parsing, textWrapping module!

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)

Hi! This is really awesome. I’d like to use it for a small book project and it seems to be perfect for that. 

Two questions, though.

  1. in textwrap.lua line 1231 you have this tokenizer statement:

    words = string.gmatch(nextChunk, “([^%s%-]+)([%s%-]*)”)

I had an issue where dialog dash characters were stripped out. E.g. input 

Bunny looked at him and said

  -Who are you, anyway?

stripped ‘-’ character away. Fixed that by removing ‘%-’ from the first gmatch capture. Not sure whether that breaks something else, though.

2. isFirstLine seems to be reset to ‘false’ too early? At least it doesn’t apply properly, and turning testing=true indicates the same.

Wasn’t sure how to fix that yet, though.

Anyway, nice work!

This might work better, to the “initial spacing” check for spaces or a hyphen:

1215: local  _, _, padding = stringFind(nextChunk, “^([%s%-]*)”)

That ensures that lines split at hyphens and that those hyphens are kept.

Also, I made the demo use HTML, by setting the “isHTML” flag, to show off lists.

Can this support link? I mean <a href="…"> … </a>

Hi! This is really awesome. I’d like to use it for a small book project and it seems to be perfect for that. 

Two questions, though.

  1. in textwrap.lua line 1231 you have this tokenizer statement:

    words = string.gmatch(nextChunk, “([^%s%-]+)([%s%-]*)”)

I had an issue where dialog dash characters were stripped out. E.g. input 

Bunny looked at him and said

  -Who are you, anyway?

stripped ‘-’ character away. Fixed that by removing ‘%-’ from the first gmatch capture. Not sure whether that breaks something else, though.

2. isFirstLine seems to be reset to ‘false’ too early? At least it doesn’t apply properly, and turning testing=true indicates the same.

Wasn’t sure how to fix that yet, though.

Anyway, nice work!

This might work better, to the “initial spacing” check for spaces or a hyphen:

1215: local  _, _, padding = stringFind(nextChunk, “^([%s%-]*)”)

That ensures that lines split at hyphens and that those hyphens are kept.

Also, I made the demo use HTML, by setting the “isHTML” flag, to show off lists.

Can this support link? I mean <a href="…"> … </a>

tried to open in the simulator 2013.2076 (2013.07.15), but it has errors:

…textwrap.lua:566: ERROR: object:setReferencePoint() is only available in v1 Compatibility mode.  Use anchor points instead.  

I would recommend running in v1 compatibility mode until @mimetic has a chance to update his library to be G2.0 compatible.

Put:    

graphicsCompatibility = 1

in your config.lua at the same place you have your width = and height = lines.

Rob

Working in Turkey until February, so I won’t be able to fix anything until then. Happy if someone else wants to take a stab at it! By the way, I got hyperlinks working, which is very cool. Now, you can attach a function to text using something like

is there any way for this to use native.systemFont and native.systemFontBold?

I just can’t get any fonts to work and everything is rendered with the system default so it all looks the same.

Be sure you have installed the fonts in the app, in the config file. They’ll work in the Corona simulator but not the app if not installed. There is a file which tells the system what is “bold” for each font…check that also. I’m not sure what happens if you use system Fontan then try to bold!

tried to open in the simulator 2013.2076 (2013.07.15), but it has errors:

…textwrap.lua:566: ERROR: object:setReferencePoint() is only available in v1 Compatibility mode.  Use anchor points instead.  

I would recommend running in v1 compatibility mode until @mimetic has a chance to update his library to be G2.0 compatible.

Put:    

graphicsCompatibility = 1

in your config.lua at the same place you have your width = and height = lines.

Rob

Working in Turkey until February, so I won’t be able to fix anything until then. Happy if someone else wants to take a stab at it! By the way, I got hyperlinks working, which is very cool. Now, you can attach a function to text using something like

is there any way for this to use native.systemFont and native.systemFontBold?

I just can’t get any fonts to work and everything is rendered with the system default so it all looks the same.

Be sure you have installed the fonts in the app, in the config file. They’ll work in the Corona simulator but not the app if not installed. There is a file which tells the system what is “bold” for each font…check that also. I’m not sure what happens if you use system Fontan then try to bold!