1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2020 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11 Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12
13 End User License Agreement: www.juce.com/juce-6-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24 */
25
26 namespace juce
27 {
28
findFontsConfFile()29 static std::unique_ptr<XmlElement> findFontsConfFile()
30 {
31 static const char* pathsToSearch[] = { "/etc/fonts/fonts.conf",
32 "/usr/share/fonts/fonts.conf" };
33
34 for (auto* path : pathsToSearch)
35 if (auto xml = parseXML (File (path)))
36 return xml;
37
38 return {};
39 }
40
getDefaultFontDirectories()41 StringArray FTTypefaceList::getDefaultFontDirectories()
42 {
43 StringArray fontDirs;
44
45 fontDirs.addTokens (String (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH"))), ";,", "");
46 fontDirs.removeEmptyStrings (true);
47
48 if (fontDirs.isEmpty())
49 {
50 if (auto fontsInfo = findFontsConfFile())
51 {
52 forEachXmlChildElementWithTagName (*fontsInfo, e, "dir")
53 {
54 auto fontPath = e->getAllSubText().trim();
55
56 if (fontPath.isNotEmpty())
57 {
58 if (e->getStringAttribute ("prefix") == "xdg")
59 {
60 auto xdgDataHome = SystemStats::getEnvironmentVariable ("XDG_DATA_HOME", {});
61
62 if (xdgDataHome.trimStart().isEmpty())
63 xdgDataHome = "~/.local/share";
64
65 fontPath = File (xdgDataHome).getChildFile (fontPath).getFullPathName();
66 }
67
68 fontDirs.add (fontPath);
69 }
70 }
71 }
72 }
73
74 if (fontDirs.isEmpty())
75 fontDirs.add ("/usr/X11R6/lib/X11/fonts");
76
77 fontDirs.removeDuplicates (false);
78 return fontDirs;
79 }
80
createSystemTypefaceFor(const Font & font)81 Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
82 {
83 return new FreeTypeTypeface (font);
84 }
85
createSystemTypefaceFor(const void * data,size_t dataSize)86 Typeface::Ptr Typeface::createSystemTypefaceFor (const void* data, size_t dataSize)
87 {
88 return new FreeTypeTypeface (data, dataSize);
89 }
90
scanFolderForFonts(const File & folder)91 void Typeface::scanFolderForFonts (const File& folder)
92 {
93 FTTypefaceList::getInstance()->scanFontPaths (StringArray (folder.getFullPathName()));
94 }
95
findAllTypefaceNames()96 StringArray Font::findAllTypefaceNames()
97 {
98 return FTTypefaceList::getInstance()->findAllFamilyNames();
99 }
100
findAllTypefaceStyles(const String & family)101 StringArray Font::findAllTypefaceStyles (const String& family)
102 {
103 return FTTypefaceList::getInstance()->findAllTypefaceStyles (family);
104 }
105
createNativeLayout(const AttributedString &)106 bool TextLayout::createNativeLayout (const AttributedString&)
107 {
108 return false;
109 }
110
111 //==============================================================================
112 struct DefaultFontNames
113 {
DefaultFontNamesjuce::DefaultFontNames114 DefaultFontNames()
115 : defaultSans (getDefaultSansSerifFontName()),
116 defaultSerif (getDefaultSerifFontName()),
117 defaultFixed (getDefaultMonospacedFontName())
118 {
119 }
120
getRealFontNamejuce::DefaultFontNames121 String getRealFontName (const String& faceName) const
122 {
123 if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
124 if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
125 if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
126
127 return faceName;
128 }
129
130 String defaultSans, defaultSerif, defaultFixed;
131
132 private:
pickBestFontjuce::DefaultFontNames133 static String pickBestFont (const StringArray& names, const char* const* choicesArray)
134 {
135 const StringArray choices (choicesArray);
136
137 for (auto& choice : choices)
138 if (names.contains (choice, true))
139 return choice;
140
141 for (auto& choice : choices)
142 for (auto& name : names)
143 if (name.startsWithIgnoreCase (choice))
144 return name;
145
146 for (auto& choice : choices)
147 for (auto& name : names)
148 if (name.containsIgnoreCase (choice))
149 return name;
150
151 return names[0];
152 }
153
getDefaultSansSerifFontNamejuce::DefaultFontNames154 static String getDefaultSansSerifFontName()
155 {
156 StringArray allFonts;
157 FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
158
159 static const char* targets[] = { "Verdana", "Bitstream Vera Sans", "Luxi Sans",
160 "Liberation Sans", "DejaVu Sans", "Sans", nullptr };
161 return pickBestFont (allFonts, targets);
162 }
163
getDefaultSerifFontNamejuce::DefaultFontNames164 static String getDefaultSerifFontName()
165 {
166 StringArray allFonts;
167 FTTypefaceList::getInstance()->getSerifNames (allFonts);
168
169 static const char* targets[] = { "Bitstream Vera Serif", "Times", "Nimbus Roman",
170 "Liberation Serif", "DejaVu Serif", "Serif", nullptr };
171 return pickBestFont (allFonts, targets);
172 }
173
getDefaultMonospacedFontNamejuce::DefaultFontNames174 static String getDefaultMonospacedFontName()
175 {
176 StringArray allFonts;
177 FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
178
179 static const char* targets[] = { "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Sans Mono",
180 "Liberation Mono", "Courier", "DejaVu Mono", "Mono", nullptr };
181 return pickBestFont (allFonts, targets);
182 }
183
184 JUCE_DECLARE_NON_COPYABLE (DefaultFontNames)
185 };
186
getDefaultTypefaceForFont(const Font & font)187 Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
188 {
189 static DefaultFontNames defaultNames;
190
191 Font f (font);
192 f.setTypefaceName (defaultNames.getRealFontName (font.getTypefaceName()));
193 return Typeface::createSystemTypefaceFor (f);
194 }
195
196 } // namespace juce
197