本文整理汇总了C++中IDWriteTextLayout::GetLineMetrics方法的典型用法代码示例。如果您正苦于以下问题:C++ IDWriteTextLayout::GetLineMetrics方法的具体用法?C++ IDWriteTextLayout::GetLineMetrics怎么用?C++ IDWriteTextLayout::GetLineMetrics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDWriteTextLayout
的用法示例。
在下文中一共展示了IDWriteTextLayout::GetLineMetrics方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getGlyphLayout
//.........这里部分代码省略.........
dwTextFormat->SetTextAlignment (DWRITE_TEXT_ALIGNMENT_CENTER);
// DirectWrite cannot justify text, default to left alignment
if (text.getTextAlignment() == AttributedString::justified)
dwTextFormat->SetTextAlignment (DWRITE_TEXT_ALIGNMENT_LEADING);
// Set Word Wrap
if (text.getWordWrap() == AttributedString::none)
dwTextFormat->SetWordWrapping (DWRITE_WORD_WRAPPING_NO_WRAP);
if (text.getWordWrap() == AttributedString::byWord)
dwTextFormat->SetWordWrapping (DWRITE_WORD_WRAPPING_WRAP);
// DirectWrite does not support wrapping by character, default to wrapping by word
if (text.getWordWrap() == AttributedString::byChar)
dwTextFormat->SetWordWrapping (DWRITE_WORD_WRAPPING_WRAP);
// DirectWrite does not automatically set reading direction
// This must be set correctly and manually when using RTL Scripts (Hebrew, Arabic)
if (text.getReadingDirection() == AttributedString::rightToLeft)
dwTextFormat->SetReadingDirection (DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
IDWriteTextLayout* dwTextLayout = nullptr;
hr = dwFactory->CreateTextLayout (
text.getText().toWideCharPointer(),
text.getText().length(),
dwTextFormat,
glyphLayout.getWidth(),
glyphLayout.getHeight(),
&dwTextLayout
);
// Character Attributes
int numCharacterAttributes = text.getCharAttributesSize();
for (int i = 0; i < numCharacterAttributes; ++i)
{
Attr* attr = text.getCharAttribute (i);
// Character Range Error Checking
if (attr->range.getStart() > text.getText().length()) continue;
if (attr->range.getEnd() > text.getText().length()) attr->range.setEnd (text.getText().length());
if (attr->attribute == Attr::font)
{
AttrFont* attrFont = static_cast<AttrFont*>(attr);
DWRITE_TEXT_RANGE dwRange;
dwRange.startPosition = attrFont->range.getStart();
dwRange.length = attrFont->range.getLength();
dwTextLayout->SetFontFamilyName (attrFont->font.getTypefaceName().toWideCharPointer(), dwRange);
// We multiply the font height by the size factor so we layout text at the correct size
const float fontHeightToEmSizeFactor = getFontHeightToEmSizeFactor (attrFont->font, *dwFontCollection);
dwTextLayout->SetFontSize (attrFont->font.getHeight() * fontHeightToEmSizeFactor, dwRange);
}
if (attr->attribute == Attr::foregroundColour)
{
AttrColour* attrColour = static_cast<AttrColour*>(attr);
DWRITE_TEXT_RANGE dwRange;
dwRange.startPosition = attrColour->range.getStart();
dwRange.length = attrColour->range.getLength();
ID2D1SolidColorBrush* d2dBrush = nullptr;
d2dDCRT->CreateSolidColorBrush (D2D1::ColorF (D2D1::ColorF(attrColour->colour.getFloatRed(),
attrColour->colour.getFloatGreen(), attrColour->colour.getFloatBlue(),
attrColour->colour.getFloatAlpha())), &d2dBrush);
// We need to call SetDrawingEffect with a legimate brush to get DirectWrite to break text based on colours
dwTextLayout->SetDrawingEffect (d2dBrush, dwRange);
safeRelease (&d2dBrush);
}
}
UINT32 actualLineCount = 0;
hr = dwTextLayout->GetLineMetrics (nullptr, 0, &actualLineCount);
// Preallocate GlyphLayout Line Array
glyphLayout.setNumLines (actualLineCount);
HeapBlock <DWRITE_LINE_METRICS> dwLineMetrics (actualLineCount);
hr = dwTextLayout->GetLineMetrics (dwLineMetrics, actualLineCount, &actualLineCount);
int location = 0;
// Create GlyphLine structures for each line in the layout
for (UINT32 i = 0; i < actualLineCount; ++i)
{
// Get string range
Range<int> lineStringRange (location, (int) location + dwLineMetrics[i].length);
location = dwLineMetrics[i].length;
GlyphLine* glyphLine = new GlyphLine();
glyphLine->setStringRange (lineStringRange);
glyphLayout.addGlyphLine (glyphLine);
}
// To copy glyph data from DirectWrite into our own data structures we must create our
// own CustomTextRenderer. Instead of passing the draw method an actual graphics context,
// we pass it the GlyphLayout object that needs to be filled with glyphs.
CustomDirectWriteTextRenderer* textRenderer = nullptr;
textRenderer = new CustomDirectWriteTextRenderer();
hr = dwTextLayout->Draw (
&glyphLayout,
textRenderer,
glyphLayout.getX(),
glyphLayout.getY()
);
safeRelease (&textRenderer);
safeRelease (&dwTextLayout);
safeRelease (&dwTextFormat);
safeRelease (&d2dDCRT);
safeRelease (&d2dFactory);
safeRelease (&dwFontCollection);
safeRelease (&dwFactory);
}