macOS: drawToCGContext fails for certain fonts

Certain fonts fail to render with drawToCGContext. You can see this in action by
updating CodeEditorDemo.h to change the font to “courier” (not “courier new”):

editor.reset (new CodeEditorComponent (codeDocument, &cppTokeniser));
+ editor->setFont(Font("Courier", 12., Font::plain));

and then launching the DemoRunner and opening the CodeEditorDemo. Observe that no code renders.

There is already an “ugly hack” in place for this issue, but apparently it’s not good enough of a hack. Specifically, drawToCGContext in juce_mac_Fonts.mm has the following:

// Ugly hack to fix a bug in OS X Sierra where the CTFrame needs to be slightly
// larger than the font height - otherwise the CTFrame will be invalid
if (verticalJustification == Justification::verticallyCentred)
    ctFrameArea = area.withSizeKeepingCentre (area.getWidth(), area.getHeight() * 1.0f);

I investigated somewhat and found that the height that CoreText wants can apparently be seen by adding to createCTFrame:

auto path = CGPathCreateMutable();
CGPathAddRect (path, nullptr, bounds);

+ CFRange fitrange;
+ auto size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake (0, 0), nullptr, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX),&fitrange);
    
auto frame = CTFramesetterCreateFrame (framesetter, CFRangeMake (0, 0), path, nullptr);
CFRelease (framesetter);
CGPathRelease (path);

Some searching brings up this S.O. suggesting it maybe has to do with the kCTParagraphStyleSpecifierLineSpacingAdjustment setting in the CTParagraphStyle. JUCE already does that in the createCFAttributedString function and playing with that, trying to shrink or grow the spacing, didn’t seem to have an affect.

Any suggestions on a proper fix for this? Thanks for taking a look!

Rob