Fonts suggestion


#1

Hi Jules,

I’ve been testing the fonts stuff and it’d be really great to be able to change the fonts in each control directly (like setFont(…) in labels).
Override L&F just to change the font of a button or a combo it’s a very big cannon for such a tiny fly :slight_smile:
I think an extremely simple way to do it without breaking existing code and L&Fs is just to include a Font pointer in TextButton/etc classes, by default that pointer would be null so it’d be used the default font in L&F render code:

void LookAndFeel::drawButtonText (...)
{
    if(button.GetInternalFontPtr() == nullptr) 
    {
        // Standard behaviour: the button has no font, use default font... 
        Font font (getFontForTextButton (button));
        g.setFont (font);
    } 
    else 
    { 
        // Button has its own font, use it... 
        g.setFont(*(button.GetInternalFontPtr())); 
    } 

    g.setColour (button.findColour (button.getToggleState() ? TextButton::textColourOnId
                                                            : TextButton::textColourOffId)
                       .withMultipliedAlpha (button.isEnabled() ? 1.0f : 0.5f));

    const int yIndent = jmin (4, button.proportionOfHeight (0.3f));
    const int cornerSize = jmin (button.getHeight(), button.getWidth()) / 2;

    const int fontHeight = roundToInt (font.getHeight() * 0.6f);
    const int leftIndent  = jmin (fontHeight, 2   cornerSize / (button.isConnectedOnLeft() ? 4 : 2));
    const int rightIndent = jmin (fontHeight, 2   cornerSize / (button.isConnectedOnRight() ? 4 : 2));

    g.drawFittedText (button.getButtonText(),
                      leftIndent,
                      yIndent,
                      button.getWidth() - leftIndent - rightIndent,
                      button.getHeight() - yIndent * 2,
                      Justification::centred, 2);
}

Cheers,

Javi


#2

I forgot height and style…

class TextButton  (...) 
{ 

  (...) 

  Font *m_internal_font_ptr; 
  float m_internal_font_height; 
  int m_internal_font_style; 

public:
  TextButton() : 
    m_internal_font_ptr(nullptr), 
    m_internal_font_height(-1), 
    m_internal_font_style(-1)
  { 
    (...)
  } 

  void setFont(Font *font, float height=-1, int style=-1) 
  { 
    m_internal_font_ptr = font; 
    m_internal_font_height = height; 
    m_internal_font_style = style; 
  } 
  
}; 

By default height and style are -1, in that case the predefined height and style of the assigned font are used:

void LookAndFeel::drawButtonText(...)
{
    if(button.GetInternalFontPtr() == nullptr) 
    {
        // Button has no font, use default font... 
        Font font (getFontForTextButton (button));
        g.setFont (font);
    } 
    else 
    { 
       // Button has its own font, use it... 
       Font font(*(button.GetInternalFontPtr())); 
       const float height = button.GetInternalFontHeight(); 
       if(height >= 0) 
       { 
         font.setHeight(height); 
       } 
       const int style = button.GetInternalFontStyle(); 
       if(style >= 0) 
       { 
         font.setStyleFlags(style); 
       } 
       g.setFont(font); 
    } 

    g.setColour (button.findColour (button.getToggleState() ? TextButton::textColourOnId
                                                            : TextButton::textColourOffId)
                       .withMultipliedAlpha (button.isEnabled() ? 1.0f : 0.5f));

    const int yIndent = jmin (4, button.proportionOfHeight (0.3f));
    const int cornerSize = jmin (button.getHeight(), button.getWidth()) / 2;

    const int fontHeight = roundToInt (font.getHeight() * 0.6f);
    const int leftIndent  = jmin (fontHeight, 2   cornerSize / (button.isConnectedOnLeft() ? 4 : 2));
    const int rightIndent = jmin (fontHeight, 2   cornerSize / (button.isConnectedOnRight() ? 4 : 2));

    g.drawFittedText (button.getButtonText(),
                      leftIndent,
                      yIndent,
                      button.getWidth() - leftIndent - rightIndent,
                      button.getHeight() - yIndent * 2,
                      Justification::centred, 2);
}