| 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 "gfx/font.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <fontconfig/fontconfig.h> | |
| 9 #include <gtk/gtk.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/string_piece.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "gfx/gtk_util.h" | |
| 15 | |
| 16 namespace gfx { | |
| 17 | |
| 18 Font* Font::default_font_ = NULL; | |
| 19 | |
| 20 // Find the best match font for |family_name| in the same way as Skia | |
| 21 // to make sure CreateFont() successfully creates a default font. In | |
| 22 // Skia, it only checks the best match font. If it failed to find | |
| 23 // one, SkTypeface will be NULL for that font family. It eventually | |
| 24 // causes a segfault. For example, family_name = "Sans" and system | |
| 25 // may have various fonts. The first font family in FcPattern will be | |
| 26 // "DejaVu Sans" but a font family returned by FcFontMatch will be "VL | |
| 27 // PGothic". In this case, SkTypeface for "Sans" returns NULL even if | |
| 28 // the system has a font for "Sans" font family. See FontMatch() in | |
| 29 // skia/ports/SkFontHost_fontconfig.cpp for more detail. | |
| 30 static std::wstring FindBestMatchFontFamilyName(const char* family_name) { | |
| 31 FcPattern* pattern = FcPatternCreate(); | |
| 32 FcValue fcvalue; | |
| 33 fcvalue.type = FcTypeString; | |
| 34 char* family_name_copy = strdup(family_name); | |
| 35 fcvalue.u.s = reinterpret_cast<FcChar8*>(family_name_copy); | |
| 36 FcPatternAdd(pattern, FC_FAMILY, fcvalue, 0); | |
| 37 FcConfigSubstitute(0, pattern, FcMatchPattern); | |
| 38 FcDefaultSubstitute(pattern); | |
| 39 FcResult result; | |
| 40 FcPattern* match = FcFontMatch(0, pattern, &result); | |
| 41 DCHECK(match) << "Could not find font: " << family_name; | |
| 42 FcChar8* match_family; | |
| 43 FcPatternGetString(match, FC_FAMILY, 0, &match_family); | |
| 44 | |
| 45 std::wstring font_family = UTF8ToWide( | |
| 46 reinterpret_cast<char*>(match_family)); | |
| 47 FcPatternDestroy(match); | |
| 48 FcPatternDestroy(pattern); | |
| 49 free(family_name_copy); | |
| 50 return font_family; | |
| 51 } | |
| 52 | |
| 53 // Pango scales font sizes. This returns the scale factor. See | |
| 54 // pango_cairo_context_set_resolution for details. | |
| 55 // NOTE: this isn't entirely accurate, in that Pango also consults the | |
| 56 // FC_PIXEL_SIZE first (see get_font_size in pangocairo-fcfont), but this | |
| 57 // seems to give us the same sizes as used by Pango for all our fonts in both | |
| 58 // English and Thai. | |
| 59 float Font::GetPangoScaleFactor() { | |
| 60 static float scale_factor = gfx::GetPangoResolution(); | |
| 61 static bool determined_scale = false; | |
| 62 if (!determined_scale) { | |
| 63 if (scale_factor <= 0) | |
| 64 scale_factor = 1; | |
| 65 else | |
| 66 scale_factor /= 72.0; | |
| 67 determined_scale = true; | |
| 68 } | |
| 69 return scale_factor; | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 Font Font::CreateFont(PangoFontDescription* desc) { | |
| 74 gint size = pango_font_description_get_size(desc); | |
| 75 const char* family_name = pango_font_description_get_family(desc); | |
| 76 | |
| 77 // Find best match font for |family_name| to make sure we can get | |
| 78 // a SkTypeface for the default font. | |
| 79 // TODO(agl): remove this. | |
| 80 std::wstring font_family = FindBestMatchFontFamilyName(family_name); | |
| 81 | |
| 82 Font font = CreateFont(font_family, size / PANGO_SCALE); | |
| 83 int style = 0; | |
| 84 if (pango_font_description_get_weight(desc) == PANGO_WEIGHT_BOLD) { | |
| 85 // TODO(davemoore) What should we do about other weights? We currently | |
| 86 // only support BOLD. | |
| 87 style |= BOLD; | |
| 88 } | |
| 89 if (pango_font_description_get_style(desc) == PANGO_STYLE_ITALIC) { | |
| 90 // TODO(davemoore) What about PANGO_STYLE_OBLIQUE? | |
| 91 style |= ITALIC; | |
| 92 } | |
| 93 if (style != 0) { | |
| 94 font = font.DeriveFont(0, style); | |
| 95 } | |
| 96 return Font(font); | |
| 97 } | |
| 98 | |
| 99 // Get the default gtk system font (name and size). | |
| 100 Font::Font() { | |
| 101 if (default_font_ == NULL) { | |
| 102 GtkSettings* settings = gtk_settings_get_default(); | |
| 103 | |
| 104 gchar* font_name = NULL; | |
| 105 g_object_get(settings, "gtk-font-name", &font_name, NULL); | |
| 106 | |
| 107 // Temporary CHECK for helping track down | |
| 108 // http://code.google.com/p/chromium/issues/detail?id=12530 | |
| 109 CHECK(font_name) << " Unable to get gtk-font-name for default font."; | |
| 110 | |
| 111 PangoFontDescription* desc = | |
| 112 pango_font_description_from_string(font_name); | |
| 113 default_font_ = new Font(CreateFont(desc)); | |
| 114 pango_font_description_free(desc); | |
| 115 g_free(font_name); | |
| 116 | |
| 117 DCHECK(default_font_); | |
| 118 } | |
| 119 | |
| 120 CopyFont(*default_font_); | |
| 121 } | |
| 122 | |
| 123 // static | |
| 124 PangoFontDescription* Font::PangoFontFromGfxFont( | |
| 125 const gfx::Font& gfx_font) { | |
| 126 gfx::Font font = gfx_font; // Copy so we can call non-const methods. | |
| 127 PangoFontDescription* pfd = pango_font_description_new(); | |
| 128 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); | |
| 129 // Set the absolute size to avoid overflowing UI elements. | |
| 130 pango_font_description_set_absolute_size(pfd, | |
| 131 font.FontSize() * PANGO_SCALE * Font::GetPangoScaleFactor()); | |
| 132 | |
| 133 switch (font.style()) { | |
| 134 case gfx::Font::NORMAL: | |
| 135 // Nothing to do, should already be PANGO_STYLE_NORMAL. | |
| 136 break; | |
| 137 case gfx::Font::BOLD: | |
| 138 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); | |
| 139 break; | |
| 140 case gfx::Font::ITALIC: | |
| 141 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); | |
| 142 break; | |
| 143 case gfx::Font::UNDERLINED: | |
| 144 // TODO(deanm): How to do underlined? Where do we use it? Probably have | |
| 145 // to paint it ourselves, see pango_font_metrics_get_underline_position. | |
| 146 break; | |
| 147 } | |
| 148 | |
| 149 return pfd; | |
| 150 } | |
| 151 | |
| 152 } // namespace gfx | |
| OLD | NEW |