{"id":289,"date":"2017-12-24T16:55:28","date_gmt":"2017-12-24T08:55:28","guid":{"rendered":"http:\/\/kogasa.moe\/?p=289"},"modified":"2018-04-29T13:41:50","modified_gmt":"2018-04-29T05:41:50","slug":"directwrite-%e6%96%87%e5%ad%97%e6%8f%8f%e8%be%b9","status":"publish","type":"post","link":"https:\/\/flandre-scarlet.moe\/blog\/289\/","title":{"rendered":"DirectWrite \u6587\u5b57\u63cf\u8fb9"},"content":{"rendered":"<p>\u76f4\u63a5\u7528 rendertarget \u7684 DrawText \u65b9\u6cd5\u663e\u793a\u6587\u5b57\u7684\u8bdd\u65e0\u6cd5\u4e3a\u6587\u5b57\u589e\u52a0\u63cf\u8fb9\uff0c\u7528\u8def\u5f84\u5bf9\u8c61\u914d\u5408\u5b57\u4f53\u5b57\u5f62\u8fdb\u884c\u663e\u793a\uff0c\u5e76\u5728\u5185\u90e8\u7528\u753b\u5237\u586b\u5145\u7684\u8bdd\u53ef\u4ee5\u8fbe\u5230\u63cf\u8fb9\u7684\u6548\u679c\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u9996\u5148\u521d\u59cb\u5316\u7684\u65f6\u5019\u521b\u5efa\u5b57\u4f53\uff0c\u8981\u663e\u793a\u4e2d\u6587\u7684\u8bdd\u9700\u8981\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\uff0c\u5426\u5219\u4f1a\u56e0\u4e3a\u627e\u4e0d\u5230\u5b57\u6a21\u800c\u663e\u793a\u4e3a&#8221;\u56d7&#8221;\uff0c\u8fd9\u91cc\u9009\u62e9\u4e86\u9ed1\u4f53\u4f5c\u4e3a\u6837\u4f8b\u3002<\/br>\u6e32\u67d3\u7684\u65f6\u5019\u6839\u636e\u8981\u663e\u793a\u7684\u5185\u5bb9\u83b7\u53d6\u5bf9\u5e94\u5b57\u6a21\uff0c\u5e76\u4ee5\u6b64\u521b\u5efa\u8def\u5f84\u5bf9\u8c61\uff0c\u521b\u5efa\u5b8c\u6210\u540e\u5c06\u5176\u7ed8\u5236\u5230\u5408\u9002\u7684\u4f4d\u7f6e\uff0c\u540c\u65f6\u7528\u6307\u5b9a\u989c\u8272\u586b\u5145\u5185\u90e8\u5373\u53ef\u3002<\/p>\n<p>\u9644\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid Init()\r\n{\r\n    ...\r\n\r\n    hr = DWriteCreateFactory(\r\n        DWRITE_FACTORY_TYPE_SHARED,\r\n        __uuidof(m_pDWriteFactory),\r\n        reinterpret_cast&lt;IUnknown **&gt;(&amp;m_pDWriteFactory)\r\n    );\r\n    IDWriteFontFile *pFontFiles{ nullptr };\r\n    hr = m_pDWriteFactory-&gt;CreateFontFileReference(\r\n        L&quot;simhei.ttf&quot;, NULL, &amp;pFontFiles\r\n    );\r\n    assert(SUCCEEDED(hr));\r\n    IDWriteFontFile *fontFileArray&#x5B;] = { pFontFiles };\r\n    hr = m_pDWriteFactory-&gt;CreateFontFace(\r\n        DWRITE_FONT_FACE_TYPE_TRUETYPE,\r\n        ARRAYSIZE(fontFileArray), \/\/ file count\r\n        fontFileArray,\r\n        0,\r\n        DWRITE_FONT_SIMULATIONS_NONE,\r\n        &amp;m_pFontFace\r\n    );\r\n    assert(SUCCEEDED(hr));\r\n    SafeRelease(&amp;pFontFiles);\r\n\r\n    ...\r\n}\r\n\r\nvoid DrawText(\r\n    const wchar_t * string,\r\n    unsigned int stringLength,\r\n    IDWriteTextFormat * textFormat,\r\n    const Rectangle &amp; layoutRect\r\n)\r\n{\r\n    UINT* pCodePoints = new UINT&#x5B;stringLength];\r\n    UINT16* pGlyphIndices = new UINT16&#x5B;stringLength];\r\n    ZeroMemory(pCodePoints, sizeof(UINT) * stringLength);\r\n    ZeroMemory(pGlyphIndices, sizeof(UINT16) * stringLength);\r\n    for (int i = 0; i &lt; stringLength; ++i)\r\n    {\r\n        pCodePoints&#x5B;i] = string&#x5B;i];\r\n    }\r\n    HRESULT hr;\r\n    \/\/ \u786e\u8ba4\u5b57\u5f62\u662f\u5426\u5b58\u5728\r\n    hr = m_pFontFace-&gt;GetGlyphIndicesW(pCodePoints, stringLength, pGlyphIndices);\r\n    \/\/ \u5c06\u6587\u5b57\u63cf\u8fb9\u50a8\u5b58\u81f3\u8def\u5f84\u51e0\u4f55\u5bf9\u8c61, 18.0f \u4e3a\u5b57\u53f7\u5927\u5c0f\r\n    hr = m_pDirect2dFactory-&gt;CreatePathGeometry(&amp;m_pPathGeometry);\r\n    hr = m_pPathGeometry-&gt;Open(&amp;m_pGeometrySink);\r\n    hr = m_pFontFace-&gt;GetGlyphRunOutline(18.0f, pGlyphIndices,\r\n        NULL, NULL, stringLength, FALSE, FALSE, m_pGeometrySink\r\n    );\r\n    hr = m_pGeometrySink-&gt;Close();\r\n\r\n    IDWriteTextLayout* pTextLayout = NULL;\r\n    \/\/ \u83b7\u53d6\u6587\u672c\u5c3a\u5bf8 \r\n    hr = m_pDWriteFactory-&gt;CreateTextLayout(string, stringLength, textFormat, 0.0f, 0.0f, &amp;pTextLayout);\r\n    DWRITE_TEXT_METRICS textMetrics;\r\n    hr = pTextLayout-&gt;GetMetrics(&amp;textMetrics);\r\n    SafeRelease(&amp;pTextLayout);\r\n\r\n    D2D1_MATRIX_3X2_F old;\r\n    D2D1_RECT_F bounds;\r\n    m_pRenderTarget-&gt;GetTransform(&amp;old);\r\n    m_pPathGeometry-&gt;GetBounds(old, &amp;bounds);\r\n    m_pRenderTarget-&gt;SetTransform(D2D1::Matrix3x2F::Translation(\r\n        layoutRect.GetLeft() + (layoutRect.GetWidth() - bounds.right + bounds.left + textMetrics.left) \/ 2.0f,\r\n        layoutRect.GetBottom() - (layoutRect.GetHeight() - bounds.bottom + bounds.top) \/ 2.0f));\r\n    m_pRenderTarget-&gt;DrawGeometry(m_pPathGeometry, m_pBlackBrush, 1.0f);\r\n    m_pRenderTarget-&gt;FillGeometry(m_pPathGeometry, m_pWhiteBrush);\r\n    m_pRenderTarget-&gt;SetTransform(old);\r\n\r\n    SafeDeleteArray(&amp;pCodePoints);\r\n    SafeDeleteArray(&amp;pGlyphIndices);\r\n    SafeRelease(&amp;m_pGeometrySink);\r\n    SafeRelease(&amp;m_pPathGeometry);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f4\u63a5\u7528 rendertarget \u7684 DrawText \u65b9\u6cd5\u663e\u793a\u6587\u5b57\u7684\u8bdd\u65e0\u6cd5\u4e3a\u6587\u5b57\u589e\u52a0\u63cf\u8fb9\uff0c\u7528\u8def\u5f84\u5bf9\u8c61\u914d\u5408\u5b57\u4f53&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/flandre-scarlet.moe\/blog\/289\/\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,9,10,19],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-directx","tag-c","tag-directwrite","tag-directx","tag-outline"],"_links":{"self":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":0,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"wp:attachment":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}