首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GDI+ --为什么MeasureString函数在快递字体中失败?

GDI+ --为什么MeasureString函数在快递字体中失败?
EN

Stack Overflow用户
提问于 2017-09-02 16:12:39
回答 1查看 521关注 0票数 1

我正在使用XE7。在几个项目中,我使用一个通用代码获取文本周围的边框(以像素为单位),使用GDI+ MeasureString函数:

代码语言:javascript
运行
复制
// 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作为错误消息返回,例如字体参数如下:

代码语言:javascript
运行
复制
pTextFont->Name   =  L"Courier";
pTextFont->Height = -16;

我最初认为这可能是一个字体回退问题。我尝试实现以下解决方案以获得回退字体:How to automatically choose most suitable font for different language?

然而,这并没有解决我的问题:

  1. 在上述情况下,返回的回退字体也是Courier,这意味着在某种程度上,我的系统认为这种字体是有效的(除非字体回退代码本身被破坏,但我认为并非如此)
  2. 对于用Delphi编写的另一个项目,我翻译了上述代码,并面临着完全相同的问题。
  3. GDI (而不是GDI+)似乎能够通过SelectObject和DrawText等函数使用这种字体(当用DrawText和DT_CALCRECT度量时,返回的rect似乎是有效的)
  4. 另一方面,GDI+似乎无法处理字体(在pFont中实例化的字体)。绘图功能也会失败。

有人能向我解释我做错了什么吗?

EN

Stack Overflow用户

发布于 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,要么提供一个替换字体。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46015756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档