Change font type / size of RTF field

I have RTF text stored as a string where the user may have used one or more font types and sizes.

Is there a way to programmatically reformat it with a specified font type and / or size?

In my program RTF export the font(s) are set near the top of the document in a font table e.g.

{\fonttbl
{\f0\fswiss\fcharset0\fprq2 Arial;}
{\f1\fswiss\fcharset0\fprq2 Arial;}
{\f2\fswiss\fcharset0\fprq2 Arial Rounded MT Bold;}

So font 0 is swiss (sans-serif PS), \fcharset0: ANSI, \fprq2 proportionally spaced, font name: Arial

Then in the document body I have multiple constructs like this

{\pard\f0\fs20\cf1\b0\i0 \sb144 …

\pard: new para, font 0 (from font table), fs20: 10 point (RTF measures font sizes in half points), colour foreground 1 (references colour table similar to font table), bold off, italic off, \sb144: space before, 144 twips (a twip = 1/20 point, or 1/1440 inch).

So (I’m sure that some other members know more about this than me) it looks like changing the font name is relatively easy - just one change in the font table. Changing the size would mean finding and changing all the individual \fs entries.

More advanced RTF can use style sheets in which case changing font might be easier.

If you’ve not already found it, lots of good info at Rich Text Format (RTF) Specification, version 1.6

Thanks for that. I was hoping for a simple solution.

I have had a go at loading the RTF into a rich edit control, selecting all and changing it programmatically via the SelAttributes without success so far.

I ended up getting this working just now. The trick is how you load and retrieve the RTF from the rich edit control. I created a new function and procedure to do it.

function RichTextGet(EditControl: TRichEdit):string;
var
  SS: TStringStream;
begin
  SS := TStringStream.Create('');
  try
    EditControl.Lines.SaveToStream(SS);
    Result := SS.DataString;
  finally
    SS.Free;
  end;
end;

procedure RichTextSet(EditControl: TRichEdit; const RTF: string);
var
  SS: TStringStream;
begin
  SS := TStringStream.Create(RTF);
  try
    EditControl.Lines.LoadFromStream(SS);
  finally
    SS.Free;
  end;
end;