0

My goal is to let users set specific font on selection in the RichEdit control.

So I do it as such in code:

#define SIZEOF(f) (sizeof(f)/sizeof(f[0]))

CString m_strName = L"Script";

CHARFORMAT2 cf3;
memset(&cf3, 0, sizeof(cf3));
cf3.cbSize = sizeof(cf3);
cf3.dwMask = CFM_FACE | CFM_CHARSET;

//Set type face
memcpy(cf3.szFaceName, m_strName.GetString(), min(SIZEOF(cf3.szFaceName), m_strName.GetLength() + 1) * sizeof(TCHAR));
cf3.szFaceName[SIZEOF(cf3.szFaceName) - 1] = 0;

//Both m_nCharSet and m_nPitchAndFamily are taken from EnumFontFamiliesEx() API
cf3.bCharSet = m_nCharSet;
cf3.bPitchAndFamily = m_nPitchAndFamily;

//pRichEdit is CWnd for RichEdit box of class RichEdit50W
if(pRichEdit->SendMessage(EM_SETCHARFORMAT, SCF_SELECTION | SCF_USEUIRULES, (LPARAM)&cf3))
{
    bRes = TRUE;
}

This works on my Windows 8.1 desktop, but on my test Windows 7 machine the following happens:

  • If I first select "Script" font, it will render it correctly:

enter image description here

  • But then if I close and then reopen the app and first select "WingDings", it will also render correctly:

enter image description here

and then if I switch font selection immediately to "Script" I get this:

enter image description here

Which is incorrect.

So I'm wondering what am I doing wrong there?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • I don't understand, what `SCF_USEUIRULES` is for. Since you are using it, maybe you do? – IInspectable Jun 04 '16 at 01:43
  • @IInspectable: I got it from here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb774230(v=vs.85).aspx So no, I don't know what it does. Unfortunately removing it doesn't change anything. – c00000fd Jun 04 '16 at 02:28
  • remove `CFM_CHARSET` flag, remove `bCharSet` and `bPitchAndFamily`. This next part doesn't relate to your error, but copy the font name like this `wcscpy_s(format.szFaceName, LF_FACESIZE, fontname)` – Barmak Shemirani Jun 04 '16 at 04:22
  • @BarmakShemirani: Thanks for `wcscpy_s` suggestion. As for removing `CFM_CHARSET` flag with `bCharSet` and `bPitchAndFamily` if I do so and try to set font to `"Script"` it comes out looking like `"Arial"` so that's not the way to do it. – c00000fd Jun 04 '16 at 09:50
  • 1
    @IInspectable: Oh wow, guys, I just realized it might be a bug in RichEdit. See if you can repro. On Win7 open WordPad and type "Hello world". Then select all, and change font to "Wingdings". Then without unselecting it, try to change font to "Script". The Wingdings symbols won't change. Bug 1. Then try to change font to "Symbol". It will change. Then change it to "Roman" and it will look like Wingdings symbols again. Bug 2. Is it happening just on my end? – c00000fd Jun 04 '16 at 10:59
  • Charset is old and so is "Script" font. Try the same thing with Greek text, example ελληνικά. – Barmak Shemirani Jun 04 '16 at 17:20
  • Tested the sequence you outlined above in Windows 10 64-bit. All font changes have the expected result in Windows 10. So possibly a bug in the Rich Edit Control that has been fixed in Windows 8/8.1/10. – IInspectable Jun 04 '16 at 17:24
  • @IInspectable: No, it works since Windows 8.1. This seems to happen only on Windows 7. As for text itself, I don't think it depends on it. – c00000fd Jun 04 '16 at 19:28
  • Script font is OEM character set. There are other newer script font. If you insist on Script font then it has to be converted properly, and it can't properly handle Unicode. – Barmak Shemirani Jun 05 '16 at 02:09
  • @BarmakShemirani: Yes I know. For my specs I needed scripts backward compatible to Windows XP. – c00000fd Jun 05 '16 at 04:38

0 Answers0