DirectWrite文字排版——字符串去尾
DirectWrite是 DirectX 家族中专门用来做文本处理的部分,主要配合Direct2D进行渲染工作。
设置溢出布局宽度的文本的剪裁选项。
语法
virtual HRESULT SetTrimming(
[in] const DWRITE_TRIMMING * trimmingOptions,
IDWriteInlineObject * trimmingSign
) = 0;
参数
- trimmingOptions [in]
-
文本剪裁选项。
- trimmingSign
-
应用程序定义的忽略符号。此参数可为 NULL。注意:我们想要用到省略号代替裁剪部分就需要用到这个参数。
返回值
如果该方法成功,则返回 S_OK。 否则,将返回错误代码。HRESULT.
(2)DWRITE_TRIMMING 结构 (SetTrimming函数的第一个参数)
指定用于溢出布局框的文本的剪裁选项。
语法
struct DWRITE_TRIMMING {
DWRITE_TRIMMING_GRANULARITY granularity;
UINT32 delimiter;
UINT32 delimiterCount;
};
成员
- granularity
-
一个值,指定用于对溢出布局框的文本进行剪裁的文本粒度。
enum DWRITE_TRIMMING_GRANULARITY {
DWRITE_TRIMMING_GRANULARITY_NONE, //不进行任何剪裁。文本流超出布局宽度。
DWRITE_TRIMMING_GRANULARITY_CHARACTER, //在字符群集边界处进行剪裁
DWRITE_TRIMMING_GRANULARITY_WORD //在字边界处进行剪裁
}; - delimiter
-
一个字符代码,用作指示要保留的文本部分开头的分隔符。对路径省略号最有用,其中分隔符为斜杠。
- delimiterCount
-
一个值,指示要后退的分隔符数量。
(3)DWriteFactory::CreateEllipsisTrimmingSign()方法
此方法可以生成表示省略号的 IDWriteInlineObject(此方法得到的IDWriteInlineObject可以作为SetTrimming方法的第二个参数)。
二、实现字符串去尾效果
1 // 字体格式1:不使用去尾 2 if (SUCCEEDED(hr)) 3 { 4 DWRITE_TRIMMING trim1; 5 IDWriteInlineObject* trim2 = NULL; 6 hr = m_pDWriteFactory->CreateTextFormat( L"Arial Black", NULL, DWRITE_FONT_WEIGHT_NORMAL, 7 DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20.0, L"en-us",&m_pTF1); 8 9 if (SUCCEEDED(hr)) 10 { // 换行模式(DWRITE_WORD_WRAPPING)设置为不换行 11 hr = m_pTF1->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); 12 } 13 14 if (SUCCEEDED(hr)) 15 { 16 trim1.granularity = DWRITE_TRIMMING_GRANULARITY_NONE; 17 trim1.delimiter = 1; 18 trim1.delimiterCount = 10; 19 hr = m_pTF1->SetTrimming(&trim1,trim2); 20 } 21 }
1 // 字体格式2:以字符为单位去尾 2 if (SUCCEEDED(hr)) 3 { 4 DWRITE_TRIMMING trim1; 5 IDWriteInlineObject* trim2 = NULL; 6 hr = m_pDWriteFactory->CreateTextFormat( L"Arial Black", NULL, DWRITE_FONT_WEIGHT_NORMAL, 7 DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20.0, L"en-us",&m_pTF2); 8 9 if (SUCCEEDED(hr)) 10 { // 换行模式(DWRITE_WORD_WRAPPING)设置为不换行 11 hr = m_pTF2->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); 12 } 13 14 if (SUCCEEDED(hr)) 15 { 16 trim1.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER; 17 trim1.delimiter = 1; 18 trim1.delimiterCount = 10; 19 hr = m_pTF2->SetTrimming(&trim1,trim2); 20 } 21 }
1 // 字体格式3:以单词为单位去尾 2 if (SUCCEEDED(hr)) 3 { 4 DWRITE_TRIMMING trim1; 5 IDWriteInlineObject* trim2 = NULL; 6 hr = m_pDWriteFactory->CreateTextFormat( L"Arial Black", NULL, DWRITE_FONT_WEIGHT_NORMAL, 7 DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20.0, L"en-us",&m_pTF3); 8 9 if (SUCCEEDED(hr)) 10 { // 换行模式(DWRITE_WORD_WRAPPING)设置为不换行 11 hr = m_pTF3->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); 12 } 13 14 if (SUCCEEDED(hr)) 15 { 16 trim1.granularity = DWRITE_TRIMMING_GRANULARITY_WORD; 17 trim1.delimiter = 1; 18 trim1.delimiterCount = 10; 19 hr = m_pTF3->SetTrimming(&trim1,trim2); 20 } 21 }
1 // 字体格式4:以字符为单位去尾,用省略号代替略去部分 2 if (SUCCEEDED(hr)) 3 { 4 DWRITE_TRIMMING trim1; 5 IDWriteInlineObject* trim2 = NULL; 6 hr = m_pDWriteFactory->CreateTextFormat( L"Arial Black", NULL, DWRITE_FONT_WEIGHT_NORMAL, 7 DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20.0, L"en-us",&m_pTF4); 8 9 if (SUCCEEDED(hr)) 10 { // 换行模式(DWRITE_WORD_WRAPPING)设置为不换行 11 hr = m_pTF4->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); 12 } 13 14 if (SUCCEEDED(hr)) 15 { // 16 m_pDWriteFactory->CreateEllipsisTrimmingSign(m_pTF4,&trim2); 17 } 18 19 if (SUCCEEDED(hr)) 20 { 21 trim1.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER; 22 trim1.delimiter = 1; 23 trim1.delimiterCount = 10; 24 hr = m_pTF4->SetTrimming(&trim1,trim2); 25 } 26 }
1 // 字体格式4:以字符为单位去尾,用省略号代替略去部分 2 if (SUCCEEDED(hr)) 3 { 4 DWRITE_TRIMMING trim1; 5 IDWriteInlineObject* trim2 = NULL; 6 hr = m_pDWriteFactory->CreateTextFormat( L"Arial Black", NULL, DWRITE_FONT_WEIGHT_NORMAL, 7 DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20.0, L"en-us",&m_pTF4); 8 9 if (SUCCEEDED(hr)) 10 { // 换行模式(DWRITE_WORD_WRAPPING)设置为不换行 11 hr = m_pTF4->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); 12 } 13 14 if (SUCCEEDED(hr)) 15 { 16 m_pDWriteFactory->CreateEllipsisTrimmingSign(m_pTF4,&trim2); 17 } 18 19 if (SUCCEEDED(hr)) 20 { 21 trim1.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER; 22 trim1.delimiter = 1; 23 trim1.delimiterCount = 10; 24 hr = m_pTF4->SetTrimming(&trim1,trim2); 25 } 26 }
想看源码的朋友可以点击此处下载,Demo源码是Direct2DTests目录下的DWriteStringTrimming文件。
出处:http://www.cnblogs.com/Ray1024/
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的作者及详细链接,否则作者将保留追究其法律责任。
欢迎大家学习、共享,如果文章中有错误或漏洞,请大家在评论区留言!!