Beispiel #1
0
IFACEMETHODIMP CustomTextRenderer::DrawGlyphRun(
    _In_opt_ void* clientDrawingContext,
    FLOAT baselineOriginX,
    FLOAT baselineOriginY,
    DWRITE_MEASURING_MODE measuringMode,
    _In_ DWRITE_GLYPH_RUN const* glyphRun,
    _In_ DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
    IUnknown* clientDrawingEffect
    )
{
    HRESULT hr = S_OK;
    HRESULT hrColor = DWRITE_E_NOCOLOR;
    DWRITE_MATRIX* worldToDeviceTransform = nullptr;

    Microsoft::WRL::ComPtr<IDWriteColorGlyphRunEnumerator> colorLayers;
    hrColor = m_dwriteFactory2->TranslateColorGlyphRun(
        baselineOriginX,
        baselineOriginY,
        glyphRun,
        nullptr,
        measuringMode,
        worldToDeviceTransform,
        0,
        &colorLayers
        );

    if (hrColor != DWRITE_E_NOCOLOR)
    {
        Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solidBrush;

        for (;;){
        BOOL haveRun;
        DX::ThrowIfFailed(colorLayers->MoveNext(&haveRun));
        if (!haveRun)
            break;

            DWRITE_COLOR_GLYPH_RUN const* colorRun;
            DX::ThrowIfFailed(colorLayers->GetCurrentRun(&colorRun));

            ID2D1Brush* layerBrush;
            if (colorRun->paletteIndex == 0xFFFF)
            {
                layerBrush = m_outlineBrush.Get();
            }
            else
            {
                if (solidBrush == nullptr)
                {
                    DX::ThrowIfFailed(m_d2dDeviceContext->CreateSolidColorBrush(&colorRun->runColor, nullptr, &solidBrush));
                }
                else
                {
                    solidBrush->SetColor(colorRun->runColor);
                }
                layerBrush = solidBrush.Get();
            }

            m_d2dDeviceContext->DrawGlyphRun(
                D2D1::Point2(colorRun->baselineOriginX,colorRun->baselineOriginY),
                &colorRun->glyphRun,
                colorRun->glyphRunDescription,
                layerBrush,
                measuringMode
            );
        }
    }

    if (hrColor == DWRITE_E_NOCOLOR)
    {
        // Usual case: the run does not have any colored glyphs.
        m_d2dDeviceContext->DrawGlyphRun(
            D2D1::Point2(baselineOriginX,baselineOriginY), 
            glyphRun, 
            m_outlineBrush.Get(),
            measuringMode
            );
    }

    return hr;
}