JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Fonts_linux.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - 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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-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
26namespace juce
27{
28
29static std::unique_ptr<XmlElement> findFontsConfFile()
30{
31 static const char* pathsToSearch[] = { "/etc/fonts/fonts.conf",
32 "/usr/share/fonts/fonts.conf",
33 "/usr/local/etc/fonts/fonts.conf",
34 "/usr/share/defaults/fonts/fonts.conf" };
35
36 for (auto* path : pathsToSearch)
37 if (auto xml = parseXML (File (path)))
38 return xml;
39
40 return {};
41}
42
43StringArray FTTypefaceList::getDefaultFontDirectories()
44{
45 StringArray fontDirs;
46
47 fontDirs.addTokens (String (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH"))), ";,", "");
48 fontDirs.removeEmptyStrings (true);
49
50 if (fontDirs.isEmpty())
51 {
52 if (auto fontsInfo = findFontsConfFile())
53 {
54 for (auto* e : fontsInfo->getChildWithTagNameIterator ("dir"))
55 {
56 auto fontPath = e->getAllSubText().trim();
57
58 if (fontPath.isNotEmpty())
59 {
60 if (e->getStringAttribute ("prefix") == "xdg")
61 {
62 auto xdgDataHome = SystemStats::getEnvironmentVariable ("XDG_DATA_HOME", {});
63
64 if (xdgDataHome.trimStart().isEmpty())
65 xdgDataHome = "~/.local/share";
66
67 fontPath = File (xdgDataHome).getChildFile (fontPath).getFullPathName();
68 }
69
70 fontDirs.add (fontPath);
71 }
72 }
73 }
74 }
75
76 if (fontDirs.isEmpty())
77 fontDirs.add ("/usr/X11R6/lib/X11/fonts");
78
79 fontDirs.removeDuplicates (false);
80 return fontDirs;
81}
82
84{
85 return new FreeTypeTypeface (font);
86}
87
88Typeface::Ptr Typeface::createSystemTypefaceFor (const void* data, size_t dataSize)
89{
90 return new FreeTypeTypeface (data, dataSize);
91}
92
94{
95 FTTypefaceList::getInstance()->scanFontPaths (StringArray (folder.getFullPathName()));
96}
97
99{
100 return FTTypefaceList::getInstance()->findAllFamilyNames();
101}
102
104{
105 return FTTypefaceList::getInstance()->findAllTypefaceStyles (family);
106}
107
108bool TextLayout::createNativeLayout (const AttributedString&)
109{
110 return false;
111}
112
113//==============================================================================
115{
117 : defaultSans (getDefaultSansSerifFontName()),
118 defaultSerif (getDefaultSerifFontName()),
119 defaultFixed (getDefaultMonospacedFontName())
120 {
121 }
122
123 String getRealFontName (const String& faceName) const
124 {
125 if (faceName == Font::getDefaultSansSerifFontName()) return defaultSans;
126 if (faceName == Font::getDefaultSerifFontName()) return defaultSerif;
127 if (faceName == Font::getDefaultMonospacedFontName()) return defaultFixed;
128
129 return faceName;
130 }
131
132 String defaultSans, defaultSerif, defaultFixed;
133
134private:
135 template <typename Range>
136 static String pickBestFont (const StringArray& names, Range&& choicesArray)
137 {
138 for (auto& choice : choicesArray)
139 if (names.contains (choice, true))
140 return choice;
141
142 for (auto& choice : choicesArray)
143 for (auto& name : names)
144 if (name.startsWithIgnoreCase (choice))
145 return name;
146
147 for (auto& choice : choicesArray)
148 for (auto& name : names)
149 if (name.containsIgnoreCase (choice))
150 return name;
151
152 return names[0];
153 }
154
155 static String getDefaultSansSerifFontName()
156 {
158 FTTypefaceList::getInstance()->getSansSerifNames (allFonts);
159
160 static constexpr const char* targets[] { "Verdana",
161 "Bitstream Vera Sans",
162 "Luxi Sans",
163 "Liberation Sans",
164 "DejaVu Sans",
165 "Sans" };
166 return pickBestFont (allFonts, targets);
167 }
168
169 static String getDefaultSerifFontName()
170 {
172 FTTypefaceList::getInstance()->getSerifNames (allFonts);
173
174 static constexpr const char* targets[] { "Bitstream Vera Serif",
175 "Times",
176 "Nimbus Roman",
177 "Liberation Serif",
178 "DejaVu Serif",
179 "Serif" };
180 return pickBestFont (allFonts, targets);
181 }
182
183 static String getDefaultMonospacedFontName()
184 {
186 FTTypefaceList::getInstance()->getMonospacedNames (allFonts);
187
188 static constexpr const char* targets[] { "DejaVu Sans Mono",
189 "Bitstream Vera Sans Mono",
190 "Sans Mono",
191 "Liberation Mono",
192 "Courier",
193 "DejaVu Mono",
194 "Mono" };
195 return pickBestFont (allFonts, targets);
196 }
197
199};
200
202{
203 static const DefaultFontInfo defaultInfo;
204
205 Font f (font);
206
207 const auto name = font.getTypefaceName();
208 const auto realName = defaultInfo.getRealFontName (name);
210
212
213 if (! styles.contains (font.getTypefaceStyle()))
215
217}
218
219} // namespace juce
A text string with a set of colour/font settings that are associated with sub-ranges of the text.
Represents a local file or directory.
Definition juce_File.h:45
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
Definition juce_File.h:153
Represents a particular font, including its size, style, etc.
Definition juce_Font.h:42
static Typeface::Ptr getDefaultTypefaceForFont(const Font &font)
Returns the default system typeface for the given font.
static const String & getDefaultSansSerifFontName()
Returns a typeface font family that represents the default sans-serif font.
String getTypefaceName() const noexcept
Returns the font family of the typeface that this font uses.
static const String & getDefaultMonospacedFontName()
Returns a typeface font family that represents the default monospaced font.
String getTypefaceStyle() const noexcept
Returns the font style of the typeface that this font uses.
static StringArray findAllTypefaceStyles(const String &family)
Returns a list of all the available typeface font styles.
void setTypefaceStyle(const String &newStyle)
Changes the font style of the typeface.
void setTypefaceName(const String &faceName)
Changes the font family of the typeface.
static StringArray findAllTypefaceNames()
Returns a list of all the available typeface font families.
static const String & getDefaultSerifFontName()
Returns a typeface font family that represents the default serif font.
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
A special array for holding a list of strings.
bool contains(StringRef stringToLookFor, bool ignoreCase=false) const
Searches for a string in the array.
The JUCE String class!
Definition juce_String.h:53
static String getEnvironmentVariable(const String &name, const String &defaultValue)
Returns an environment variable.
static void scanFolderForFonts(const File &folder)
On some platforms, this allows a specific path to be scanned.
static Ptr createSystemTypefaceFor(const Font &font)
Creates a new system typeface.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
JUCE Namespace.
std::unique_ptr< XmlElement > parseXML(const String &textToParse)
Attempts to parse some XML text, returning a new XmlElement if it was valid.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88