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.

juce_linux_Fonts.cpp 5.9KB

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