Underline is drawn outside of rectangle reported by DrawText with DT_CALCRECT

别说谁变了你拦得住时间么 提交于 2019-12-10 20:44:20

问题


Symptoms

I'm having an issue with the recent version 2.0 of the Lato font in its "Regular" variant. This issue doesn't appear with Lato 1.0.

It seems that the underline is drawn 1px below the rectangle reported by DrawText() with the DT_CALCRECT flag.

In the following screenshots the calculated rectangle is indicated by the blue background. I've added 10px to the right of this rectangle so you can see the discrepancy to the position where the underline is drawn.

Lato 2.0 - underline is incorrectly drawn outside of rectangle reported by DT_CALCRECT:

Lato 1.0 - underline is correctly drawn inside of rectangle reported by DT_CALCRECT:

MCVE

  • can be reproduced on Win 10 and Win 7 (Win 8 not tested)
  • Download and install the Lato 2.0 Regular font (Lato-Regular.ttf).
  • Create a dialog based MFC application using the wizard and replace the OnPaint() handler of the dialog with the sample code.

Sample code for OnPaint():

CPaintDC dc{ this };

CRect rect; GetClientRect( &rect );

LOGFONTW lf{};
wcscpy_s( lf.lfFaceName, L"Lato" );
lf.lfHeight = 20 * 10;  // tenths of a point
lf.lfWeight = FW_NORMAL;
lf.lfUnderline = TRUE;

CFont font;
VERIFY( font.CreatePointFontIndirect( &lf ) );

const int saved = dc.SaveDC();

dc.SelectObject( &font );
dc.SetBkMode( TRANSPARENT );
dc.SetTextColor( RGB( 0, 0, 0 ) );  

const CString text = L"Hello Lato";
const DWORD dtFlags = 0;

// Calculate the size required by the text and fill this rectangle
CRect textRect = rect;
dc.DrawText( text, textRect, dtFlags | DT_CALCRECT );
textRect.right += 10;
dc.FillSolidRect( 0, 0, textRect.right, textRect.bottom, RGB( 180, 180, 255 ) );

// Actually draw the text
dc.DrawTextW( text, rect, dtFlags );

if( saved )
    dc.RestoreDC( saved );

Question

Do you think this is an issue in my code, a bug in the OS or a bug in the font?

Unfortunately I can't just use Lato 1.0 as Lato 2.0 added support for many new languages my software needs to support.

来源:https://stackoverflow.com/questions/45694393/underline-is-drawn-outside-of-rectangle-reported-by-drawtext-with-dt-calcrect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!