forked from mia/Aegisub
Fix memory leak in aegisub.text_extents on Windows
The old object returned by SelectObject has to be made active again before the DC is destroyed or it doesn't get deleted.
This commit is contained in:
parent
06367d5cd9
commit
a9019421f1
1 changed files with 18 additions and 15 deletions
|
@ -70,12 +70,12 @@ namespace Automation4 {
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
// This is almost copypasta from TextSub
|
// This is almost copypasta from TextSub
|
||||||
HDC thedc = CreateCompatibleDC(0);
|
auto dc = CreateCompatibleDC(nullptr);
|
||||||
if (!thedc) return false;
|
if (!dc) return false;
|
||||||
SetMapMode(thedc, MM_TEXT);
|
|
||||||
|
|
||||||
LOGFONTW lf;
|
SetMapMode(dc, MM_TEXT);
|
||||||
ZeroMemory(&lf, sizeof(lf));
|
|
||||||
|
LOGFONTW lf = {0};
|
||||||
lf.lfHeight = (LONG)fontsize;
|
lf.lfHeight = (LONG)fontsize;
|
||||||
lf.lfWeight = style->bold ? FW_BOLD : FW_NORMAL;
|
lf.lfWeight = style->bold ? FW_BOLD : FW_NORMAL;
|
||||||
lf.lfItalic = style->italic;
|
lf.lfItalic = style->italic;
|
||||||
|
@ -88,33 +88,36 @@ namespace Automation4 {
|
||||||
lf.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
|
lf.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
|
||||||
wcsncpy(lf.lfFaceName, agi::charset::ConvertW(style->font).c_str(), 31);
|
wcsncpy(lf.lfFaceName, agi::charset::ConvertW(style->font).c_str(), 31);
|
||||||
|
|
||||||
HFONT thefont = CreateFontIndirect(&lf);
|
auto font = CreateFontIndirect(&lf);
|
||||||
if (!thefont) return false;
|
if (!font) return false;
|
||||||
SelectObject(thedc, thefont);
|
|
||||||
|
auto old_font = SelectObject(dc, font);
|
||||||
|
|
||||||
std::wstring wtext(agi::charset::ConvertW(text));
|
std::wstring wtext(agi::charset::ConvertW(text));
|
||||||
SIZE sz;
|
|
||||||
if (spacing != 0 ) {
|
if (spacing != 0 ) {
|
||||||
width = 0;
|
width = 0;
|
||||||
for (auto c : wtext) {
|
for (auto c : wtext) {
|
||||||
GetTextExtentPoint32(thedc, &c, 1, &sz);
|
SIZE sz;
|
||||||
|
GetTextExtentPoint32(dc, &c, 1, &sz);
|
||||||
width += sz.cx + spacing;
|
width += sz.cx + spacing;
|
||||||
height = sz.cy;
|
height = sz.cy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
GetTextExtentPoint32(thedc, &wtext[0], (int)wtext.size(), &sz);
|
SIZE sz;
|
||||||
|
GetTextExtentPoint32(dc, &wtext[0], (int)wtext.size(), &sz);
|
||||||
width = sz.cx;
|
width = sz.cx;
|
||||||
height = sz.cy;
|
height = sz.cy;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEXTMETRIC tm;
|
TEXTMETRIC tm;
|
||||||
GetTextMetrics(thedc, &tm);
|
GetTextMetrics(dc, &tm);
|
||||||
descent = tm.tmDescent;
|
descent = tm.tmDescent;
|
||||||
extlead= tm.tmExternalLeading;
|
extlead = tm.tmExternalLeading;
|
||||||
|
|
||||||
DeleteObject(thedc);
|
SelectObject(dc, old_font);
|
||||||
DeleteObject(thefont);
|
DeleteObject(font);
|
||||||
|
DeleteObject(dc);
|
||||||
|
|
||||||
#else // not WIN32
|
#else // not WIN32
|
||||||
wxMemoryDC thedc;
|
wxMemoryDC thedc;
|
||||||
|
|
Loading…
Reference in a new issue