getOutlineForGlyph path bounds wrong?


#1

This generates the following in Projucer’s Live component preview:

class Test : public Component
{
  Test() {}
  void paint(Graphics& g) override
  {
    int fontHeight = 60;
    Font font("Times", fontHeight, Font::FontStyleFlags::plain);

    Array<int> pos;
    Array<float> offsets;
    font.getGlyphPositions("D", pos, offsets);
    Path p;
    if( font.getTypeface()->getOutlineForGlyph(pos[0], p) )
    {
        p.applyTransform(AffineTransform::scale(fontHeight));
    }
    auto r = p.getBounds();

    g.setColour(Colours::black);
    g.setFont(font);
    g.drawSingleLineText("D", 0, r.getHeight() + 10);
    g.setColour(Colours::red);
    g.drawRect(r.withPosition(r.getX(), r.getY() + r.getHeight() + 10));    
  }
};


the red rectangle is 60 pixels high, right in line with the specified font height, and is positioned right at the 10px mark.


Custom fonts not being properly centred
#2

Ok, so as an update to figuring this out, here’s what happens if I just take the path returned from getOutlineForGlyph and g.strokePath()

    Font font("Times", 60, Font::FontStyleFlags::plain);

    Array<int> pos;
    Array<float> offsets;
    font.getGlyphPositions("D", pos, offsets);
    Path p;
    if( font.getTypeface()->getOutlineForGlyph(pos[0], p) )
    {
        p.applyTransform(AffineTransform::scale(fontHeight));
    }
    auto r = p.getBounds();
    r = r.withPosition(r.getX(), r.getY() + r.getHeight() + 10);

    g.setColour(Colours::red);
    g.drawRect(r);

    g.setColour(Colours::black);
    g.setFont(font);
    g.strokePath(p,
                 PathStrokeType(JUCE_LIVE_CONSTANT(10) / 100.f),
                 p.getTransformToScaleToFit(r, true));

draws this:
36 PM

g.fillPath(p, p.getTransformToScaleToFit(r, true)); results in this:
58 PM


#3

Did you figure it out in the end? I have a similar issue, when centring text.


#4

man, the code is right there above your post!