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.

200 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. std::unique_ptr<XmlElement> fontsInfo (findFontsConfFile());
  38. if (fontsInfo != nullptr)
  39. {
  40. forEachXmlChildElementWithTagName (*fontsInfo, e, "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 DefaultFontNames
  88. {
  89. DefaultFontNames()
  90. : defaultSans (getDefaultSansSerifFontName()),
  91. defaultSerif (getDefaultSerifFontName()),
  92. defaultFixed (getDefaultMonospacedFontName())
  93. {
  94. }
  95. String getRealFontName (const String& faceName) const
  96. {
  97. if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
  98. if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
  99. if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
  100. return faceName;
  101. }
  102. String defaultSans, defaultSerif, defaultFixed;
  103. private:
  104. static String pickBestFont (const StringArray& names, const char* const* choicesArray)
  105. {
  106. const StringArray choices (choicesArray);
  107. for (auto& choice : choices)
  108. if (names.contains (choice, true))
  109. return choice;
  110. for (auto& choice : choices)
  111. for (auto& name : names)
  112. if (name.startsWithIgnoreCase (choice))
  113. return name;
  114. for (auto& choice : choices)
  115. for (auto& name : names)
  116. if (name.containsIgnoreCase (choice))
  117. return name;
  118. return names[0];
  119. }
  120. static String getDefaultSansSerifFontName()
  121. {
  122. StringArray allFonts;
  123. FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
  124. static const char* targets[] = { "Verdana", "Bitstream Vera Sans", "Luxi Sans",
  125. "Liberation Sans", "DejaVu Sans", "Sans", nullptr };
  126. return pickBestFont (allFonts, targets);
  127. }
  128. static String getDefaultSerifFontName()
  129. {
  130. StringArray allFonts;
  131. FTTypefaceList::getInstance()->getSerifNames (allFonts);
  132. static const char* targets[] = { "Bitstream Vera Serif", "Times", "Nimbus Roman",
  133. "Liberation Serif", "DejaVu Serif", "Serif", nullptr };
  134. return pickBestFont (allFonts, targets);
  135. }
  136. static String getDefaultMonospacedFontName()
  137. {
  138. StringArray allFonts;
  139. FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
  140. static const char* targets[] = { "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Sans Mono",
  141. "Liberation Mono", "Courier", "DejaVu Mono", "Mono", nullptr };
  142. return pickBestFont (allFonts, targets);
  143. }
  144. JUCE_DECLARE_NON_COPYABLE (DefaultFontNames)
  145. };
  146. Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
  147. {
  148. static DefaultFontNames defaultNames;
  149. Font f (font);
  150. f.setTypefaceName (defaultNames.getRealFontName (font.getTypefaceName()));
  151. return Typeface::createSystemTypefaceFor (f);
  152. }
  153. } // namespace juce