Composite Fonts, How to use?

With ue4 release 4.6 there came in composite fonts. Within the game content there can be created font objects containing multiple fonts. I only have found a description how to use font picker in a blueprint.

But how to use composite fonts in C++ to draw text on the HUD canvas?

E.g. I have created a composite font containing 4 fonts:

Now using the font to draw a text on the screen.

void AmyHUD::DrawHUD()
{
  // Font'/Game/Fonts/CompositeVerdana.CompositeVerdana'
  UObject* obj_ptr = StaticLoadObject( UFont::StaticClass(), NULL,
                                 TEXT( "/Game/Fonts/CompositeVerdana" ) );
  UFont* font_ptr = Cast<UFont>( obj_ptr );
  this->DrawText( TEXT( "CompositeVerdana" ), FColor::White, 100.0f, 100.0f, font_ptr, 1 );
}

The text “CompositeVerdana” appears on the screen with the first font, named “Normal”. I assume that the first font will be used by default.

But how to address the the other fonts, named “Italic”, “Bold”, and “BoldItalic”, and how to make use of them?

Is it intented to use composite font with C++, does it has any advantages for C++, or is it just for blueprint comfort, and better don’t make use of composite fonts in C++.?

That’s correct, Canvas will use the first font unless you specify otherwise.

Specifying which font to use for a HUD is a little cumbersome though, as AHUD doesn’t have a function that takes a FCanvasTextItem, so you’ll have to use the underlying Canvas directly… something like this:

if (IsCanvasValid_WarnIfNot())
{
	FCanvasTextItem TextItem(FVector2D(PosX, PosY), YourText, FSlateFontInfo(Font, FontSize,"Italic"), FLinearColor::White);
	Canvas->DrawItem(TextItem);
}

It’s fine to use composite fonts in C++, however they are best supported by Slate/UMG, rather than Canvas/HUD, and we recommend that UMG or Slate be used for any non-debug UIs.