Page 1 of 1

How to specify default font when printing HTML with TRichView v21.0

Posted: Thu Mar 02, 2023 10:00 pm
by svinson
I have updated to the latest TRichView v21.0. I have two components dropped into my Delphi form:
[list]RichView1
RVStyle1[/list]

I then set RichView1.Style to RVStyle1

I then have the following function to display HTML:

[code]procedure TMyForm.renderHtml( const htmlMsg : string );
var s : TStringStream;
begin
s := TStringStream.Create( htmlMsg );
try
RichView1.Clear;
RichView1.LoadHTMLFromStream( s );
RichView1.Format;
finally
s.Free;
end;
end;[/code]

My problem is that the HTML is showing in what appears to be Times Roman font.

Using Delphi's Object Inspector, I review what is in RVStyle1.TextStyles and see this list of TFontInfo instances: 0 - Normal text, 1 - Heading, 2 - Subheading, 3 - Keywords, 4 - Jump 1, and 5 - Jump 2

Looking into each of these TFontInfo instances, they all have FontName = 'Arial'. So I do not understand why Arial is not being used. As I said above, the HTML looks like it is using font "Times Roman".

Reading a variety of TRichView support posts, I see that the following needs to be performed, although what this code does is simply set the FontName for each style to "Arial" which is what I already see in the object inspector:

[code]procedure TFontTestForm.FontReplace(rv:TRichView);
// Ref: https://richedit.com/support/trichview. ... ment_3.htm
var i:Integer;
RVEStyle: TRVStyle;
begin
RVEStyle := rv.Style;
for i := 0 to RVEStyle.TextStyles.Count-1 do
RVEStyle.TextStyles[i].FontName := 'Tahoma';
rv.Format;
end;[/code]

So, even after calling FontReplace(RichView1) in my FormCreate method, I am still seeing my HTML printed in Times Roman when calling renderHtml(). For example, I tried this:

[code]renderHtml( 'Hello, <b>this</b> <u>is</u> a <i>senseless</i> <font color="red">sentence</font>.');[/code]

Then I thought I needed to specify the font style ("Normal text"), so I tried this:

[code]renderHtml( '<font style="Normal text">' +
'Hello, <b>this</b> <u>is</u> a <i>senseless</i> <font color="red">sentence</font>.'
+ '</font>'
);[/code]

Please help. This should be a simple task, but I do not know what I am doing wrong.

Thank you,

- SV

Re: How to specify default font when printing HTML with TRichView v21.0

Posted: Thu Mar 02, 2023 10:04 pm
by svinson
Note: in my code sample above, I temporarily tried changing the font to "Tahoma", simply to see if I could induce any changes. My original intent is to use "Arial". I just want to learn how to set the font to something OTHER than the default Times Roman font.

Re: How to specify default font when printing HTML with TRichView v21.0

Posted: Fri Mar 03, 2023 7:50 am
by Sergey Tkachenko
Default fonts for HTML are specified in the global singleton object RVDefaultLoadProperties (RVDefReadProps unit).

Specifically, this object has properties: DefaultFontName, DefaultMonoSpacedFontName.

Re: How to specify default font when printing HTML with TRichView v21.0

Posted: Fri Mar 03, 2023 6:07 pm
by svinson
Based on your response, I searched your source code to see RVDefReadProps.pas in the TRichView\Source folder. Looking in that unit, I see the setter property DefaultFontName. With the parent class being the singleton that you mentioned, I see in the Initialization section of that unit the code instantiates RVDefaultLoadProperties. So far, so good.

So, I added RVDefReadProps to my USES list. Then in my FormCreate method, I added this line:

RVDefaultLoadProperties.DefaultFontName := 'Arial';

Unfortunately, the HTML text in my RichView1 panel has not changed. It is still being shown in what looks like Times-Roman font.

There are other setters in that class -- one for mono font as you mentioned, another for bulletizing, etc. etc. I only made use of DefaultFontName -- do I need to set anything else?

Sorry if I'm missing something else. Let me know.

Thanks,

Steve

Re: How to specify default font when printing HTML with TRichView v21.0

Posted: Fri Mar 03, 2023 7:53 pm
by Sergey Tkachenko
This is a bug, sorry.
A fix is modifying RVHTMLTypes.pas. Change TRVHTMLStylesInfo.SetFontFamiliesDef to:

Code: Select all

procedure TRVHTMLStylesInfo.SetFontFamiliesDef;
begin
  FFontFamilies.Clear;
end;
Here is my test:

Code: Select all

procedure AddHTML(rv: TCustomRichView; const S: UnicodeString);
var
  Stream: TMemoryStream;
  S2: UTF8String;
begin
  S2 := UTF8Encode(S);
  Stream := TMemoryStream.Create;
  Stream.WriteBuffer(PAnsiChar(S2)^, Length(S2));
  Stream.Position := 0;
  rv.LoadHTMLFromStream(Stream, '', CP_UTF8);
  Stream.Free;
end;

procedure TForm3.ToolButton12Click(Sender: TObject);
begin
  RichViewEdit1.Clear;
  RichViewEdit1.StyleTemplateInsertMode := rvstimUseSourceFormatting;
  RVDefaultLoadProperties.DefaultFontName := 'Arial';
  AddHTML(RichViewEdit1, 'Hello <b>world</b>!');
  RVDefaultLoadProperties.DefaultFontName := 'Times New Roman';
  AddHTML(RichViewEdit1, 'Hello <b>world</b>!');
  RVDefaultLoadProperties.DefaultFontName := 'Comic Sans MS';
  AddHTML(RichViewEdit1, 'Hello <b>world</b>!');
  RichViewEdit1.Format;
end;
Please note that I assigned RichViewEdit1.StyleTemplateInsertMode := rvstimUseSourceFormatting.
With the default value, if RichViewEdit1.UseStyleTemplates = True, TRichView converts HTML formatting to the formatting specified in its StyleTemplates (it happens when loading HTML in non-empty editor; TRichView considers it as an insertion and applies StyleTemplateInsertMode).

Re: How to specify default font when printing HTML with TRichView v21.0

Posted: Fri Mar 03, 2023 11:31 pm
by svinson
That was the fix! Thank you.

Re: How to specify default font when printing HTML with TRichView v21.0

Posted: Fri Mar 10, 2023 12:38 pm
by Sergey Tkachenko
Fixed in TRichView v21.1