Audio plugin host https://kx.studio/carla
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.

juce_linux_Fonts.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. struct Characteristics
  90. {
  91. explicit Characteristics (String nameIn) : name (nameIn) {}
  92. Characteristics withStyle (String styleIn) const
  93. {
  94. auto copy = *this;
  95. copy.style = std::move (styleIn);
  96. return copy;
  97. }
  98. String name, style;
  99. };
  100. DefaultFontInfo()
  101. : defaultSans (getDefaultSansSerifFontCharacteristics()),
  102. defaultSerif (getDefaultSerifFontCharacteristics()),
  103. defaultFixed (getDefaultMonospacedFontCharacteristics())
  104. {
  105. }
  106. Characteristics getRealFontCharacteristics (const String& faceName) const
  107. {
  108. if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
  109. if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
  110. if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
  111. return Characteristics { faceName };
  112. }
  113. Characteristics defaultSans, defaultSerif, defaultFixed;
  114. private:
  115. template <typename Range>
  116. static Characteristics pickBestFont (const StringArray& names, Range&& choicesArray)
  117. {
  118. for (auto& choice : choicesArray)
  119. if (names.contains (choice.name, true))
  120. return choice;
  121. for (auto& choice : choicesArray)
  122. for (auto& name : names)
  123. if (name.startsWithIgnoreCase (choice.name))
  124. return Characteristics { name }.withStyle (choice.style);
  125. for (auto& choice : choicesArray)
  126. for (auto& name : names)
  127. if (name.containsIgnoreCase (choice.name))
  128. return Characteristics { name }.withStyle (choice.style);
  129. return Characteristics { names[0] };
  130. }
  131. static Characteristics getDefaultSansSerifFontCharacteristics()
  132. {
  133. StringArray allFonts;
  134. FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
  135. static const Characteristics targets[] { Characteristics { "Verdana" },
  136. Characteristics { "Bitstream Vera Sans" }.withStyle ("Roman"),
  137. Characteristics { "Luxi Sans" },
  138. Characteristics { "Liberation Sans" },
  139. Characteristics { "DejaVu Sans" },
  140. Characteristics { "Sans" } };
  141. return pickBestFont (allFonts, targets);
  142. }
  143. static Characteristics getDefaultSerifFontCharacteristics()
  144. {
  145. StringArray allFonts;
  146. FTTypefaceList::getInstance()->getSerifNames (allFonts);
  147. static const Characteristics targets[] { Characteristics { "Bitstream Vera Serif" }.withStyle ("Roman"),
  148. Characteristics { "Times" },
  149. Characteristics { "Nimbus Roman" },
  150. Characteristics { "Liberation Serif" },
  151. Characteristics { "DejaVu Serif" },
  152. Characteristics { "Serif" } };
  153. return pickBestFont (allFonts, targets);
  154. }
  155. static Characteristics getDefaultMonospacedFontCharacteristics()
  156. {
  157. StringArray allFonts;
  158. FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
  159. static const Characteristics targets[] { Characteristics { "DejaVu Sans Mono" },
  160. Characteristics { "Bitstream Vera Sans Mono" }.withStyle ("Roman"),
  161. Characteristics { "Sans Mono" },
  162. Characteristics { "Liberation Mono" },
  163. Characteristics { "Courier" },
  164. Characteristics { "DejaVu Mono" },
  165. Characteristics { "Mono" } };
  166. return pickBestFont (allFonts, targets);
  167. }
  168. JUCE_DECLARE_NON_COPYABLE (DefaultFontInfo)
  169. };
  170. Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
  171. {
  172. static const DefaultFontInfo defaultInfo;
  173. Font f (font);
  174. const auto name = font.getTypefaceName();
  175. const auto characteristics = defaultInfo.getRealFontCharacteristics (name);
  176. f.setTypefaceName (characteristics.name);
  177. const auto styles = findAllTypefaceStyles (name);
  178. if (! styles.contains (font.getTypefaceStyle()))
  179. f.setTypefaceStyle (characteristics.style);
  180. return Typeface::createSystemTypefaceFor (f);
  181. }
  182. } // namespace juce