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.

220 lines
7.1KB

  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. namespace juce
  19. {
  20. static std::unique_ptr<XmlElement> findFontsConfFile()
  21. {
  22. static const char* pathsToSearch[] = { "/etc/fonts/fonts.conf",
  23. "/usr/share/fonts/fonts.conf",
  24. "/usr/local/etc/fonts/fonts.conf",
  25. "/usr/share/defaults/fonts/fonts.conf" };
  26. for (auto* path : pathsToSearch)
  27. if (auto xml = parseXML (File (path)))
  28. return xml;
  29. return {};
  30. }
  31. StringArray FTTypefaceList::getDefaultFontDirectories()
  32. {
  33. StringArray fontDirs;
  34. fontDirs.addTokens (String (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH"))), ";,", "");
  35. fontDirs.removeEmptyStrings (true);
  36. if (fontDirs.isEmpty())
  37. {
  38. if (auto fontsInfo = findFontsConfFile())
  39. {
  40. for (auto* e : fontsInfo->getChildWithTagNameIterator ("dir"))
  41. {
  42. auto fontPath = e->getAllSubText().trim();
  43. if (fontPath.isNotEmpty())
  44. {
  45. if (e->getStringAttribute ("prefix") == "xdg")
  46. {
  47. auto xdgDataHome = SystemStats::getEnvironmentVariable ("XDG_DATA_HOME", {});
  48. if (xdgDataHome.trimStart().isEmpty())
  49. xdgDataHome = "~/.local/share";
  50. fontPath = File (xdgDataHome).getChildFile (fontPath).getFullPathName();
  51. }
  52. fontDirs.add (fontPath);
  53. }
  54. }
  55. }
  56. }
  57. if (fontDirs.isEmpty())
  58. fontDirs.add ("/usr/X11R6/lib/X11/fonts");
  59. fontDirs.removeDuplicates (false);
  60. return fontDirs;
  61. }
  62. Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
  63. {
  64. return new FreeTypeTypeface (font);
  65. }
  66. Typeface::Ptr Typeface::createSystemTypefaceFor (const void* data, size_t dataSize)
  67. {
  68. return new FreeTypeTypeface (data, dataSize);
  69. }
  70. void Typeface::scanFolderForFonts (const File& folder)
  71. {
  72. FTTypefaceList::getInstance()->scanFontPaths (StringArray (folder.getFullPathName()));
  73. }
  74. StringArray Font::findAllTypefaceNames()
  75. {
  76. return FTTypefaceList::getInstance()->findAllFamilyNames();
  77. }
  78. StringArray Font::findAllTypefaceStyles (const String& family)
  79. {
  80. return FTTypefaceList::getInstance()->findAllTypefaceStyles (family);
  81. }
  82. bool TextLayout::createNativeLayout (const AttributedString&)
  83. {
  84. return false;
  85. }
  86. //==============================================================================
  87. struct DefaultFontInfo
  88. {
  89. DefaultFontInfo()
  90. : defaultSans (getDefaultSansSerifFontName()),
  91. defaultSerif (getDefaultSerifFontName()),
  92. defaultFixed (getDefaultMonospacedFontName())
  93. {
  94. }
  95. String getRealFontName (const String& faceName) const
  96. {
  97. if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
  98. if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
  99. if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
  100. return faceName;
  101. }
  102. String defaultSans, defaultSerif, defaultFixed;
  103. private:
  104. template <typename Range>
  105. static String pickBestFont (const StringArray& names, Range&& choicesArray)
  106. {
  107. for (auto& choice : choicesArray)
  108. if (names.contains (choice, true))
  109. return choice;
  110. for (auto& choice : choicesArray)
  111. for (auto& name : names)
  112. if (name.startsWithIgnoreCase (choice))
  113. return name;
  114. for (auto& choice : choicesArray)
  115. for (auto& name : names)
  116. if (name.containsIgnoreCase (choice))
  117. return name;
  118. return names[0];
  119. }
  120. static String getDefaultSansSerifFontName()
  121. {
  122. StringArray allFonts;
  123. FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
  124. static constexpr const char* targets[] { "Verdana",
  125. "Bitstream Vera Sans",
  126. "Luxi Sans",
  127. "Liberation Sans",
  128. "DejaVu Sans",
  129. "Sans" };
  130. return pickBestFont (allFonts, targets);
  131. }
  132. static String getDefaultSerifFontName()
  133. {
  134. StringArray allFonts;
  135. FTTypefaceList::getInstance()->getSerifNames (allFonts);
  136. static constexpr const char* targets[] { "Bitstream Vera Serif",
  137. "Times",
  138. "Nimbus Roman",
  139. "Liberation Serif",
  140. "DejaVu Serif",
  141. "Serif" };
  142. return pickBestFont (allFonts, targets);
  143. }
  144. static String getDefaultMonospacedFontName()
  145. {
  146. StringArray allFonts;
  147. FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
  148. static constexpr const char* targets[] { "DejaVu Sans Mono",
  149. "Bitstream Vera Sans Mono",
  150. "Sans Mono",
  151. "Liberation Mono",
  152. "Courier",
  153. "DejaVu Mono",
  154. "Mono" };
  155. return pickBestFont (allFonts, targets);
  156. }
  157. JUCE_DECLARE_NON_COPYABLE (DefaultFontInfo)
  158. };
  159. Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
  160. {
  161. static const DefaultFontInfo defaultInfo;
  162. Font f (font);
  163. const auto name = font.getTypefaceName();
  164. const auto realName = defaultInfo.getRealFontName (name);
  165. f.setTypefaceName (realName);
  166. const auto styles = findAllTypefaceStyles (realName);
  167. if (! styles.contains (font.getTypefaceStyle()))
  168. f.setTypefaceStyle (styles[0]);
  169. return Typeface::createSystemTypefaceFor (f);
  170. }
  171. } // namespace juce