scottacusj
scottacusj

Reputation: 31

Rich Text Box Not Allowing a Default font size of 11

I'm currently combining multiple Rich text objects, to create one larger document (A word Doc).In doing this I've encountered a problem where if i set the font size to 11, it doesn't get properly translated.Rich Text contains a FS(number) entry which indicates the font size, it then doubles it.

(In this example im assigning the Font size after assigning text, this doesn't matter and occurs if its set before or after as you are setting what appears to be the global FS property)

Dim masterFont = New System.Drawing.Font("Times New Roman", 11, FontStyle.Regular)
Dim RTB As New RichTextBox
RTB.Font = masterFon
RTB.AppendText("this is the start of a test")

Yeilds:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}
\viewkind4\uc1\pard\f0\fs23 this is the start of a test\par
}

In this case, the number is 23 which is 11.5 Doubled, when this gets Pasted, sure enough the Font size is 11.5

If i do the same thing with 10 or 13 (Testing odds and evens) it works as expected!!

Dim masterFont = New System.Drawing.Font("Times New Roman", 13, FontStyle.Regular)
Dim RTB As New RichTextBox
RTB.Font = masterFont
RTB.AppendText("this is the start of a test")

Yeilds

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}
\viewkind4\uc1\pard\f0\fs26 this is the start of a test\par
}

In this case, the font size is 26, 13 doubled.

Now, if i set the SelectionFont property to the MasterFont initially it will work however it pushes the Default Font size to the bottom (Adding extra linebreaks and potentially changing the font of any included documents)

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil Times New Roman;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs22 this is the start of a test\b  OF BOLD \b0 and this is After bold\f1\fs17\par
}

As the Font Size is doubled, there is some kind of conversion happening here and to that I've tried the Overload of all available Graphics Units and none of them matched!

Now, if i do a RichText.SelectAll (When the RTB object is ready to be pasted) and set the selection font, this works however it overrides formatting assigned to the combined paragraphs!

Im thinking there is a way to simply specify the exact FS Units however, through testing the Graphics Units, i did not see any FS11 So im really unsure if thats even possible.

Any help with this would be more than appreciated.

Upvotes: 3

Views: 2244

Answers (1)

jo0ls
jo0ls

Reputation: 389

I'm struggling with the same problem. The RTF spec says that the font size (fs) is in half points, which explains the doubling.

Another observation is that when you set the Font of the windows forms RichTextBox to size 11, then the SelectionFont will become a different value, and this depends on the dpi of your display. You get different RTF created at 96dpi to that at 120dpi if you just set the Font property.

The font size in points is converted to pixels. There are 72 points per inch. The dpi setting of your monitor (or device context) says how many pixels per inch.

At 120dpi, a size 11 font = 11 * 120/72 = 18.333 pixels. This is rounded down to 18 pixels. This corresponds to a point size of 18 * 72 / 120 = 10.8 points. This is 21.6 half points, and rounds up to 22. You get fs22 in the rtf.

At 96dpi, a size 11 font = 11 * 96/72 = 14.667 pixels. Rounded up to 15 pixels and backcalculates to 11.25. This is 22.5 half pixels, rounded up to 23. You get fs23 in the rtf.

If you add a handler to the selectionchanged event of the RTB then you can see that the SelectionFont.Size takes this display-dependent font size.

I think that you have to be more specific, RichTextBox controls work with rich text which can have multiple fonts, colors, and objects embedded. To append text with a particular font append the text, then select it, then set the font of the selection. Don't rely on just setting the Font property.

On another note, if you show the Windows Forms FontDialog, then the font that is returned will also have this display-dependent font size.

And finally, if you want to just set up an empty RichTextBox with the font set to say, 11, then you might find that as soon as the user starts typing, the RTF has the 11.5 size. This is because you need to set the font of the insertion point, and you can't seem to do that from .Net. I've managed to do it using SendMessage to set the insertion point, and then setting the SelectionFont. The rtf will then start with the correct dpi-independent fs value:

Dim masterFont As New System.Drawing.Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point, 0)
' select insertion point on an empty rtb.
Dim result As IntPtr = SendMessage(RichTextBox1.Handle, &HB1, IntPtr.Zero, New IntPtr(-1)) 
RichTextBox1.Font = masterFont
RichTextBox1.SelectionFont = masterFont    

Upvotes: 1

Related Questions