From 3d3f7cd4d4355f550f34e79435f8b21287d5d366 Mon Sep 17 00:00:00 2001 From: gambas Date: Wed, 15 Dec 2021 15:54:39 +0100 Subject: [PATCH] TextHighlighter: Fix Paint() method, and add an optional argument that defines the position of the first character of the highlighted text to paint. [GB.EVAL.HIGHLIGHT] * BUG: TextHighlighter: Paint() correctly takes into account the new highlighter theme design. * NEW: TextHighlighter: Paint() method takes an optional argument that defines the position of the first character of the highlighted text to paint. --- comp/src/gb.eval.highlight/.src/Main.module | 12 ++++++----- .../.src/TextHighlighter.class | 20 +++++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/comp/src/gb.eval.highlight/.src/Main.module b/comp/src/gb.eval.highlight/.src/Main.module index 145df0802..9d484affe 100644 --- a/comp/src/gb.eval.highlight/.src/Main.module +++ b/comp/src/gb.eval.highlight/.src/Main.module @@ -46,10 +46,12 @@ End Public Sub Main() - ' Dim hHighlighter As TextHighlighter - ' - ' hHighlighter = TextHighlighter["webpage"] - ' - ' Print hHighlighter.ToHTML(File.Load("~/gambas/git/master/app/src/gambas-wiki/.src/Wiki.webpage")) + Dim hHighlighter As TextHighlighter + Dim hTheme As TextHighlighterTheme + + hHighlighter = TextHighlighter["gambas"] + hTheme = New TextHighlighterTheme + 'hTheme.Invert() + Print hHighlighter.ToANSI(File.Load("UnitTest/TestProgram"), hTheme) End diff --git a/comp/src/gb.eval.highlight/.src/TextHighlighter.class b/comp/src/gb.eval.highlight/.src/TextHighlighter.class index f0b570704..b19eb97a0 100644 --- a/comp/src/gb.eval.highlight/.src/TextHighlighter.class +++ b/comp/src/gb.eval.highlight/.src/TextHighlighter.class @@ -823,7 +823,7 @@ Static Private Sub GetStyles(hTheme As Variant) As TextHighlighterStyle[] End -Public Sub Paint(Text As String, X As Float, Y As Float, Optional Theme As Variant) +Public Sub Paint(Text As String, X As Float, Y As Float, Optional Theme As Variant, Optional Pos As Integer) Dim sLine As String Dim aHighlight As Byte[] @@ -846,6 +846,8 @@ Public Sub Paint(Text As String, X As Float, Y As Float, Optional Theme As Varia aStyles = GetStyles(Theme) + Pos = Max(Pos, 1) + fCharWidth = Paint._EstimateFixedFontCharWidth(Paint.Font) LH = Paint.Font.Height + 1 BW = 1 + LH \ 6 @@ -878,16 +880,26 @@ Public Sub Paint(Text As String, X As Float, Y As Float, Optional Theme As Varia For I = 0 To aHighlight.Max Step 2 - iState = aHighlight[I] And 31 + iState = aHighlight[I] And 63 Try hStyle = aStyles[iState] If Error Then hStyle = aStyles[0] bAlt = aHighlight[I] >= 128 iLen = aHighlight[I + 1] + If (P + iLen) < Pos Then + P += iLen + Continue + Else + If Pos > P Then + iLen -= Pos - P + P = Pos + Endif + Endif + If fCharWidth Then - X = X0 + fCharWidth * (P - 1) + X = X0 + fCharWidth * (P - Pos) Else - X = X0 + Paint.Font.TextWidth(String.Left(sLine, P - 1)) + X = X0 + Paint.Font.TextWidth(String.Mid(sLine, Pos, P - Pos)) Endif sText = String.Mid$(sLine, P, iLen)