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.

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