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.

198 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. static XmlElement* findFontsConfFile()
  22. {
  23. static const char* pathsToSearch[] = { "/etc/fonts/fonts.conf",
  24. "/usr/share/fonts/fonts.conf" };
  25. for (auto* path : pathsToSearch)
  26. if (auto* xml = XmlDocument::parse (File (path)))
  27. return xml;
  28. return nullptr;
  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 (ScopedPointer<XmlElement> fontsInfo = findFontsConfFile())
  38. {
  39. forEachXmlChildElementWithTagName (*fontsInfo, e, "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 DefaultFontNames
  87. {
  88. DefaultFontNames()
  89. : defaultSans (getDefaultSansSerifFontName()),
  90. defaultSerif (getDefaultSerifFontName()),
  91. defaultFixed (getDefaultMonospacedFontName())
  92. {
  93. }
  94. String getRealFontName (const String& faceName) const
  95. {
  96. if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
  97. if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
  98. if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
  99. return faceName;
  100. }
  101. String defaultSans, defaultSerif, defaultFixed;
  102. private:
  103. static String pickBestFont (const StringArray& names, const char* const* choicesArray)
  104. {
  105. const StringArray choices (choicesArray);
  106. for (auto& choice : choices)
  107. if (names.contains (choice, true))
  108. return choice;
  109. for (auto& choice : choices)
  110. for (auto& name : names)
  111. if (name.startsWithIgnoreCase (choice))
  112. return name;
  113. for (auto& choice : choices)
  114. for (auto& name : names)
  115. if (name.containsIgnoreCase (choice))
  116. return name;
  117. return names[0];
  118. }
  119. static String getDefaultSansSerifFontName()
  120. {
  121. StringArray allFonts;
  122. FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
  123. static const char* targets[] = { "Verdana", "Bitstream Vera Sans", "Luxi Sans",
  124. "Liberation Sans", "DejaVu Sans", "Sans", nullptr };
  125. return pickBestFont (allFonts, targets);
  126. }
  127. static String getDefaultSerifFontName()
  128. {
  129. StringArray allFonts;
  130. FTTypefaceList::getInstance()->getSerifNames (allFonts);
  131. static const char* targets[] = { "Bitstream Vera Serif", "Times", "Nimbus Roman",
  132. "Liberation Serif", "DejaVu Serif", "Serif", nullptr };
  133. return pickBestFont (allFonts, targets);
  134. }
  135. static String getDefaultMonospacedFontName()
  136. {
  137. StringArray allFonts;
  138. FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
  139. static const char* targets[] = { "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Sans Mono",
  140. "Liberation Mono", "Courier", "DejaVu Mono", "Mono", nullptr };
  141. return pickBestFont (allFonts, targets);
  142. }
  143. JUCE_DECLARE_NON_COPYABLE (DefaultFontNames)
  144. };
  145. Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
  146. {
  147. static DefaultFontNames defaultNames;
  148. Font f (font);
  149. f.setTypefaceName (defaultNames.getRealFontName (font.getTypefaceName()));
  150. return Typeface::createSystemTypefaceFor (f);
  151. }
  152. } // namespace juce