Saving Unicode Characters to RTF in OnSaveComponentToFile

General TRichView support forum. Please post your questions here
Post Reply
nostra
Posts: 5
Joined: Thu Feb 16, 2012 12:38 pm

Saving Unicode Characters to RTF in OnSaveComponentToFile

Post by nostra »

I have a couple of controls (TEdit for example) inserted into a RichView Document and want to produce RTF containing text entered in those controls like this:

Code: Select all

procedure TRichForm.RichMemoSaveComponentToFile(Sender: TCustomRichView;
  Path: string; SaveMe: TPersistent; SaveFormat: TRVSaveFormat;
  var OutStr: string);
begin
 if SaveMe is TCustomEdit then
  OutStr := TCustomEdit(SaveMe).Text;
end;
Unfortunately if those controls contain special Unicode characters like ☒ (U+2612) they do not get converted correctly.

What is the best way of saving components with Unicode characters to RTF to preserve all possible characters?
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

The component saves the returned OutStr to RTF as it is, without any processing. For Unicode versions of Delphi, it simply typecasts it to AnsiString before saving.

So you need to encode text to RTF yourself.
You can do it using an undocumented function from RVFuncs unit:

Code: Select all

procedure TRichForm.RichMemoSaveComponentToFile(Sender: TCustomRichView; 
  Path: string; SaveMe: TPersistent; SaveFormat: TRVSaveFormat; 
  var OutStr: string); 
begin 
 if SaveMe is TCustomEdit then 
  OutStr := String(RVMakeRTFStrW(TCustomEdit(SaveMe).Text, Sender.Style.DefCodePage,
            rvrtfDuplicateUnicode in Sender.RTFOptions, False, False, False))
end;
Note: this code is valid only for Unicode versions of Delphi (starting from Delphi 2009)
nostra
Posts: 5
Joined: Thu Feb 16, 2012 12:38 pm

Post by nostra »

Thanks, works like a charm
Post Reply