I want to find out the width and height of a font in SDL_ttf. I have the font (TTF_Font *), and the text (const char *). Is there any way I could calculate the with and height with these two properties? I tried searching the internet but there wasn't much information on this topic.
I don't want to use something like:
calculate_font_example(font, text, width, height);
I want to use something like this:
calculate_font_example(font, text) // Outputs font width and height
A ttf font is like a svg, it is defined using curves and lines, so it has no fixed size. It can be drawn in any hight and width (or even thickness). So you don't need to calculate the font size, use any size you like.
Related
I am trying to write code in Motif to change a dialog warning box to resize size it if the box is not wide enough. The width and height is always being set by the calling classes and its not always wide enough for the message being displayed and the end of the line is truncated off. Instead of fixing everywhere to use auto sizing (i.e. width is 0 or not set at all) they want to figure out what the pixel width size is for a character in the dialog. They can then multiple the longest line X pixels width to get the lines length in pixels. Then we would see if the dialog declared width needs to be reset to stop the truncation. Only dialogs that are too short will be changed (dialogs too wide are not to be changed).
However; I can't find any example on how get the character width in pixels anywhere. I remember years ago I was on a project where they created some type of widget, inserted a character into it, and then did a XtGetValues to get the width and height so I think it can be done. So does anyone know how to do this?
That was a long time ago, but if memory serves, Xt doesn't have any specific support for fonts, it relies on plain libx11. You will need to call XQueryFont or XLoadQueryFont to get the XFontStruct describing your font, then grovel through the per_char array to find the extents of individual glyphs.
I am trying to align center the text I have to draw. I use ID2D1RenderTarget::DrawTextLayout method. The problemn is that if I set the text horizontal alignment to DWRITE_TEXT_ALIGNMENT_LEADING (the default value) the text is drawn proberly, but if I change this value to DWRITE_TEXT_ALIGNMENT_CENTER the text is shifted right.
The example string is
Internal Amazing
Scupper
Following are the outcomes (the first is alignment leading):
My comment as an answer (yeah, guessed right :)):
Just a quick guess: Did you check that the maxwidth your layout box
isn't too broad, so the center would end there at the right?
The IDWriteTextLayout used by ID2D1RenderTarget::DrawTextLayout method defines a maximum width of the layout box, which determines where the text is centered. It can be manipulated by the methods of the interface (GetMaxWidth and SetMaxWidth).
When you create the text layout, you cannot change the width and height to something greater afterwards. You should use max screen coords when creating the layout then change the max width and height to the desired size. You should use render_tgt->DrawText(...) method for this example or release and recreate the layout interface every time the text, font name, and a lot of other various things like typography are changed. I created an array of layout and typography events that can be reapplied to the layout interface every time it is recreated. You do not need to recreate the layout for font size, since you can resize the text or individual characters with the layout interface
// Set the layout to maximum screen size
FLOAT maxX = (FLOAT)GetSystemMetrics(SM_CXFULLSCREEN); // * XD2D::pix_to_dips.x;
FLOAT maxY = (FLOAT)GetSystemMetrics(SM_CYFULLSCREEN); // * XD2D::pix_to_dips.y;
XS_DWRITE_DEV_FACTORY->CreateTextLayout
(
dstring,
dlength,
(*pp_txt_format),
maxX,
maxY,
pp_txt_layout
);
// Resize to the requested size or minimum allowed size
(*pp_txt_layout)->SetMaxWidth(max(req_xsize, (*pp_txt_layout)->GetFontSize()));
(*pp_txt_layout)->SetMaxHeight(max(req_ysize, (*pp_txt_layout)->GetFontSize()));
As of now, I'm using the following to code to resize my CStatic controls:
WINDOWPLACEMENT wndpl;
m_myStaticControl.GetWindowPlacement(&wndpl);
// Increase the static box's width
wndpl.rcNormalPosition.right += 10;
m_myStaticControl.SetWindowPlacement(&wndpl);
m_myStaticControl.SetWindowText("Some text");
I obtain the constant (in the above case, 10) by trial-and-error. Because this seems like a really inelegant and hard-to-maintain solution, I want to change this. After some research, I think I have a basic idea; which is:
Get the pixel width and height of the required text using GetTextExtentPoint32.
Get the current window placement of the CStatic control as in code example above.
If the current width < obtained pixel width, add obtained pixel width. Do the same for height.
Set the window placement as in code example above.
Set the window text as in code example above.
Would this be a good, efficient approach? Also, does GetTextExtentPoint32 use pixels or dialog units?
I am working with sdl ttf for rendering text to opengl, actually everything it's fine, but like you know, when you open a ttf sdl file you specify it on pixels, and you can't change the font size. Actually you can change the font size but you need to close and re-open the font, and that will be a little slow, so, is there any way to change the font size or something like that?
I need to do the following: Font size that can get resized and adjusted to the window screen (like the opengl -1.0f - 1.0f range that works with something similar to percent), multiline rendering support with no background.
You could put it on a surface and scale the surface. That will probably deform the text, which might be an issue.
Or you could have an array of text sizes. Load the same font with a range of text sizes. As you scale your image, use the different fonts instead of changing the size of one font.
My program draws text strings into rectangles by working out the width and height of the text, and selecting a smaller font if its too big for the rectangle. But originally i only used single lined text, now i need some multi lined, i used to use GetTextExtentPoint32 but if theres \n in the string, it seems to think of that as a normal character.
DrawText with DT_CALCRECT only returns the height of the text...
Is there a simple way to do this?
Docs for DrawText state that while it only returns the height, it modifies the rectangle you pass it. Are you checking the rectangle, or only the return value? It sounds like you would actually want to pass in a rectangle with a large width (ie. maximum width you want to allow), and DrawText will reduce as necessary. (If you pass in a small width, it will expand it only enough to fit the largest word.)
From MSDN:
If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the largest word is wider than the rectangle, the width is expanded.
You should do roughly this (pseudo code):
size text_dim(0,0);
foreach( line in text.split("\n") )
{
size line_dim = GetTextExtentPoint32(line.start,line.length);
text_dim.y += line_dim.y;
text_dim.x = max(text_dim.x,line_dim.x);
}
return text_dim;