I'm trying to learn about the DirectWrite API so I can get a flexible solution to the problem of rendering text from any culture, and potentially use it to incorporate rich text editing into an application with a lot more control than the "canned" ways of editing/displaying text.
The PadWrite sample looks great, and shows off how well-designed the API is - this is Microsoft's own screenshot of how it should look:
The example text includes a line that starts "Mixed scripts:" In the source code all the sample text is specified by a single C++ wchar_t[]
literal, and the mixed scripts all display correctly in Visual Studio 2012's code editor. If I copy and paste them into ordinary Windows WordPad they also display correctly. And the Arabic right-to-left section has the correct caret-navigation behaviour in both of those editors.
But none of this works when I actually run the PadWrite sample. Instead I see this:
It almost looks like it's being converted into UTF-8 and then misinterpreted (which doesn't make sense because it's all being done with wchar_t
.)
I'm running Windows 8.1. What do I need to do to make PadWrite work as well as WordPad or Visual Studio 2012? Is it a defunct sample from a pre-release version of the API that never got updated?
NB. first warning sign was that PadWrite didn't even compile at first - in the files EditableLayout.cpp
and TextEditor.cpp
I had to add this after the #include
statements:
#undef max
#undef min
I got an answer on the MSDN forums. The problem is that since the sample was created, the Visual C++ compiler has been changed so that it no longer able to detect UTF-8 encoding without the UTF-8 signature being present, whereas Visual Studio still can. And the sample was create so the source files do not have the UTF-8 signature.
As a result, VS's code editor displays the characters correctly in the source file, but when the source is compiled the characters are misinterpreted.
So the solution is to use Save as, Save with Encoding... and then pick Unicode (UTF-8 with signature).