Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Here is my styled-text, HTML parsing, textWrapping module!
Started by mimetic Jun 26 2013 05:37 PM

* * * * * 1 votes
8 replies to this topic
text type html ebook book text wrap wrap module
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

mimetic

[GLOBAL: userInfoPane.html]
mimetic
  • Contributor

  • 161 posts
  • Corona SDK

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)

 

 



[TOPIC: post.html]
#2

jyrki.hokkanen

[GLOBAL: userInfoPane.html]
jyrki.hokkanen
  • Observer

  • 21 posts
  • Corona SDK

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!



[TOPIC: post.html]
#3

mimetic

[GLOBAL: userInfoPane.html]
mimetic
  • Contributor

  • 161 posts
  • Corona SDK

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.


[TOPIC: post.html]
#4

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

mfor1967

[GLOBAL: userInfoPane.html]
mfor1967
  • Enthusiast

  • 34 posts
  • Corona SDK

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.  

 

 



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,400 posts
  • Enterprise

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



[TOPIC: post.html]
#7

mimetic

[GLOBAL: userInfoPane.html]
mimetic
  • Contributor

  • 161 posts
  • Corona SDK

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

[TOPIC: post.html]
#8

mfor1967

[GLOBAL: userInfoPane.html]
mfor1967
  • Enthusiast

  • 34 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

mimetic

[GLOBAL: userInfoPane.html]
mimetic
  • Contributor

  • 161 posts
  • Corona SDK

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!


[topic_controls]
[/topic_controls]