HtmlPanel - rich text on the device

JoaoAfonso

Member
Licensed User
Good evening.

I am using Basic 4 PPC to build an application for Mobile Phones to act as a client to play MUD's. MUD's are text based multiplayer games (check more info in wikipedia, if you wish), and in resume clients to play those games are terminals that simply read and write text.
There are some protocols MUD server's use like colouring parts of text, that clients should be able to decypher. As I wanted to implement colours in my B4PPC MUD client, I realized I couldn't use text boxes to show coloured incoming text.
After a bit of search I've found out that HTML Panel should be the better solution to show incoming text, and so I did it. It runs ok on Desktop, and in my pocket device runs a little slower (is supposed to act this way).
The problem is that the more text it receives, the slower it becomes, and finally shoots an "Out of memory exception".

Two things before finishing the post:
- Usually, per each 10 lines of received text (like a paragraph, or so), considering I need to adjust it with HTML tags for changing font color multiple times, need to loop to read each character after special entities characters and adjust to proper HTML references, etc, incoming text ends up in HTML panel with something like 5 times bigger than when received.
- I know that if I receive a lot of text (I can fill a 50 page text document after playing a MUD for one hour) HTML text gets bigger and bigger. To avoid it, each time text reaches 6kb of text I erase first 3kb and just keep last 3kb, so I can still keep a small log of old text and not use too much memory

So why I still receive such exception?

Thanks in advance
 

agraham

Expert
Licensed User
Longtime User
It will probably be the bitmaps displayed in HtmlPanel that are causing the problem by running out of unmanaged graphics memory. Normally you should not do this but try calling GCCollect in the Hardware library each time you ShowHtml. The reason for trying this is explained in the "Bitmap memory overview" topic in the help for my ImageLibEx library.
 

JoaoAfonso

Member
Licensed User
Still need to try.

Two questions arised meanwhile:
- Is HTML panel the best solution for my intention? Text box would work faster for sure, but I couldn't use coloured text... Is there any other option?
- Please comment the following pseudo code, considering optimization (I believe that stills too slow and probably can run faster with some optimization), that is what happens when my app receive text (example):

TEXT RECEIVED:
- "The sun shines brightly in the [1;33meastern[0m sky."
MMC (my app)
- Receives the text in buffer() ("length=stream.ReadBytes(buffer(),4096)")
- Loops all bytes in search for Telnet protocols codes (it always start with byte 255 followed by two bytes more)
- Transform bytes to string (text=stream.BytesToString(buffer(),0,counter))
- Loop again all chars in search for special characters (like ", &, line breaks, etc), and when find one substitutes it for the corresponding HTML syntax (" for &#34 ; or line break for <br/>). Also when find ANSI color syntax, like the "[1;33m" (to put yellow color) and "[0m" (to return to standard color) in the previous example, substitutes it by "</font> <font color='yellow'>eastern</font><font color='white'>"
- Adds start HTML tag ("<HTML><font bgcolor='black' color='white'>") and end tag ("</font></HTML>") to the previous changed text
- Adds the resulting text to previous text already received
- Html.showHtml (resultingText)

I can only see one optimization at the starting point, when the app does a loop/substitution in all bytes received, then a new loop to change bytes to string, then a new loop on all string chars/substitution. Better do every change in bytes and then do bytesToString (will then be a code harder to read/maintain), do immediately bytesToString and all substitutions then (I tried this before but was having some troubles tracking byte 255 and escape char), or keep it this way?

Thank you in advance (once again)

Best regards
 

mjcoon

Well-Known Member
Licensed User
When I posted (http://www.b4x.com/android/forum/threads/pretty-printing-library.1080/page-4#post-221732) in the pretty-printing library instead of here, it was because I was wondering whether to use RTF to generate a table layout from the contents of a Table control (itself derived from SELECTed SQLite).

But I soon decided that HTML as described for the HTMLPanel might be a better bet: it already has defined <TAB> etc tags. (Also I am accustomed to using the XML library, e.g. for KML.)

But I found that external renderers (like IE) needed some <STYLE> tags to smooth the appearance of the table. However the HTMLPanel renders the content of my <STYLE> tag as if text.

I am not suggesting that HTMLPanel should interpret the style indicators, but is there an easy way in which content of a <STYLE> tag, or even of the whole enclosing <HEAD> tag, could be omitted from the rendering?
 
Top