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.

144 lines
5.2KB

  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. auto extraKerning = font.getExtraKerningFactor();
  65. if (typefaceName == getDefaultFont()) return Font (font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  66. if (typefaceName == getDefaultSans()) return Font (Font::getDefaultSansSerifFontName(), font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  67. if (typefaceName == getDefaultSerif()) return Font (Font::getDefaultSerifFontName(), font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  68. if (typefaceName == getDefaultMono()) return Font (Font::getDefaultMonospacedFontName(), font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  69. auto f = Font (typefaceName, font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  70. if (f.getAvailableStyles().contains (font.getTypefaceStyle()))
  71. f.setTypefaceStyle (font.getTypefaceStyle());
  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. };