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()));
Related
How do I change the border size for a table in Dear ImGui?
By default, in Dear ImGui, the thickness of the table borders is 1 pixel (this can be seen if you draw the table). I want to change this value. How can this be done? And is it possible at all?
I didn't find the necessary field in the enum ImGuiStyleVar_ for the Push_Style_Var() function :(
I just stumbeld upon this as well...
In imgui_tables.cpp the table bordersize is currently fixed:
static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded because of clipping assumptions with outer borders rendering.
So you either need to change this value or submit a issue/request on the github repo.
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.
After filling a QTableWidget table I call:
table->resizeRowsToContents();
This makes the rows narrower but there is still a lot of padding between each row, as you can see here:
Looking at the documentation I found I could set the hight of each row with:
table->verticalHeader()->setDefaultSectionSize(16);
This removes the padding and gives me the tight spacing I want:
However, I'm concerned about using an absolute size in pixels. If the the system font is set to something large the text might not fit in the row.
Is there a way to remove the padding between rows while ensuring the text will always fit?
Use a custom delegate with the QAbstractItemDelegate::sizeHint() method implemented in a way that it will return a correct size according to the font used.
To compute a correct height, you can use a QFontMetrics provided with the option parameter of the sizeHint() method.
int height = option.fontMetrics.height();
or more precisely
int height = option.fontMetrics.boundingRect(index.data().toString()).height();
EDIT: How to use a delegate:
CMyDelegate* delegate = new CMyDelegate(tableView);
tableView->setItemDelegate(delegate);
Whenever I render a text using text layout object, it's apparent that actual width of text is not same as the width of layout depending on enable option in text wrapping.
I would like to know if there is anyway I could do to set width and layout width to be the same?
So When I pass in width and height in a creation of text layout object, I want it to render the text in the exact dimension I provided.
Initial layout dimensions are used to control word wrapping or trimming, if enabled. So it depends on what you want to achieve, normally you set desired layout box, and text does not have to fit or fully fill to any degree. When rendering you might be interested in effective rectangle that your text fits in fully, you need to call GetMetrics for that, and use returned DWRITE_TEXT_METRICS struct fields. Metrics data will contain actual rectangle sizes for your text, regardless of what you specified on layout creation.
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?