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.

177 lines
6.0KB

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