0

In my WinForms program, I have a rtf text. It contains this char '≈'. The problem is, that '≈' always has the font Cambria Math which is not supported by another part of the program. So i want to replace the font for this char to Arial since this font is supported. Therefore I try to do this:

for (int i = 0; i < this.rtfBox.Text.Length; i++)
{
      this.rtfBox.Select(i, 1);

      if (this.rtfBox.SelectionFont != null && this.rtfBox.SelectionFont.FontFamily.Name == "Cambria Math")
      {
            this.rtfBox.SelectionFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point, 2);
      }
}

Replacing the font works at first glance. The font of '≈' indeed changes but the problem occurs when I save this rtf and reload it. The '≈' suddenly becomes a captial 'H'. When I check the rtf itself, the char is indeed just an H.

Anything I can do?

HimmDawg
  • 97
  • 10
  • I did not have any trouble here… Copy the following inside a empty file with `.rtf` as extension, and open it with your `RichTextBox`: `{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset204{\*\fname Arial;}Arial CYR;}}\viewkind4\uc1\pard\lang1046\fs18 text in Arial: \u8776?\lang1033\f1\par}` – D.Kastier Jan 05 '22 at 13:01
  • @D.Kastier yes, your RTF works fine. But the problem occurs when you put it into the `RichTextBox` and then retrieve the RTF text again. Since the textbox "optimizes" its RTF automatically, it'll add Cambria Math to the fonttable and apply it to the rounding character. And that's what I don't want – HimmDawg Jan 05 '22 at 16:13
  • Sorry to hear… This `RTF` was generated on a `RichTextBox` (saved, read and saved again). I use a highly modified version of [this project](https://www.codeproject.com/Articles/868653/EXTENDED-Version-of-Extended-Rich-Text-Box-RichTex). – D.Kastier Jan 05 '22 at 18:53
  • Have you tried to read my `RTF`, save it and read again? — without doing any modifications. – D.Kastier Jan 05 '22 at 18:54
  • I have. The textbox will just add Cambria Math. But I finally found a solution. First I need to check, if the selected font is Cambria Math, then check if the character is indeed the rounding symbol. Then replace the font with the "Symbol" font (gdiCharset = 2) and replace the rounding symbol with `»`. It really doesn't make sense, but it works :) – HimmDawg Jan 06 '22 at 09:44
  • Well if it is working, then good for you! And nice to known about this `gdiCharset`. Unrelated to this question, but since you are working with `RichTextBox`, [here is some trick](https://stackoverflow.com/a/50956806/5734097) to fix a problem with images. – D.Kastier Jan 06 '22 at 11:22

0 Answers0