DirectWrite-无法使用多种系统字体,例如“脚本”

时间:2018-12-17 14:18:45

标签: fonts font-awesome-5 directwrite

使用RAD Studio XE7编译器,我试图使用DirectWrite和提供的VCL TDirect2DCanvas绘制文本。

只要我使用属于Segoe家族的字体,文本绘图就可以很好地工作。但是,DirectWrite似乎无法使用Script字体等几种字体,而当我尝试使用它们时会回退到另一种字体。

当我尝试使用GDI绘制完全相同的文本时,很好地使用了Script字体。我无法弄清楚自己在做什么。有人可以向我解释如何在DirectWrite中使用诸如Script字体之类的字体吗?

这是我当前使用DirectWrite绘制文本的代码:

void __fastcall TMainForm::btDirectWriteClick(TObject *Sender)
{
    const std::wstring text = L"Test        ";

    HDC hDC = ::GetDC(Handle);

    std::auto_ptr<TCanvas> pGDICanvas(new TCanvas());
    pGDICanvas->Handle = hDC;

    pGDICanvas->Brush->Color = clWhite;
    pGDICanvas->Brush->Style = bsSolid;
    pGDICanvas->FillRect(TRect(0, 0, ClientWidth, ClientHeight));

    ::D2D1_RECT_F textRect;
    textRect.left   = 10;
    textRect.top    = 10;
    textRect.right  = ClientWidth  - 10;
    textRect.bottom = ClientHeight - 10;

    std::unique_ptr<TDirect2DCanvas> pCanvas(new TDirect2DCanvas(hDC, TRect(0, 0, ClientWidth, ClientHeight)));

    // configure Direct2D font
    pCanvas->Font->Size        = 40;
    //pCanvas->Font->Name        = L"Segoe Script"; // this works...
    pCanvas->Font->Name        = L"Script";         // ... but this not!
    pCanvas->Font->Orientation = 0;
    pCanvas->Font->Pitch       = System::Uitypes::TFontPitch::fpVariable;
    pCanvas->Font->Style       = TFontStyles();

    // get DirectWrite text format object
    _di_IDWriteTextFormat pFormat = pCanvas->Font->Handle;

    // found it?
    if (!pFormat)
        return;

    pCanvas->RenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE);

    ::D2D1_COLOR_F color;
    color.r = 0.0f;
    color.g = 0.0f;
    color.b = 0.0f;
    color.a = 1.0f;

    ::ID2D1SolidColorBrush* pBrush = NULL;

    // create solid color brush, use pen color if rect is completely filled with outline
    pCanvas->RenderTarget->CreateSolidColorBrush(color, &pBrush);

    // found it?
    if (!pBrush)
        return;

    // set horiz alignment
    pFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);

    // set vert alignment
    pFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);

    // set reading direction
    pFormat->SetReadingDirection(DWRITE_READING_DIRECTION_LEFT_TO_RIGHT);

    // set word wrapping mode
    pFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);

    IDWriteInlineObject* pInlineObject = NULL;

    ::DWRITE_TRIMMING trimming;
    trimming.delimiter      = 0;
    trimming.delimiterCount = 0;
    trimming.granularity    = DWRITE_TRIMMING_GRANULARITY_NONE;

    // set text trimming
    pFormat->SetTrimming(&trimming, pInlineObject);

    pCanvas->BeginDraw();

    pCanvas->RenderTarget->DrawText(text.c_str(), text.length(), pFormat, textRect, pBrush);

    pCanvas->EndDraw();
}

作为一个额外的问题,我也非常有兴趣在我的应用中使用自定义字体,例如Font-Awesome 5(https://fontawesome.com/)。如何使用DirectWrite绘制文本?

1 个答案:

答案 0 :(得分:1)

如果您的意思是Windows 10中称为“脚本标准”的字体,据我所知它的文件为script.fon。 DirectWrite不支持这种位图字体,您可以通过枚举系统字体集合来判断,该字体将不存在。

关于自定义字体,msdn https://docs.microsoft.com/en-us/windows/desktop/directwrite/custom-font-collections上有一个操作方法。