Multiline Canvas.DrawString issue in Windows

Is anyone aware of an issue where drawing a multiline string into a canvas when using an alpha value does not work correctly in Windows? The following works correctly on Mac OS X but in windows only the first line get’s displayed:

In the canvas “Paint” event:

[code]tmpStr = tmpStr + “Line 1” + EndOfLine
tmpStr = tmpStr + “Line 2” + EndOfLine
tmpStr = tmpStr + “Line 3” + EndOfLine
tmpStr = tmpStr + “Line 4” + EndOfLine

g.TextFont = “System”
g.TextSize = 0
g.ForeColor = &cFFFFFF60 ’ <----Notice the opacity value
g.DrawString(tmpStr, 0, 0)[/code]

If the ForeColor is changed to &cFFFFFF then the entire string is drawn. On Mac OS X, all lines get drawn irregardless of the alpha channel value.

Any thoughts? Bug?

Cheers.

-bill k

Did you try with GDI+ enabled?

Yes, GDI+ is enabled. I would imagine that if it weren’t none of the text would show. I’m going to file a feedback case. It will just require me to to disable the opacity adjustment in the Windows build of my application until this is fixed…

You might be better off trying to StringShape http://documentation.xojo.com/index.php/StringShape. It has its own peculiarities but is generally newer and more robust, IMO.

Thanks Bob I’ll try that. Do you know what the CPU requirements are? I’m trying to do this at video frame rates (30x/sec) and I’m already taxing the CPU with all sorts of other real-time image adjustments…

I honestly have no idea. Sorry.

If you add the width parameter, it should work.

g.DrawString(tmpStr, 0, 0,1000)

[quote=195410:@William Koperwhats]Is anyone aware of an issue where drawing a multiline string into a canvas when using an alpha value does not work correctly in Windows? The following works correctly on Mac OS X but in windows only the first line get’s displayed:

In the canvas “Paint” event:

[code]tmpStr = tmpStr + “Line 1” + EndOfLine
tmpStr = tmpStr + “Line 2” + EndOfLine
tmpStr = tmpStr + “Line 3” + EndOfLine
tmpStr = tmpStr + “Line 4” + EndOfLine

g.TextFont = “System”
g.TextSize = 0
g.ForeColor = &cFFFFFF60 ’ <----Notice the opacity value
g.DrawString(tmpStr, 0, 0)[/code]

If the ForeColor is changed to &cFFFFFF then the entire string is drawn. On Mac OS X, all lines get drawn irregardless of the alpha channel value.

Any thoughts? Bug?

Cheers.

-bill k[/quote]

I am sorry, but in 2015R22 under Windows with the addition of GDIPLus, Y 20 otherwise the first line does not show, and over a window background RGB(128,128,128) (darker than the default) the 4 lines do display very nicely.

[code]Sub Paint(g As Graphics, areas() As REALbasic.Rect)
app.UseGDIPlus = True
dim tmpStr as string
tmpStr = tmpStr + “Line 1” + EndOfLine
tmpStr = tmpStr + “Line 2” + EndOfLine
tmpStr = tmpStr + “Line 3” + EndOfLine
tmpStr = tmpStr + “Line 4” + EndOfLine

g.TextFont = “System”
g.TextSize = 0
g.ForeColor = &cFFFFFF60 ’ <----Notice the opacity value
g.DrawString(tmpStr, 0, 20)
End Sub
[/code]

Michel, you are correct, the initial Y value needs to be something other than 0. In my actual application the code is much more complicated and the positioning takes into account the text height as well as where the user has positioned it around the screen. When I was simplifying the example I got a bit carried away.

I’m displaying the text over a live video feed, so the background is constantly changing, but Interestingly I’m only able to ever see the top line when the alpha value is anything other than x00.

Feedback Case

Did you try adding a width parameter like I mentioned above. It’s a workaround, but it works.

