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

General TRichView support forum. Please post your questions here
Post Reply
svinson
Posts: 9
Joined: Thu May 10, 2007 8:03 pm

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

Post 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
svinson
Posts: 9
Joined: Thu May 10, 2007 8:03 pm

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

Post 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.
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

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

Post by Sergey Tkachenko »

Default fonts for HTML are specified in the global singleton object RVDefaultLoadProperties (RVDefReadProps unit).

Specifically, this object has properties: DefaultFontName, DefaultMonoSpacedFontName.
svinson
Posts: 9
Joined: Thu May 10, 2007 8:03 pm

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

Post 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
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

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

Post 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).
svinson
Posts: 9
Joined: Thu May 10, 2007 8:03 pm

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

Post by svinson »

That was the fix! Thank you.
Post Reply