Menu

[r3207]: / trunk / Src / URTFSnippetDoc.pas  Maximize  Restore  History

Download this file

392 lines (353 with data), 11.1 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
{
* 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) 2008-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class that renders a document that describes a snippet as rich
* text.
}
unit URTFSnippetDoc;
interface
uses
// Delphi
Graphics,
// Project
ActiveText.UMain, ActiveText.URTFRenderer, Hiliter.UGlobals, UEncodings,
UIStringList, USnippetDoc, URTFBuilder, URTFStyles, URTFUtils;
type
/// <summary>Renders a document that describes a snippet as rich text.
/// </summary>
/// <remarks>Highlighting of document's source code can be customised.
/// </remarks>
TRTFSnippetDoc = class(TSnippetDoc)
strict private
var
/// <summary>Attributes that determine formatting of highlighted source
/// code formatting.</summary>
fHiliteAttrs: IHiliteAttrs;
/// <summary>Object used to build rich text document.</summary>
fBuilder: TRTFBuilder;
/// <summary>Flag indicates whether to output in colour.</summary>
fUseColour: Boolean;
fDescStyles: TActiveTextRTFStyleMap;
fExtraStyles: TActiveTextRTFStyleMap;
/// <summary>Styling applied to URLs.</summary>
fURLStyle: TRTFStyle;
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;
/// <summary>Size of paragraph font.</summary>
ParaFontSize = 10;
/// <summary>Paragraph spacing in points.</summary>
ParaSpacing = 12.0;
/// <summary>Size of font used for database information.</summary>
DBInfoFontSize = 9;
strict private
/// <summary>Initialises RTF style used when rendering active text as RTF.
/// </summary>
procedure InitStyles;
strict protected
/// <summary>Initialises rich text document.</summary>
procedure InitialiseDoc; override;
/// <summary>Adds given heading (i.e. snippet name) to document. Can be
/// user defined or from main database.</summary>
/// <remarks>Heading is coloured according to whether user defined or not.
/// </remarks>
procedure RenderHeading(const Heading: string; const UserDefined: Boolean);
override;
/// <summary>Adds given snippet description to document.</summary>
/// <remarks>Active text formatting is observed and styled to suit
/// document.</remarks>
procedure RenderDescription(const Desc: IActiveText); override;
/// <summary>Hilights given source code and adds to document.</summary>
procedure RenderSourceCode(const SourceCode: string); override;
/// <summary>Adds given title followed by given text to document.</summary>
procedure RenderTitledText(const Title, Text: string); override;
/// <summary>Adds a comma-separated list of text, preceded by given title,
/// to document.</summary>
procedure RenderTitledList(const Title: string; List: IStringList);
override;
/// <summary>Adds given compiler info, preceeded by given heading, to
/// document.</summary>
procedure RenderCompilerInfo(const Heading: string;
const Info: TCompileDocInfoArray); override;
/// <summary>Interprets and adds given extra information to document.
/// </summary>
/// <remarks>Active text formatting is observed and styled to suit
/// document.</remarks>
procedure RenderExtra(const ExtraText: IActiveText); override;
/// <summary>Adds given information about code snippets database to
/// document.</summary>
procedure RenderDBInfo(const Text: string); override;
/// <summary>Finalises document and returns content as encoded data.
/// </summary>
function FinaliseDoc: TEncodedData; override;
public
/// <summary>Constructs object to render a snippet.</summary>
/// <param name="HiliteAttrs">IHiliteAttrs [in] Defines style of syntax
/// highlighting used for source code.</param>
/// <param name="UseColour">Boolean [in] Flag that whether document is
/// printed in colour (True) or black and white (False).</param>
constructor Create(const HiliteAttrs: IHiliteAttrs;
const UseColour: Boolean = True);
/// <summary>Destroys object.</summary>
destructor Destroy; override;
end;
implementation
uses
// Delphi
SysUtils,
// Project
Hiliter.UHiliters, UColours, UConsts, UPreferences, UStrUtils;
{ TRTFSnippetDoc }
constructor TRTFSnippetDoc.Create(const HiliteAttrs: IHiliteAttrs;
const UseColour: Boolean = True);
begin
inherited Create;
fHiliteAttrs := HiliteAttrs;
fUseColour := UseColour;
fDescStyles := TActiveTextRTFStyleMap.Create;
fExtraStyles := TActiveTextRTFStyleMap.Create;
InitStyles;
end;
destructor TRTFSnippetDoc.Destroy;
begin
fExtraStyles.Free;
fDescStyles.Free;
inherited;
end;
function TRTFSnippetDoc.FinaliseDoc: TEncodedData;
begin
Result := TEncodedData.Create(fBuilder.Render.ToBytes, etASCII);
fBuilder.Free;
end;
procedure TRTFSnippetDoc.InitialiseDoc;
begin
// Create object used to build main rich text document
fBuilder := TRTFBuilder.Create(0); // Use default code page
// Set up font table
fBuilder.FontTable.Add(MainFontName, rgfSwiss, 0);
fBuilder.FontTable.Add(fHiliteAttrs.FontName, rgfModern, 0);
// set up colour table
fBuilder.ColourTable.Add(clWarningText);
fBuilder.ColourTable.Add(clVarText);
fBuilder.ColourTable.Add(clExternalLink);
fBuilder.ColourTable.Add(Preferences.DBHeadingColours[False]);
fBuilder.ColourTable.Add(Preferences.DBHeadingColours[True]);
end;
procedure TRTFSnippetDoc.InitStyles;
begin
fURLStyle := TRTFStyle.Create(
[scColour], TRTFFont.CreateNull, 0.0, [], clExternalLink
);
fExtraStyles.Add(
ekPara,
TRTFStyle.Create(
TRTFParaSpacing.Create(ParaSpacing, 0.0)
)
);
fDescStyles.Add(
ekPara,
TRTFStyle.Create(
TRTFParaSpacing.Create(0.0, ParaSpacing)
)
);
fExtraStyles.Add(
ekHeading,
TRTFStyle.Create(
[scParaSpacing, scFontStyles],
TRTFParaSpacing.Create(ParaSpacing, 0.0),
TRTFFont.CreateNull,
0.0,
[fsBold],
clNone
)
);
fDescStyles.Add(
ekHeading,
TRTFStyle.Create(
[scParaSpacing, scFontStyles],
TRTFParaSpacing.Create(0.0, ParaSpacing),
TRTFFont.CreateNull,
0.0,
[fsBold],
clNone
)
);
fExtraStyles.Add(
ekStrong,
TRTFStyle.Create(
[scFontStyles],
TRTFFont.CreateNull,
0.0,
[fsBold],
clNone
)
);
fDescStyles.Add(ekStrong, fExtraStyles[ekStrong]);
fExtraStyles.Add(
ekEm,
TRTFStyle.Create(
[scFontStyles],
TRTFFont.CreateNull,
0.0,
[fsItalic],
clNone
)
);
fDescStyles.Add(ekEm, fExtraStyles[ekEm]);
fExtraStyles.Add(
ekVar,
TRTFStyle.Create(
[scFontStyles, scColour],
TRTFFont.CreateNull,
0.0,
[fsItalic],
clVarText
)
);
fDescStyles.Add(ekVar, fExtraStyles[ekVar]);
fExtraStyles.Add(
ekWarning,
TRTFStyle.Create(
[scFontStyles, scColour],
TRTFFont.CreateNull,
0.0,
[fsBold],
clWarningText
)
);
fDescStyles.Add(ekWarning, fExtraStyles[ekWarning]);
fExtraStyles.Add(
ekMono,
TRTFStyle.Create(
[scFont],
TRTFFont.Create(MonoFontName, rgfModern),
0.0,
[],
clNone
)
);
fDescStyles.Add(ekMono, fExtraStyles[ekMono]);
if not fUseColour then
begin
fDescStyles.MakeMonochrome;
fExtraStyles.MakeMonochrome;
fURLStyle.MakeMonochrome;
end;
end;
procedure TRTFSnippetDoc.RenderCompilerInfo(const Heading: string;
const Info: TCompileDocInfoArray);
var
Idx: Integer; // loops compiler information table
begin
fBuilder.SetFontStyle([fsBold]);
fBuilder.SetParaSpacing(
TRTFParaSpacing.Create(ParaSpacing, ParaSpacing / 3)
);
fBuilder.AddText(Heading);
fBuilder.ResetCharStyle;
fBuilder.EndPara;
fBuilder.ClearParaFormatting;
fBuilder.SetFontSize(ParaFontSize);
for Idx := Low(Info) to High(Info) do
begin
fBuilder.AddText(Info[Idx].Compiler);
fBuilder.AddText(TAB);
fBuilder.BeginGroup;
fBuilder.SetFontStyle([fsItalic]);
fBuilder.AddText(Info[Idx].Result);
fBuilder.EndGroup;
fBuilder.EndPara;
end;
end;
procedure TRTFSnippetDoc.RenderDBInfo(const Text: string);
begin
fBuilder.SetParaSpacing(TRTFParaSpacing.Create(ParaSpacing, 0.0));
fBuilder.SetFontSize(DBInfoFontSize);
fBuilder.SetFontStyle([fsItalic]);
fBuilder.AddText(Text);
fBuilder.EndPara;
fBuilder.ClearParaFormatting;
fBuilder.ResetCharStyle;
end;
procedure TRTFSnippetDoc.RenderDescription(const Desc: IActiveText);
var
RTFWriter: TActiveTextRTF; // Object that generates RTF from active text
begin
fBuilder.ResetCharStyle;
fBuilder.SetFontSize(ParaFontSize);
RTFWriter := TActiveTextRTF.Create;
try
RTFWriter.ElemStyleMap := fDescStyles;
RTFWriter.DisplayURLs := True;
RTFWriter.URLStyle := fURLStyle;
RTFWriter.Render(Desc, fBuilder);
finally
RTFWriter.Free;
end;
end;
procedure TRTFSnippetDoc.RenderExtra(const ExtraText: IActiveText);
var
RTFWriter: TActiveTextRTF; // Object that generates RTF from active text
begin
Assert(not ExtraText.IsEmpty, ClassName + '.RenderExtra: ExtraText is empty');
RTFWriter := TActiveTextRTF.Create;
try
RTFWriter.ElemStyleMap := fExtraStyles;
RTFWriter.DisplayURLs := True;
RTFWriter.URLStyle := fURLStyle;
RTFWriter.Render(ExtraText, fBuilder);
finally
RTFWriter.Free;
end;
end;
procedure TRTFSnippetDoc.RenderHeading(const Heading: string;
const UserDefined: Boolean);
begin
fBuilder.SetFontStyle([fsBold]);
fBuilder.SetFontSize(HeadingFontSize);
if fUseColour then
fBuilder.SetColour(Preferences.DBHeadingColours[UserDefined]);
fBuilder.SetParaSpacing(TRTFParaSpacing.Create(0.0, ParaSpacing));
fBuilder.AddText(Heading);
fBuilder.EndPara;
end;
procedure TRTFSnippetDoc.RenderSourceCode(const SourceCode: string);
var
Renderer: IHiliteRenderer; // renders highlighted source as RTF
begin
fBuilder.ClearParaFormatting;
Renderer := TRTFHiliteRenderer.Create(fBuilder, fHiliteAttrs);
TSyntaxHiliter.Hilite(SourceCode, Renderer);
fBuilder.EndPara;
end;
procedure TRTFSnippetDoc.RenderTitledList(const Title: string;
List: IStringList);
begin
RenderTitledText(Title, CommaList(List));
end;
procedure TRTFSnippetDoc.RenderTitledText(const Title, Text: string);
begin
fBuilder.ClearParaFormatting;
fBuilder.ResetCharStyle;
fBuilder.SetFont(MainFontName);
fBuilder.SetFontSize(ParaFontSize);
fBuilder.SetParaSpacing(TRTFParaSpacing.Create(ParaSpacing, 0.0));
fBuilder.BeginGroup;
fBuilder.SetFontStyle([fsBold]);
fBuilder.AddText(Title);
fBuilder.EndGroup;
fBuilder.AddText(' ' + Text);
fBuilder.EndPara;
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.