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/chrome_font.h" | 5 #include "app/gfx/chrome_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 ChromeFont* ChromeFont::default_font_ = NULL; | 12 namespace gfx { |
| 13 |
| 14 Font* Font::default_font_ = NULL; |
13 | 15 |
14 // 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 |
15 // to make sure CreateFont() successfully creates default font. | 17 // to make sure CreateFont() successfully creates default font. |
16 // In Skia, it only checks the best match font. If it failed to find, | 18 // In Skia, it only checks the best match font. If it failed to find, |
17 // SkTypeface will be NULL for that font family. It eventually causes segfault. | 19 // SkTypeface will be NULL for that font family. It eventually causes segfault. |
18 // For example, family_name = "Sans" and system may have various fonts. | 20 // For example, family_name = "Sans" and system may have various fonts. |
19 // The first font family in FcPattern will be "DejaVu Sans" but a font family | 21 // The first font family in FcPattern will be "DejaVu Sans" but a font family |
20 // returned by FcFontMatch will be "VL PGothic". | 22 // returned by FcFontMatch will be "VL PGothic". |
21 // In this case, SkTypeface for "Sans" returns NULL even if system has font | 23 // In this case, SkTypeface for "Sans" returns NULL even if system has font |
22 // for "Sans" font family. | 24 // for "Sans" font family. |
(...skipping 15 matching lines...) Expand all Loading... |
38 | 40 |
39 std::wstring font_family = UTF8ToWide( | 41 std::wstring font_family = UTF8ToWide( |
40 reinterpret_cast<char*>(match_family)); | 42 reinterpret_cast<char*>(match_family)); |
41 FcPatternDestroy(match); | 43 FcPatternDestroy(match); |
42 FcPatternDestroy(pattern); | 44 FcPatternDestroy(pattern); |
43 free(family_name_copy); | 45 free(family_name_copy); |
44 return font_family; | 46 return font_family; |
45 } | 47 } |
46 | 48 |
47 // Get the default gtk system font (name and size). | 49 // Get the default gtk system font (name and size). |
48 ChromeFont::ChromeFont() { | 50 Font::Font() { |
49 if (default_font_ == NULL) { | 51 if (default_font_ == NULL) { |
50 gtk_init(NULL, NULL); | 52 gtk_init(NULL, NULL); |
51 GtkSettings* settings = gtk_settings_get_default(); | 53 GtkSettings* settings = gtk_settings_get_default(); |
52 | 54 |
53 GValue value = {0}; | 55 GValue value = {0}; |
54 g_value_init(&value, G_TYPE_STRING); | 56 g_value_init(&value, G_TYPE_STRING); |
55 g_object_get_property(G_OBJECT(settings), "gtk-font-name", &value); | 57 g_object_get_property(G_OBJECT(settings), "gtk-font-name", &value); |
56 | 58 |
57 // gtk-font-name may be wrapped in quotes. | 59 // gtk-font-name may be wrapped in quotes. |
58 gchar* font_name = g_strdup_value_contents(&value); | 60 gchar* font_name = g_strdup_value_contents(&value); |
59 gchar* font_ptr = font_name; | 61 gchar* font_ptr = font_name; |
60 if (font_ptr[0] == '\"') | 62 if (font_ptr[0] == '\"') |
61 font_ptr++; | 63 font_ptr++; |
62 if (font_ptr[strlen(font_ptr) - 1] == '\"') | 64 if (font_ptr[strlen(font_ptr) - 1] == '\"') |
63 font_ptr[strlen(font_ptr) - 1] = '\0'; | 65 font_ptr[strlen(font_ptr) - 1] = '\0'; |
64 | 66 |
65 PangoFontDescription* desc = | 67 PangoFontDescription* desc = |
66 pango_font_description_from_string(font_ptr); | 68 pango_font_description_from_string(font_ptr); |
67 gint size = pango_font_description_get_size(desc); | 69 gint size = pango_font_description_get_size(desc); |
68 const char* family_name = pango_font_description_get_family(desc); | 70 const char* family_name = pango_font_description_get_family(desc); |
69 | 71 |
70 // Find best match font for |family_name| to make sure we can get | 72 // Find best match font for |family_name| to make sure we can get |
71 // SkTypeface for default font. | 73 // SkTypeface for default font. |
72 // TODO(agl): remove this. | 74 // TODO(agl): remove this. |
73 std::wstring font_family = FindBestMatchFontFamilyName(family_name); | 75 std::wstring font_family = FindBestMatchFontFamilyName(family_name); |
74 | 76 |
75 default_font_ = new ChromeFont(CreateFont(font_family, size / PANGO_SCALE)); | 77 default_font_ = new Font(CreateFont(font_family, size / PANGO_SCALE)); |
76 | 78 |
77 pango_font_description_free(desc); | 79 pango_font_description_free(desc); |
78 g_free(font_name); | 80 g_free(font_name); |
79 g_value_unset(&value); | 81 g_value_unset(&value); |
80 | 82 |
81 DCHECK(default_font_); | 83 DCHECK(default_font_); |
82 } | 84 } |
83 | 85 |
84 CopyChromeFont(*default_font_); | 86 CopyFont(*default_font_); |
85 } | 87 } |
| 88 |
| 89 } // namespace gfx |
OLD | NEW |