DrawText VS TextOut Win32 - c++

I find have used both these functions before, but I don't quite see the the difference between them. Well, I know that DrawText requires a formatting rectangle,and can do some text formatting, and textout only the starting coordinates, are there any other differences?

DrawText
It draws a text string into a rectangle region specified in logical coordinates.
It provides convenient ways of formatting multiline text.
It is mainly used for
wordbreaking paragraph formatting, expanding tabs etc.
TextOut
It is a simple text-drawing function which is easy to use.
It draws a character string at a specified location, using the currently selected text attributes.
The text
string to draw does not need to be zero terminated.
Also, take a look at ExtTextOut and DrawTextEx

DrawText() is User32.dll
TextOut() is Gdi32.dll
DrawText most likely calls TextOut in its implementation.

Draw text can be used to just give the length or size of text without actually displaying it. This is useful when you have to fine the maximum display length of a set of strings. Also if you supply a null terminated string as the input in DrawText, it is not necessary to supply the length of the string - that is automatically created.
Take a look at this and this.

Related

Print chess unicode characters in C++, and make characters square sized

I'd like to ask what's the simplest way of writing the chess unicode characters in a console window in C++? (♙♘♗♖♕♔♟♞♝♜♛♚) They are part of the "Miscellaneous Symbols" block in unicode. https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode
I also want to print characters with square size, right now my chess board is not square, because each character is a rectangle and not a square.
It'd also be good to be able to write with ordinary non-square characters below the chess board, but that might be impossible? To mix different fonts/formattings in the same console window?
Ok, thanks in advance! :)
The first part of your question, outputting those characters, is platform-dependent. Linux consoles often use UTF-8, if that is the case you can encode the characters as UTF-8 and write them to standard output. On Windows you can use the Console API (the WriteConsole function):
HANDLE handle = GetStandardHandle(STD_OUTPUT_HANDLE);
DWORD written = 0;
// explicitly call the wide version (which always accepts UTF-16)
WriteConsoleW(handle, L"\u2658", 1, &written, NULL);
One caveat which is hard to work around is that you need a console font containing those characters.
For getting square cells, this is dependent on a lot of specifics about the way the console renders text. If it uses font substitution, then there is a chance the text will not actually be monospaced.
Now, if you have a console font with these characters, and if that font is monospaced, then you may be able to draw a square board by adding some spacing between the characters. You can use block elements like ▌ U+258C — LEFT HALF BLOCK to draw the chequerboard: ▌♘▐█▌ ▐.

DirectWrite: Use Different Font for Latin and Complex Script

I use CreateTextLayout and CreateTextFormat to draw text with DirectWrite (C++), the text is mixed Hebrew/English, is there a way to use a different font/font size for the Latin and Hebrew characters?
Thanks.
When you create the IDWriteTextFormat using CreateTextFormat, you can pass the name of the font family in the first parameter and you can change the size of the font in sixth parameter.
You can get the CreateTextFormat parameters from MSDN.
Here is a list of Microsoft Windows font families, you will find ones in Latin and Hebrew there.
I ended up calling IDWriteFont::HasCharacter to build text ranges inside which all characters use the same font (the hebrew or the latin one).
If that text range uses the hebrew font, I call textLayout->SetFontSize on it to increase the font size.
This amounts to anticipating the font callback DirectWrite will do, which is automatic and cannot be customized.

Correct way to render cursor in custom text area?

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).

word wrapping block of text with variable font/formatting using winapi

I need to draw an arbitrary string directly on the screen (not inside a rich text control) using MFC and/or the Windows API. Characters within the string can vary in font face or color, and can be bold, italic, underlined, or any combination of the two. Additionally, users can choose to wrap the text within bounds of their choosing, with an option to force it to fit by adjusting the font size.
I'm thinking of doing something along the lines of:
parsing it into a vector of substrings and their formats
using GetCharABCWidthsFloat to calculate the widths of all the substrings
manually calculating where the line breaks need to fall
repeating the above iteratively with smaller fonts if necessary to make the text fit
drawing each of the substrings individually with DrawTextW, determining their locations using the widths of the preceding substrings and the line break points
Is there a better approach? If not, are there any gotchas or tricks I should be aware of when implementing this?
(I'm aware of the DT_CALCRECT | DT_WORDBREAK flags for DrawText, but I don't think that'll work for me because a) it assumes consistent font/formatting across the whole string and b) I saw some message board posts stating that it doesn't calculate the widths of italic characters correctly. True?)
XAML is your friend. Not natively supported without .NET, but there are 3rd party libraries for that (e.g. Xtreme Toolkit Pro). Here's a demonstration of the XAML Markup feature:
http://www.codejock.com/download/win32/markupsample.zip
Been using it for years. Can't complain.

How can I change font leading (linespacing) in wxWidgets?

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.