Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: [Solved] How to convert this Delphi code to FPC - Lazarus?  (Read 2168 times)

lainz

• Hero Member
• Posts: 4592
• Web, Desktop & Android developer
[Solved] How to convert this Delphi code to FPC - Lazarus?
« on: May 11, 2023, 04:55:38 pm »
How to convert this Delphi code to FPC - Lazarus?

I'm using TRichMemo downloaded with OPM, and it doesn't have some of the properties...

Code: Pascal  [Select][+][-]
1. procedure InterpretESCPOS(const Data: String; ATarget: TRichMemo);
2. var
3.   Bold, Underline: Boolean;
4.   FontSize: Integer;
5.   Alignment: TAlignment;
6.   I: Integer;
7.   C: Char;
8.   S: String;
9. begin
10.   Bold := False;
11.   Underline := False;
12.   FontSize := 10; // default font size
13.   Alignment := taLeftJustify; // default alignment
14.   S := '';
15.   I := 1;
16.   while I <= Length(Data) do
17.   begin
18.     C := Data[I];
19.     Inc(I);
20.     if C = #27 then // ESC
21.     begin
22.       if I > Length(Data) then Break;
23.       C := Data[I];
24.       Inc(I);
25.       case C of
26.         'E': // Bold
27.           begin
28.             Bold := True;
29.             ATarget.SelAttributes.Style := [fsBold];
30.           end;
31.         'F': // Not bold
32.           begin
33.             Bold := False;
34.             ATarget.SelAttributes.Style := [];
35.           end;
36.         '-': // Underline
37.           begin
38.             if I > Length(Data) then Break;
39.             C := Data[I];
40.             Inc(I);
41.             case C of
42.               #0: Underline := False;
43.               #1: Underline := True;
44.             end;
45.             if Underline then
46.               ATarget.SelAttributes.Style := ATarget.SelAttributes.Style + [fsUnderline]
47.             else
48.               ATarget.SelAttributes.Style := ATarget.SelAttributes.Style - [fsUnderline];
49.           end;
50.         'a': // Alignment
51.           begin
52.             if I > Length(Data) then Break;
53.             C := Data[I];
54.             Inc(I);
55.             case C of
56.               #0: Alignment := taLeftJustify;
57.               #1: Alignment := taCenter;
58.               #2: Alignment := taRightJustify;
59.             end;
60.             ATarget.Paragraph.Alignment := Alignment;
61.           end;
62.       end;
63.     end
64.     else if C = #29 then // GS
65.     begin
66.       if I > Length(Data) then Break;
67.       C := Data[I];
68.       Inc(I);
69.       case C of
70.         '!': // Font size
71.           begin
72.             if I > Length(Data) then Break;
73.             C := Data[I];
74.             Inc(I);
75.             case C of
76.               #0: FontSize := 10; // replace with your desired sizes
77.               #1: FontSize := 15;
78.               #2: FontSize := 20;
79.             end;
80.             ATarget.SelAttributes.Size := FontSize;
81.           end;
82.       end;
83.     end
84.     else
85.     begin
86.       S := S + C;
87.     end;
88.   end;
90. end;
« Last Edit: May 11, 2023, 11:47:05 pm by lainz »

rvk

• Hero Member
• Posts: 6325
Re: How to convert this Delphi code to FPC - Lazarus?
« Reply #1 on: May 11, 2023, 05:32:37 pm »
https://wiki.freepascal.org/RichMemo#TRichEditForMemo

Quote
TRichEditForMemo
The class helper that implements RichEdit programmatic interface. The helper is declared at RichEditHelpers unit, so you have to add it to the uses section. Helpers are available in FPC 2.6.0 or later.

So, just include RichEditHelpers in your uses.
(You'll need to be on the latest Laz 2.6.0)

lainz

• Hero Member
• Posts: 4592
• Web, Desktop & Android developer
Re: How to convert this Delphi code to FPC - Lazarus?
« Reply #2 on: May 11, 2023, 05:47:10 pm »
https://wiki.freepascal.org/RichMemo#TRichEditForMemo

Quote
TRichEditForMemo
The class helper that implements RichEdit programmatic interface. The helper is declared at RichEditHelpers unit, so you have to add it to the uses section. Helpers are available in FPC 2.6.0 or later.

So, just include RichEditHelpers in your uses.
(You'll need to be on the latest Laz 2.6.0)

Many thanks

lainz

• Hero Member
• Posts: 4592
• Web, Desktop & Android developer
Re: How to convert this Delphi code to FPC - Lazarus?
« Reply #3 on: May 11, 2023, 11:46:50 pm »
Solved, notice that the name of the unit is RichMemoHelpers and not RichEditHelpers.

And the code I posted has a bug, please if you need to use it replace this line:

Code: Pascal  [Select][+][-]
1. Alignment: TAlignment;

With this line:

Code: Pascal  [Select][+][-]
1. Alignment: TRichEditAlignment;