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.

143 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  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. auto extraKerning = font.getExtraKerningFactor();
  64. if (typefaceName == getDefaultFont()) return Font (font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  65. if (typefaceName == getDefaultSans()) return Font (Font::getDefaultSansSerifFontName(), font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  66. if (typefaceName == getDefaultSerif()) return Font (Font::getDefaultSerifFontName(), font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  67. if (typefaceName == getDefaultMono()) return Font (Font::getDefaultMonospacedFontName(), font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  68. auto f = Font (typefaceName, font.getHeight(), font.getStyleFlags()).withExtraKerningFactor (extraKerning);
  69. if (f.getAvailableStyles().contains (font.getTypefaceStyle()))
  70. f.setTypefaceStyle (font.getTypefaceStyle());
  71. return f;
  72. }
  73. static String getTypefaceNameCode (const String& typefaceName)
  74. {
  75. if (typefaceName == getDefaultFont()) return {};
  76. if (typefaceName == getDefaultSans()) return "juce::Font::getDefaultSansSerifFontName(), ";
  77. if (typefaceName == getDefaultSerif()) return "juce::Font::getDefaultSerifFontName(), ";
  78. if (typefaceName == getDefaultMono()) return "juce::Font::getDefaultMonospacedFontName(), ";
  79. return "\"" + typefaceName + "\", ";
  80. }
  81. static String getFontStyleCode (const Font& font)
  82. {
  83. if (font.isBold() && font.isItalic()) return "juce::Font::bold | juce::Font::italic";
  84. if (font.isBold()) return "juce::Font::bold";
  85. if (font.isItalic()) return "juce::Font::italic";
  86. return "juce::Font::plain";
  87. }
  88. static String getCompleteFontCode (const Font& font, const String& typefaceName)
  89. {
  90. String s;
  91. s << "juce::Font ("
  92. << getTypefaceNameCode (typefaceName)
  93. << CodeHelpers::floatLiteral (font.getHeight(), 2)
  94. << ", ";
  95. if (font.getAvailableStyles().contains (font.getTypefaceStyle()))
  96. s << "juce::Font::plain).withTypefaceStyle ("
  97. << CodeHelpers::stringLiteral (font.getTypefaceStyle())
  98. << ")";
  99. else
  100. s << getFontStyleCode (font)
  101. << ")";
  102. if (! approximatelyEqual (font.getExtraKerningFactor(), 0.0f))
  103. s << ".withExtraKerningFactor ("
  104. << CodeHelpers::floatLiteral (font.getExtraKerningFactor(), 3)
  105. << ")";
  106. return s;
  107. }
  108. };