B4A Library [B4X] BCTextEngine / BBCodeView - Text engine + BBCode parser + Rich Text View

Erel

Administrator
Staff member
Licensed User
Put this code in the click event:
B4X:
Sub btn_Click
    TextEngine.KerningEnabled = Not(TextEngine.KerningEnabled)
    BBCodeView1.Text = BBCodeView1.Text
End Sub
And run the example.
The text just looks better with kerning.

I marked some of the more obvious flaws:
 

peacemaker

Well-Known Member
Licensed User
Thanks, indeed, there is difference, but, seems, not each is able to detect. I remember that this "eye feature" may get negative result, if an app development customer needs such design nuances and is able to see, but me, as developer, - not :confused:
 

js486dog

Member
Licensed User
v1.73 released with support for kerning.

Without kerning:



With kerning:

Erel please I have a little problem with 1.73 version.

Please look at the pictures.
1.73
BCTextEngine_173.jpg
1.72
BCTextEngine_172.jpg
I load UTF8 text file in the BCTextEngine.
Here is my code:
B4X:
TView.TextEngine.WordBoundaries = "&*+-/<>=\' :{}" & TAB & CRLF & Chr(13)
If pis_nt.Text ="N" Then TView.Text = $" [Alignment=${alig}][TextSize=${txtsize}][color=${texcolor}][PLAIN]${te1}[/PLAIN][/color][/TextSize][/alignment] "$ _
Else TView.Text = $" [Alignment=${alig}][TextSize=${txtsize}][color=${texcolor}][b][PLAIN]${te1}[/PLAIN][/b][/color][/TextSize][/alignment] "$
 

Attachments

Last edited:

Toky Olivier

Active Member
Licensed User
Erel please I have a little problem with 1.73 version.

Please look at the pictures.
1.73
View attachment 88581
1.72
View attachment 88582
I load UTF8 text file in the BCTextEngine.
Here is my code:
B4X:
TView.TextEngine.WordBoundaries = "&*+-/<>=\' :{}" & TAB & CRLF & Chr(13)
If pis_nt.Text ="N" Then TView.Text = $" [Alignment=${alig}][TextSize=${txtsize}][color=${texcolor}][PLAIN]${te1}[/PLAIN][/color][/TextSize][/alignment] "$ _
Else TView.Text = $" [Alignment=${alig}][TextSize=${txtsize}][color=${texcolor}][b][PLAIN]${te1}[/PLAIN][/b][/color][/TextSize][/alignment] "$
It seems that the problem comes from some punctuations only: ! . "
 

William Lancee

Active Member
Licensed User
@js486dog

Those word boundaries work fine in my app. So it must have something to do with your content "te1".
I added tabs and crlfs to my content and it still works fine. Are there any unprintable characters between "." and "Beda"?
 

William Lancee

