Skip to content

Commit 4b65a56

Browse files
committed
Merge branch 'feature/100-refactor-RTFUtils' into develop
Fixes #100
2 parents c74924c + 57e4d74 commit 4b65a56

14 files changed

+328
-237
lines changed

Src/ClassHelpers.RichEdit.pas

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at https://mozilla.org/MPL/2.0/
5+
*
6+
* Copyright (C) 2025, Peter Johnson (gravatar.com/delphidabbler).
7+
*
8+
* Class helper for TRichEdit.
9+
}
10+
11+
unit ClassHelpers.RichEdit;
12+
13+
interface
14+
15+
uses
16+
// Delphi
17+
ComCtrls,
18+
// Project
19+
URTFUtils;
20+
21+
type
22+
TRichEditHelper = class helper for TRichEdit
23+
public
24+
procedure Load(const ARTFMarkup: TRTFMarkup);
25+
end;
26+
27+
implementation
28+
29+
uses
30+
// Delphi
31+
SysUtils,
32+
Classes;
33+
34+
{ TRichEditHelper }
35+
36+
procedure TRichEditHelper.Load(const ARTFMarkup: TRTFMarkup);
37+
var
38+
Stream: TStream;
39+
begin
40+
PlainText := False;
41+
Stream := TMemoryStream.Create;
42+
try
43+
ARTFMarkup.ToStream(Stream);
44+
Stream.Position := 0;
45+
// must set MaxLength or long documents may not display
46+
MaxLength := Stream.Size;
47+
Lines.LoadFromStream(Stream, TEncoding.ASCII);
48+
finally
49+
Stream.Free;
50+
end;
51+
end;
52+
53+
end.

Src/CodeSnip.dpr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ uses
375375
FmRegisterCompilersDlg in 'FmRegisterCompilersDlg.pas' {RegisterCompilersDlg},
376376
ClassHelpers.UGraphics in 'ClassHelpers.UGraphics.pas',
377377
ClassHelpers.UActions in 'ClassHelpers.UActions.pas',
378-
USaveInfoMgr in 'USaveInfoMgr.pas';
378+
USaveInfoMgr in 'USaveInfoMgr.pas',
379+
ClassHelpers.RichEdit in 'ClassHelpers.RichEdit.pas';
379380

380381
// Include resources
381382
{$Resource ExternalObj.tlb} // Type library file

Src/CodeSnip.dproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@
582582
<DCCReference Include="ClassHelpers.UGraphics.pas"/>
583583
<DCCReference Include="ClassHelpers.UActions.pas"/>
584584
<DCCReference Include="USaveInfoMgr.pas"/>
585+
<DCCReference Include="ClassHelpers.RichEdit.pas"/>
585586
<None Include="CodeSnip.todo"/>
586587
<BuildConfiguration Include="Base">
587588
<Key>Base</Key>

Src/FrHiliterPrefs.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ THiliterPrefsFrame = class(TPrefsBaseFrame)
131131
/// <summary>Generates and returns RTF representation of currently selected
132132
/// highlighter element.</summary>
133133
/// <remarks>This RTF is used to display elememt in preview pane.</remarks>
134-
function GenerateRTF: TRTF;
134+
function GenerateRTF: TRTFMarkup;
135135
public
136136
/// <summary>Constructs frame instance and initialises controls.</summary>
137137
/// <param name="AOwner">TComponent [in] Component that owns the frame.
@@ -178,6 +178,7 @@ implementation
178178
// Delphi
179179
SysUtils, ExtCtrls, Windows, Graphics, Dialogs,
180180
// Project
181+
ClassHelpers.RichEdit,
181182
FmPreferencesDlg, FmNewHiliterNameDlg, FmUserHiliterMgrDlg, Hiliter.UAttrs,
182183
IntfCommon, UCtrlArranger, UFontHelper, UIStringList, UMessageBox,
183184
URTFBuilder, URTFStyles, UUtils;
@@ -478,7 +479,7 @@ function THiliterPrefsFrame.DisplayName: string;
478479
Result := sDisplayName;
479480
end;
480481

481-
function THiliterPrefsFrame.GenerateRTF: TRTF;
482+
function THiliterPrefsFrame.GenerateRTF: TRTFMarkup;
482483
var
483484
RTFBuilder: TRTFBuilder; // object used to create and render RTFBuilder
484485
EgLines: IStringList; // list of lines in the example
@@ -614,7 +615,7 @@ procedure THiliterPrefsFrame.UpdatePopupMenu;
614615

615616
procedure THiliterPrefsFrame.UpdatePreview;
616617
begin
617-
TRichEditHelper.Load(frmExample.RichEdit, GenerateRTF);
618+
frmExample.RichEdit.Load(GenerateRTF);
618619
end;
619620

620621
initialization

