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.

169 lines
5.8KB

  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. void FontFileIterator::findFontDirectories()
  19. {
  20. fontDirs.addTokens (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH")), ";,", String::empty);
  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::empty));
  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. }
  48. Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
  49. {
  50. return new FreeTypeTypeface (font);
  51. }
  52. StringArray Font::findAllTypefaceNames()
  53. {
  54. return FTTypefaceList::getInstance()->findAllFamilyNames();
  55. }
  56. StringArray Font::findAllTypefaceStyles (const String& family)
  57. {
  58. return FTTypefaceList::getInstance()->findAllTypefaceStyles (family);
  59. }
  60. bool TextLayout::createNativeLayout (const AttributedString&)
  61. {
  62. return false;
  63. }
  64. //==============================================================================
  65. struct DefaultFontNames
  66. {
  67. DefaultFontNames()
  68. : defaultSans (getDefaultSansSerifFontName()),
  69. defaultSerif (getDefaultSerifFontName()),
  70. defaultFixed (getDefaultMonospacedFontName())
  71. {
  72. }
  73. String getRealFontName (const String& faceName) const
  74. {
  75. if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
  76. if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
  77. if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
  78. return faceName;
  79. }
  80. String defaultSans, defaultSerif, defaultFixed;
  81. private:
  82. static String pickBestFont (const StringArray& names, const char* const* choicesArray)
  83. {
  84. const StringArray choices (choicesArray);
  85. for (int j = 0; j < choices.size(); ++j)
  86. if (names.contains (choices[j], true))
  87. return choices[j];
  88. for (int j = 0; j < choices.size(); ++j)
  89. for (int i = 0; i < names.size(); ++i)
  90. if (names[i].startsWithIgnoreCase (choices[j]))
  91. return names[i];
  92. for (int j = 0; j < choices.size(); ++j)
  93. for (int i = 0; i < names.size(); ++i)
  94. if (names[i].containsIgnoreCase (choices[j]))
  95. return names[i];
  96. return names[0];
  97. }
  98. static String getDefaultSansSerifFontName()
  99. {
  100. StringArray allFonts;
  101. FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
  102. const char* targets[] = { "Verdana", "Bitstream Vera Sans", "Luxi Sans",
  103. "Liberation Sans", "DejaVu Sans", "Sans", nullptr };
  104. return pickBestFont (allFonts, targets);
  105. }
  106. static String getDefaultSerifFontName()
  107. {
  108. StringArray allFonts;
  109. FTTypefaceList::getInstance()->getSerifNames (allFonts);
  110. const char* targets[] = { "Bitstream Vera Serif", "Times", "Nimbus Roman",
  111. "Liberation Serif", "DejaVu Serif", "Serif", nullptr };
  112. return pickBestFont (allFonts, targets);
  113. }
  114. static String getDefaultMonospacedFontName()
  115. {
  116. StringArray allFonts;
  117. FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
  118. const char* targets[] = { "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Sans Mono",
  119. "Liberation Mono", "Courier", "DejaVu Mono", "Mono", nullptr };
  120. return pickBestFont (allFonts, targets);
  121. }
  122. JUCE_DECLARE_NON_COPYABLE (DefaultFontNames)
  123. };
  124. Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
  125. {
  126. static DefaultFontNames defaultNames;
  127. Font f (font);
  128. f.setTypefaceName (defaultNames.getRealFontName (font.getTypefaceName()));
  129. return Typeface::createSystemTypefaceFor (f);
  130. }