WaldB
WaldB

Reputation: 1261

Why does the function GetOutlineTextMetrics() returns 0 for a non-existent font facename?

I want to know which "FamilyName" was used by Windows to create a font with an unknown facename "Blah".

As you can see below, the font is created normally (WM_PAINT uses it to print the return value of the function GetOutlineTextMetric().

#include <windows.h>
#include <iostream>

LRESULT CALLBACK WndProc(HWND, UINT, UINT, LONG);

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pszCmdLine, int nCmdShow)
{
    WNDCLASSEX  wndclassx;

    wndclassx.cbSize        = sizeof(WNDCLASSEX);
    wndclassx.style         = CS_HREDRAW | CS_VREDRAW;
    wndclassx.lpfnWndProc   = WndProc;
    wndclassx.cbClsExtra    = 0;
    wndclassx.cbWndExtra    = 0;
    wndclassx.hInstance     = hInstance;
    wndclassx.hIcon         = nullptr;
    wndclassx.hCursor       = LoadCursor(nullptr, IDC_ARROW);
    wndclassx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wndclassx.lpszMenuName  = nullptr;
    wndclassx.lpszClassName = L"WndProc";
    wndclassx.hIconSm       = nullptr;

    if( !RegisterClassEx(&wndclassx) ) return 0;

    HWND hWnd = CreateWindow(L"WndProc", nullptr, WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL, CW_USEDEFAULT,
                             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);

    ShowWindow(hWnd, SW_NORMAL);
    UpdateWindow(hWnd);

    MSG msg;
    while( GetMessage(&msg, nullptr, 0, 0) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    //  Retorna msg.wParam

    return (int)msg.wParam;
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
    static HFONT s_hFont;
    static int s_int = -1;
    static TEXTMETRIC s_tm;

    switch ( message )
    {
        case WM_CREATE:
        {
            HDC hDC;
            if( !(hDC = CreateIC(L"Display", nullptr, nullptr, nullptr)) ) return -1;

            LOGFONT lf;
            memset(&lf, 0, sizeof(LOGFONT));

            wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Blah");

            if( !(s_hFont = CreateFontIndirect(&lf)) )
            {
                DeleteDC(hDC);
                return -1;
            }

            s_hFont = (HFONT)SelectObject(hDC, s_hFont);
            GetTextMetrics(hDC, &s_tm);

            //   Call GetOutlineTextMetrics() with a null buffer address, to get the size of the buffer.
            s_int = GetOutlineTextMetrics(hDC, 0, nullptr);

            s_hFont = (HFONT)SelectObject(hDC, s_hFont);
            DeleteDC(hDC);
        }
        break;

        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(hwnd, &ps);
            s_hFont = (HFONT)SelectObject(ps.hdc, s_hFont);

            TextOut(ps.hdc, 20, 20, L"Value returned by GetOutlineTextMetrics() function", 50);

            wchar_t buffer[4];
            swprintf(buffer, 4, L"%3d", s_int);
            TextOut(ps.hdc, 20 + 55 * s_tm.tmAveCharWidth, 20, buffer, 3);
            EndPaint(hwnd, &ps);
        }
        break;

        case WM_DESTROY:
        DeleteObject(s_hFont);
        PostQuitMessage(0);
        break;

        default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

Output

enter image description here

Edit : there was an error in the code which I corrected. Instead of SelectObject(hDC, s_hFont) in WM_CREATE, I should have s_hFont = (HFONT)SelectObject(hDC, s_hFont). Didn't change the output though. But the final result is the same, that is, the function GetOutlineTextMetrics() continues returning 0.

Upvotes: 1

Views: 1541

Answers (2)

ymett
ymett

Reputation: 2454

My crystal ball suggests the font selected into the DC is not a TrueType font.

Some versions of MSDN say you can call GetLastError to find out why the function failed, but other (later?) versions of MSDN have that sentence removed.

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941635

GetOutlineTextMetrics() can only work on TrueType fonts. Problem is, you didn't ask for one. The font mapper has no reason to substitute your request with a TrueType font.

Solve your problem by insisting that the mapper always returns a TT font:

        wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Blah");
        lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;

Upvotes: 0

Related Questions