# Fonts suggestion

**URL:** <https://forum.juce.com/t/fonts-suggestion/8144>\
**Category:** General JUCE discussion\
**Created:** [February 16, 2012, 7:54pm UTC](https://forum.juce.com/t/fonts-suggestion/8144 "2012-02-16T19:54:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavi](https://avatars.discourse-cdn.com/v4/letter/x/8e8cbc/32.png) [@Xavi](https://forum.juce.com/u/Xavi)\
**Post date:** [February 16, 2012, 7:54pm UTC](https://forum.juce.com/t/fonts-suggestion/8144/1 "2012-02-16T19:54:53Z")

</div>

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 🙂  
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Xavi](https://avatars.discourse-cdn.com/v4/letter/x/8e8cbc/32.png) [@Xavi](https://forum.juce.com/u/Xavi)\
**Post date:** [February 16, 2012, 8:27pm UTC](https://forum.juce.com/t/fonts-suggestion/8144/2 "2012-02-16T20:27:26Z")

</div>

I forgot height and style…

```auto
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:

```auto
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);
}
```
