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.

136 lines
5.0KB

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