Menu

[r3166]: / trunk / Src / URTFCategoryDoc.pas  Maximize  Restore  History

Download this file

268 lines (243 with data), 7.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class that renders a rich text document that lists the snippets
* in a category.
}
unit URTFCategoryDoc;
interface
uses
// Delphi
Graphics,
// Project
ActiveText.URTFRenderer, DB.UCategory, DB.USnippet, UEncodings, URTFBuilder,
URTFStyles;
type
/// <summary>Renders a rich text document that lists the snippets in a
/// category.</summary>
TRTFCategoryDoc = class(TObject)
strict private
const
/// <summary>Name of main document font.</summary>
MainFontName = 'Tahoma';
/// <summary>Name of mono font.</summary>
MonoFontName = 'Courier New';
/// <summary>Size of heading font.</summary>
HeadingFontSize = 16.0;
/// <summary>Heading spacing in points</summary>
HeadingSpacing = 0.0;
/// <summary>Size of sub-heading font.</summary>
SubHeadingFontSize = 10.0;
/// <summary>Sub-heading spacing in points.</summary>
SubHeadingSpacing = 14.0;
/// <summary>Size of paragraph font.</summary>
ParaFontSize = 10.0;
/// <summary>Paragraph spacing in points.</summary>
ParaSpacing = 12.0;
strict private
var
/// <summary>Object used to build rich text document.</summary>
fBuilder: TRTFBuilder;
/// <summary>Flag indicates whether to output in colour.</summary>
fUseColour: Boolean;
/// <summary>Map of active text action elements to their RTF styling.
/// </summary>
fDescStyles: TActiveTextRTFStyleMap;
/// <summary>Styling applied to URLs.</summary>
fURLStyle: TRTFStyle;
/// <summary>Outputs description of given category as a main heading as
/// RTF.</summary>
procedure OutputCategoryHeading(const Category: TCategory);
/// <summary>Outputs name of given snippet as sub-heading as RTF.</summary>
procedure OutputSnippetSubHeading(const Snippet: TSnippet);
/// <summary>Outputs description of given snippet as RTF.</summary>
procedure OutputSnippetText(const Snippet: TSnippet);
/// <summary>Uses given font colour for subsequent text unless caller has
/// specified that colour is not to be used.</summary>
/// <remarks>Font colour is used until next call to this method.</remarks>
procedure SetColour(const Colour: TColor);
/// <summary>Initialises RTF styles used when writing active text of
/// a snippet description.</summary>
procedure InitStyles;
public
/// <summary>Constructs and configures object.</summary>
/// <param name="UseColour">Boolean [in] Flag that whether document is
/// printed in colour (True) or black and white (False).</param>
constructor Create(const UseColour: Boolean);
/// <summary>Destroys object.</summary>
destructor Destroy; override;
/// <summary>Generates a document that lists contents of given category and
/// returns as encoded data using an encoding suitable for RTF.
/// </summary>
function Generate(const Category: TCategory): TEncodedData;
end;
implementation
uses
// Project
ActiveText.UMain, UColours, UPreferences;
{ TRTFCategoryDoc }
constructor TRTFCategoryDoc.Create(const UseColour: Boolean);
begin
inherited Create;
fUseColour := UseColour;
fBuilder := TRTFBuilder.Create(0); // use default code page for RTF document
// Set up font table
fBuilder.FontTable.Add(MainFontName, rgfSwiss, 0);
fBuilder.FontTable.Add(MonoFontName, rgfModern, 0);
// Set up colour table
fBuilder.ColourTable.Add(Preferences.DBHeadingColours[False]);
fBuilder.ColourTable.Add(Preferences.DBHeadingColours[True]);
fBuilder.ColourTable.Add(clExternalLink);
fDescStyles := TActiveTextRTFStyleMap.Create;
InitStyles;
end;
destructor TRTFCategoryDoc.Destroy;
begin
fDescStyles.Free;
fBuilder.Free;
inherited;
end;
function TRTFCategoryDoc.Generate(const Category: TCategory): TEncodedData;
var
Snippet: TSnippet;
begin
OutputCategoryHeading(Category);
for Snippet in Category.Snippets do
begin
OutputSnippetSubHeading(Snippet);
OutputSnippetText(Snippet);
end;
Result := TEncodedData.Create(fBuilder.Render.ToBytes, etASCII);
end;
procedure TRTFCategoryDoc.InitStyles;
begin
fURLStyle := TRTFStyle.Create(
[scColour], TRTFFont.CreateNull, 0.0, [], clExternalLink
);
fDescStyles.Add(
ekPara, TRTFStyle.Create(TRTFParaSpacing.Create(ParaSpacing, 0.0))
);
fDescStyles.Add(
ekHeading,
TRTFStyle.Create(
[scParaSpacing, scFontStyles],
TRTFParaSpacing.Create(ParaSpacing, 0.0),
TRTFFont.CreateNull,
0.0,
[fsUnderline],
clNone
)
);
fDescStyles.Add(
ekStrong,
TRTFStyle.Create(
[scFontStyles],
TRTFFont.CreateNull,
0.0,
[fsBold],
clNone
)
);
fDescStyles.Add(
ekEm,
TRTFStyle.Create(
[scFontStyles],
TRTFFont.CreateNull,
0.0,
[fsItalic],
clNone
)
);
fDescStyles.Add(
ekVar,
TRTFStyle.Create(
[scFontStyles, scColour],
TRTFFont.CreateNull,
0.0,
[fsItalic],
clVarText
)
);
fDescStyles.Add(
ekWarning,
TRTFStyle.Create(
[scFontStyles, scColour],
TRTFFont.CreateNull,
0.0,
[fsBold],
clWarningText
)
);
fDescStyles.Add(
ekMono,
TRTFStyle.Create(
[scFont],
TRTFFont.Create(MonoFontName, rgfModern),
0.0,
[],
clNone
)
);
if not fUseColour then
begin
fDescStyles.MakeMonochrome;
fURLStyle.MakeMonochrome;
end;
end;
procedure TRTFCategoryDoc.OutputCategoryHeading(const Category: TCategory);
begin
fBuilder.BeginGroup;
fBuilder.SetParaSpacing(TRTFParaSpacing.Create(HeadingSpacing, 0.0));
fBuilder.SetFont(MainFontName);
fBuilder.SetFontSize(HeadingFontSize);
fBuilder.SetFontStyle([fsBold]);
SetColour(Preferences.DBHeadingColours[Category.UserDefined]);
fBuilder.AddText(Category.Description);
fBuilder.EndPara;
fBuilder.EndGroup;
end;
procedure TRTFCategoryDoc.OutputSnippetSubHeading(const Snippet: TSnippet);
begin
fBuilder.BeginGroup;
fBuilder.SetParaSpacing(TRTFParaSpacing.Create(SubHeadingSpacing, 0.0));
fBuilder.SetFont(MainFontName);
fBuilder.SetFontSize(SubHeadingFontSize);
fBuilder.SetFontStyle([fsBold]);
SetColour(Preferences.DBHeadingColours[Snippet.UserDefined]);
fBuilder.AddText(Snippet.DisplayName);
fBuilder.EndPara;
fBuilder.EndGroup;
end;
procedure TRTFCategoryDoc.OutputSnippetText(const Snippet: TSnippet);
var
RTFWriter: TActiveTextRTF; // Object that generates RTF from active text
begin
fBuilder.BeginGroup;
fBuilder.SetParaSpacing(TRTFParaSpacing.Create(ParaSpacing, 0.0));
fBuilder.SetFont(MainFontName);
fBuilder.SetFontSize(ParaFontSize);
fBuilder.SetFontStyle([]);
RTFWriter := TActiveTextRTF.Create;
try
RTFWriter.ElemStyleMap := fDescStyles;
RTFWriter.DisplayURLs := True;
RTFWriter.URLStyle := fURLStyle;
RTFWriter.Render(Snippet.Description, fBuilder);
finally
RTFWriter.Free;
end;
fBuilder.EndGroup;
end;
procedure TRTFCategoryDoc.SetColour(const Colour: TColor);
begin
if fUseColour then
fBuilder.SetColour(Colour);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.