Some fonts like Microsoft’s Gabriola support a number of typographic features which allow users to control how they look, e.g by enabling some fancy stylistic text rendering. With DirectWrite we can do that by passing a IDWriteTypography object to IDWriteTextLayout::SetTypography method.
Set text layout typography with DirectWrite in an MFC application
We have not an MFC class which wraps IDWriteTypography but that’s not a problem. First, we can get a pointer to IDWriteFactory from a global AFX_GLOBAL_DATA structure. Further create an IDWriteTypography instance, add desired font feature(s) and finally, set the typography in a range of the text layout. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void CDirectWriteDemoView::_SetTextLayoutFontTypographicFeature( std::shared_ptr<CD2DTextLayout> spTextLayout, DWRITE_FONT_FEATURE_TAG featureTag, DWRITE_TEXT_RANGE textRange) { // get the global IDWriteFactory pointer IDWriteFactory* pIDWriteFactory = afxGlobalData.GetWriteFactory(); // create IDWriteTypography instance CComPtr<IDWriteTypography> spIDWriteTypography; HRESULT hr = pIDWriteFactory->CreateTypography(&spIDWriteTypography); ASSERT(SUCCEEDED(hr)); // add font feature DWRITE_FONT_FEATURE fontFeature; fontFeature.nameTag = featureTag; fontFeature.parameter = 1; spIDWriteTypography->AddFontFeature(fontFeature); // set the typography in a range of the text layout spTextLayout->Get()->SetTypography(spIDWriteTypography, textRange); } |
Later update
In Visual Studio 2012, IDWriteFactory instance has been moved in a D2D-specific structure of type _AFX_D2D_STATE that can be accessed by calling AfxGetD2DState.
So, if using Visual Studio 2012 or newer you have to replace
1 |
IDWriteFactory* pIDWriteFactory = afxGlobalData.GetWriteFactory(); |
with
1 |
IDWriteFactory* pDirectWriteFactory = AfxGetD2DState()->GetWriteFactory(); |
See also: Getting Direct2D, DirectWrite and WIC Factories in MFC.
Demo application
Download: MFC Support for DirectWrite Demo (Part 5).zip (1361)
Resources and related articles
- Marc Gregoire’s Blog: Introduction to DirectWrite
- MSDN: IDWriteTextLayout::SetTypography method
- MSDN: IDWriteTypography interface
- MSDN: DWRITE_FONT_FEATURE_TAG enumeration
- Microsoft typography: Gabriola
- Wikipedia: List of typographic features
- Codexpert blog: Direc2D and DirectWrite topics