mimetic / corona-textrender

A pure-Lua text rendering module for Corona SDK which can handle basic HTML, fonts, font-styles, and even basic font metrics. (Takes over from corona-styled-textwrap)
24 stars 7 forks source link

Get reference to autoWrappedText from handler #10

Open stephan507 opened 4 months ago

stephan507 commented 4 months ago

Hi, it seems like, that the link object from the example below is not part of the event.target in the handler function when the link was tapped. Am I missing something? It would be great to find the matching object after the click. The textrender.lua file is so complex, I have a really hard time to understand all the code and make appropriate changes myself. Otherwise a really great library for rendering HTML content!

E.g.:

local options = { text ="Click me", font = "Futura-Bold", size = "20", lineHeight = "20", hyperlinkTextColor = "255,255,255,255", -- an RGBa color in a string, like this: "200,120,255,100" opacity = "100%", width = 200, -- width of the column of text alignment = "Left", -- default text alignment, note the initial capital letter minCharCount = 100, -- Minimum number of characters per line. Start low. maxHeight = 26, handler = touchCallback, -- a function that will accept a 'tap' event isHTML = true, -- TRUE if the text is simplified HTML styled text } local link = textrender.autoWrappedText( options )

function touchCallback(event) if event.target == link then print("found it") end end

mimetic commented 4 months ago

Hi Stephan,

I haven’t worked in this code in, hmm, ten years? I’m terribly sorry, but I have no recollection of how to tweak it.

Yes, it is rather complex. There are some heuristics required so it works fast enough to use (and caching). For a lot of my ebooks, I used it to render HTML data files, not on the fly, but before app publication! That way, the cached result was included in the app, and opening the app didn’t take as long (the first time).

I assume you could call the code using new options at any time…maybe I misunderstood your question about changing the options at runtime? You’d have to regenerate the object to use it; you can’t modify the object by changing options.

You might have found a bug that I didn’t catch — that is very possible.

I wish I could help more, but I just don’t recall!

Best Regards, David

On May 8, 2024, at 10:41 AM, stephan507 @.***> wrote:

Hi, it seems like, that the link object from the example below is not part of the event.target in the handler function when the link was tapped. Am I missing something? It would be great to find the matching object after the click. The textrender.lua file is so complex, I have a really hard time to understand all the code and make appropriate changes myself. Otherwise a really great library for rendering HTML content!

E.g.:

local options = { text ="Click me <x-msg://2/test>", font = "Futura-Bold", size = "20", lineHeight = "20", hyperlinkTextColor = "255,255,255,255", -- an RGBa color in a string, like this: "200,120,255,100" opacity = "100%", width = 200, -- width of the column of text alignment = "Left", -- default text alignment, note the initial capital letter minCharCount = 100, -- Minimum number of characters per line. Start low. maxHeight = 26, handler = touchCallback, -- a function that will accept a 'tap' event isHTML = true, -- TRUE if the text is simplified HTML styled text } local link = textrender.autoWrappedText( options )

function touchCallback(event) if event.target == link then print("found it") end end

— Reply to this email directly, view it on GitHub https://github.com/mimetic/corona-textrender/issues/10, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAHL4Y5KAOSG67H5YN4LMDZBHQL3AVCNFSM6AAAAABHMQQGWWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI4DKMBTGMYDIMI. You are receiving this because you are subscribed to this thread.

...David I. Gross... (Berlin, Germany)

Email: @. @.>

Germany: +49 (174) 478-4302 USA Tel. : +1 (408) 464-6182 Skype : davidgross Facebook : http://facebook.com/digross

LinkedIn: http://www.linkedin.com/in/davidigross Photography: https://www.davidgrossphoto.com/ Inside-Outside Project: https://insideoutsideproject.com/

CONFIDENTIALITY NOTE: This e-mail and any attachments or previous e-mail messages attached to it are confidential and may be protected by legal privilege. If you are not the intended recipient, you are hereby notified that any disclosure, distribution, or copying of this message, or any attachment, is prohibited. If you have received this message in error, please notify the original sender immediately by returning it to the sender and delete this copy, along with any attachments, from your computer without reading or saving it in any manner. Thank you for your cooperation.

stephan507 commented 4 months ago

Hi David, thanks for getting back to me so quickly. I have actually posted two questions and your answer might actually be for the other issue I had opened. But the two issues are related.

Basically, what I am try to do is to render a text in Solar2d(Corona) with a subscript value in a label like log₂(x). Since Solar2d does not provide a better way, AFAIK, I thought of using html tags like this: log2(x)

