| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 "app/gfx/chrome_font.h" | |
| 6 | |
| 7 #include <fontconfig/fontconfig.h> | |
| 8 #include <gtk/gtk.h> | |
| 9 | |
| 10 #include "base/string_util.h" | |
| 11 | |
| 12 namespace gfx { | |
| 13 | |
| 14 Font* Font::default_font_ = NULL; | |
| 15 | |
| 16 // Find the best match font for |family_name| in the same way as Skia | |
| 17 // to make sure CreateFont() successfully creates default font. | |
| 18 // In Skia, it only checks the best match font. If it failed to find, | |
| 19 // SkTypeface will be NULL for that font family. It eventually causes segfault. | |
| 20 // For example, family_name = "Sans" and system may have various fonts. | |
| 21 // The first font family in FcPattern will be "DejaVu Sans" but a font family | |
| 22 // returned by FcFontMatch will be "VL PGothic". | |
| 23 // In this case, SkTypeface for "Sans" returns NULL even if system has font | |
| 24 // for "Sans" font family. | |
| 25 // See FontMatch() in skia/ports/SkFontHost_fontconfig.cpp for more detail. | |
| 26 static std::wstring FindBestMatchFontFamilyName(const char* family_name) { | |
| 27 FcPattern* pattern = FcPatternCreate(); | |
| 28 FcValue fcvalue; | |
| 29 fcvalue.type = FcTypeString; | |
| 30 char* family_name_copy = strdup(family_name); | |
| 31 fcvalue.u.s = reinterpret_cast<FcChar8*>(family_name_copy); | |
| 32 FcPatternAdd(pattern, FC_FAMILY, fcvalue, 0); | |
| 33 FcConfigSubstitute(0, pattern, FcMatchPattern); | |
| 34 FcDefaultSubstitute(pattern); | |
| 35 FcResult result; | |
| 36 FcPattern* match = FcFontMatch(0, pattern, &result); | |
| 37 DCHECK(match) << "Could not find font: " << family_name; | |
| 38 FcChar8* match_family; | |
| 39 FcPatternGetString(match, FC_FAMILY, 0, &match_family); | |
| 40 | |
| 41 std::wstring font_family = UTF8ToWide( | |
| 42 reinterpret_cast<char*>(match_family)); | |
| 43 FcPatternDestroy(match); | |
| 44 FcPatternDestroy(pattern); | |
| 45 free(family_name_copy); | |
| 46 return font_family; | |
| 47 } | |
| 48 | |
| 49 // Get the default gtk system font (name and size). | |
| 50 Font::Font() { | |
| 51 if (default_font_ == NULL) { | |
| 52 gtk_init(NULL, NULL); | |
| 53 GtkSettings* settings = gtk_settings_get_default(); | |
| 54 | |
| 55 GValue value = {0}; | |
| 56 g_value_init(&value, G_TYPE_STRING); | |
| 57 g_object_get_property(G_OBJECT(settings), "gtk-font-name", &value); | |
| 58 | |
| 59 // gtk-font-name may be wrapped in quotes. | |
| 60 gchar* font_name = g_strdup_value_contents(&value); | |
| 61 gchar* font_ptr = font_name; | |
| 62 if (font_ptr[0] == '\"') | |
| 63 font_ptr++; | |
| 64 if (font_ptr[strlen(font_ptr) - 1] == '\"') | |
| 65 font_ptr[strlen(font_ptr) - 1] = '\0'; | |
| 66 | |
| 67 PangoFontDescription* desc = | |
| 68 pango_font_description_from_string(font_ptr); | |
| 69 gint size = pango_font_description_get_size(desc); | |
| 70 const char* family_name = pango_font_description_get_family(desc); | |
| 71 | |
| 72 // Find best match font for |family_name| to make sure we can get | |
| 73 // SkTypeface for default font. | |
| 74 // TODO(agl): remove this. | |
| 75 std::wstring font_family = FindBestMatchFontFamilyName(family_name); | |
| 76 | |
| 77 default_font_ = new Font(CreateFont(font_family, size / PANGO_SCALE)); | |
| 78 | |
| 79 pango_font_description_free(desc); | |
| 80 g_free(font_name); | |
| 81 g_value_unset(&value); | |
| 82 | |
| 83 DCHECK(default_font_); | |
| 84 } | |
| 85 | |
| 86 CopyFont(*default_font_); | |
| 87 } | |
| 88 | |
| 89 } // namespace gfx | |
| OLD | NEW |