How can I calculate the height and width of a given text? OpenGL

342 Views Asked by At

I draw the text using this two functions: https://pastebin.com/JVc5xFFT

I draw the text - GlText(dc, "Test", ERGB{ 155, 179, 0 }, 5, 220);

I build a font to uint with this function

GLvoid BuildFont(HDC hDC, UINT* FontBase, int PointerWidth)
{
    HFONT   font;
    HFONT   oldfont;
    *FontBase = glGenLists(96);

    font = CreateFontW(-MulDiv(PointerWidth, GetDeviceCaps(hDC, LOGPIXELSY), 72),
        0,
        0,
        0,
        FW_BOLD,
        FALSE,
        FALSE,
        FALSE,
        ANSI_CHARSET,
        OUT_OUTLINE_PRECIS,
        CLIP_DEFAULT_PRECIS,
        ANTIALIASED_QUALITY,
        VARIABLE_PITCH | FF_SWISS,
        L"Trebuchet MS");

    oldfont = (HFONT)SelectObject(hDC, font);
    wglUseFontBitmaps(hDC, 32, 96, *FontBase);
    SelectObject(hDC, oldfont);
    DeleteObject(font);
}

How can I find out the height and width of a given text?

1

There are 1 best solutions below

0
On

Function for calculating(badly code. but working):

SIZE CalculateTextSize(HDC dc, std::string text) {
    SIZE szi;
    SelectObject(dc, Font);
    SetTextCharacterExtra(dc, 1);
    GetTextExtentPoint32A(dc, text.c_str(), strlen(text.c_str()), &szi);
    szi.cx -= GetTextCharacterExtra(dc) * (strlen(text.c_str()) - 2);
    szi.cy -= ((FontSize % 2) % 2 == 0) ? FontSize / 2 : (FontSize / 2) - 1;

    return szi;
}