Graphics::drawSingleLineText()


#1

Hi Jules,

Would you object to expanding/overloading this function (drawSingleLineText()) to support a Justification parameter?
A lot of the time I just want to plop some text down anchored by a single point, but unless i want it anchored at the bottomLeft, I have to do all sorts of rejiggery myself! The normal drawText function takes one, but using that helps little as I still need to shift the rectangle.

It’d be wondrous for the Graphics class to do all that for me :slight_smile:


#2

If it had a justification param, it’d also need a bounding box to put it in… It’d be a better idea to just use a GlyphArrangement object directly (that’s all that happens inside the Graphics methods, but if you create the object yourself you can do all kinds of extra tweaks on it before rendering).


#3

that’s the thing - you need to make a bounding box to put them in, but the calculations for generating the box (from a point and justification) are always going to be the same. As such, it seems like the sort of thing that could be taken care of by a function.

For example, this is approximately the function I use at the moment…

void drawSingleLineText (Graphics& g, const String& text, int x, int y, const Justification& just)
{
   int h = roundFloatToInt(g.getCurrentFont().getHeight());
   int w = g.getCurrentFont().getStringWidth (text);

   if (just.testFlags(Justification::bottom))
      y -= h;
   else if (just.testFlags(Justification::verticallyCentred))
      y -= h/2;

   if (just.testFlags(Justification::right))
      x -= w;
   else if (just.testFlags(Justification::verticallyCentred))
      x -= w/2;

   g.drawText (text, x,y,w,h, just, false);
}

#4

Yeah… that kind of feels wrong to me, though it took me a minute to work out why I don’t like it. The reason is that the Justification class indicates how an object should be placed within a larger rectangle, but here you’re using it to indicate which corner of an object you want to align with a point, and that’s semantically different… I know I’m being picky there, but it just doesn’t sit right somehow!


#5

I see your point, but you are indeed being picky; the difference is so subtle as to be utterly insignificant!

With that attitude, I’m surprised you allow the ‘drawSingleLintText()’ function to exist at all :slight_smile: I mean, whether you choose to acknowledge it or not, that function prints text aligned to the bottom left of a virtual rectangle (one which you don’t need to define).

Anyway, it’s not as if I need it implemented in Juce (I already have a function to do it) - I just (*politely!) disagree with your reason for it to not be there.


#6

Yes, I totally understand that I’m being anal about it! Thing is, if I was going to add it, the correct concept to use would be something called a FontAnchorPoint, with 3 horizontal (left, right, centre), and 4 vertical (top, bottom, centre, baseline) options… And TBH that sounds like a lot of typing…!


#7

I totally agree with haydxn. I have been evaluating juce for quite some time now and although I find it can be useful and very powerful, my main complaint is that the simple things we need to do in every program are not straight forward. I am trying to convert a VST/AU plugin to juce and am spending most of my time figuring how to do simple things that were very easy in the VST SDK.

This text justification is a prime example. I do think this is anal because every graphic environment that I know of can draw left, right, or centered text. You do not need a full rectangle. Just a drawText(text, x, y, just). That’s all we need. This is not for editing but for simple text on the screen. Come on now. Make it easy on us and we’ll support you. I am almost ready to purchase a full license, but I am still trying to make sure all this works.


#8

TBH, looking at haydxn’s suggestion again, it’s really not such a bad idea. Maybe I was having a grumpy day when I first read this a year ago.

As long as it’s only horizontal justification that’s involved (vertical justification would be a bit confusing in terms of what happens with baseline vs overall height), then I’ll probably add it.

I’m saddened that you think that, because I’ve always tried very hard to make things as straightforward as possible. If you see anything that you think could be improved or added, please let me know - it’s only by getting feedback about what people think is important that I can improve it.


#9

Yes just simple horizontal justification.

I have written several very successful music applications (don’t want to mention which, until I fully commit to juce) that you have heard of. The first thing I do, is write a graphics layer that makes everything simple. For example I always write a TextAt( x, y, text ), TextLeft( x, y, text ), TextCenter( x, y, text ), TextRight( x, y, text ), DrawLine( x1, y1, x2, y2), etc. functions and similar if the environment doesn’t have them. Don’t get me wrong, I appreciate all the work you have put into juce and it’s very powerful. I’m currently writing a sample player and was almost done when I decided to try juce. I should have it all working in a couple of days and then I’ll know if juce works for my needs.

If it does work I’m considering converting my main application which is 16 years old (million lines of code) to juce. It will be a major piece of work and I’ll have to pay a juce expert to help me with two different areas.

The other issue I have found with using juce, is most popular applications stick to the GUI of the operating system. So all my dialog/windows/widgets, etc. have the look and feel of the OS. This is very important to major applications and to me. I’ll talk about this more later on.

BTW, my main reason for considering juce is Apple and the possibilities of other platforms. I just got tired of having to rewrite my code for apple every few years. I thank you for curing this for me.

Thanks for the quick reply.


#10

Jules, there is bug in drawSingleLineText if the text is not left justified.

If the justification is not left and the startX is greater than the clipping bounds, but the justified text crosses the clipping bounds because of the justification, the text is not drawn. You can't believe how long it took me to track down the bug. It is easy to see why in the code. 

 


#11

Goddamn! Thanks, fixed now. Strange that I never hit the same one myself - I use that method a lot.


#12

You would only see this if you were dragging a component from right to left that uncovered the text. It literally took me four hours to track down the bug, because I knew it was a clipping problem and thought it was in my code.