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.

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