I just started using SDL2_ttf. I've figured out how to get some text on the screen with TTF_RenderText_Blended, but how do can I get it to do line-breaks and automatic wrapping?
It doesn't seem to support \n; it just creates a space instead of going down a line. Is there a way to add support for this? Specifically, using the proper line-height of the text, not by multiple calls to RenderText at different Y coordinates.
Given an X, Y coordinate and a width, how can I have it automatically go down a line whenever that width is reached (breaking between words)?
Instead of using TTF_RenderText_Blended, use TTF_RenderText_Blended_Wrapped. It takes additional parameter: width in pixels after which the text will break into next line.
SDL_TTF does not do wrapping, you have to write your own.
TTF_Font* ttf;
TTF_SizeText(ttf, "Hello World", &w, &h);
gives you the width and height of a string.
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 creating a custom single line edit control, with a custom font in win32 api on windows 7, the font is not a fixed width font, and I need to move caret according to the mouse click, The edit control is not empty and if I know the horizontal position of the mouse click within the window, how do I calculate the number of characters after which I need to move caret to ?
I really am out of ideas, if it was a fixed width font, I would have divided the horizontal mouse click position with average character width, that would have been simpler, doing the same with not a fixed width font, is prone to errors.
Given that it's a single-line control, you probably don't plan on working with immensely long input (at least normally). That being the case, one possibility would be to just store the character positions in an array (or vector, etc.) Then you can use (for example) a binary search in that array to find character positions. Of course, you can do the same even for longer strings--though it can increase storage requirements quite a bit.
This is a familiar problem. You are in essence trying to do hit testing on text and for that you need the location on the screen of each character of the text.
My preferred strategy is to calculate an array of RECT, one for each character of displayed text. The array needs to be updated when text is added or deleted, but it easily handles single or multiple lines. The function GetCharWidth32 retrieves all the widths for a string of text in a particular font selected into a DC. For single line one call is enough, and calculating the array of RECTs is simple. It's not much harder to do multiline.
Handle the mouse down message, loop through the array and find the right character. A brute force search is plenty fast enough.
This method is simple and easily generalises to a range of similar problems.
I am using SDL and libfreetype and constructing a very basic GUI, I'm implementing a textBox, but I haven't been able to figure out how I'm supposed to generate the standard blinking cursor part. It doesn't seem to be exactly the same as a | character. And moreover if I draw it as a | character that changes the text width.
What's the canonically correct way to render text in a textbox with a cursor?
The easiest way is to just draw a line primitive, this gives you a lot more control over the spacing, length and width of the caret.
And if you want to keep it as a text character in your font system, you can do a render-to-texture and copy it out, or do a simple memory blit onto your font atlas (so you can can keep pipe character separate, an use a control char like 0x01 for the caret).
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;
Then I use wxDC::DrawLabel for drawing multiline text I want to control font leading (linespacing). Is it possible?
There is no existing function to do that.
You can create your own. Use DrawText() to draw the individual lines with the spacing you want, but you draw each line individually. Use GetTextExtent() to find the height of the lines, and hence how much linespacing you'll need.