Src/FrPrintingPrefs.pas

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ implementation
101101
// Delphi
102102
SysUtils, Windows, Graphics, Math, ComCtrls,
103103
// Project
104+
ClassHelpers.RichEdit,
104105
FmPreferencesDlg, Hiliter.UAttrs, Hiliter.UHiliters, IntfCommon, UColours,
105106
UConsts, UEncodings, UFontHelper, UKeysHelper, UPrintInfo, URTFBuilder,
106-
URTFStyles, URTFUtils, UStrUtils, UUtils;
107+
URTFStyles, UStrUtils, UUtils;
107108

108109

109110
{$R *.dfm}
@@ -379,7 +380,7 @@ procedure TPrintingPrefsPreview.Generate(const UseColor, SyntaxPrint: Boolean);
379380
HiliteSource(UseColor, SyntaxPrint, Builder);
380381
Builder.EndPara;
381382
// Load document into rich edit
382-
TRichEditHelper.Load(fRe, Builder.Render);
383+
fRe.Load(Builder.Render);
383384
finally
384385
FreeAndNil(Builder);
385386
end;

Src/FrRTFPreview.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ implementation
5959

6060
uses
6161
// Project
62+
ClassHelpers.RichEdit,
6263
URTFUtils;
6364

6465

@@ -80,7 +81,7 @@ procedure TRTFPreviewFrame.LoadContent(const DocContent: TEncodedData);
8081
@param DocContent [in] Valid RTF document to be displayed.
8182
}
8283
begin
83-
TRichEditHelper.Load(reView, TRTF.Create(DocContent));
84+
reView.Load(TRTFMarkup.Create(DocContent));
8485
end;
8586

8687
procedure TRTFPreviewFrame.SetPopupMenu(const Menu: TPopupMenu);

Src/FrSourcePrefs.pas

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ implementation
112112
// Delphi
113113
SysUtils, Math,
114114
// Project
115+
ClassHelpers.RichEdit,
115116
FmPreferencesDlg, Hiliter.UAttrs, Hiliter.UFileHiliter, Hiliter.UHiliters,
116117
IntfCommon, UConsts, UCtrlArranger, URTFUtils;
117118

