Editor demo supporting bidirectional text (Arabic, Hebrew)

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17521
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Editor demo supporting bidirectional text (Arabic, Hebrew)

Post by Sergey Tkachenko »

This is a modification of Demos\DelphiUnicode\Editors\Editor 2\ improved to support bi-directional text:
reditor_bidi.zip
(9.24 KiB) Downloaded 2799 times
Changes:

1) A default text direction (rve.BiDiMode) is made equal to rvbdLeftToRight:
- rve.BiDiMode := rvbdLeftToRight is assigned in "File | New"
- in "File | Open", if rve.BiDiMode=rvbdUnspecified, it is assigned equal to rvbdLeftToRight.
These changes do not allow rve.BiDiMode to be equal to rvbdUnspecified - it must be either RTL or LTR. In a real application, you can implement:
- commands to change the document default BiDiMode (just assign a value to rve.BiDiMode)
- implement an option dialog where users can choose the default text direction for new documents.

2) Two new buttons are added for changing BiDiMode of paragraphs to RTL or RTL. The buttons have a new unique value of GroupIndex and AllowAllUp=True.
Possible state of these buttons:
- LTR button is pressed: rvbdLeftToRight
- RTL button is pressed: rvbdRightToLeft
- both buttons unpressed: rvbdUnspecified (using a default document text direction, i.e. rvbdLeftToRight in this demo).
This value is returned in function GetParaBiDiFromUI: TRVBiDiMode.
A state of the buttons is updated in rve.OnCurParaStyleChanged.

A new constant is added to PARA_*** constants:
PARA_BIDIMODE = 5;
When the buttons are clicked:

Code: Select all

  rve.ApplyParaStyleConversion(PARA_BIDIMODE);
A new code is added to TForm1.rveParaStyleConversion:

Code: Select all

var ParaInfo: TParaInfo;
    OldBiDiMode, NewBiDiMode: TRVBiDiMode;
...
      PARA_BIDIMODE:
        begin
          OldBiDiMode := ParaInfo.BiDiMode;
          if OldBiDiMode=rvbdUnspecified then
            OldBiDiMode := rve.BiDiMode;
          NewBiDiMode := GetParaBiDiFromUI;
          if NewBiDiMode=rvbdUnspecified then
            NewBiDiMode := rve.BiDiMode;
          if OldBiDiMode<>NewBiDiMode then
            case ParaInfo.Alignment of
              rvaLeft: ParaInfo.Alignment := rvaRight;
              rvaRight: ParaInfo.Alignment := rvaLeft;
            end;
          ParaInfo.BiDiMode := GetParaBiDiFromUI;
        end;
As you can see, if text direction is changed to the opposite value, paragraph alignment is changed to the opposite value as well.

3) A new button is added to show special characters. It is useful, because TRichViewEdit shows a paragraph text direction in "end of paragraph" marks.

This demo does not include commands to change BiDiMode of selected text fragments, because changing paragraph properties is enough for the most cases.

PS: If you use RichViewActions, you can just use the following actions: TrvActionParaRTL, TrvActionParaLTR, TrvActionTextRTL, TrvActionTextLTR.
[+] History of updates
2021-12-07: compatibility with new version of TRichView
Sergey Tkachenko
Site Admin
Posts: 17521
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Editor demo supporting bidirected text (Arabic, Hebrew)

Post by Sergey Tkachenko »

Known problem: in old versions of Delphi, this demo may crash on start.
Workaround: in Unit1.pas, add ActiveX in "uses" and the following code before the final "end.":

Code: Select all

initialization
  OleInitialize(nil)
finalization
  OleUninitialize;
Nostradamus
Posts: 67
Joined: Sat May 13, 2006 1:56 pm
Location: Australia

Re: Editor demo supporting bidirectional text (Arabic, Hebrew)

Post by Nostradamus »

Hi Sergey,
This demo failed to compile on Delphi 11 Ent. in Win 10 64 Bit

cheers
Richard
Sergey Tkachenko
Site Admin
Posts: 17521
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Editor demo supporting bidirectional text (Arabic, Hebrew)

Post by Sergey Tkachenko »

RVTypes must be added in "uses".
I updated the file.
Nostradamus
Posts: 67
Joined: Sat May 13, 2006 1:56 pm
Location: Australia

Re: Editor demo supporting bidirectional text (Arabic, Hebrew)

Post by Nostradamus »

Thanks Sergey,
It works now.
cheers

Richard
Post Reply