我正在使用XE7。在几个项目中,我使用一个通用代码获取文本周围的边框(以像素为单位),使用GDI+ MeasureString函数:
// configure the font to use. NOTE be careful, the SVG font size matches with the
// GDI font HEIGHT property, and not with the font SIZE
std::auto_ptr<TFont> pTextFont(new TFont());
pTextFont->Name = fontFamily;
pTextFont->Height = -fontSize;
std::auto_ptr<Gdiplus::Font> pFont(new Gdiplus::Font(pCanvas->Handle, pTextFont->Handle));
// char range is required to extract the first char bounds
Gdiplus::CharacterRange charRange;
charRange.First = 0;
charRange.Length = 1;
// configure the text format
std::auto_ptr<Gdiplus::StringFormat> pTextFormat(new Gdiplus::StringFormat());
pTextFormat->SetAlignment(Gdiplus::StringAlignmentNear);
pTextFormat->SetLineAlignment(Gdiplus::StringAlignmentNear);
pTextFormat->SetFormatFlags(Gdiplus::StringFormatFlagsNoWrap);
pTextFormat->SetTrimming(Gdiplus::StringTrimmingNone);
pTextFormat->SetMeasurableCharacterRanges(1, &charRange);
Gdiplus::RectF boundingBox;
Gdiplus::RectF viewBoxF(viewBox);
// measure the rect surrounding the text
if (pGraphics->MeasureString(text.c_str(), text.length(), pFont.get(), viewBoxF,
pTextFormat.get(), &boundingBox) != Gdiplus::Ok)
{
// error is handled here
}
此函数在几种情况下运行良好,但有时MeasureString会莫名其妙地失败,将InvalidParameter作为错误消息返回,例如字体参数如下:
pTextFont->Name = L"Courier";
pTextFont->Height = -16;
我最初认为这可能是一个字体回退问题。我尝试实现以下解决方案以获得回退字体:How to automatically choose most suitable font for different language?
然而,这并没有解决我的问题:
有人能向我解释我做错了什么吗?
发布于 2017-09-16 13:45:07
与我所想的相反,GDI+不支持所有已安装的字体,如DrawString函数备注(https://msdn.microsoft.com/en-us/library/windows/desktop/ms535991(v=vs.85).aspx:
请注意,GDI+不支持没有TrueType大纲的PostScript字体或OpenType字体。
看起来,信使字体属于那种不能与GDI+一起使用的字体。因此,如果我的绘图在GDI+中失败了,我需要要么使用GDI,要么提供一个替换字体。
https://stackoverflow.com/questions/46015756
复制相似问题