Incorrect text width


#1

I am attempting to center text in a rectangle.  The attached PNG shows that the text is drawn too far left in the bounding (black) rectangle, because the text rectangle (red) is too wide.

I am using the getCurrentFont() on the Graphics, so I am definitely using the same Font for measuring and for drawing.  I'm using the getStringWidthFloat() function to get the right extent.

The same occurs on Linux and OS X, so it's not OS specific.  And before you ask: there is no trailing whitespace in the string, and if I change the starting offset I draw at, the end remains at the same relative offset.


#2

Posting a code snippet might help understand what the problem is.
If you use extra kerning it might be this issue, but the text doesn’t look much like it.


#3

I'm actually using the drawGlyph() routine, like so

f.getGlyphPositions(sline, glyphs, offsets);
...
for (int ix=0; ...)
{
  int glyph_to_draw = glyphs[ix];
  x = offsets[ix];
  context.drawGlyph (ch, AffineTransform::translation (x,y));
}

I suppose there should be some kern adjustment done, but I dont' know.  Don't ask why I need to do this...

 

 


#4

Jules?  Any ideas on this?


#5

Hi there,

I'm afraid this one has to wait until Jules is back from NAMM... Fabian and myself are both clueless about what's going on here ;-)


#6

Ah, fair enough.  I'm glad it's not obvious to people "in the know"...


#7

Are you sure you can't just use GlyphArrangement to do this? That class does exactly the same thing, but will be much more efficient, and can also give you its layout size.

Otherwise, the only thing I can think is that perhaps your font has some extra kerning or spacing, which you'd need to take into account if you do it manually.


#8

I would prefer to use GlyphArrangement, but it does not work correctly on RTL text; certainly, it does not work the same across platforms.  So I need to draw the text myself.

I see the same problem with different fonts, so I guess it could be a kerning/spacing issue.  I would have assumed that getGlyphPositions() would give me the correct ... positions.


#9

If the problem is RTL then you can use the TextLayout class, which uses native APIs that understand RTL.

Yes, getGlyphPositions does give you the right positions - it's used internally everywhere, so if it's wrong for you then must be something else going wrong, but not sure what.


#10

As stated elsewhere (multiple places), the RTL layout doesn't work on Linux.  So while it works fine on Windows and OS X, Linux is the abandoned baby where text-layout is only so-so.  And text input (especially of mixed RTL/LTR) is also not good.


#11

Ah yes, for linux it is a different matter.