How can I get the DirectWrite PadWrite sample to work?

929 views Asked by At

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:

Screenshot of PadWrite

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:

enter image description here

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
2

There are 2 answers

1
Daniel Earwicker On BEST ANSWER

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).

0
Adrian McCarthy On

To build with VS 2013, let Visual Studio convert the project and solution files. In Common.h delete these lines:

#if (_MSC_VER >= 1200) // want to use std::min and std::max
#undef min
#undef max
#define min(x,y) _cpp_min(x,y)
#define max(x,y) _cpp_max(x,y)
#endif

The compiler will also complain about unreachable code in a catch (...) block in EditableLayout::Clear. Simply comment out the one-line body of the catch block. I think there's some mixup between C++-style exceptions and SEH exceptions.