谁能解释一下?
下面的代码:
// - change font size to specified point(pt) size.
// - returns false if there is an error, or true otherwise.
bool GraphicsDirect2D::TextSetFontSize(int size)
{
if (size <= 0)
{
assert(0 && "Error: Invalid size parameter.");
return false;
}
// if size has not changed, return
float currentFontSizeDIP = m_pDWTextFormat->GetFontSize();
float currentFontSizePoints = ConvertDIPToPointSize(currentFontSizeDIP);
if (currentFontSizePoints == size)
return true;
// otherwise, create new object for new text size
IDWriteTextFormat *pNewTextFormat = NULL;
if (FAILED(m_pDWFactory->CreateTextFormat(
StringToWString(m_textCurrentSettings.strFont).c_str(),
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
ConvertPointSizeToDIP(size),
L"en-us",
&pNewTextFormat)))
{
assert(0 && "Error.");
return false;
}
{
IDWriteTextLayout *m_pDWTextLayout = NULL;
DWRITE_TEXT_METRICS tm;
if (FAILED(m_pDWFactory->CreateTextLayout(
L"Arial",
0,
pNewTextFormat,
100,
100,
&m_pDWTextLayout)))
{
Destroy();
assert(0 && "Error.");
return false;
}
if (FAILED(m_pDWTextLayout->GetMetrics(&tm)))
{
assert(0 && "Error.");
}
m_fontHeight = tm.height;
m_pDWTextLayout->Release();
}
// release the old object
if (m_pDWTextFormat)
{
m_pDWTextFormat->Release();
m_pDWTextFormat = NULL;
}
// and point to the new one
m_pDWTextFormat = pNewTextFormat;
m_textCurrentSettings.size = size;
return true;
}
This function is from a class I'm using to provide a graphics interface for Direct2D (and DirectWrite for text). The function replaces a font it is storing as the 'current' one to one of a new size, and updates a size variable for reference.
The problem is that if I pass in 16 to the function (for example), the font I am given is BIGGER than point size 16 when created using Direct2D. (Using WinGDI it produces my reference size of 16 on the screen.) The Win32 function is my benchmark, and the directD version creates a bigger font than the Win32 code.
我认为我需要将点大小输入转换为该函数,以便为 Direct2D CreateTextFormat() 函数提供 DIP 值。然后我创建一个长度为 0 的字符串的布局,以获取创建的默认字体的高度。这与我要求开始的内容不同......
我找不到任何足够详细地描述这一点的文档来解决这个问题。每个关于功能和结构的网站都只是模仿 Windows 网站关于结构内容的内容。(Grrrr !!)它实际上并没有说明文本度量函数输出是否为 DIP,我假设它是但看不到从另一种格式明显转换回 16!请帮忙 :)
谢谢