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.

119 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. class FontPropertyComponent : public ChoicePropertyComponent
  19. {
  20. public:
  21. FontPropertyComponent (const String& name)
  22. : ChoicePropertyComponent (name)
  23. {
  24. choices.add (getDefaultFont());
  25. choices.add (getDefaultSans());
  26. choices.add (getDefaultSerif());
  27. choices.add (getDefaultMono());
  28. choices.add (String());
  29. static StringArray fontNames;
  30. if (fontNames.size() == 0)
  31. {
  32. Array<Font> fonts;
  33. Font::findFonts (fonts);
  34. for (int i = 0; i < fonts.size(); ++i)
  35. fontNames.add (fonts[i].getTypefaceName());
  36. }
  37. choices.addArray (fontNames);
  38. }
  39. static String getDefaultFont() { return "Default font"; }
  40. static String getDefaultSans() { return "Default sans-serif font"; }
  41. static String getDefaultSerif() { return "Default serif font"; }
  42. static String getDefaultMono() { return "Default monospaced font"; }
  43. //==============================================================================
  44. virtual void setTypefaceName (const String& newFontName) = 0;
  45. virtual String getTypefaceName() const = 0;
  46. //==============================================================================
  47. void setIndex (int newIndex)
  48. {
  49. String type (choices [newIndex]);
  50. if (type.isEmpty())
  51. type = getDefaultFont();
  52. if (getTypefaceName() != type)
  53. setTypefaceName (type);
  54. }
  55. int getIndex() const
  56. {
  57. return choices.indexOf (getTypefaceName());
  58. }
  59. static Font applyNameToFont (const String& typefaceName, const Font& font)
  60. {
  61. if (typefaceName == getDefaultFont()) return Font (font.getHeight(), font.getStyleFlags());
  62. if (typefaceName == getDefaultSans()) return Font (Font::getDefaultSansSerifFontName(), font.getHeight(), font.getStyleFlags());
  63. if (typefaceName == getDefaultSerif()) return Font (Font::getDefaultSerifFontName(), font.getHeight(), font.getStyleFlags());
  64. if (typefaceName == getDefaultMono()) return Font (Font::getDefaultMonospacedFontName(), font.getHeight(), font.getStyleFlags());
  65. return Font (typefaceName, font.getHeight(), font.getStyleFlags());
  66. }
  67. static String getTypefaceNameCode (const String& typefaceName)
  68. {
  69. if (typefaceName == getDefaultFont()) return String();
  70. if (typefaceName == getDefaultSans()) return "Font::getDefaultSansSerifFontName(), ";
  71. if (typefaceName == getDefaultSerif()) return "Font::getDefaultSerifFontName(), ";
  72. if (typefaceName == getDefaultMono()) return "Font::getDefaultMonospacedFontName(), ";
  73. return "\"" + typefaceName + "\", ";
  74. }
  75. static String getFontStyleCode (const Font& font)
  76. {
  77. if (font.isBold() && font.isItalic()) return "Font::bold | Font::italic";
  78. if (font.isBold()) return "Font::bold";
  79. if (font.isItalic()) return "Font::italic";
  80. return "Font::plain";
  81. }
  82. static String getCompleteFontCode (const Font& font, const String& typefaceName)
  83. {
  84. return "Font ("
  85. + getTypefaceNameCode (typefaceName)
  86. + CodeHelpers::floatLiteral (font.getHeight(), 2)
  87. + ", "
  88. + getFontStyleCode (font)
  89. + ")";
  90. }
  91. };