Page 1 of 1

Saving Unicode Characters to RTF in OnSaveComponentToFile

Posted: Wed Jul 15, 2015 9:03 am
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?

Posted: Fri Jul 17, 2015 9:31 am
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)

Posted: Fri Jul 17, 2015 9:49 am
by nostra
Thanks, works like a charm