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:

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); } 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

IDWriteFactory* pIDWriteFactory = afxGlobalData.GetWriteFactory(); 1 IDWriteFactory * pIDWriteFactory = afxGlobalData . GetWriteFactory ( ) ;

with

IDWriteFactory* pDirectWriteFactory = AfxGetD2DState()->GetWriteFactory(); 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 (1275)

Resources and related articles

Share this: Twitter

LinkedIn

StumbleUpon

Facebook

Reddit

More

Google

Email



Print

