Unable to draw text glyph with getOutlineForGlyph()


#1

This is sample code which demonstrates the issue I'm having.

What I want to do is to get a glyph from a font into a Path, and render it.  The usual drawtext works fine; but extracting the glyph and drawing it does not work.

What am I doing wrong?


#include "JuceHeader.h"
class mywindow : public DocumentWindow
{
public:
    mywindow() : DocumentWindow( String(), Colour(0xFFFFFFFF), 7, false)
    {
    }
    virtual ~mywindow() 
    {
    }
    virtual void paint(Graphics &g) 
    {
        Colour c(0xFFFFFFFF);
        g.fillAll(c);
        g.setColour(Colour(0xFF000000));
        Path p;
        Font f;
        /* this works:
        g.setFont(f);
        g.drawText("hi", 50,50, 200, 50, Justification::left, true);
        */
        // this doesn't work:
        Typeface *tface = f.getTypeface();
        Array<int> glyphs;
        Array<float> offsets;
        tface->getGlyphPositions("hi", glyphs, offsets);
        Path outline;
        tface->getOutlineForGlyph(glyphs[0], outline);
        p.addPath(outline, AffineTransform::scale(20));
        g.fillPath(p);
    }
};
//==============================================================================
class testcaseApplication  : public JUCEApplication
{
public:
    //==============================================================================
    testcaseApplication() {}
    const String getApplicationName() override       { return ProjectInfo::projectName; }
    const String getApplicationVersion() override    { return ProjectInfo::versionString; }
    bool moreThanOneInstanceAllowed() override       { return true; }
    
    DocumentWindow *doc;
    //==============================================================================
    void initialise (const String& commandLine) override
    {
        // Add your application's initialisation code here..
        doc = new mywindow();
        doc->setUsingNativeTitleBar (true); 
        doc->setResizable (true, false); 
        doc->setBounds(0,0,400,400);
        doc->setVisible(true);
        doc->addToDesktop(ComponentPeer::windowHasTitleBar);
    }
    void shutdown() override
    {
        // Add your application's shutdown code here..
    }
    //==============================================================================
    void systemRequestedQuit() override
    {
        // This is called when the app is being asked to quit: you can ignore this
        // request and let the app carry on running, or call quit() to allow the app to close.
        quit();
    }
    void anotherInstanceStarted (const String& commandLine) override
    {
        // When another instance of the app is launched while this one is running,
        // this method is invoked, and the commandLine parameter tells you what
        // the other instance's command-line arguments were.
    }
};
//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (testcaseApplication)

#2

It's most likely off-screen or not scaled to a visible size. Try checking the bounds of the path, or using getTransformToScaleToFit


#3

Yes, that was it.  I was surprised that using "startNewSubpath()" didn't seem to make the added path show up at the expected location.