WebKit Roll 56306:56312.
[chromium-blink-merge.git] / app / gfx / font_gtk.cc
blob85640b0801021754b304e575f13fbc4624df5b0a
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.
5 #include "app/gfx/font.h"
7 #include <fontconfig/fontconfig.h>
8 #include <gtk/gtk.h>
10 #include "base/logging.h"
11 #include "base/string_piece.h"
12 #include "base/utf_string_conversions.h"
14 namespace gfx {
16 Font* Font::default_font_ = NULL;
18 // Find the best match font for |family_name| in the same way as Skia
19 // to make sure CreateFont() successfully creates a default font. In
20 // Skia, it only checks the best match font. If it failed to find
21 // one, SkTypeface will be NULL for that font family. It eventually
22 // causes a segfault. For example, family_name = "Sans" and system
23 // may have various fonts. The first font family in FcPattern will be
24 // "DejaVu Sans" but a font family returned by FcFontMatch will be "VL
25 // PGothic". In this case, SkTypeface for "Sans" returns NULL even if
26 // the system has a font for "Sans" font family. See FontMatch() in
27 // skia/ports/SkFontHost_fontconfig.cpp for more detail.
28 static std::wstring FindBestMatchFontFamilyName(const char* family_name) {
29 FcPattern* pattern = FcPatternCreate();
30 FcValue fcvalue;
31 fcvalue.type = FcTypeString;
32 char* family_name_copy = strdup(family_name);
33 fcvalue.u.s = reinterpret_cast<FcChar8*>(family_name_copy);
34 FcPatternAdd(pattern, FC_FAMILY, fcvalue, 0);
35 FcConfigSubstitute(0, pattern, FcMatchPattern);
36 FcDefaultSubstitute(pattern);
37 FcResult result;
38 FcPattern* match = FcFontMatch(0, pattern, &result);
39 DCHECK(match) << "Could not find font: " << family_name;
40 FcChar8* match_family;
41 FcPatternGetString(match, FC_FAMILY, 0, &match_family);
43 std::wstring font_family = UTF8ToWide(
44 reinterpret_cast<char*>(match_family));
45 FcPatternDestroy(match);
46 FcPatternDestroy(pattern);
47 free(family_name_copy);
48 return font_family;
51 // static
52 Font Font::CreateFont(PangoFontDescription* desc) {
53 gint size = pango_font_description_get_size(desc);
54 const char* family_name = pango_font_description_get_family(desc);
56 // Find best match font for |family_name| to make sure we can get
57 // a SkTypeface for the default font.
58 // TODO(agl): remove this.
59 std::wstring font_family = FindBestMatchFontFamilyName(family_name);
61 Font font = CreateFont(font_family, size / PANGO_SCALE);
62 int style = 0;
63 if (pango_font_description_get_weight(desc) == PANGO_WEIGHT_BOLD) {
64 // TODO(davemoore) What should we do about other weights? We currently
65 // only support BOLD.
66 style |= BOLD;
68 if (pango_font_description_get_style(desc) == PANGO_STYLE_ITALIC) {
69 // TODO(davemoore) What about PANGO_STYLE_OBLIQUE?
70 style |= ITALIC;
72 if (style != 0) {
73 font = font.DeriveFont(0, style);
75 return Font(font);
78 // Get the default gtk system font (name and size).
79 Font::Font() {
80 if (default_font_ == NULL) {
81 GtkSettings* settings = gtk_settings_get_default();
83 gchar* font_name = NULL;
84 g_object_get(settings, "gtk-font-name", &font_name, NULL);
86 // Temporary CHECK for helping track down
87 // http://code.google.com/p/chromium/issues/detail?id=12530
88 CHECK(font_name) << " Unable to get gtk-font-name for default font.";
90 PangoFontDescription* desc =
91 pango_font_description_from_string(font_name);
92 default_font_ = new Font(CreateFont(desc));
93 pango_font_description_free(desc);
94 g_free(font_name);
96 DCHECK(default_font_);
99 CopyFont(*default_font_);
102 // static
103 PangoFontDescription* Font::PangoFontFromGfxFont(
104 const gfx::Font& gfx_font) {
105 gfx::Font font = gfx_font; // Copy so we can call non-const methods.
106 PangoFontDescription* pfd = pango_font_description_new();
107 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str());
108 pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE);
110 switch (font.style()) {
111 case gfx::Font::NORMAL:
112 // Nothing to do, should already be PANGO_STYLE_NORMAL.
113 break;
114 case gfx::Font::BOLD:
115 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD);
116 break;
117 case gfx::Font::ITALIC:
118 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC);
119 break;
120 case gfx::Font::UNDERLINED:
121 // TODO(deanm): How to do underlined? Where do we use it? Probably have
122 // to paint it ourselves, see pango_font_metrics_get_underline_position.
123 break;
126 return pfd;
129 } // namespace gfx