Scilex paints only the start of words

Discussions related to Visual Prolog
User avatar
Ferenc Nagy
VIP Member
Posts: 289
Joined: 24 Apr 2007 12:26

Scilex paints only the start of words

Unread post by Ferenc Nagy » 5 Feb 2016 19:14

Hi,
I want to use sciLex to format a legend.
My code is

Code: Select all

def_style:appendStyledLine("ЗАДАННЫЕ ЦВЕТЫ",sciLexer_native::style_default),         _=             [ SciStyle  ||                 list::memberIndex_nd(State,Index, [cell::empty,cell::unlivable,cell::poisoned(1),cell::deserted]),                 cell::stateToFillColor(State)=FillColor,                 cell::stateToLetterColor(State)=LetterColor,                 SciStyle=Index+1,                 cell::translateState(State,_,RussianTranslation),                 def_style: used_lexer:styleSetFore(SciStyle,LetterColor),                 def_style: used_lexer:styleSetBack(SciStyle,FillColor),                 def_style:used_lexer:styleSetFont(SciStyle,Name),                 def_style:used_lexer:styleSetBold(SciStyle,false),                 def_style:used_lexer:styleSetUnderline(SciStyle,false),                 def_style:used_lexer:styleSetItalic(SciStyle,true),                 def_style:used_lexer:styleSetSize(SciStyle,FontSize),                 def_style:appendStyledLine(RussianTranslation,SciStyle)             ],             def_style:appendStyledLine("",sciLexer_native::style_default).
where the lines are appended this way

Code: Select all

% Append styled line to the end of the contents of the control.     appendStyledLine(TextWithoutNewLine,UsedStyle) :-         appendStyledText(string::concat(TextWithoutNewLine,"\n"),UsedStyle).       % Append styled text to the end of the contents of the control.     % Use default style instead of missing styles.     appendStyledText(Text,RequestedStyle) :-         P1=used_lexer:currentPos,         used_lexer:addText(Text),         used_lexer:startStyling(P1,31), % Wired-in magic constant         if toBoolean(styleNumSym(RequestedStyle,_))=true then             AppliedStyle=RequestedStyle         else             AppliedStyle=style_default         end if,         used_lexer:setStyling(string::length(Text),AppliedStyle).
The setStyling is the unchanged library procedure

Code: Select all

setStyling(Length, Style) :-         sciLexer_api::setStyling(native, Length, Style).
I am astonished at the uncolored ends of the lines.
Attachments
Partly colored.PNG
Result of run.
Partly colored.PNG (3.84 KiB) Viewed 3140 times
TIA, Regards,
Frank Nagy

User avatar
Thomas Linder Puls
VIP Member
Posts: 1625
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls » 5 Feb 2016 23:29

I am not sure I get all the details, but I am quite sure I know what the problem is.

You measure the string length when the string is in utf16 representation (i.e. the normal string format), but the sciLexer editor holds text in utf8 format.

In utf16 a Unicode code-point (~character) is represented by one or two 16bit numbers. All Latin, Greek and Cyrillic letters, numbers, (standard) punctuations, etc. are represented by one 16bit number but "historical" alphabets, Chinese symbols and the like occupy two 16bit numbers.

In utf8 on the other hand each code-point occupies from one to four 8 bit numbers/bytes. Letters from the Cyrillic alphabet happens to occupy two bytes each.

The style bytes corresponds to the utf8 bytes so if the editor contains two Cyrillic letters it will contain four utf8 bytes and should therefore also have four style bytes, the first style byte will be used for the first letter, the second will not be used, the third will be used for the second letter and the fourth will not be used.

All in all, you should rely on the editor positions instead of the text lengths:

Code: Select all

        StartPos=used_lexer:currentPos,         used_lexer:addText(Text),         EndPos=used_lexer:currentPos,         used_lexer:startStyling(StartPos,31), % Wired-in magic constant         used_lexer:setStyling(EndPos-StartPos, AppliedStyle).
Regards Thomas Linder Puls
PDC

User avatar
Thomas Linder Puls
VIP Member
Posts: 1625
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls » 6 Feb 2016 0:01

Did you mean:

Code: Select all

         foreach list::memberIndex_nd(State, Index, [cell::empty, cell::unlivable, cell::poisoned(1), cell::deserted]) do             cell::stateToFillColor(State) = FillColor,             cell::stateToLetterColor(State) = LetterColor,             SciStyle = Index + 1,             cell::translateState(State, _, RussianTranslation),             def_style:used_lexer:styleSetFore(SciStyle, LetterColor),             def_style:used_lexer:styleSetBack(SciStyle, FillColor),             def_style:used_lexer:styleSetFont(SciStyle, Name),             def_style:used_lexer:styleSetBold(SciStyle, false),             def_style:used_lexer:styleSetUnderline(SciStyle, false),             def_style:used_lexer:styleSetItalic(SciStyle, true),             def_style:used_lexer:styleSetSize(SciStyle, FontSize),             def_style:appendStyledLine(RussianTranslation, SciStyle)         end foreach,
Regards Thomas Linder Puls
PDC

User avatar
Ferenc Nagy
VIP Member
Posts: 289
Joined: 24 Apr 2007 12:26

EndPos-StartPos is a good advice

Unread post by Ferenc Nagy » 6 Feb 2016 11:16

Thank you, Thomas:
You suggestion EndPos-StartPos instead of string::length solved the problem.
The words are fully painted as shown below. The extra painted spaces are not important.

The winning code is

Code: Select all

% Append styled line to the end of the contents of the control.     appendStyledLine(TextWithoutNewLine,UsedStyle) :-         appendStyledText(string::concat(TextWithoutNewLine,"\n"),UsedStyle).       appendStyledText(Text,RequestedStyle) :-         StartPos=used_lexer:currentPos,         used_lexer:addText(Text),         EndPos=used_lexer:currentPos,         used_lexer:startStyling(StartPos,31), % Wired-in magic constant         if toBoolean(styleNumSym(RequestedStyle,_))=true then             AppliedStyle=RequestedStyle         else             AppliedStyle=style_default         end if,         used_lexer:setStyling(EndPos-StartPos,AppliedStyle).
Attachments
Endpos-startpos.PNG
The words are fully painted.
Endpos-startpos.PNG (3.52 KiB) Viewed 3119 times
TIA, Regards,
Frank Nagy

Post Reply