B4J Question BBCode and use of LaTex

paragkini

Member
Licensed User
Longtime User
Hi,
Is it possible to use below given tags in BBCode view? I have checked the (Original Post) with supported tags and also checked the LaTeX parser view Displaying Math Formulas in pretty form . But was wondering if it can be achieved as given in below text / link.

Link : BBCode and use of LaTex

1633395297979.png
 

William Lancee

Well-Known Member
Licensed User
Longtime User
There are no [Tex] tags. You can get superscripts and subscripts fairly easily, but not both at the same horizontal position.
And more complex LaTex stuff is beyond current capabilities. For mathematicians it would be nice, but there is probably not much demand in general.

B4X:
    BBCodeView1.Text = "A[TextSize=12][Vertical=-8]2[/Vertical][Vertical=8]n-1[/Vertical][/TextSize]"
 
Upvote 1

William Lancee

Well-Known Member
Licensed User
Longtime User
However, BBCodeview has great flexibility. You could create you own tag and preprocess the text string, capitalizing on
the fact that you can insert small drawings inside other text.

B4X:
    Dim cv As B4XCanvas
    BBCodeView1.TextEngine = textEngine1
    Dim mathPanel As B4XView = xui.CreatePanel("")
    mathPanel.SetLayoutAnimated(0, 0, 0, 25dip, 25dip)
    BBCodeView1.Views.Put("mpan", mathPanel)
    cv.Initialize(mathPanel)
    cv.DrawText("2", 0, 10dip, xui.CreateDefaultFont(12), xui.Color_Black, "LEFT")
    cv.DrawText("n-1", 0, 25dip, xui.CreateDefaultFont(12), xui.Color_Black, "LEFT")
    Dim contents As String = _
    $"
        A[View=mpan Vertical=5/]
    "$
    BBCodeView1.Text = contents.Replace(TAB, "")

screenShot1.png
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I am interested, since I have a math background myself.
Therefore I put this together. It needs a bit of tweaking and should be made more robust, but it works.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private cvMath As B4XCanvas
    Private textEngine1 As BCTextEngine
    Private BBCodeView1 As BBCodeView
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("bbc")
 
    textEngine1.Initialize(Root)
    BBCodeView1.TextEngine = textEngine1
    Dim contents As String = _
    $"
        A[Math ^2 _n-1/]    B[Math ^3 _n-2/]   ${Chr(0x03A3)}[Math ^n-1 _0/]
    "$
    BBCodeView1.Text = preprocessMath(BBCodeView1, contents)
End Sub

Private Sub preprocessMath(BBV As BBCodeView, s As String) As String
    Dim sb As StringBuilder: sb.Initialize
    s = s.Replace(TAB, "").Replace("[Math", "[math").Replace("[MATH", "[math")
    Dim v() As String = Regex.Split("\[math", s)
    If v.Length = 1 Then Return s
    sb.append(v(0))
    Dim fnt As B4XFont = xui.CreateDefaultFont(12)
    For i = 1 To v.Length - 1
        Dim t As String = v(i)
        Dim q() As String = Regex.Split("\]", t)
        Dim mathPanel As B4XView = xui.CreatePanel("")
        Dim w() As String = Regex.Split(" ", q(0).Replace("  ", " "))
        Dim superScript As String = w(1).SubString(1).Replace("/","")
        Dim subScript As String = w(2).SubString(1).Replace("/","")
        Dim rect1 As B4XRect = cvMath.MeasureText(superScript, fnt)
        Dim rect2 As B4XRect = cvMath.MeasureText(subScript, fnt)
        mathPanel.SetLayoutAnimated(0, 0, 0, 5dip + Max(rect1.Width, rect2.Width), 3 * rect1.height)
        cvMath.Initialize(mathPanel)
        cvMath.DrawText(superScript, 0, rect1.height + 1dip, xui.CreateDefaultFont(12), xui.Color_Black, "LEFT")
        cvMath.DrawText(subScript, 0, 3 * rect1.height - 1dip, xui.CreateDefaultFont(12), xui.Color_Black, "LEFT")
        sb.Append($"[View=math${i} Vertical=${rect1.height}/]"$ & q(1))
        BBV.Views.Put("math" & i, mathPanel)
    Next
'    Log(sb.toString)
    Return sb.toString
End Sub
screenShot3.png
 
Last edited:
Upvote 0
Top