@@ -159,7 +160,7 @@ TSourcePrefsPreview = class(TObject)
159160
@param HiliteAttrs [in] Attributes of highlighter used to render
160161
preview.
161162
}
162-
function Generate: TRTF;
163+
function Generate: TRTFMarkup;
163164
{Generate RTF code used to render preview.
164165
@return Required RTF code.
165166
}
@@ -358,8 +359,7 @@ procedure TSourcePrefsFrame.UpdatePreview;
358359
// Generate and display preview with required comment style
359360
Preview := TSourcePrefsPreview.Create(GetCommentStyle, fHiliteAttrs);
360361
try
361-
// Display preview
362-
TRichEditHelper.Load(frmPreview.RichEdit, Preview.Generate);
362+
frmPreview.RichEdit.Load(Preview.Generate);
363363
finally
364364
Preview.Free;
365365
end;
@@ -400,12 +400,14 @@ constructor TSourcePrefsPreview.Create(const CommentStyle: TCommentStyle;
400400
fHiliteAttrs := HiliteAttrs;
401401
end;
402402

403-
function TSourcePrefsPreview.Generate: TRTF;
403+
function TSourcePrefsPreview.Generate: TRTFMarkup;
404404
{Generate RTF code used to render preview.
405405
@return Required RTF code.
406406
}
407407
begin
408-
Result := TRTF.Create(TRTFDocumentHiliter.Hilite(SourceCode, fHiliteAttrs));
408+
Result := TRTFMarkup.Create(
409+
TRTFDocumentHiliter.Hilite(SourceCode, fHiliteAttrs)
410+
);
409411
end;
410412

411413
function TSourcePrefsPreview.SourceCode: string;

Src/UCopyViewMgr.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ class procedure TCopyViewMgr.Execute(View: IView);
6666
var
6767
Clip: TClipboardHelper; // object used to update clipboard
6868
UnicodeText: UnicodeString; // Unicode plain text representation of view
69-
RTF: TRTF; // rich text representation of view
69+
RTFMarkup: TRTFMarkup; // rich text representation of view
7070
begin
7171
Assert(Assigned(View), ClassName + '.Execute: View is nil');
7272
Assert(CanHandleView(View), ClassName + '.Execute: View not supported');
7373
// Generate plain text and rich text representation of view
7474
UnicodeText := GeneratePlainText(View).ToString;
75-
RTF := TRTF.Create(GenerateRichText(View));
75+
RTFMarkup := TRTFMarkup.Create(GenerateRichText(View));
7676
// Open clipboard and add both plain and rich text representations of snippet
7777
Clip := TClipboardHelper.Create;
7878
try
7979
Clip.Open;
8080
try
8181
Clip.AddUnicodeText(UnicodeText);
82-
Clip.AddRTF(RTF.ToRTFCode);
82+
Clip.AddRTF(RTFMarkup.ToRTFCode);
8383
finally
8484
Clip.Close;
8585
end;

Src/UPrintDocuments.pas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface
3131
IPrintDocument = interface(IInterface)
3232
['{56E4CA97-7F04-427A-A95F-03CE55910DC0}']
3333
/// <summary>Generates and returns print document.</summary>
34-
function Generate: TRTF;
34+
function Generate: TRTFMarkup;
3535
end;
3636

3737
type
@@ -53,7 +53,7 @@ TSnippetPrintDocument = class(TInterfacedObject,
5353
constructor Create(const Snippet: TSnippet);
5454
/// <summary>Generates and returns print document.</summary>
5555
/// <remarks>Method of IPrintDocument.</remarks>
56-
function Generate: TRTF;
56+
function Generate: TRTFMarkup;
5757
end;
5858

5959
type
@@ -72,7 +72,7 @@ TCategoryPrintDocument = class(TInterfacedObject,
7272
constructor Create(const Category: TCategory);
7373
/// <summary>Generates and returns print document.</summary>
7474
/// <remarks>Method of IPrintDocument.</remarks>
75-
function Generate: TRTF;
75+
function Generate: TRTFMarkup;
7676
end;
7777

7878
implementation
@@ -91,15 +91,15 @@ constructor TSnippetPrintDocument.Create(const Snippet: TSnippet);
9191
fSnippet := Snippet;
9292
end;
9393

94-
function TSnippetPrintDocument.Generate: TRTF;
94+
function TSnippetPrintDocument.Generate: TRTFMarkup;
9595
var
9696
Doc: TRTFSnippetDoc; // object that renders snippet document in RTF
9797
begin
9898
Doc := TRTFSnippetDoc.Create(
9999
GetHiliteAttrs, poUseColor in PrintInfo.PrintOptions
100100
);
101101
try
102-
Result := TRTF.Create(Doc.Generate(fSnippet));
102+
Result := TRTFMarkup.Create(Doc.Generate(fSnippet));
103103
finally
104104
Doc.Free;
105105
end;
@@ -127,13 +127,13 @@ constructor TCategoryPrintDocument.Create(const Category: TCategory);
127127
fCategory := Category;
128128
end;
129129

130-
function TCategoryPrintDocument.Generate: TRTF;
130+
function TCategoryPrintDocument.Generate: TRTFMarkup;
131131
var
132132
Doc: TRTFCategoryDoc; // object that renders category document in RTF
133133
begin
134134
Doc := TRTFCategoryDoc.Create(poUseColor in PrintInfo.PrintOptions);
135135
try
136-
Result := TRTF.Create(Doc.Generate(fCategory));
136+
Result := TRTFMarkup.Create(Doc.Generate(fCategory));
137137
finally
138138
Doc.Free;
139139
end;

Src/UPrintEngine.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TPrintEngine = class(THiddenRichEdit)
4848
function GetPrintMargins: TPrintMargins;
4949
public
5050
/// <summary>Prints given RTF document.</summary>
51-
procedure Print(const Document: TRTF);
51+
procedure Print(const Document: TRTFMarkup);
5252
/// <summary>Title of document that appears in print spooler.</summary>
5353
/// <remarks>A default title is used if Title is not set or is set to
5454
/// empty string.</remarks>
@@ -63,6 +63,7 @@ implementation
6363
// Delphi
6464
Printers,
6565
// Project
66+
ClassHelpers.RichEdit,
6667
UMeasurement, UPrintInfo;
6768

6869

@@ -94,15 +95,15 @@ function TPrintEngine.GetPrintMargins: TPrintMargins;
9495
Result.Bottom := InchesToPixelsY(MMToInches(PrintInfo.PageMargins.Bottom));
9596
end;
9697

97-
procedure TPrintEngine.Print(const Document: TRTF);
98+
procedure TPrintEngine.Print(const Document: TRTFMarkup);
9899
var
99100
PrintMargins: TPrintMargins; // page margins
100101
DocTitle: string; // document title for print spooler
101102
resourcestring
102103
sDefTitle = 'CodeSnip document'; // default document title
103104
begin
104105
// Load document into engine
105-
TRichEditHelper.Load(RichEdit, Document);
106+
RichEdit.Load(Document);
106107
// Set up page margins
107108
PrintMargins := GetPrintMargins;
108109
RichEdit.PageRect := Rect(

Src/UPrintMgr.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class function TPrintMgr.CanPrint(ViewItem: IView): Boolean;
8585
procedure TPrintMgr.DoPrint;
8686
var
8787
PrintEngine: TPrintEngine; // object that prints the print document
88-
Document: TRTF; // generated print document
88+
Document: TRTFMarkup; // generated print document
8989
begin
9090
PrintEngine := TPrintEngine.Create;
9191
try

0 commit comments

Comments
 (0)