Y is the “baseline” not “the top to draw from” (see http://documentation.xojo.com/index.php/Graphics.DrawString)
You probably clip the first line right off with y = 0
I’m surprised it doesn’t behave the same on OS X

Usually I set Y = text ascent + a couple pixels for padding

Try this
new desktop app
in app.open set UseGDIPlus = true
drag two canvases onto the default window
in canvas1.paint put

  dim tmpstr as string
  tmpStr = tmpStr + "Line 1" + EndOfLine
  tmpStr = tmpStr + "Line 2" + EndOfLine
  tmpStr = tmpStr + "Line 3" + EndOfLine
  tmpStr = tmpStr + "Line 4" + EndOfLine
  
  g.TextFont = "System"
  g.TextSize = 0
  g.ForeColor = &cFF000060 
  g.DrawString(tmpStr, 0, 0)

in canvas2.paint put

  dim tmpstr as string
  tmpStr = tmpStr + "Line 1" + EndOfLine
  tmpStr = tmpStr + "Line 2" + EndOfLine
  tmpStr = tmpStr + "Line 3" + EndOfLine
  tmpStr = tmpStr + "Line 4" + EndOfLine
  
  g.TextFont = "System"
  g.TextSize = 0
  g.ForeColor = &cFF000060 
  g.DrawString(tmpStr, 0, g.textascent)

this way they o the same but one draws at 0,0 the other at 0, textascent
one will show, the other won’t (and there’s that difference about draw string NOT drawing all lines)
THAT seems a bug
But the opacity doesn’t seem to influence it in the way you describe

In fact you can see this if instead of using a line with all ascenders you raw something like “agjpytu” a line with mixed ascenders & descenders

[quote=195548:@William Koperwhats]I’m displaying the text over a live video feed, so the background is constantly changing, but Interestingly I’m only able to ever see the top line when the alpha value is anything other than x00.
[/quote]

You should have mentioned the video feed. I just tried to lay a canvas over a MoviePlayer, and where the video shows, the canvas drawing disappears. I guess like it happens with HTMLViewer, the video pops on top of the Zorder.

I tried to work around that by doing a drawinto of the movie player into the canvas but the picture does not show.

[quote]@Norman Palardy Y is the “baseline” not “the top to draw from” (see http://documentation.xojo.com/index.php/Graphics.DrawString )
You probably clip the first line right off with y = 0
I’m surprised it doesn’t behave the same on OS X

Usually I set Y = text ascent + a couple pixels for padding[/quote]

Norm, I realize that, and I mentioned in a later post that I had over-simplified my example. I’ve been using g.TextHeight rather than g.TextAscent to set my initial position (the user can drag the text around the screen), but changing it doesn’t seem to make a difference.

@Michel Bujardet When I say that I’m putting the text over live video, I’m actually doing all the work in a canvas with standard Picture objects. The video images come in from MBS DirectX or QuickTime plug-ins as Pictures, get rotated, scaled, adjusted and then graphical and text overlays drawn onto them before being displayed on the canvas. I’m not making use of an HTML view at all.

@jim mckay Jim, you were absolutely correct, adding the width parameter solved the problem of all the lines not getting displayed! Thanks for the suggestion.

The only issue still is that when the alpha channel value is anything other than x00, the font looks like it changes. You can see this difference in the two images below where the user has selected an opacity of 99%. I would probably chalk this up to anti-aliassing or the like, but if you look closely the character spacing changes. Of course none of this occurs in the Mac build.

Sorry for the potato quality screen grabs, it’s daytime and I don’t have access to the telescope to come up with ones that are useful…

4 lines of text, alpha value at x00 (100% opacity), no Width parameter:

4 lines of text, alpha value at x03 (99% opacity), no Width parameter:

4 lines of text, alpha value at x03 (99% opacity), .Width parameter set to Canvas.Width:

Hi William,

I can’t speak to the other issues, but have you tried alternate fonts instead of System? I get much better results on Windows systems if I specifically choose Helvetica.

@Tim Jones Tim, using other fonts doesn’t fix the original issue, although as Jim McKay pointed out putting the Width parameter in worked. Interestingly as the alpha value is changed from x00, the difference in how the text is drawn is very pronounced. With only this single change (g.ForeColor = &cFFFFFF00 to g.ForeColor = &cFFFFFF01), even the line spacing changes. Very weird.

I would bet the difference is GDI+ vs GDI. Maybe when GDI+ support was added, it resulted in GDI still being used when alpha=0

DrawString vs DrawText?