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