Menu

[r4300]: / branches / parsnip / Src / Main / CS.ActiveText.Renderers.RTF.pas  Maximize  Restore  History

Download this file

347 lines (296 with data), 9.8 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
{
* 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-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Provides an object that can be passed to IActiveText.Render to render active
* text in rich text format.
}
unit CS.ActiveText.Renderers.RTF;
interface
uses
// Delphi
Generics.Collections,
// Project
CS.ActiveText,
UBaseObjects,
URTFBuilder,
URTFStyles;
type
TActiveTextRTFStyleMap = class(TObject)
strict private
var
fMap: TDictionary<TActiveTextActionElemKind,TRTFStyle>;
function GetStyle(ElemKind: TActiveTextActionElemKind): TRTFStyle;
public
constructor Create;
destructor Destroy; override;
procedure Add(const ElemKind: TActiveTextActionElemKind;
const Style: TRTFStyle);
procedure Assign(const Src: TActiveTextRTFStyleMap);
procedure MakeMonochrome;
// enumerator enumerates styles, not pair or action element kind
function GetEnumerator: TEnumerator<TRTFStyle>;
property Styles[ElemKInd: TActiveTextActionElemKind]: TRTFStyle
read GetStyle; default;
end;
type
TActiveTextRTFRenderer = class(TInterfacedObject, IActiveTextRenderer)
public
type
TOptions = class(TObject)
strict private
var
fElemStyleMap: TActiveTextRTFStyleMap;
fDisplayURLs: Boolean;
fIgnoreInterBlockText: Boolean;
fURLStyle: TRTFStyle;
procedure SetElemStyleMap(const ElemStyleMap: TActiveTextRTFStyleMap);
public
constructor Create;
destructor Destroy; override;
property ElemStyleMap: TActiveTextRTFStyleMap
read fElemStyleMap write SetElemStyleMap;
property DisplayURLs: Boolean read fDisplayURLs write fDisplayURLs
default False;
property IgnoreInterBlockText: Boolean read fIgnoreInterBlockText
write fIgnoreInterBlockText default True;
property URLStyle: TRTFStyle read fURLStyle write fURLStyle;
end;
strict private
var
fInBlock: Boolean;
fOptions: TOptions;
fBuilder: TRTFBuilder;
function SkipOutput: Boolean;
public
constructor Create(const Builder: TRTFBuilder);
destructor Destroy; override;
property Options: TOptions read fOptions;
/// <summary>Called before active text is processed.</summary>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure Initialise;
/// <summary>Called after last active text element has been processed.
/// </summary>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure Finalise;
/// <summary>Called when plain text should be output.</summary>
/// <param name="AText">string. Text to be output.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure OutputText(const AText: string);
/// <summary>Called at the start of a new block of active text.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of block being
/// opened. This will be an active text element with DisplayStyle =
/// dsBlock.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure BeginBlock(const Kind: TActiveTextActionElemKind);
/// <summary>Called when the current active text block ends.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of block being
/// opened. This will be an active text element with DisplayStyle =
/// dsBlock.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure EndBlock(const Kind: TActiveTextActionElemKind);
/// <summary>Called at the start of a new active text styling element.
/// </summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of styling
/// element. The kind indicates the type of styling to be applied. This
/// will be an active text element with DisplayStyle = dsInline that is
/// not ekLink.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure BeginInlineStyle(const Kind: TActiveTextActionElemKind);
/// <summary>Called the current active text styling element ends.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of styling
/// element. The kind indicates the type of styling to be applied. This
/// will be an active text element with DisplayStyle = dsInline that is
/// not ekLink.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure EndInlineStyle(const Kind: TActiveTextActionElemKind);
/// <summary>Called at the start of a new active text link element.
/// </summary>
/// <param name="URL">string. URL to accessed from the link.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure BeginLink(const URL: string);
/// <summary>Called when the current active text link element ends.
/// </summary>
/// <param name="URL">string. URL to accessed from the link.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure EndLink(const URL: string);
end;
implementation
uses
// Project
SysUtils, Generics.Defaults;
{ TActiveTextRTFStyleMap }
procedure TActiveTextRTFStyleMap.Add(const ElemKind: TActiveTextActionElemKind;
const Style: TRTFStyle);
begin
Assert(not fMap.ContainsKey(ElemKind),
ClassName + '.Add: ElemKind already in map');
fMap.Add(ElemKind, Style);
end;
procedure TActiveTextRTFStyleMap.Assign(const Src: TActiveTextRTFStyleMap);
var
Entry: TPair<TActiveTextActionElemKind, TRTFStyle>;
begin
fMap.Clear;
for Entry in Src.fMap do
fMap.Add(Entry.Key, Entry.Value);
end;
constructor TActiveTextRTFStyleMap.Create;
begin
inherited Create;
fMap := TDictionary<TActiveTextActionElemKind,TRTFStyle>.Create(
TDelegatedEqualityComparer<TActiveTextActionElemKind>.Create(
function (const Left, Right: TActiveTextActionElemKind): Boolean
begin
Result := Left = Right;
end,
function (const ElemKind: TActiveTextActionElemKind): Integer
begin
Result := Ord(ElemKind);
end
)
)
end;
destructor TActiveTextRTFStyleMap.Destroy;
begin
fMap.Free;
inherited;
end;
function TActiveTextRTFStyleMap.GetEnumerator: TEnumerator<TRTFStyle>;
begin
Result := fMap.Values.GetEnumerator;
end;
function TActiveTextRTFStyleMap.GetStyle(ElemKind: TActiveTextActionElemKind):
TRTFStyle;
begin
if fMap.ContainsKey(ElemKind) then
Result := fMap[ElemKind]
else
Result := TRTFStyle.CreateNull;
end;
procedure TActiveTextRTFStyleMap.MakeMonochrome;
var
Style: TRTFStyle;
Kind: TActiveTextActionElemKind;
begin
for Kind := Low(TActiveTextActionElemKind)
to High(TActiveTextActionElemKind) do
begin
if fMap.ContainsKey(Kind) then
begin
Style := fMap[Kind];
Style.MakeMonochrome;
fMap[Kind] := Style;
end;
end;
end;
{ TActiveTextRTFRenderer }
procedure TActiveTextRTFRenderer.BeginBlock(
const Kind: TActiveTextActionElemKind);
begin
fInBlock := True;
fBuilder.BeginGroup;
fBuilder.ApplyStyle(fOptions.ElemStyleMap[Kind]);
end;
procedure TActiveTextRTFRenderer.BeginInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
if SkipOutput then
Exit;
fBuilder.BeginGroup;
fBuilder.ApplyStyle(fOptions.ElemStyleMap[Kind]);
end;
procedure TActiveTextRTFRenderer.BeginLink(const URL: string);
begin
// do nothing
end;
constructor TActiveTextRTFRenderer.Create(const Builder: TRTFBuilder);
begin
Assert(not Assigned(fBuilder), ClassName + '.Create: Builder is nil');
inherited Create;
fBuilder := Builder;
fOptions := TOptions.Create;
end;
destructor TActiveTextRTFRenderer.Destroy;
begin
fOptions.Free;
inherited;
end;
procedure TActiveTextRTFRenderer.EndBlock(
const Kind: TActiveTextActionElemKind);
begin
fBuilder.EndPara;
fBuilder.EndGroup;
fInBlock := False;
end;
procedure TActiveTextRTFRenderer.EndInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
if SkipOutput then
Exit;
fBuilder.EndGroup;
end;
procedure TActiveTextRTFRenderer.EndLink(const URL: string);
resourcestring
sURL = ' (%s)'; // formatting for URLs from hyperlinks
begin
if SkipOutput or not fOptions.DisplayURLs then
Exit;
fBuilder.BeginGroup;
fBuilder.ApplyStyle(fOptions.URLStyle);
fBuilder.AddText(Format(sURL, [URL]));
fBuilder.EndGroup;
end;
procedure TActiveTextRTFRenderer.Finalise;
begin
// do nothing
end;
procedure TActiveTextRTFRenderer.Initialise;
var
Style: TRTFStyle;
begin
fInBlock := False;
for Style in fOptions.ElemStyleMap do
begin
fBuilder.FontTable.AddFromStyle(Style);
fBuilder.ColourTable.AddFromStyle(Style);
end;
fBuilder.FontTable.AddFromStyle(fOptions.URLStyle);
fBuilder.ColourTable.AddFromStyle(fOptions.URLStyle);
end;
procedure TActiveTextRTFRenderer.OutputText(const AText: string);
begin
if SkipOutput then
Exit;
fBuilder.AddText(AText);
end;
function TActiveTextRTFRenderer.SkipOutput: Boolean;
begin
Result := not fInBlock and fOptions.IgnoreInterBlockText;
end;
{ TActiveTextRTFRenderer.TOptions }
constructor TActiveTextRTFRenderer.TOptions.Create;
begin
inherited Create;
fElemStyleMap := TActiveTextRTFStyleMap.Create;
fDisplayURLs := False;
fIgnoreInterBlockText := True;
fURLStyle := TRTFStyle.CreateNull;
end;
destructor TActiveTextRTFRenderer.TOptions.Destroy;
begin
fElemStyleMap.Free;
inherited;
end;
procedure TActiveTextRTFRenderer.TOptions.SetElemStyleMap(
const ElemStyleMap: TActiveTextRTFStyleMap);
begin
fElemStyleMap.Assign(ElemStyleMap);
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.