The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: FontsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays different font styles and types.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics
  26. exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: FontsDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class FontsDemo final : public Component,
  37. private ListBoxModel,
  38. private Slider::Listener
  39. {
  40. public:
  41. FontsDemo()
  42. {
  43. setOpaque (true);
  44. addAndMakeVisible (listBox);
  45. addAndMakeVisible (demoTextBox);
  46. addAndMakeVisible (heightSlider);
  47. addAndMakeVisible (heightLabel);
  48. addAndMakeVisible (kerningLabel);
  49. addAndMakeVisible (kerningSlider);
  50. addAndMakeVisible (scaleLabel);
  51. addAndMakeVisible (horizontalJustificationLabel);
  52. addAndMakeVisible (verticalJustificationLabel);
  53. addAndMakeVisible (scaleSlider);
  54. addAndMakeVisible (boldToggle);
  55. addAndMakeVisible (italicToggle);
  56. addAndMakeVisible (underlineToggle);
  57. addAndMakeVisible (styleBox);
  58. addAndMakeVisible (horizontalJustificationBox);
  59. addAndMakeVisible (verticalJustificationBox);
  60. addAndMakeVisible (resetButton);
  61. kerningLabel .attachToComponent (&kerningSlider, true);
  62. heightLabel .attachToComponent (&heightSlider, true);
  63. scaleLabel .attachToComponent (&scaleSlider, true);
  64. styleLabel .attachToComponent (&styleBox, true);
  65. horizontalJustificationLabel.attachToComponent (&horizontalJustificationBox, true);
  66. verticalJustificationLabel .attachToComponent (&verticalJustificationBox, true);
  67. heightSlider .addListener (this);
  68. kerningSlider.addListener (this);
  69. scaleSlider .addListener (this);
  70. boldToggle .onClick = [this] { refreshPreviewBoxFont(); };
  71. italicToggle .onClick = [this] { refreshPreviewBoxFont(); };
  72. underlineToggle.onClick = [this] { refreshPreviewBoxFont(); };
  73. styleBox .onChange = [this] { refreshPreviewBoxFont(); };
  74. Font::findFonts (fonts); // Generate the list of fonts
  75. listBox.setTitle ("Fonts");
  76. listBox.setRowHeight (20);
  77. listBox.setModel (this); // Tell the listbox where to get its data model
  78. listBox.setColour (ListBox::textColourId, Colours::black);
  79. listBox.setColour (ListBox::backgroundColourId, Colours::white);
  80. heightSlider .setRange (3.0, 150.0, 0.01);
  81. scaleSlider .setRange (0.2, 3.0, 0.01);
  82. kerningSlider.setRange (-2.0, 2.0, 0.01);
  83. // set up the layout and resizer bars..
  84. verticalLayout.setItemLayout (0, -0.2, -0.8, -0.35); // width of the font list must be
  85. // between 20% and 80%, preferably 50%
  86. verticalLayout.setItemLayout (1, 8, 8, 8); // the vertical divider drag-bar thing is always 8 pixels wide
  87. verticalLayout.setItemLayout (2, 150, -1.0, -0.65); // the components on the right must be
  88. // at least 150 pixels wide, preferably 50% of the total width
  89. verticalDividerBar.reset (new StretchableLayoutResizerBar (&verticalLayout, 1, true));
  90. addAndMakeVisible (verticalDividerBar.get());
  91. // ..and pick a random font to select initially
  92. listBox.selectRow (Random::getSystemRandom().nextInt (fonts.size()));
  93. demoTextBox.setMultiLine (true);
  94. demoTextBox.setReturnKeyStartsNewLine (true);
  95. demoTextBox.setText ("Aa Bb Cc Dd Ee Ff Gg Hh Ii\n"
  96. "Jj Kk Ll Mm Nn Oo Pp Qq Rr\n"
  97. "Ss Tt Uu Vv Ww Xx Yy Zz\n"
  98. "0123456789\n\n"
  99. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
  100. "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
  101. "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
  102. "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
  103. "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
  104. demoTextBox.setCaretPosition (0);
  105. demoTextBox.setColour (TextEditor::textColourId, Colours::black);
  106. demoTextBox.setColour (TextEditor::backgroundColourId, Colours::white);
  107. demoTextBox.setWhitespaceUnderlined (false);
  108. resetButton.onClick = [this] { resetToDefaultParameters(); };
  109. setupJustificationOptions();
  110. resetToDefaultParameters();
  111. setSize (750, 750);
  112. }
  113. //==============================================================================
  114. void paint (Graphics& g) override
  115. {
  116. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  117. }
  118. void resized() override
  119. {
  120. auto r = getLocalBounds().reduced (5);
  121. // lay out the list box and vertical divider..
  122. Component* vcomps[] = { &listBox, verticalDividerBar.get(), nullptr };
  123. verticalLayout.layOutComponents (vcomps, 3,
  124. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  125. false, // lay out side-by-side
  126. true); // resize the components' heights as well as widths
  127. r.removeFromLeft (verticalDividerBar->getRight());
  128. resetButton.setBounds (r.removeFromBottom (30).reduced (jmax (20, r.getWidth() / 5), 0));
  129. r.removeFromBottom (8);
  130. const int labelWidth = 60;
  131. auto styleArea = r.removeFromBottom (26);
  132. styleArea.removeFromLeft (labelWidth);
  133. styleBox.setBounds (styleArea);
  134. r.removeFromBottom (8);
  135. auto row = r.removeFromBottom (30);
  136. row.removeFromLeft (labelWidth);
  137. auto toggleWidth = row.getWidth() / 3;
  138. boldToggle .setBounds (row.removeFromLeft (toggleWidth));
  139. italicToggle .setBounds (row.removeFromLeft (toggleWidth));
  140. underlineToggle.setBounds (row);
  141. r.removeFromBottom (8);
  142. horizontalJustificationBox.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth * 3));
  143. r.removeFromBottom (8);
  144. verticalJustificationBox.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth * 3));
  145. r.removeFromBottom (8);
  146. scaleSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  147. r.removeFromBottom (8);
  148. kerningSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  149. r.removeFromBottom (8);
  150. heightSlider.setBounds (r.removeFromBottom (30).withTrimmedLeft (labelWidth));
  151. r.removeFromBottom (8);
  152. demoTextBox.setBounds (r);
  153. }
  154. void sliderValueChanged (Slider* sliderThatWasMoved) override
  155. {
  156. if (sliderThatWasMoved == &heightSlider) refreshPreviewBoxFont();
  157. else if (sliderThatWasMoved == &kerningSlider) refreshPreviewBoxFont();
  158. else if (sliderThatWasMoved == &scaleSlider) refreshPreviewBoxFont();
  159. }
  160. // The following methods implement the ListBoxModel virtual methods:
  161. int getNumRows() override
  162. {
  163. return fonts.size();
  164. }
  165. void paintListBoxItem (int rowNumber, Graphics& g,
  166. int width, int height, bool rowIsSelected) override
  167. {
  168. if (rowIsSelected)
  169. g.fillAll (Colours::lightblue);
  170. auto font = fonts[rowNumber];
  171. AttributedString s;
  172. s.setWordWrap (AttributedString::none);
  173. s.setJustification (Justification::centredLeft);
  174. s.append (getNameForRow (rowNumber), font.withHeight ((float) height * 0.7f), Colours::black);
  175. s.append (" " + font.getTypefaceName(), Font ((float) height * 0.5f, Font::italic), Colours::grey);
  176. s.draw (g, Rectangle<int> (width, height).expanded (-4, 50).toFloat());
  177. }
  178. String getNameForRow (int rowNumber) override
  179. {
  180. return fonts[rowNumber].getTypefaceName();
  181. }
  182. void selectedRowsChanged (int /*lastRowselected*/) override
  183. {
  184. refreshPreviewBoxFont();
  185. }
  186. private:
  187. Array<Font> fonts;
  188. StringArray currentStyleList;
  189. ListBox listBox;
  190. TextEditor demoTextBox;
  191. const double defaultScale = 1.0, defaultHeight = 20.0, defaultKerning = 0.0;
  192. const bool defaultBold = false, defaultItalic = false, defaultUnderlined = false;
  193. const int defaultStyle = 0, defaultHorizontalJustification = 0, defaultVerticalJustification = 0;
  194. Label heightLabel { {}, "Height:" },
  195. kerningLabel { {}, "Kerning:" },
  196. scaleLabel { {}, "Scale:" },
  197. styleLabel { {}, "Style:" },
  198. horizontalJustificationLabel { {}, "Justification (horizontal):" },
  199. verticalJustificationLabel { {}, "Justification (vertical):" };
  200. ToggleButton boldToggle { "Bold" },
  201. italicToggle { "Italic" },
  202. underlineToggle { "Underlined" };
  203. TextButton resetButton { "Reset" };
  204. Slider heightSlider, kerningSlider, scaleSlider;
  205. ComboBox styleBox, horizontalJustificationBox, verticalJustificationBox;
  206. StretchableLayoutManager verticalLayout;
  207. std::unique_ptr<StretchableLayoutResizerBar> verticalDividerBar;
  208. StringArray horizontalJustificationStrings { "Left", "Centred", "Right" },
  209. verticalJustificationStrings { "Top", "Centred", "Bottom" };
  210. Array<int> horizontalJustificationFlags { Justification::left, Justification::horizontallyCentred, Justification::right },
  211. verticalJustificationFlags { Justification::top, Justification::verticallyCentred, Justification::bottom};
  212. //==============================================================================
  213. void resetToDefaultParameters()
  214. {
  215. scaleSlider .setValue (defaultScale);
  216. heightSlider .setValue (defaultHeight);
  217. kerningSlider.setValue (defaultKerning);
  218. boldToggle .setToggleState (defaultBold, sendNotificationSync);
  219. italicToggle .setToggleState (defaultItalic, sendNotificationSync);
  220. underlineToggle.setToggleState (defaultUnderlined, sendNotificationSync);
  221. styleBox.setSelectedItemIndex (defaultStyle);
  222. horizontalJustificationBox.setSelectedItemIndex (defaultHorizontalJustification);
  223. verticalJustificationBox .setSelectedItemIndex (defaultVerticalJustification);
  224. }
  225. void setupJustificationOptions()
  226. {
  227. horizontalJustificationBox.addItemList (horizontalJustificationStrings, 1);
  228. verticalJustificationBox .addItemList (verticalJustificationStrings, 1);
  229. auto updateJustification = [this]()
  230. {
  231. auto horizontalIndex = horizontalJustificationBox.getSelectedItemIndex();
  232. auto verticalIndex = verticalJustificationBox.getSelectedItemIndex();
  233. auto horizontalJustification = horizontalJustificationFlags[horizontalIndex];
  234. auto verticalJustification = verticalJustificationFlags[verticalIndex];
  235. demoTextBox.setJustification (horizontalJustification | verticalJustification);
  236. };
  237. horizontalJustificationBox.onChange = updateJustification;
  238. verticalJustificationBox .onChange = updateJustification;
  239. }
  240. void refreshPreviewBoxFont()
  241. {
  242. auto bold = boldToggle .getToggleState();
  243. auto italic = italicToggle.getToggleState();
  244. auto useStyle = ! (bold || italic);
  245. auto font = fonts[listBox.getSelectedRow()];
  246. font = font.withPointHeight ((float) heightSlider .getValue())
  247. .withExtraKerningFactor ((float) kerningSlider.getValue())
  248. .withHorizontalScale ((float) scaleSlider .getValue());
  249. if (bold) font = font.boldened();
  250. if (italic) font = font.italicised();
  251. updateStylesList (font);
  252. styleBox.setEnabled (useStyle);
  253. if (useStyle)
  254. font = font.withTypefaceStyle (styleBox.getText());
  255. font.setUnderline (underlineToggle.getToggleState());
  256. demoTextBox.applyFontToAllText (font);
  257. }
  258. void updateStylesList (const Font& newFont)
  259. {
  260. auto newStyles = newFont.getAvailableStyles();
  261. if (newStyles != currentStyleList)
  262. {
  263. currentStyleList = newStyles;
  264. styleBox.clear();
  265. styleBox.addItemList (newStyles, 1);
  266. styleBox.setSelectedItemIndex (defaultStyle);
  267. }
  268. }
  269. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo)
  270. };