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.

142 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. class FontPropertyComponent : public ChoicePropertyComponent
  22. {
  23. public:
  24. FontPropertyComponent (const String& name)
  25. : ChoicePropertyComponent (name)
  26. {
  27. choices.add (getDefaultFont());
  28. choices.add (getDefaultSans());
  29. choices.add (getDefaultSerif());
  30. choices.add (getDefaultMono());
  31. choices.add (String());
  32. static StringArray fontNames;
  33. if (fontNames.size() == 0)
  34. {
  35. Array<Font> fonts;
  36. Font::findFonts (fonts);
  37. for (int i = 0; i < fonts.size(); ++i)
  38. fontNames.add (fonts[i].getTypefaceName());
  39. }
  40. choices.addArray (fontNames);
  41. }
  42. static String getDefaultFont() { return "Default font"; }
  43. static String getDefaultSans() { return "Default sans-serif font"; }
  44. static String getDefaultSerif() { return "Default serif font"; }
  45. static String getDefaultMono() { return "Default monospaced font"; }
  46. //==============================================================================
  47. virtual void setTypefaceName (const String& newFontName) = 0;
  48. virtual String getTypefaceName() const = 0;
  49. //==============================================================================
  50. void setIndex (int newIndex)
  51. {
  52. String type (choices [newIndex]);
  53. if (type.isEmpty())
  54. type = getDefaultFont();
  55. if (getTypefaceName() != type)
  56. setTypefaceName (type);
  57. }
  58. int getIndex() const
  59. {
  60. return choices.indexOf (getTypefaceName());
  61. }
  62. static Font applyNameToFont (const String& typefaceName, const Font& font)
  63. {
  64. if (typefaceName == getDefaultFont()) return Font (font.getHeight(), font.getStyleFlags());
  65. if (typefaceName == getDefaultSans()) return Font (Font::getDefaultSansSerifFontName(), font.getHeight(), font.getStyleFlags());
  66. if (typefaceName == getDefaultSerif()) return Font (Font::getDefaultSerifFontName(), font.getHeight(), font.getStyleFlags());
  67. if (typefaceName == getDefaultMono()) return Font (Font::getDefaultMonospacedFontName(), font.getHeight(), font.getStyleFlags());
  68. auto f = Font (typefaceName, font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (font.getExtraKerningFactor());
  69. if (f.getAvailableStyles().contains (font.getTypefaceStyle()))
  70. {
  71. f.setTypefaceStyle (font.getTypefaceStyle());
  72. }
  73. return f;
  74. }
  75. static String getTypefaceNameCode (const String& typefaceName)
  76. {
  77. if (typefaceName == getDefaultFont()) return {};
  78. if (typefaceName == getDefaultSans()) return "Font::getDefaultSansSerifFontName(), ";
  79. if (typefaceName == getDefaultSerif()) return "Font::getDefaultSerifFontName(), ";
  80. if (typefaceName == getDefaultMono()) return "Font::getDefaultMonospacedFontName(), ";
  81. return "\"" + typefaceName + "\", ";
  82. }
  83. static String getFontStyleCode (const Font& font)
  84. {
  85. if (font.isBold() && font.isItalic()) return "Font::bold | Font::italic";
  86. if (font.isBold()) return "Font::bold";
  87. if (font.isItalic()) return "Font::italic";
  88. return "Font::plain";
  89. }
  90. static String getCompleteFontCode (const Font& font, const String& typefaceName)
  91. {
  92. String s;
  93. s << "Font ("
  94. << getTypefaceNameCode (typefaceName)
  95. << CodeHelpers::floatLiteral (font.getHeight(), 2)
  96. << ", ";
  97. if (font.getAvailableStyles().contains(font.getTypefaceStyle()))
  98. s << "Font::plain).withTypefaceStyle ("
  99. << CodeHelpers::stringLiteral (font.getTypefaceStyle())
  100. << ")";
  101. else
  102. s << getFontStyleCode (font)
  103. << ")";
  104. if (font.getExtraKerningFactor() != 0.0f)
  105. s << ".withExtraKerningFactor ("
  106. << CodeHelpers::floatLiteral (font.getExtraKerningFactor(), 3)
  107. << ")";
  108. return s;
  109. }
  110. };