TextDiff

I'll try to publish some code I use in my program.
Post Reply
User avatar
Rickard Johansson
Site Admin
Posts: 5929
Joined: 19 Jul 2006 14:29

TextDiff

Post by Rickard Johansson » 19 May 2020 13:20

The zip file contain source code for the freeware component - TDiff - written and tested in Delphi 10.x and Lazarus.

The TDiff component is used in RJ Texted to compare documents. Two simple demos are included to help you include the component in your own applications.

TDiff was written by Angus Johnson in 2001 - 2008. I simply made some changes to handle Unicode strings, make some simple updates and add Lazarus support.

Download (version 5.02)
TextDiff.zip
(563.9 KiB) Downloaded 4 times

History

11 November 2018
* Added TList<Cardinal> to store hash values
* Made some minor code formatting and code changes
* Fixed Unicode string issues

19 May 2020
* Added Lazarus support

23 May 2020
* Replaced almost all code in demo 1. It should be much easier to understand now.
* Fixed a few issues in demo 2.
* Fixed an issue in diff.AddChangeChr()

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 19 May 2020 15:46

Thanks for this code! If you won't put it on Github, can I do it? my GH account is Alexey-T.

User avatar
Rickard Johansson
Site Admin
Posts: 5929
Joined: 19 Jul 2006 14:29

Re: TextDiff

Post by Rickard Johansson » 19 May 2020 16:24

Yes, you can put it on Github :)

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 21 May 2020 06:38

Ok, thanks, it is here:
https://github.com/Alexey-T/TextDiff

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 23 May 2020 06:27

RJ, small report:

- made small fixes to demo1 and demo2 to compile them on Linux in Lazarus (in Github)
- I run demo2, choose 2 files:

https://github.com/Alexey-T/TRegExpr/bl ... egExpr.pas
https://github.com/Alexey-T/ATSynEdit/b ... egexpr.pas

I compare them.
now command "Next diff" (Ctrl+N or Ctrl+P) goes to next diff. only 2-3 times. after n times, command dont jump more.
but more changes exist.

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 23 May 2020 07:05

Wish for demo1 code: it has function which heavily uses paitning via DC. better to rewrite it to use TCanvas methods.
e.g. TextOut(canvas1.handle, .....) -> canvas1.TextOut(....).
This is more compatible for all WSes in Lazarus.
Current code may work or may not (if LCL Widgetset is not complete).

Code: Select all

procedure MarkupTextOut(canvas: TCanvas; x,y: integer; text: string);
var
  i,len, clr: integer;
  savedTextAlign, SavedBkColor, savedTextColor: cardinal;
  savedPt: TPoint;
begin
  i := pos('<',text);
  if i = 0 then
  begin
    canvas.TextOut(x,y,text);
    exit;
  end;

  savedTextColor := GetTextColor(canvas.Handle);
  SavedBkColor := GetBkColor(canvas.handle);
  {$ifdef windows}
  savedTextAlign := GetTextAlign(canvas.Handle);
  SetTextAlign(canvas.Handle, savedTextAlign or TA_UPDATECP);
  {$endif}
  MoveToEx(canvas.Handle, x, y, @savedPt);

  repeat
    if i > 1 then
      TextOut(canvas.handle,0,0,pchar(text),i-1);
    delete(text,1,i);
    len := length(text);
    if len < 3 then
      break
    else if (text[1] = 'F') and (text[2] ='C') and (text[3] = ':') and
      (len > 9) and (text[10] = '>') then
    begin
      clr := strtointdef('$'+copy(text,4,6),$0);
      SetTextColor(canvas.handle, clr);
      delete(text,1,10);
      dec(len,10);
    end
    else if (text[1] = 'B') and (text[2] ='C') and (text[3] = ':') and
      (len > 9) and (text[10] = '>') then
    begin
      clr := strtointdef('$'+copy(text,4,6),$1FFFFFF);
      if clr > $FFFFFF then
        SetBkColor(canvas.handle, SavedBkColor) else
        SetBkColor(canvas.handle, clr);
      delete(text,1,10);
      dec(len,10);
    end
    else
      break;
    i := pos('<',text);
  until (i = 0);
  TextOut(canvas.handle,0,0,pchar(text),len);

  SetTextColor(canvas.handle,savedTextColor);
  SetBkColor(canvas.handle, SavedBkColor);
  {$ifdef windows}
  SetTextAlign(canvas.Handle, savedTextAlign);
  {$endif}
  with savedPt do
    MoveToEx(canvas.Handle, X,Y, nil);
end;

User avatar
Rickard Johansson
Site Admin
Posts: 5929
Joined: 19 Jul 2006 14:29

Re: TextDiff

Post by Rickard Johansson » 23 May 2020 17:35

Version 5.01 is available:
Rewrote demo 1 and fixed issues in demo 2.

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 23 May 2020 18:13

Pls remove "uses Windows" in HashUnit becase of Unix in FPC.
In both demos, too...

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 23 May 2020 18:15

a)
h := PaintBox1.Canvas.TextHeight('W');
w := PaintBox1.Canvas.TextWidth('W');
better to calc in FormShow 1 time

b)

Code: Select all

    y := y+h+5;
    PaintBox1.Canvas.Brush.Color := clBtnFace;
    PaintBox1.Canvas.TextOut(0,y,'  Matches : '+inttostr(matches));
    y := y+h+2;
    PaintBox1.Canvas.Brush.Color := $AAFFAA;
    PaintBox1.Canvas.TextOut(0,y,'  Modifies: '+inttostr(modifies));
    y := y+h+2;
    PaintBox1.Canvas.Brush.Color := $FFAAAA;
    PaintBox1.Canvas.TextOut(0,y,'  Adds    : '+inttostr(adds));
    y := y+h+2;
    PaintBox1.Canvas.Brush.Color := $AAAAFF;
    PaintBox1.Canvas.TextOut(0,y,'  Deletes : '+inttostr(deletes));
  end;
better to make var C: TCanvas for it.

User avatar
Rickard Johansson
Site Admin
Posts: 5929
Joined: 19 Jul 2006 14:29

Re: TextDiff

Post by Rickard Johansson » 23 May 2020 22:00

Updated to v5.02. Minor fixes in both source files and demos.

Alextp
Posts: 65
Joined: 23 Aug 2014 23:36

Re: TextDiff

Post by Alextp » 24 May 2020 16:30

updated in GH. thanks. I am not sure it is ok to put Textdiff.exe in GH. they allow open src only.

User avatar
Rickard Johansson
Site Admin
Posts: 5929
Joined: 19 Jul 2006 14:29

Re: TextDiff

Post by Rickard Johansson » 24 May 2020 21:23

The license text doesn't say anything about the TextDiff utility. Only the source code.

So it's probably okey to remove the "TextDiff" folder and remove the textdiff utility notice in the readme file...

Post Reply