Calculating width of a character

Hi,

I am trying to paint a string in a canvas character by character but it seems hard to make a final image that resembles the drawstring method.

I have read the documentation for the stringwidth (which I use to get the position of the next character) but appears it is hard to find the next position of the character and not the baseline.

Have you got any ideas how to deal with this? I am trying on Win.

Thanks

John

Stringwidth should do it, provided you have set the font and size first. Post some code?

Typical letters are not drawn one after another but moved closer together.
Do sum of character width will not match stringwidth function.

Can you post some code ?

I just ran a quick test using a variety of fonts and sizes and I see no difference between drawing the string as a whole vs. drawing each character individually. And the StringWidth of the entire string is the same as the sum of the StringWidth of each character.

Again, just a quick and dirty test, but it seems to indicate that this should work.

This is on Windows 7, btw.

I think there are some combinations that may look different. Try “fin” for example. The ef eye should overlap.

No difference here. Different OS’s may act differently, of course.

You’re on Windows, right Tim? I’ll have to test on Mac.

Perhaps you’ve confused us with the reference to StringWidth. What is this about baseline?

Kem, yes, Windows.

There could be a difference if Xojo takes kerning into account. For instance, when a font contains kerning, ‘AV’, ‘To’ or ‘Va’ use closer spacing between characters. Then drawing characters individually will not report the correct character spacing, whereas drawing the entire string will.

On Mac, the system takes kerning into account for all applications, so it should be apparent. For Windows, it requires additional settings, and chances are it does not show for DrawString.

In any event, drawing individual characters is not the best approach. I have always build the string to draw, word by word or character by character, until it fits the space allowed, and draw it all at once. (For word wrapping, obviously.) The three things I use StringWidth for are

  1. Justifying (right/center/decimal aligned) a column of text
  2. Wrapping a string in a fixed width column
  3. Drawing styled text manually

In no case do I ever deal with individual character widths. Build the string and measure the width of the entire thing.

OK. I just tested a special font to better see kerning.

On Mac, Kerning is taken into account. So drawing character by character and using stringwidth will lead to incorrect character spacing with elaborate fonts. Drawing an entire string is preferable.

On PC, stringwidth like many applications does not use kerning, so drawing character by character and using stringwidth should provide the same result as drawing a string.

I do not know what the OP wants to achieve, but using Drawstring should be possible, even for an animation.

I’ve had to draw character by character when applying different formatting (like color) to different characters.

Forgive me, I tend to reason as regular typography. You are right, mixing colors requires character by character. At any rate it is difficult to speculate until the OP provides more information.

If kerning is used depends on who draws it. I bet there are older and newer APIs on Mac and Windows which do things differently.
Not to forget that rendering in console/web projects is completely different.

[quote=93985:@Christian Schmitz]If kerning is used depends on who draws it. I bet there are older and newer APIs on Mac and Windows which do things differently.
Not to forget that rendering in console/web projects is completely different.[/quote]

Mac has been rendering kerning for some 20 years. As for Windows, I tested under Windows 8.1, and it seems the situation has not changed since Vista : kerning is available buried somewhere in the API, but not used as default. Word has been using it since 2007 or so, but not WordPad for instance.

Well, you never know what renders text with drawstring. I bet there are at least 5 ways it’s implemented internally in Xojo or Real Studio…

For the problem with colored text, I’d suggest to apply color using StyledText and use the StyledTextPrinter class to let the OS do it.

Proof is in the pudding. The same DrawString code in Paint uses kerning on Mac, and not on PC.

As for Word, I just checked : it does not provide kerning as default. One has to instruct it in the Fonts dialog :confused:

Unfortunately, that’s not an option in a ListBox, and that’s where I’ve needed to do it.