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.

141 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. class FontPropertyComponent : public ChoicePropertyComponent
  21. {
  22. public:
  23. FontPropertyComponent (const String& name)
  24. : ChoicePropertyComponent (name)
  25. {
  26. choices.add (getDefaultFont());
  27. choices.add (getDefaultSans());
  28. choices.add (getDefaultSerif());
  29. choices.add (getDefaultMono());
  30. choices.add (String());
  31. static StringArray fontNames;
  32. if (fontNames.size() == 0)
  33. {
  34. Array<Font> fonts;
  35. Font::findFonts (fonts);
  36. for (int i = 0; i < fonts.size(); ++i)
  37. fontNames.add (fonts[i].getTypefaceName());
  38. }
  39. choices.addArray (fontNames);
  40. }
  41. static String getDefaultFont() { return "Default font"; }
  42. static String getDefaultSans() { return "Default sans-serif font"; }
  43. static String getDefaultSerif() { return "Default serif font"; }
  44. static String getDefaultMono() { return "Default monospaced font"; }
  45. //==============================================================================
  46. virtual void setTypefaceName (const String& newFontName) = 0;
  47. virtual String getTypefaceName() const = 0;
  48. //==============================================================================
  49. void setIndex (int newIndex)
  50. {
  51. String type (choices [newIndex]);
  52. if (type.isEmpty())
  53. type = getDefaultFont();
  54. if (getTypefaceName() != type)
  55. setTypefaceName (type);
  56. }
  57. int getIndex() const
  58. {
  59. return choices.indexOf (getTypefaceName());
  60. }
  61. static Font applyNameToFont (const String& typefaceName, const Font& font)
  62. {
  63. if (typefaceName == getDefaultFont()) return Font (font.getHeight(), font.getStyleFlags());
  64. if (typefaceName == getDefaultSans()) return Font (Font::getDefaultSansSerifFontName(), font.getHeight(), font.getStyleFlags());
  65. if (typefaceName == getDefaultSerif()) return Font (Font::getDefaultSerifFontName(), font.getHeight(), font.getStyleFlags());
  66. if (typefaceName == getDefaultMono()) return Font (Font::getDefaultMonospacedFontName(), font.getHeight(), font.getStyleFlags());
  67. auto f = Font (typefaceName, font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (font.getExtraKerningFactor());
  68. if (f.getAvailableStyles().contains (font.getTypefaceStyle()))
  69. {
  70. f.setTypefaceStyle (font.getTypefaceStyle());
  71. }
  72. return f;
  73. }
  74. static String getTypefaceNameCode (const String& typefaceName)
  75. {
  76. if (typefaceName == getDefaultFont()) return {};
  77. if (typefaceName == getDefaultSans()) return "Font::getDefaultSansSerifFontName(), ";
  78. if (typefaceName == getDefaultSerif()) return "Font::getDefaultSerifFontName(), ";
  79. if (typefaceName == getDefaultMono()) return "Font::getDefaultMonospacedFontName(), ";
  80. return "\"" + typefaceName + "\", ";
  81. }
  82. static String getFontStyleCode (const Font& font)
  83. {
  84. if (font.isBold() && font.isItalic()) return "Font::bold | Font::italic";
  85. if (font.isBold()) return "Font::bold";
  86. if (font.isItalic()) return "Font::italic";
  87. return "Font::plain";
  88. }
  89. static String getCompleteFontCode (const Font& font, const String& typefaceName)
  90. {
  91. String s;
  92. s << "Font ("
  93. << getTypefaceNameCode (typefaceName)
  94. << CodeHelpers::floatLiteral (font.getHeight(), 2)
  95. << ", ";
  96. if (font.getAvailableStyles().contains(font.getTypefaceStyle()))
  97. s << "Font::plain).withTypefaceStyle ("
  98. << CodeHelpers::stringLiteral (font.getTypefaceStyle())
  99. << ")";
  100. else
  101. s << getFontStyleCode (font)
  102. << ")";
  103. if (font.getExtraKerningFactor() != 0.0f)
  104. s << ".withExtraKerningFactor ("
  105. << CodeHelpers::floatLiteral (font.getExtraKerningFactor(), 3)
  106. << ")";
  107. return s;
  108. }
  109. };