TRichView - Replace Format (Font,Style,Color etc.)

General TRichView support forum. Please post your questions here
Post Reply
a.weber
Posts: 63
Joined: Wed Mar 02, 2022 7:02 am

TRichView - Replace Format (Font,Style,Color etc.)

Post by a.weber »

Hello,
if we insert one RTF/DOCX into another - the inserted rtf should inherit the font, size, and style of the surrounding document. For this I have implemented this piece of code.
- First we are loading the piece which will be inserted into a TRichtViewEditor instance.

- instead of useing global variables for the FParent* I would prefer it to make a class with the required values and pass this through
userdata in ApplyStyleConversion(...) but currently userdata is just an Integer - so it will work only for Win32 to cast the class into Integer,
but for Win64 -it won't work that way - are there any plans to change such parameters to NativeInt in the near future?

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
   // Fake surrounding Font of the target RTF
   FParentFontName  := 'Segoe UI';
   FParentFontColor := clBlack;
   FParentFontBGColor := clNone;
   FParentFontSize  := 14;
   FParentFontStyle := [];

  rve.OnStyleConversion := ReplaceTextStyleConversion;
  try
    rve.SelectAll;
    rve.ApplyStyleConversion(0{only int32});
    rve.RVData.DeleteUnusedStyles(true, true, true);
  finally
     rve.OnStyleConversion := nil;
  end;
  rve.SaveRTF('d:\insertmemo.rtf',false);
end;

procedure TForm1.ReplaceTextStyleConversion(Sender: TCustomRichViewEdit; StyleNo, UserData: Integer; AppliedToText: Boolean;
                                     var NewStyleNo: Integer);
var
  FontInfo: TFontInfo;
begin
  FontInfo := TFontInfo.Create(nil);
  try
    // copy fontinfo from the this..
    FontInfo.Assign(rvs.TextStyles[StyleNo]);

    // replace font if its still the memo default font
    if SameText(FontInfo.FontName, DefMemoFontName) then
       FontInfo.FontName := FParentFontName;

    FontInfo.Size := MulDiv( FParentFontSize, FontInfo.Size, DefMemoFontSize );

    if FontInfo.Style = DefMemoFontStyle then
       FontInfo.Style := FParentFontStyle;

    if FontInfo.Color = DefMemoFontColor then
       FontInfo.Color := FParentFontColor;

    if FontInfo.BackColor = DefMemoFontBGColor then
       FontInfo.BackColor := FParentFontBGColor;

    NewStyleNo := rvs.FindTextStyle(FontInfo);
  finally
     FontInfo.Free;
  end;
end;
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: TRichView - Replace Format (Font,Style,Color etc.)

Post by Sergey Tkachenko »

First, consider using the built-in features.

1) If real named styles (StyleTemplates) are used, when you insert RTF or DocX, the formatting of the existing named styles is used instead of the formatting of the inserted document (providing that RichViewEdit.StyleTemplateInsertMode = rvstimUseTargetFormatting).
For example, if in the existing document, StyleTemplate "Normal" is "Tahoma, 12", then all text of "Normal" style will be inserted as "Tahoma, 12", regardless of its original formatting (only formatting applied on top of named style will be kept)

2) If you do not use StyleTemplates, check properties of RichViewEdit.RTFReadProperties: TextStyleMode and ParaStyleMode.
If rvrsUseSpecified, all inserted text will have the specified TextStyle/ParaStyle.
If rvrsUseClosest, the most similar existing TextStyle/ParaStyle will be used.

---

No, I do not plan to change the parameter type, to avoid compatibility problems in older projects.
Post Reply