Measuring direct2d text size using Directwrite

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/Ray1024

I. Overview

Recently using the DIRECT2D and Directwrite write engines, the text label control needs to be implemented in the engine. However, the size of the text label is not the best for us to specify, or else the text will appear in the label overflow, blank retention and so on, which greatly affect the aesthetics. This requires us to determine the size of the label when the text label is created, based on the font format of the text, the text layout, and so on, rather than artificially specifying the size, which is the adaptive function that implements the label size (the size of the label is set based on the width of the content, font, format, etc.) of the text.

However, to achieve the adaptive functionality of the label size, you need to measure the size of the direct2d text. However, how to measure the size of direct2d text, I found in the direct2d for a long time did not find the right method, in the search engine also found no ideal solution. Finally, the workaround is found in the MSDN documentation for Directwrite.

This article will show you how to use directwrite to measure the size of direct2d text.

Second, the solution

The following is a function I implemented for measuring the width and height of text, which is preceded by code:

Use IDWriteTextLayout to get the text size HRESULT gettextsize (const wchar* text, idwritetextformat* Ptextformat, d2d1_size_f& Size) {       HRESULT hr = S_OK;       idwritetextlayout* ptextlayout = NULL;         Create a text layout       hr = m_pdwritefactory->createtextlayout (text, wcslen (text), Ptextformat, 0.0f, 0.0f, &ptextlayout );         if (SUCCEEDED (HR))       {               //Get text size            dwrite_text_metrics textmetrics;            hr = Ptextlayout->getmetrics (&textmetrics);            Size = D2d1::sizef (Ceil (Textmetrics.widthincludingtrailingwhitespace), Ceil (Textmetrics.height));      }       Saferelease (&ptextlayout);       return hr;  }

As in the code above, the measurement text size process is as follows:

1. Creating IDWriteTextLayout objects with text and fonts

2. Get the text size information from the IDWriteTextLayout object that created the success Dwrite_text_metrics structure

3. Place the text size in the text information structure in the size of the parameter to return the text size

Third, in-depth analysis

The key tool in the code for measuring text size above is the IDWriteTextLayout interface in Directwrite. The IDWriteTextLayout interface represents a fully parsed and formatted block of text, simply a laid-back text. This interface has a member function idwritetextlayout::getmetrics (), we need the text size information can be obtained through this function. Highlight this function:

The Idwritetextlayout::getmetrics method describes the ability to    retrieve the overall metric of a formatted string. Syntax     virtual HRESULT getmetrics ([out]  dwrite_text_metrics *textmetrics); parameter textmetrics [out]         when this method returns, Contains the measured distance from the formatted text to the associated content. return value         

The function's function is simply to get the text information after the layout of the Idwritelayout object, using parameters to return the information we need.


About the parameters of the Dwrite_text_metrics type, which is a struct that is used to put out the text layout information. Let's look at its members:

Text-related information after layout struct Dwrite_text_metrics {         float left;//Format Text layout box The leftmost point         float top;//Format text layout box top point    float Widt H The width of the formatted text ignores the extra white space       float widthincludingtrailingwhitespace;//Format the width of the text, taking into account the trailing space at the end of each line       float height; The height of the formatted text, the height of the empty string is determined by the size of the default font line,         float layoutwidth;//The initial width of the layout, depending on whether the text is wrapped, it can be greater than or less than the width of the text content         float layoutheight; The initial height of the layout, depending on the length of the text, it can be greater than or less than the text content height         UINT32 maxbidireorderingdepth;//The maximum reorder count of any line of text used to calculate the maximum number of hit test boxes, If the layout has no bidirectional text or no text at all, the minimum level is 1         UINT32 linecount;//Total number of rows of text     };         

There are a lot of members in this structure, and they are very useful information. Once we understand the structure of dwrite_text_metrics, we know what the text layout information is. In addition to the text size, we can get the upper-left coordinate of the text, the total number of rows and so on, it seems we can learn a lot of text from this information.

Iv. Conclusion

The above introduction should give you a complete understanding of how to measure text size using Directwrite.

If there is any mistake, please correct me.

Measuring direct2d text size using Directwrite

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.