Menu

[r375]: / trunk / Src / URTFUtils.pas  Maximize  Restore  History

Download this file

376 lines (332 with data), 12.6 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
{
* URTFUtils.pas
*
* Utility functions used when processing RTF.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is URTFUtils.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit URTFUtils;
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$WARN UNSAFE_CODE OFF}
interface
uses
// Delphi
Classes, ComCtrls;
const
// Constants relating to RTF
cRTFVersion = 1; // RTF version
cDefCodePage = 1252; // default code paeg
cDefLanguage = 1033; // default language
type
{
TRTFControl:
Identifiers for each RTF control word supported by the program.
}
TRTFControl = (
rcRTF, // RTF version
rcAnsi, // use ANSI character set
rcAnsiCodePage, // specifies ANSI code page
rcDefFontNum, // default font number
rcDefLanguage, // default language
rcFontTable, // introduces font table
rcFontPitch, // font pitch
rcFontCharset, // font character set
rcFontFamilyNil, // unknown font family
rcFontFamilyRoman, // serif, proportional fonts
rcFontFamilySwiss, // sans-serif, proportional fonts
rcFontFamilyModern, // fixed pitch serif and sans-serif fonts
rcFontFamilyScript, // script fonts
rcFontFamilyDecor, // decorative fonts
rcFontFamilyTech, // technical, symbol and maths fonts
rcColorTable, // introduces colour table
rcRed, // defines red colour component
rcGreen, // defines gree colour component
rcBlue, // defines blue colour component
rcInfo, // introduces information group
rcTitle, // sets document title
rcPard, // resets to default paragraph format
rcPar, // begins new paragraph
rcPlain, // reset font (character) formatting properties
rcFontNum, // font number (index to font table)
rcForeColorNum, // foreground colour number (index to colour table)
rcBold, // sets or toggles bold style
rcItalic, // sets or toggles italic style
rcUnderline, // sets or toggles underline style
rcFontSize, // font size in 1/2 points
rcSpaceBefore, // space before paragraphs in twips
rcSpaceAfter // space after paragraph in twips
);
function IsValidRTFCode(const Content: string): Boolean;
{Checks if document content is valid rich text code.
@param Content [in] Document content to be checked.
@return True if valid rich text.
}
function RTFControl(const Ctrl: TRTFControl): string; overload;
{Creates a parameterless RTF control word.
@param Ctrl [in] Id of required control word.
@return RTF control.
}
function RTFControl(const Ctrl: TRTFControl; const Param: Integer): string;
overload;
{Creates an RTF control word with parameter.
@param Ctrl [in] Id of required control word.
@param Param [in] Required parameter.
@return RTF control.
}
function RTFEscape(const Ch: AnsiChar): string;
{Creates RTF escape sequence for a character.
@param Ch [in] Character to escape.
@return Escape sequence.
}
function RTFHexEscape(const Ch: AnsiChar): string;
{Creates RTF hexadecimal escape sequence for a character.
@param Ch [in] Character to escape.
@return Escape sequence.
}
function RTFMakeSafeText(const TheText: string): string;
{Encodes text so that any RTF-incompatible characters are replaced with
suitable control words.
@param TheText [in] Text to be encoded.
@return Encoded text.
}
procedure RTFInsertString(const RE: TRichEdit; const RTF: string);
{Inserts rich text code to current selection position in a rich edit control.
If text is selected in the control the selection is first deleted.
@param RE [in] Rich edit control in which RTF code is to be inserted.
@param RTF [in] String containing valid rich text code to be inserted.
}
procedure RTFLoadFromStream(const RE: TRichEdit; const Stream: TStream);
{Loads RTF code into an RTF control, replacing all the control's current
content.
@param RE [in] Rich edit control to receive RTF code.
@param RTF [in] Stream containing valid rich text code.
}
procedure RTFLoadFromString(const RE: TRichEdit; const RTF: string);
{Loads RTF code into an RTF control, replacing all the control's current
content.
@param RE [in] Rich edit control to receive RTF code.
@param RTF [in] String containing valid rich text code.
}
procedure RTFSaveToStream(const RE: TRichEdit; const Stream: TStream);
{Saves RTF code from rich edit control into a stream.
@param RE [in] Rich edit control whose content is to be saved.
@param Stream [in] Stream that receives rich text code.
}
implementation
uses
// Delphi
SysUtils, StrUtils, Windows, RichEdit,
// Project
UExceptions;
const
// Map of RTF control ids to control word
cControls: array[TRTFControl] of string = (
'rtf', 'ansi', 'ansicpg', 'deff', 'deflang', 'fonttbl', 'fprq', 'fcharset',
'fnil', 'froman', 'fswiss', 'fmodern', 'fscript', 'fdecor', 'ftech',
'colortbl', 'red', 'green', 'blue', 'info', 'title', 'pard', 'par', 'plain',
'f', 'cf', 'b', 'i', 'ul', 'fs', 'sb', 'sa'
);
function IsValidRTFCode(const Content: string): Boolean;
{Checks if document content is valid rich text code.
@param Content [in] Document content to be checked.
@return True if valid rich text.
}
begin
Result := AnsiStartsText('{' + RTFControl(rcRTF, 1), Content);
end;
function RTFControl(const Ctrl: TRTFControl): string;
{Creates a parameterless RTF control word.
@param Ctrl [in] Id of required control word.
@return RTF control.
}
begin
Result := '\' + cControls[Ctrl];
end;
function RTFControl(const Ctrl: TRTFControl; const Param: Integer): string;
{Creates an RTF control word with parameter.
@param Ctrl [in] Id of required control word.
@param Param [in] Required parameter.
@return RTF control.
}
begin
Result := RTFControl(Ctrl) + IntToStr(Param);
end;
function RTFEscape(const Ch: AnsiChar): string;
{Creates RTF escape sequence for a character.
@param Ch [in] Character to escape.
@return Escape sequence.
}
begin
Result := '\' + Ch;
end;
function RTFHexEscape(const Ch: AnsiChar): string;
{Creates RTF hexadecimal escape sequence for a character.
@param Ch [in] Character to escape.
@return Escape sequence.
}
begin
Result := '\''' + IntToHex(Ord(Ch), 2);
end;
function RTFMakeSafeText(const TheText: string): string;
{Encodes text so that any RTF-incompatible characters are replaced with
suitable control words.
@param TheText [in] Text to be encoded.
@return Encoded text.
}
var
I: Integer; // loops thru characters in string
Ch: AnsiChar; // character being processed
begin
Result := '';
for I := 1 to Length(TheText) do
begin
Ch := TheText[I];
case Ch of
#0..#$19, #$80..#$FF:
// Replace these chars by hex control word
Result := Result + RTFHexEscape(Ch);
'{', '\', '}':
// Escape these reserved chars
Result := Result + RTFEscape(Ch);
else
// Pass remaining chars thru unchanged
Result := Result + Ch;
end;
end;
end;
function EditStreamReader(Stream: TStream; pBuff: Pointer;
cb: LongInt; pcb: PLongInt): DWORD; stdcall;
{Callback routine used to read data from stream that is being inserted into a
rich edit control. This routine is called by rich edit control when processing
EM_STREAMIN message.
@param Stream [in] Reference to TStream being read. (Actual param is
supposed to be DWORD, but we cast to TStream).
@param pBuff [in] Pointer to buffer to receive data read from stream.
@param cb [in] Number of bytes requested to be read.
@param pcb [in] Pointer to variable to receive number of bytes actually
read from stream.
@return Success / error code. 0 indicates succcess. None-zero indicates an
error condition.
}
begin
// Assume no error
Result := $0000;
try
// Read required data from stream, recording bytes actually read
pcb^ := Stream.Read(pBuff^, cb);
except
// Indicates error to calling routine
Result := $FFFF;
end;
end;
procedure RTFInsertStream(const RE: TRichEdit; const Stream: TStream);
{Inserts a stream of rich text code to current selection position in a rich
edit control. If text is selected in the control the selection is first
deleted.
@param RE [in] Rich edit control in which RTF code is to be inserted.
@param Stream [in] Stream containing valid rich text code to be inserted.
}
const
// Flags used in EM_STREAMIN message call
cFlags = SFF_SELECTION or SF_RTF or SFF_PLAINRTF;
// Bug error message ** do not localise
cStreamErrMsg = 'RTFInsertStream: Error inserting stream';
var
EditStream: TEditStream; // defines callback used to read inserted RTF
begin
RE.Lines.BeginUpdate;
try
// Make sure rich edit control size is large enough to take inserted code
RE.MaxLength := RE.MaxLength + Stream.Size;
// Stream in the RTF via EM_STREAMIN message
EditStream.dwCookie := DWORD(Stream);
EditStream.dwError := $0000;
EditStream.pfnCallback := @EditStreamReader;
RE.Perform(EM_STREAMIN, cFlags, LPARAM(@EditStream));
// Report any errors as a bug
if EditStream.dwError <> $0000 then
raise EBug.Create(cStreamErrMsg);
finally
RE.Lines.EndUpdate;
end;
end;
procedure RTFInsertString(const RE: TRichEdit; const RTF: string);
{Inserts rich text code to current selection position in a rich edit control.
If text is selected in the control the selection is first deleted.
@param RE [in] Rich edit control in which RTF code is to be inserted.
@param RTF [in] String containing valid rich text code to be inserted.
}
var
Stm: TStringStream; // stream onto RTF code string
begin
Stm := TStringStream.Create(RTF);
try
RTFInsertStream(RE, Stm);
finally
Stm.Free;
end;
end;
procedure RTFLoadFromStream(const RE: TRichEdit; const Stream: TStream);
{Loads RTF code into an RTF control, replacing all the control's current
content.
@param RE [in] Rich edit control to receive RTF code.
@param RTF [in] Stream containing valid rich text code.
}
begin
RE.PlainText := False;
RE.MaxLength := Stream.Size; // long docs may not display if MaxLength not set
RE.Lines.LoadFromStream(Stream);
end;
procedure RTFLoadFromString(const RE: TRichEdit; const RTF: string);
{Loads RTF code into an RTF control, replacing all the control's current
content.
@param RE [in] Rich edit control to receive RTF code.
@param RTF [in] String containing valid rich text code.
}
var
Stm: TStringStream; // stream onto RTF code
begin
// Load stream containing RTF code into control
Stm := TStringStream.Create(RTF);
try
RTFLoadFromStream(RE, Stm);
finally
Stm.Free;
end;
end;
procedure RTFSaveToStream(const RE: TRichEdit; const Stream: TStream);
{Saves RTF code from rich edit control into a stream.
@param RE [in] Rich edit control whose content is to be saved.
@param Stream [in] Stream that receives rich text code.
}
begin
RE.Lines.SaveToStream(Stream);
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.