0

I have a Windows rich text box control (RichTextBox) on my form. The box has Consolas monospaced font set. For most contents the box indeed renders it with the selected font. But when I load/set contents with Box-drawing characters, the box renders when with variable-width font (Segoe UI Symbol). What defies the very purpose of those characters.

Simple example:

private void InitializeComponent()
{
    // ...
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.richTextBox1.Font =
       new System.Drawing.Font("Consolas", 12F, System.Drawing.FontStyle.Regular,
                               System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.richTextBox1.Location = new System.Drawing.Point(45, 34);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(533, 318);
   this.richTextBox1.TabIndex = 0;
   this.richTextBox1.Text =
       "╔════════════════════════════════════════════════╗\n" +
       "║                 This is a test                 ║\n" +
       "╚════════════════════════════════════════════════╝";
    // ...
}

Results in:
enter image description here

If I copy-paste the contents to e.g. Microsoft Word, I can see that only this parts has the Consolas font:
This is a test
The rest (all the Box-drawing characters) has Segoe UI Symbol font.

What I can I do to prevent RichTextBox from replacing the font?

My search resulted only in this article:
https://devblogs.microsoft.com/math-in-office/richedit-font-binding/
But it says that Rich text box replaces font only for characters that are not supported by the font. I'm sure that Consolas does support Box-drawing characters.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • [How can a Label control display Japanese characters properly when the Font used doesn't support this language?](https://stackoverflow.com/a/51612395/7444103). In short, you have to send `EM_SETLANGOPTIONS` removing the default `IMF_AUTOFONT` IME option, otherwise the Font mappings set the user-interface's default Font for the Symbols -- I assume you have RichTextBox v. 50W (.NET Framework 4.7.2+ / .NET) – Jimi Feb 27 '23 at 11:39
  • To test the normal result, you could paste that text in WordPad (which uses MsftEdit itself) and see whether you can change the Font of those symbols -- The *managed* alternative, may be useful in case you don't have a Custom Control (almost mandatory when it comes to the RTB) is here: [Some Alt keys changes my RichTextBox font](https://stackoverflow.com/a/51451303/7444103) – Jimi Feb 27 '23 at 11:44
  • @Jimi Thanks for your response. Yes, indeed removing the `IMF_AUTOFONT` fixes the problem. But doesn't that actually show that the font replacement was inappropriate, as it imo shows that Consolas supports those characters? And I likely want to keep the `IMF_AUTOFONT`, as it might be useful sometimes (the font is user-configurable in my application). (Btw, indeed in WordWrap I cannot change font to Consolas for those characters) – Martin Prikryl Feb 27 '23 at 12:25
  • 1
    In case it's needed after, in relation to the managed version, you can most probably remove `RichTextBoxLanguageOptions.AutoFont` before you send formatted text to the Control, then set it back again (maybe set `RichTextBoxLanguageOptions.AutoKeyboard` before you remove `AutoFont`) – Jimi Feb 27 '23 at 12:48
  • @Jimi But I'm loading plain-text file. So I'd like it to correctly select the best font for the loaded file, if the configured font does not support some characters from the file (say Japanese). But stop it replacing the characters that the configured font actually supports. Why is it replacing the font for Box-drawing characters, when the font supports them? – Martin Prikryl Feb 27 '23 at 13:19
  • The fact that WordPad has the same *issue* can tell you that this is *systemic*, Font Mappings (followed blindly, you may say). You could take ownership of the [Text Object Model](https://learn.microsoft.com/en-us/windows/win32/controls/text-object-model), then you have the last say -- As a note, disabling `IMF_AUTOFONT` doesn't mean that the Control won't use Font substitutes when a different **language** is used, that still happens (better with `IMF_AUTOKEYBOARD` on) – Jimi Feb 27 '23 at 14:20
  • @Jimi It definitely behaves differently for Asian scripts with `IMF_AUTOFONT` than without. With the default settings, the script-specific fonts are used, while with disabled `IMF_AUTOFONT`, `Consolas` is used for everything. As far as I can tell, it technically displays the Asian scripts correctly (they are supported by Consolas), but imo the result is unreadable: https://i.stack.imgur.com/zcU77.png – But ok, that's a different problem. The Text Object Model API looks more complicated than what I'm willing to endeavor into :) Can you post an answer based on all your useful comments? – Martin Prikryl Feb 28 '23 at 12:11
  • Sorry for the delay. Quite busy. I'll see to post something tomorrow, so I can take a look at this issue, maybe something will come to mind -- As mentioned before, you could add a tool that allows to paste / insert *formatted text*, where you disable the IME auto-selector and then reactivate it right after – Jimi Feb 28 '23 at 23:45

0 Answers0