1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Font: Allow overriding typeface ascent and descent metrics

This commit is contained in:
reuk 2025-01-20 17:43:35 +00:00
parent f12d29899c
commit a381fdf81d
No known key found for this signature in database
7 changed files with 190 additions and 48 deletions

View file

@ -54,8 +54,7 @@
//==============================================================================
class FontsDemo final : public Component,
private ListBoxModel,
private Slider::Listener
private ListBoxModel
{
public:
FontsDemo()
@ -68,6 +67,10 @@ public:
addAndMakeVisible (heightLabel);
addAndMakeVisible (kerningLabel);
addAndMakeVisible (kerningSlider);
addAndMakeVisible (ascentLabel);
addAndMakeVisible (ascentSlider);
addAndMakeVisible (descentLabel);
addAndMakeVisible (descentSlider);
addAndMakeVisible (scaleLabel);
addAndMakeVisible (horizontalJustificationLabel);
addAndMakeVisible (verticalJustificationLabel);
@ -84,12 +87,13 @@ public:
heightLabel .attachToComponent (&heightSlider, true);
scaleLabel .attachToComponent (&scaleSlider, true);
styleLabel .attachToComponent (&styleBox, true);
ascentLabel .attachToComponent (&ascentSlider, true);
descentLabel .attachToComponent (&descentSlider, true);
horizontalJustificationLabel.attachToComponent (&horizontalJustificationBox, true);
verticalJustificationLabel .attachToComponent (&verticalJustificationBox, true);
heightSlider .addListener (this);
kerningSlider.addListener (this);
scaleSlider .addListener (this);
for (auto* slider : { &heightSlider, &kerningSlider, &scaleSlider, &ascentSlider, &descentSlider })
slider->onValueChange = [this] { refreshPreviewBoxFont(); };
boldToggle .onClick = [this] { refreshPreviewBoxFont(); };
italicToggle .onClick = [this] { refreshPreviewBoxFont(); };
@ -107,6 +111,11 @@ public:
heightSlider .setRange (3.0, 150.0, 0.01);
scaleSlider .setRange (0.2, 3.0, 0.01);
kerningSlider.setRange (-2.0, 2.0, 0.01);
ascentSlider .setRange (0.0, 2.0, 0.01);
descentSlider.setRange (0.0, 2.0, 0.01);
ascentSlider .setValue (1, dontSendNotification);
descentSlider.setValue (1, dontSendNotification);
// set up the layout and resizer bars..
verticalLayout.setItemLayout (0, -0.2, -0.8, -0.35); // width of the font list must be
@ -190,6 +199,10 @@ public:
r.removeFromBottom (8);
verticalJustificationBox.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth * 3));
r.removeFromBottom (8);
descentSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
r.removeFromBottom (8);
ascentSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
r.removeFromBottom (8);
scaleSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
r.removeFromBottom (8);
kerningSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
@ -199,13 +212,6 @@ public:
demoTextBox.setBounds (r);
}
void sliderValueChanged (Slider* sliderThatWasMoved) override
{
if (sliderThatWasMoved == &heightSlider) refreshPreviewBoxFont();
else if (sliderThatWasMoved == &kerningSlider) refreshPreviewBoxFont();
else if (sliderThatWasMoved == &scaleSlider) refreshPreviewBoxFont();
}
// The following methods implement the ListBoxModel virtual methods:
int getNumRows() override
{
@ -223,7 +229,7 @@ public:
AttributedString s;
s.setWordWrap (AttributedString::none);
s.setJustification (Justification::centredLeft);
s.append (getNameForRow (rowNumber), font.withHeight ((float) height * 0.7f), Colours::black);
s.append (getNameForRow (rowNumber), font.withPointHeight ((float) height * 0.7f), Colours::black);
s.append (" " + font.getTypefaceName(), FontOptions ((float) height * 0.5f, Font::italic), Colours::grey);
s.draw (g, Rectangle<int> (width, height).expanded (-4, 50).toFloat());
@ -236,6 +242,7 @@ public:
void selectedRowsChanged (int /*lastRowselected*/) override
{
resetMetricsSliders();
refreshPreviewBoxFont();
}
@ -251,14 +258,12 @@ private:
ListBox listBox;
TextEditor demoTextBox;
const double defaultScale = 1.0, defaultHeight = 20.0, defaultKerning = 0.0;
const bool defaultBold = false, defaultItalic = false, defaultUnderlined = false;
const int defaultStyle = 0, defaultHorizontalJustification = 0, defaultVerticalJustification = 0;
Label heightLabel { {}, "Height:" },
kerningLabel { {}, "Kerning:" },
scaleLabel { {}, "Scale:" },
styleLabel { {}, "Style:" },
ascentLabel { {}, "Ascent:" },
descentLabel { {}, "Descent:" },
horizontalJustificationLabel { {}, "Justification (horizontal):" },
verticalJustificationLabel { {}, "Justification (vertical):" };
@ -268,7 +273,7 @@ private:
TextButton resetButton { "Reset" };
Slider heightSlider, kerningSlider, scaleSlider;
Slider heightSlider, kerningSlider, scaleSlider, ascentSlider, descentSlider;
ComboBox styleBox, horizontalJustificationBox, verticalJustificationBox;
StretchableLayoutManager verticalLayout;
@ -283,17 +288,28 @@ private:
//==============================================================================
void resetToDefaultParameters()
{
scaleSlider .setValue (defaultScale);
heightSlider .setValue (defaultHeight);
kerningSlider.setValue (defaultKerning);
scaleSlider .setValue (1.0);
heightSlider .setValue (20.0);
kerningSlider.setValue (0.0);
boldToggle .setToggleState (defaultBold, sendNotificationSync);
italicToggle .setToggleState (defaultItalic, sendNotificationSync);
underlineToggle.setToggleState (defaultUnderlined, sendNotificationSync);
boldToggle .setToggleState (false, sendNotificationSync);
italicToggle .setToggleState (false, sendNotificationSync);
underlineToggle.setToggleState (false, sendNotificationSync);
styleBox.setSelectedItemIndex (defaultStyle);
horizontalJustificationBox.setSelectedItemIndex (defaultHorizontalJustification);
verticalJustificationBox .setSelectedItemIndex (defaultVerticalJustification);
styleBox.setSelectedItemIndex (0);
horizontalJustificationBox.setSelectedItemIndex (0);
verticalJustificationBox .setSelectedItemIndex (0);
resetMetricsSliders();
}
void resetMetricsSliders()
{
auto font = getFont (listBox.getSelectedRow());
font.setPointHeight (1.0f);
ascentSlider .setValue (font.getAscentInPoints());
descentSlider.setValue (font.getDescentInPoints());
}
void setupJustificationOptions()
@ -339,6 +355,8 @@ private:
font = font.withTypefaceStyle (styleBox.getText());
font.setUnderline (underlineToggle.getToggleState());
font.setAscentOverride ((float) ascentSlider .getValue());
font.setDescentOverride ((float) descentSlider.getValue());
demoTextBox.applyFontToAllText (font);
}
@ -353,7 +371,7 @@ private:
styleBox.clear();
styleBox.addItemList (newStyles, 1);
styleBox.setSelectedItemIndex (defaultStyle);
styleBox.setSelectedItemIndex (0);
}
}