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