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.

191 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. static XmlElement* findFontsConfFile()
  18. {
  19. static const char* pathsToSearch[] = { "/etc/fonts/fonts.conf",
  20. "/usr/share/fonts/fonts.conf" };
  21. for (auto* path : pathsToSearch)
  22. if (auto* xml = XmlDocument::parse (File (path)))
  23. return xml;
  24. return nullptr;
  25. }
  26. StringArray FTTypefaceList::getDefaultFontDirectories()
  27. {
  28. StringArray fontDirs;
  29. fontDirs.addTokens (String (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH"))), ";,", "");
  30. fontDirs.removeEmptyStrings (true);
  31. if (fontDirs.isEmpty())
  32. {
  33. if (ScopedPointer<XmlElement> fontsInfo = findFontsConfFile())
  34. {
  35. forEachXmlChildElementWithTagName (*fontsInfo, e, "dir")
  36. {
  37. auto fontPath = e->getAllSubText().trim();
  38. if (fontPath.isNotEmpty())
  39. {
  40. if (e->getStringAttribute ("prefix") == "xdg")
  41. {
  42. auto xdgDataHome = SystemStats::getEnvironmentVariable ("XDG_DATA_HOME", {});
  43. if (xdgDataHome.trimStart().isEmpty())
  44. xdgDataHome = "~/.local/share";
  45. fontPath = File (xdgDataHome).getChildFile (fontPath).getFullPathName();
  46. }
  47. fontDirs.add (fontPath);
  48. }
  49. }
  50. }
  51. }
  52. if (fontDirs.isEmpty())
  53. fontDirs.add ("/usr/X11R6/lib/X11/fonts");
  54. fontDirs.removeDuplicates (false);
  55. return fontDirs;
  56. }
  57. Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
  58. {
  59. return new FreeTypeTypeface (font);
  60. }
  61. Typeface::Ptr Typeface::createSystemTypefaceFor (const void* data, size_t dataSize)
  62. {
  63. return new FreeTypeTypeface (data, dataSize);
  64. }
  65. void Typeface::scanFolderForFonts (const File& folder)
  66. {
  67. FTTypefaceList::getInstance()->scanFontPaths (StringArray (folder.getFullPathName()));
  68. }
  69. StringArray Font::findAllTypefaceNames()
  70. {
  71. return FTTypefaceList::getInstance()->findAllFamilyNames();
  72. }
  73. StringArray Font::findAllTypefaceStyles (const String& family)
  74. {
  75. return FTTypefaceList::getInstance()->findAllTypefaceStyles (family);
  76. }
  77. bool TextLayout::createNativeLayout (const AttributedString&)
  78. {
  79. return false;
  80. }
  81. //==============================================================================
  82. struct DefaultFontNames
  83. {
  84. DefaultFontNames()
  85. : defaultSans (getDefaultSansSerifFontName()),
  86. defaultSerif (getDefaultSerifFontName()),
  87. defaultFixed (getDefaultMonospacedFontName())
  88. {
  89. }
  90. String getRealFontName (const String& faceName) const
  91. {
  92. if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
  93. if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
  94. if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
  95. return faceName;
  96. }
  97. String defaultSans, defaultSerif, defaultFixed;
  98. private:
  99. static String pickBestFont (const StringArray& names, const char* const* choicesArray)
  100. {
  101. const StringArray choices (choicesArray);
  102. for (auto& choice : choices)
  103. if (names.contains (choice, true))
  104. return choice;
  105. for (auto& choice : choices)
  106. for (auto& name : names)
  107. if (name.startsWithIgnoreCase (choice))
  108. return name;
  109. for (auto& choice : choices)
  110. for (auto& name : names)
  111. if (name.containsIgnoreCase (choice))
  112. return name;
  113. return names[0];
  114. }
  115. static String getDefaultSansSerifFontName()
  116. {
  117. StringArray allFonts;
  118. FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
  119. static const char* targets[] = { "Verdana", "Bitstream Vera Sans", "Luxi Sans",
  120. "Liberation Sans", "DejaVu Sans", "Sans", nullptr };
  121. return pickBestFont (allFonts, targets);
  122. }
  123. static String getDefaultSerifFontName()
  124. {
  125. StringArray allFonts;
  126. FTTypefaceList::getInstance()->getSerifNames (allFonts);
  127. static const char* targets[] = { "Bitstream Vera Serif", "Times", "Nimbus Roman",
  128. "Liberation Serif", "DejaVu Serif", "Serif", nullptr };
  129. return pickBestFont (allFonts, targets);
  130. }
  131. static String getDefaultMonospacedFontName()
  132. {
  133. StringArray allFonts;
  134. FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
  135. static const char* targets[] = { "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Sans Mono",
  136. "Liberation Mono", "Courier", "DejaVu Mono", "Mono", nullptr };
  137. return pickBestFont (allFonts, targets);
  138. }
  139. JUCE_DECLARE_NON_COPYABLE (DefaultFontNames)
  140. };
  141. Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
  142. {
  143. static DefaultFontNames defaultNames;
  144. Font f (font);
  145. f.setTypefaceName (defaultNames.getRealFontName (font.getTypefaceName()));
  146. return Typeface::createSystemTypefaceFor (f);
  147. }