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.

233 lines
8.1KB

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