How to specify a style for a font

Hello Everyone !
I am currently having trouble finding a way to specify which style of a typeface I wish using in JUCE.
Specifically I have imported the ‘Bahnschrift.ttf’ typeface that has 15 different styles such as :

  • ‘Regular’
  • ‘Bold’
  • ‘Light’
  • ‘SemiLight’
    etc…
    When using it, juce defaults the style to ‘Regular’, my question is :
    Is there a way to specify that i want to use the ‘SemiBold Condensed’ style (for instance) instead of splitting the ttf file beforehand and adding it to JUCE ?
    Thanks

Yes, you can use juce::Font::setTypefaceStyle().

IIRC correctly, some fonts on some systems will have the style in the typeface name, rather than being a separate property. So for example,

juce::Font font {"MyFont Bold"};

// vs

juce::Font font {"MyFont"};
font.setTypefaceStyle ("Bold");

Thank you very much ImJimmi ! Works flawlessly

1 Like

I had to do this in Contrast to get the same font on Windows and macOS:

1 Like

Just found out that this only works when you install the font on your system doesn’t it ?
After trying out the code on a different system, and uninstalling it from mine, the setTypefaceStyle method seem to not work on binarised font file ? :thinking:

A single font file, like an .otf typically only has one style. When you download a font from say Google Fonts, it’ll be a .zip containing a font file for each individual style.

So if you want to use different styles with an embedded font, you need to embed each individual font file. At that point there’s not much point in using setTypefaceStyle since you can just load the different styles to different juce::Font variables like so:

static const juce::Font regularFont {juce::Typeface::createSystemTypefaceFor (BinaryData::MyFontRegular_otf, BinaryData::MyFontRegular_otfSize)};
static const juce::Font boldFont {juce::Typeface::createSystemTypefaceFor (BinaryData::MyFontBold_otf, BinaryData::MyFontBold_otfSize)};

Hello ImJimmi. I work on the same project as Milos. Thank you so much for your answer ! So helpfull. At that point if we consider what you say the solution would be to find different otfs with different styles of the Bahnschrift font in it. However for the moment our search is not successfull. Do you know where we could find these otf ? Do you think we can use a font editor to split the styles ? If so, have you any recomandation on which editor we could use.