Probleme unicod avec Scintilla [resolu]
Publié : mer. 13/juil./2016 14:05
j'ai un probleme avec scintilla , et l'utilisation des fonction String() de Purebasic
ce probleme avait deja lieu avec Pb 5.42 ....
dans ce code , j'ecris une phrase avec accents, et la meme sans accents !
lorsqu'on clique sur le bouton [surligne] , le surlignage devrait se faire de la meme façon sur toutes les phrases !!
hors, on se rends compte que suivant si la phrase contient des accents ou pas , le surlignage se decale !!
le probleme se retrouve avec d'autres fonctions .. par exemple une recherche, avec findString() va bien se dérouler
mais la position renvoyé du mot trouvé , ne pourra etre utilisé avec les fonction scintilla , car celle-ci ont un comportement
qui differe des fonction Purebasic, elles decalent la position suivant si un ou plusieurs accents sont présent, ou s'il n'y a pas d'accents !!
Findstring() va renvoyer une position en nombre de caracteres ... mais scintilla a besoin qu'on tiennes compte du nombre d'octets dans la ligne apparemment !! et forcement un accent en unicode (UTF8) prends 2 octets
donc la selection (ici le surlignage ) est decalé !!
si quelqu'un a une solution , c'est cool , car la franchement, je bloque caremement ! (j'ai tout essayé meme de compter les accents
pour ajouter un decalage, mais alors, c'ets aussi la longueur de la selection qui foire ) ...
voici le code de test ...
ce probleme avait deja lieu avec Pb 5.42 ....
dans ce code , j'ecris une phrase avec accents, et la meme sans accents !
lorsqu'on clique sur le bouton [surligne] , le surlignage devrait se faire de la meme façon sur toutes les phrases !!
hors, on se rends compte que suivant si la phrase contient des accents ou pas , le surlignage se decale !!
le probleme se retrouve avec d'autres fonctions .. par exemple une recherche, avec findString() va bien se dérouler
mais la position renvoyé du mot trouvé , ne pourra etre utilisé avec les fonction scintilla , car celle-ci ont un comportement
qui differe des fonction Purebasic, elles decalent la position suivant si un ou plusieurs accents sont présent, ou s'il n'y a pas d'accents !!
Findstring() va renvoyer une position en nombre de caracteres ... mais scintilla a besoin qu'on tiennes compte du nombre d'octets dans la ligne apparemment !! et forcement un accent en unicode (UTF8) prends 2 octets
donc la selection (ici le surlignage ) est decalé !!
si quelqu'un a une solution , c'est cool , car la franchement, je bloque caremement ! (j'ai tout essayé meme de compter les accents
pour ajouter un decalage, mais alors, c'ets aussi la longueur de la selection qui foire ) ...
voici le code de test ...
Code : Tout sélectionner
Enumeration FormWindow
#Win
EndEnumeration
Enumeration FormGadget
#Button_0
#Button_1
#Scintilla
EndEnumeration
Enumeration 0
#GOSCI_TEXT_RELOAD ; Replace the current text
#GOSCI_TEXT_RELOAD_CLEARUNDOSTACK ; Replace the current text and clear undo stack
#GOSCI_TEXT_APPEND ; Add text at the end of current text
#GOSCI_TEXT_PREPEND ; Insert text before the beginning of current text
EndEnumeration
;{ les styles pour le surlignage
#INDIC_PLAIN =0 ;Underlined With a single, straight line.
#INDIC_SQUIGGLE =1 ;A squiggly underline. Requires 3 pixels of descender space.
#INDIC_TT =2 ;A line of small T shapes.
#INDIC_DIAGONAL =3 ;Diagonal hatching.
#INDIC_STRIKE =4 ;Strike out.
#INDIC_HIDDEN =5 ;An indicator With no visual effect.
#INDIC_BOX =6 ;A rectangle around the text.
#INDIC_ROUNDBOX =7 ;A rectangle With rounded corners around the text using translucent drawing With the interior usually more transparent than the border. You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA To control the alpha transparency values. The Default alpha values are 30 For fill colour And 50 For outline colour.
#INDIC_STRAIGHTBOX =8 ;A rectangle around the text using translucent drawing With the interior usually more transparent than the border. You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA To control the alpha transparency values. The Default alpha values are 30 For fill colour And 50 For outline colour. This indicator does Not colour the top pixel of the line so that indicators on contiguous lines are visually distinct And disconnected.
#INDIC_FULLBOX =16 ;A rectangle around the text using translucent drawing similar To INDIC_STRAIGHTBOX but covering the entire character area.
#INDIC_DASH =9 ;A dashed underline.
#INDIC_DOTS =10 ;A dotted underline.
#INDIC_SQUIGGLELOW =11;Similar To INDIC_SQUIGGLE but only using 2 vertical pixels so will fit under small fonts.
#INDIC_DOTBOX =12 ;A dotted rectangle around the text using translucent drawing. Translucency alternates between the alpha And outline alpha settings With the top-left pixel using the alpha setting. SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA control the alpha transparency values. The Default values are 30 For alpha And 50 For outline alpha. To avoid excessive memory allocation the maximum width of a dotted box is 4000 pixels.
#INDIC_SQUIGGLEPIXMAP =13 ;A version of INDIC_SQUIGGLE that draws using a pixmap instead of As a series of line segments For performance. Measured To be between 3 And 6 times faster than INDIC_SQUIGGLE on GTK+. Appearance will Not be As good As INDIC_SQUIGGLE on OS X in HiDPI mode.
#INDIC_COMPOSITIONTHICK =14 ;A 2-pixel thick underline located at the bottom of the line To try To avoid touching the character base. Each side is inset 1 pixel so that different indicators in this style covering a range appear isolated. This is similar To an appearance used For the target in Asian language input composition.
#INDIC_COMPOSITIONTHIN =15 ;A 1-pixel thick underline located just before the bottom of the line. Each side is inset 1 pixel so that different indicators in this style covering a range appear isolated. This is similar To an appearance used For non-target ranges in Asian language input composition.
#INDIC_TEXTFORE =17 ;Change the colour of the text To the indicator's fore colour.
Declare Surligne_text2(onglet,Txt.s,coul_selec,Style,mode)
Declare OpenWindow_0(x = 0, y = 0, width = 352, height = 160)
Declare ResizeGadgetsWindow_0()
Declare GOSCI_SetText(id, text$, mode)
Declare.S GOSCI_GetLineText(id, lineIndex)
Global Style
If InitScintilla("Scintilla.dll")=0
MessageRequester("", "pas trouvé scintilla.dll")
End
EndIf
OpenWindow_0() : SmartWindowRefresh(#Win, #True)
; ecrit le text !
For i = 1 To 14
text$=mots_aleat_deb$+" l'été, voici le mot surligné "+Str(i)+" "+mots_aleat_fin$ +Chr(10)+Chr(13)
GOSCI_SetText(#Scintilla, text$, 2)
text$=mots_aleat_deb$+" l'ete, voici le mot surligne "+Str(i)+" "+mots_aleat_fin$ +Chr(10)+Chr(13)
GOSCI_SetText(#Scintilla, text$, 2)
Next i
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case #Button_0 ; surligne !!
Style=#INDIC_ROUNDBOX
SetWindowTitle(#win, "Style :"+Str(Style))
;Surligne_text2(#Scintilla,"surligné ",RGB(0,0,255),Style,#False) ; ici on indique le mot a ne plus surligner
Surligne_text2(#Scintilla,"surligné ",RGB(0,0,255),Style,#True) ; ici on indique le mot a surligner
Surligne_text2(#Scintilla,"surligne ",RGB(0,0,255),Style,#True) ; ici on indique le mot a surligner
Case #Button_1 ; efface les surlignages
Style=#INDIC_ROUNDBOX
Surligne_text2(#Scintilla,"surligné ",RGB(0,0,255),Style,#False) ; ici on indique le mot a ne plus surligner
Surligne_text2(#Scintilla,"surligne ",RGB(0,0,255),Style,#False) ; ici on indique le mot a ne plus surligner
EndSelect
ElseIf Event = #PB_Event_SizeWindow
ResizeGadgetsWindow_0()
EndIf
Until Event = #PB_Event_CloseWindow
Procedure OpenWindow_0(x = 0, y = 0, width = 352, height = 160)
OpenWindow(#Win, x, y, width, height, "",#PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
ButtonGadget(#Button_0, 8, 12, 88, 28, "Surligne")
ButtonGadget(#Button_1, 8, 48, 88, 28, "retire surlignage")
ScintillaGadget(#Scintilla, 104, 12, 244, 144, 0)
EndProcedure
Procedure ResizeGadgetsWindow_0()
Protected FormWindowWidth, FormWindowHeight
FormWindowWidth = WindowWidth(#Win)
FormWindowHeight = WindowHeight(#Win)
ResizeGadget(#Scintilla, 104, 12, FormWindowWidth - 108, FormWindowHeight - 16)
EndProcedure
Procedure GOSCI_SetText(id, text$, mode)
Protected utf8Buffer
If IsGadget(id) And GadgetType(id)=#PB_GadgetType_Scintilla
;Need to convert to utf-8 first.
utf8Buffer=AllocateMemory(StringByteLength(text$, #PB_UTF8)+1)
If utf8Buffer
PokeS(utf8Buffer, text$, -1, #PB_UTF8)
Select mode
Case #GOSCI_TEXT_APPEND
ScintillaSendMessage(id, #SCI_APPENDTEXT, MemorySize(utf8Buffer)-1, utf8Buffer)
Case #GOSCI_TEXT_PREPEND
ScintillaSendMessage(id, #SCI_INSERTTEXT, 0, utf8Buffer)
Case #GOSCI_TEXT_RELOAD, #GOSCI_TEXT_RELOAD_CLEARUNDOSTACK
ScintillaSendMessage(id, #SCI_SETTEXT, 0, utf8Buffer)
If #GOSCI_TEXT_RELOAD_CLEARUNDOSTACK
ScintillaSendMessage(id, #SCI_EMPTYUNDOBUFFER)
EndIf
EndSelect
FreeMemory(utf8Buffer)
EndIf
EndIf
EndProcedure
ProcedureDLL.S GOSCI_GetLineText(id, lineIndex)
Protected text$, numLines, lineLength, utf8Buffer, *ptrAscii.ASCII
If IsGadget(id) And GadgetType(id) = #PB_GadgetType_Scintilla
numLines = ScintillaSendMessage(id, #SCI_GETLINECOUNT)
If lineIndex >=0 And lineIndex < numLines
lineLength = ScintillaSendMessage(id, #SCI_LINELENGTH, lineIndex)
If lineLength
utf8Buffer = AllocateMemory(lineLength+1)
If utf8Buffer
ScintillaSendMessage(id, #SCI_GETLINE, lineIndex, utf8Buffer)
;Remove any terminating EOL characters.
*ptrAscii = utf8Buffer + lineLength - 1
While (*ptrAscii\a = 10 Or *ptrAscii\a = 13) And lineLength
lineLength - 1
*ptrAscii - 1
Wend
text$ = PeekS(utf8Buffer, lineLength, #PB_UTF8)
FreeMemory(utf8Buffer)
EndIf
EndIf
EndIf
EndIf
ProcedureReturn text$
EndProcedure
Procedure Surligne_text2(onglet,Txt.s,coul_selec,Style,mode)
;By Zorro
; onglet =numero de l'editeur scintilla
; Txt.s =le text qui doit etre surligné
;coul_select =la couleur de la selection
; mode= si select ou unselect
; #INDIC_PLAIN =0 ;Underlined With a single, straight line.
; #INDIC_SQUIGGLE =1 ;A squiggly underline. Requires 3 pixels of descender space.
; #INDIC_TT =2 ;A line of small T shapes.
; #INDIC_DIAGONAL =3 ;Diagonal hatching.
; #INDIC_STRIKE =4 ;Strike out.
; #INDIC_HIDDEN =5 ;An indicator With no visual effect.
; #INDIC_BOX =6 ;A rectangle around the text.
; #INDIC_ROUNDBOX =7 ;A rectangle With rounded corners around the text using translucent drawing With the interior usually more transparent than the border. You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA To control the alpha transparency values. The Default alpha values are 30 For fill colour And 50 For outline colour.
; #INDIC_STRAIGHTBOX =8 ;A rectangle around the text using translucent drawing With the interior usually more transparent than the border. You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA To control the alpha transparency values. The Default alpha values are 30 For fill colour And 50 For outline colour. This indicator does Not colour the top pixel of the line so that indicators on contiguous lines are visually distinct And disconnected.
; #INDIC_DASH =9 ;A dashed underline.
; #INDIC_DOTS =10 ;A dotted underline.
; #INDIC_SQUIGGLELOW =11;Similar To INDIC_SQUIGGLE but only using 2 vertical pixels so will fit under small fonts.
; #INDIC_DOTBOX =12 ;A dotted rectangle around the text using translucent drawing. Translucency alternates between the alpha And outline alpha settings With the top-left pixel using the alpha setting. SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA control the alpha transparency values. The Default values are 30 For alpha And 50 For outline alpha. To avoid excessive memory allocation the maximum width of a dotted box is 4000 pixels.
; #INDIC_SQUIGGLEPIXMAP =13 ;A version of INDIC_SQUIGGLE that draws using a pixmap instead of As a series of line segments For performance. Measured To be between 3 And 6 times faster than INDIC_SQUIGGLE on GTK+. Appearance will Not be As good As INDIC_SQUIGGLE on OS X in HiDPI mode.
; #INDIC_COMPOSITIONTHICK =14 ;A 2-pixel thick underline located at the bottom of the line To try To avoid touching the character base. Each side is inset 1 pixel so that different indicators in this style covering a range appear isolated. This is similar To an appearance used For the target in Asian language input composition.
; #INDIC_COMPOSITIONTHIN =15 ;A 1-pixel thick underline located just before the bottom of the line. Each side is inset 1 pixel so that different indicators in this style covering a range appear isolated. This is similar To an appearance used For non-target ranges in Asian language input composition.
; #INDIC_FULLBOX =16 ;A rectangle around the text using translucent drawing similar To INDIC_STRAIGHTBOX but covering the entire character area.
#INDIC_TEXTFORE =17 ;Change the colour of the text To the indicator's fore colour.
Protected indicator,pos,Chhh$
Select Mode
Case #True
For i=0 To ScintillaSendMessage(#Scintilla, #SCI_GETLINECOUNT)
chhh$= LCase(GOSCI_GetLineText(onglet, i))
If FindString(chhh$,LCase(Txt.s))>0
pos=FindString(chhh$,Txt.s)
ScintillaSendMessage(onglet, #SCI_SETINDICATORCURRENT, 8,0)
ScintillaSendMessage(onglet,#SCI_INDICSETSTYLE,8,Style)
ScintillaSendMessage(onglet,#SCI_INDICSETFORE,8,coul_selec)
ScintillaSendMessage(onglet,#SCI_INDICSETALPHA,8, 255) ; ALpha=0 transparent : 255=opaque
ScintillaSendMessage(onglet,#SCI_INDICSETOUTLINEALPHA,8, 255)
ScintillaSendMessage(onglet,#SCI_INDICSETUNDER ,8, #False)
ScintillaSendMessage(onglet, #SCI_INDICATORFILLRANGE,ScintillaSendMessage(onglet, #SCI_POSITIONFROMLINE,i)+(pos-1),Len(Txt.s))
EndIf
Next i
Case #False
For i=0 To ScintillaSendMessage(#Scintilla, #SCI_GETLINECOUNT)
ScintillaSendMessage(onglet, #SCI_SETINDICATORCURRENT, 8,0)
ScintillaSendMessage(onglet, #SCI_INDICATORCLEARRANGE,ScintillaSendMessage(onglet, #SCI_POSITIONFROMLINE, i),ScintillaSendMessage(onglet, #SCI_LINELENGTH, i))
Next i
EndSelect
EndProcedure