Once I click on the item, I want to change the color of the text. So the idea was to use the tab listener and then change the color on click. I run into two problems. First how can I find the actual object in the click event and secondly, how can I then change the color. I was hopping you can point me in the right direction. But I understand that the project was not touched in years. But it seems the only suitable solution out there.

mimetic commented 4 months ago

If you haven’t looked at the photobook system, it might be worth it. VERY complex and big — it lets you make serious multimedia ebook apps with built-in games (even can connect to a wordpress driven online library!) — and it might have some of what you want.

Otherwise:

Hmm. If I recall, you could “group” the text object with another object, e.g., a button shaped object, or a even just a transparent rectangle, and make that the target of the click event.

So, I peeked at the code.

The settings.handler is set to empty -- handler for links settings.handler = {} but it can be set if you call autoWrappedText() with the “text” parameter as a table (see line 1039).

In the example, in main.lua, see line 191, where a handler is set to handle a click. The handler is a function that receives the click event.

In examples/scripts/textrender/README.md, see line 121. It says the “handler” param is a function that will accept a tap event. Also, line 58: "handler : [function] A function that will accept a 'tap' event on a hyperlink."

And in textrender.lua, line 879, is where the tap event is sent to the handler function. There are some commented lines for debugging, there!

I think the “href" HTML parameters of the hyperlink, e.g., , are sent to the handler.

This function was meant to work with my bigger ebook code system. So, if you look in the “slideview.lua” file for -- @param handlers : a table of external functions { goToShelves : goes to the shelves (book library), showFloatingBook : show a floating book }

Here is an example from line 10184 of photobook/slideview.lua of a table of functions that can be called from hyperlinks.

        local navbarHandlers = {
                goShelves = bookview.goToShelves,
                openFloatingBook = showFloatingBook,
                goToPage = bookview.goToPage,
                goMain = bookview.goToContents,
                goChapterMain = bookview.goChapterContents,
                goMap = bookview.goToMap,
                goPrev = bookview.goToPrevChapter,
                goNext = bookview.goToNextChapter,
                goBack = bookview.goBack,
                play = bookview.playBookAsShow,
                audioplay = bookview.PlayPauseCurrentAudio,
                audiopause = bookview.PlayPauseCurrentAudio,

            }

I found this example of a text entry used by the photobook system:

Glossary

Credits

Resources

Sponsors

]]>

where you can see the handler function (“goPageByID”, and the parameter sent to it, e.g., “glossary”.

SO!

I suspect you will have to add a reference to the object you want to change in the “attachLinkToObj()” function of textrender.lua. Right now, that function seems to add a tap handler to an object, presumably a hyperlink, but that handler doesn’t necessarily get a reference to the object itself. Or maybe, the tap event contains the object?

I see on line 875 of textrender.lua, the event seems to have a target object, so maybe that’s what you’re after.

Good luck!!!

Best Regards, David

On May 8, 2024, at 1:57 PM, stephan507 @.***> wrote:

Hi David, thanks for getting back to me so quickly. I have actually posted two questions and your answer might actually be for the other issue I had opened. But the two issues are related.

Basically, what I am try to do is to render a text in Solar2d(Corona) with a subscript value in a label like log₂(x). Since Solar2d does not provide a better way, AFAIK, I thought of using html tags like this: log2(x)

Once I click on the item, I want to change the color of the text. So the idea was to use the tab listener and then change the color on click. I run into two problems. First how can I find the actual object in the click event and secondly, how can I then change the color. I was hopping you can point me in the right direction. But I understand that the project was not touched in years. But it seems the only suitable solution out there.

— Reply to this email directly, view it on GitHub https://github.com/mimetic/corona-textrender/issues/10#issuecomment-2100412100, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAHL467SQO344ZIGASXCCLZBIHI5AVCNFSM6AAAAABHMQQGWWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBQGQYTEMJQGA. You are receiving this because you commented.

...David I. Gross... (Berlin, Germany)

Email: @. @.>

Germany: +49 (174) 478-4302 USA Tel. : +1 (408) 464-6182 Skype : davidgross Facebook : http://facebook.com/digross

LinkedIn: http://www.linkedin.com/in/davidigross Photography: https://www.davidgrossphoto.com/ Inside-Outside Project: https://insideoutsideproject.com/

CONFIDENTIALITY NOTE: This e-mail and any attachments or previous e-mail messages attached to it are confidential and may be protected by legal privilege. If you are not the intended recipient, you are hereby notified that any disclosure, distribution, or copying of this message, or any attachment, is prohibited. If you have received this message in error, please notify the original sender immediately by returning it to the sender and delete this copy, along with any attachments, from your computer without reading or saving it in any manner. Thank you for your cooperation.