OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "content/common/font_list.h" | |
6 | |
7 #include <fontconfig/fontconfig.h> | |
Daniel Erat
2016/08/25 13:49:57
nit: i think that system headers usually come afte
| |
8 | |
9 #include <set> | |
Daniel Erat
2016/08/25 13:49:57
nit: #include <memory> too
| |
10 #include <string> | |
11 #include <utility> | |
12 | |
13 #include "base/values.h" | |
14 | |
15 namespace content { | |
16 | |
17 std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> CreateFormatPattern( | |
18 const char* format) { | |
19 std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> pattern( | |
20 FcPatternCreate(), FcPatternDestroy); | |
21 FcPatternAddBool(pattern.get(), FC_SCALABLE, FcTrue); | |
22 FcPatternAddString(pattern.get(), FC_FONTFORMAT, | |
23 reinterpret_cast<const FcChar8*>(format)); | |
24 return pattern; | |
25 } | |
26 | |
27 std::unique_ptr<base::ListValue> GetFontList_SlowBlocking() { | |
28 std::unique_ptr<base::ListValue> font_list(new base::ListValue); | |
29 | |
30 std::unique_ptr<FcObjectSet, decltype(&FcObjectSetDestroy)> object_set( | |
31 FcObjectSetBuild(FC_FAMILY, NULL), FcObjectSetDestroy); | |
32 | |
33 std::set<std::string> sorted_families; | |
34 | |
35 // See https://www.freetype.org/freetype2/docs/reference/ft2-font_formats.html | |
36 // for the list of possible formats. | |
37 const char* allowed_formats[] = { "TrueType", "CFF" }; | |
38 for (size_t i = 0; i < arraysize(allowed_formats); ++i) { | |
39 auto format_pattern = CreateFormatPattern(allowed_formats[i]); | |
40 std::unique_ptr<FcFontSet, decltype(&FcFontSetDestroy)> fontset( | |
41 FcFontList(0, format_pattern.get(), object_set.get()), | |
42 FcFontSetDestroy); | |
43 for (int j = 0; j < fontset->nfont; ++j) { | |
44 char* family_string; | |
45 FcPatternGetString(fontset->fonts[j], FC_FAMILY, 0, | |
46 reinterpret_cast<FcChar8**>(&family_string)); | |
47 sorted_families.insert(family_string); | |
48 } | |
49 } | |
50 | |
51 // For backwards compatibility with the older pango implementation, add the | |
52 // three Fontconfig aliases that pango added. Our linux default settings for | |
53 // fixed-width was "Monospace". If we remove that, this entry is not found in | |
54 // the list anymore, see also: | |
55 // https://git.gnome.org/browse/pango/tree/pango/pangofc-fontmap.c?h=1.40.1#n1 351 | |
56 sorted_families.insert("Monospace"); | |
57 sorted_families.insert("Sans"); | |
58 sorted_families.insert("Serif"); | |
59 | |
60 for (std::set<std::string>::const_iterator iter = sorted_families.begin(); | |
61 iter != sorted_families.end(); ++iter) { | |
62 std::unique_ptr<base::ListValue> font_item(new base::ListValue()); | |
63 font_item->AppendString(*iter); | |
64 font_item->AppendString(*iter); // localized name. | |
65 // TODO(yusukes): Support localized family names. | |
66 font_list->Append(std::move(font_item)); | |
67 } | |
68 | |
69 return font_list; | |
70 } | |
71 | |
72 } // namespace content | |
OLD | NEW |