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