Active Member
Licensed User
I note that your boundary list is almost the same as the default (you have removed the . and the ,)
Does it work with the default (i.e. comment out: 'TView.TextEngine.WordBoundaries = "&*+-/<>=\' :{}" & TAB & CRLF & Chr(13)
Does your content have Chr(13) in it, as part of end of line characters?
Just thinking...

@Toky Olivier notes that the "!" is not in the word boundaries (either in yours or the default, neither is "?")
 
Last edited:

Toky Olivier

Active Member
Licensed User
I note that your boundary list is almost the same as the default (you have removed the . and the ,)
Does it work with the default (i.e. comment out: 'TView.TextEngine.WordBoundaries = "&*+-/<>=\' :{}" & TAB & CRLF & Chr(13)
Does your content have Chr(13) in it, as part of end of line characters?
Just thinking...

@Toky Olivier notes that the "!" is not in the word boundaries (either in yours or the default, neither is "?")
I think that he needs to add also the Double Quote character in the WordBoundaries by adding Chr(34) or $"""$
 

Erel

Administrator
Staff member
Licensed User
I'm curious, can you comment on how difficult this was to add?
Finding the kerning (?) space is not too difficult as it is based on the same implementation that used to extract the glyphs with the canvas:
B4X:
Private Sub GetKernSpaceBetweenChars (fm As BCFontMetrics, PrevChar As String, ThisChar As String, PrevGlyph As BCGlyph, ThisGlyph As BCGlyph) As Int
    Dim together As String = PrevChar & ThisChar
    Dim Space As Int = fm.KerningTable.GetDefault(together, -1000)
    If Space > -1000 Then Return Space
    Dim w As Int = CreateGlyph(together, fm, True).Width
    Dim res As Int = w - PrevGlyph.Width - ThisGlyph.Width
    fm.KerningTable.Put(together, res)
    Return res
End Sub
BCTextEngine is quite complex so any new feature and especially a low level feature such as this one, do require some work.
 

Sandman

Well-Known Member
Licensed User
BCTextEngine is quite complex so any new feature and especially a low level feature such as this one, do require some work.
I can imagine. I really appreciate it, thanks.

Thanks for the explanation.
 

dobro

Member
Licensed User
hello

I wanted to put an HTML text in the BBLabel
or BBCodeView, but it's causing an error.

while the display of a single line of text works

e.g. this works: sortie.Text = "[ b]BB code written in Bold [ /b]"

but not sortie.Text =HTML_code <--- variable filled with an HTML text

(Look my ZIP ) Thanks

3
Error (position - 1555): Invalid beacon: 127987,65039,8205,9895,65039
Error occurred online: 184 (BBCodeParser)
java.lang.RuntimeException: The object must first be initialized (List).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.collections.List.getSize(List.java:129)
to b4a.example.bbcodeparser._createruns(bbcodeparser.java:166)

on com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

ect ....


Translated with www.DeepL.com/Translator (free version)
 

Attachments

Last edited:

dobro

Member
Licensed User
This library is not for HTML texts but for BB Codes.
You can use another library like: https://www.b4x.com/android/forum/threads/html-textview-v1-0.50696/#content
I understand that, yes.
but I thought I'd use the View to simply display the text of the HTML code, with no desire to interpret it ...

I'm looking for a view to simply display text
if in addition I can add tags to change the look ....

But thank you for your answer, I'll look at the library you recommend. :)

REEDIT :
Ps: the library that you indicate, seems to interpret the HTML code, it's not what I'm looking for.
I'm only looking for a "Gadget_Text" (rtf for example )
 

William Lancee

Active Member
Licensed User
@dobro

If you don't want content to be parsed, use Plain tag. You have a "[" at index 916.

I don't know what the limit is, but 85108 for this string is too big on my system.

B4X:
Dim lengthLimit As Int = 2000
BBLabel1.Text = $"[Plain]${HTML_text.SubString2(0, lengthLimit)}[/Plain]"$
 
Last edited:

William Lancee

Active Member
Licensed User
Oddly enough the Plain tag also works on this Forum! It exists between "use" and "tag" without quotes. The code tags are ignored!

If you don't want content to be parsed, use tag. You have a "[" at index 916.

I don't know what the limit is, but 85108 for this string is too big on my system.

[CODE=b4x]Dim lengthLimit As Int = 2000
BBLabel1.Text = $"[Plain]${HTML_text.SubString2(0, lengthLimit)}"$[/CODE]
 

dobro

Member
Licensed User
I answer to myself

That's what EditText is for !:rolleyes:

@William Lancee

Thanks, I didn't know [plain ] :)
with the EditText, I can do what I want :)

it takes the size without any problem (on my tablet)
I also learned that ".initialize" was only useful
when the View (Gadget) is not already created by the designer

in any case thank you for everything :)
 
Last edited:

William Lancee

Active Member
Licensed User
@js486dog

Your text sometimes has chr(160) instead of chr(32)
chr(160) Unicode is "NO-BREAK SPACE"
Replacing it with chr(32) fixes the problem.

B4X:
MyText = MyText.Replace (Chr(160), Chr(32))
 
Top