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.

139 lines
5.0KB

  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. auto f = Font (typefaceName, font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (font.getExtraKerningFactor());
  66. if (f.getAvailableStyles().contains (font.getTypefaceStyle()))
  67. {
  68. f.setTypefaceStyle (font.getTypefaceStyle());
  69. }
  70. return f;
  71. }
  72. static String getTypefaceNameCode (const String& typefaceName)
  73. {
  74. if (typefaceName == getDefaultFont()) return String();
  75. if (typefaceName == getDefaultSans()) return "Font::getDefaultSansSerifFontName(), ";
  76. if (typefaceName == getDefaultSerif()) return "Font::getDefaultSerifFontName(), ";
  77. if (typefaceName == getDefaultMono()) return "Font::getDefaultMonospacedFontName(), ";
  78. return "\"" + typefaceName + "\", ";
  79. }
  80. static String getFontStyleCode (const Font& font)
  81. {
  82. if (font.isBold() && font.isItalic()) return "Font::bold | Font::italic";
  83. if (font.isBold()) return "Font::bold";
  84. if (font.isItalic()) return "Font::italic";
  85. return "Font::plain";
  86. }
  87. static String getCompleteFontCode (const Font& font, const String& typefaceName)
  88. {
  89. String s;
  90. s << "Font ("
  91. << getTypefaceNameCode (typefaceName)
  92. << CodeHelpers::floatLiteral (font.getHeight(), 2)
  93. << ", ";
  94. if (font.getAvailableStyles().contains(font.getTypefaceStyle()))
  95. s << "Font::plain).withTypefaceStyle ("
  96. << CodeHelpers::stringLiteral (font.getTypefaceStyle())
  97. << ")";
  98. else
  99. s << getFontStyleCode (font)
  100. << ")";
  101. if (font.getExtraKerningFactor() != 0)
  102. s << ".withExtraKerningFactor ("
  103. << CodeHelpers::floatLiteral (font.getExtraKerningFactor(), 3)
  104. << ")";
  105. return s;
  106. }
  107. };