OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/gfx/font_fallback.h" | |
6 | |
7 #include <fontconfig/fontconfig.h> | |
8 | |
9 namespace gfx { | |
10 | |
11 namespace { | |
12 | |
13 typedef std::map<std::string, std::vector<std::string> > FallbackCache; | |
14 base::LazyInstance<FallbackCache>::Leaky g_fallback_cache = | |
15 LAZY_INSTANCE_INITIALIZER; | |
16 | |
17 } // namespace | |
18 | |
19 std::vector<std::string> GetFallbackFontFamilies(std::string font_family) { | |
Daniel Erat
2014/07/14 14:58:20
in a later change, it may make sense to unify this
ckocagil
2014/07/16 23:42:22
Acknowledged.
| |
20 std::vector<std::string>* cache_value = &g_fallback_cache.Get()[font_family]; | |
21 if (!cache_value->empty()) | |
22 return *cache_value; | |
23 | |
24 std::vector<std::string> fallback_fonts; | |
25 | |
26 FcPattern* pattern = FcPatternCreate(); | |
27 FcValue family; | |
28 family.type = FcTypeString; | |
29 family.u.s = reinterpret_cast<const FcChar8*>(font_family.c_str()); | |
30 FcPatternAdd(pattern, FC_FAMILY, family, FcFalse); | |
31 if (FcConfigSubstitute(NULL, pattern, FcMatchPattern) == FcTrue) { | |
32 FcDefaultSubstitute(pattern); | |
33 FcResult result; | |
34 FcFontSet* fonts = FcFontSort(NULL, pattern, FcTrue, NULL, &result); | |
35 if (fonts) { | |
36 for (int i = 0; i < fonts->nfont; ++i) { | |
37 char* name = NULL; | |
38 FcPatternGetString(fonts->fonts[i], FC_FAMILY, 0, | |
39 reinterpret_cast<FcChar8**>(&name)); | |
40 // FontConfig returns multiple fonts with the same family name and | |
41 // different configurations. Check to prevent duplicate family names. | |
42 if (fallback_fonts.empty() || fallback_fonts.back() != name) | |
43 fallback_fonts.push_back(std::string(name)); | |
44 } | |
45 FcFontSetDestroy(fonts); | |
46 } | |
47 } | |
48 FcPatternDestroy(pattern); | |
49 | |
50 if (fallback_fonts.empty()) | |
51 fallback_fonts.push_back(font_family); | |
52 | |
53 *cache_value = fallback_fonts; | |
54 | |
55 return fallback_fonts; | |
56 } | |
57 | |
58 } // namespace gfx | |
OLD | NEW |