OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/platform_font_pango.h" | 5 #include "ui/gfx/platform_font_pango.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <fontconfig/fontconfig.h> | 8 #include <fontconfig/fontconfig.h> |
9 #include <map> | 9 #include <map> |
10 #include <pango/pango.h> | 10 #include <pango/pango.h> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "third_party/skia/include/core/SkTypeface.h" | 16 #include "third_party/skia/include/core/SkTypeface.h" |
17 #include "third_party/skia/include/core/SkPaint.h" | 17 #include "third_party/skia/include/core/SkPaint.h" |
18 #include "ui/gfx/canvas_skia.h" | 18 #include "ui/gfx/canvas_skia.h" |
19 #include "ui/gfx/font.h" | 19 #include "ui/gfx/font.h" |
20 #include "ui/gfx/linux_util.h" | 20 #include "ui/gfx/linux_util.h" |
| 21 #include "ui/gfx/pango_util.h" |
21 | 22 |
22 #if !defined(USE_WAYLAND) && defined(TOOLKIT_USES_GTK) | 23 #if !defined(USE_WAYLAND) && defined(TOOLKIT_USES_GTK) |
23 #include <gdk/gdk.h> | 24 #include <gdk/gdk.h> |
24 #include <gtk/gtk.h> | 25 #include <gtk/gtk.h> |
25 #endif | 26 #endif |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 // The font family name which is used when a user's application font for | 30 // The font family name which is used when a user's application font for |
30 // GNOME/KDE is a non-scalable one. The name should be listed in the | 31 // GNOME/KDE is a non-scalable one. The name should be listed in the |
31 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. | 32 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. |
32 const char* kFallbackFontFamilyName = "sans"; | 33 const char* kFallbackFontFamilyName = "sans"; |
33 | 34 |
34 // Returns the number of pixels in a point. | |
35 // - multiply a point size by this to get pixels ("device units") | |
36 // - divide a pixel size by this to get points | |
37 float GetPixelsInPoint() { | |
38 static float pixels_in_point = 1.0; | |
39 static bool determined_value = false; | |
40 | |
41 if (!determined_value) { | |
42 // http://goo.gl/UIh5m: "This is a scale factor between points specified in | |
43 // a PangoFontDescription and Cairo units. The default value is 96, meaning | |
44 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)." | |
45 double pango_dpi = gfx::GetPangoResolution(); | |
46 if (pango_dpi <= 0) | |
47 pango_dpi = 96.0; | |
48 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch | |
49 determined_value = true; | |
50 } | |
51 | |
52 return pixels_in_point; | |
53 } | |
54 | |
55 // Retrieves the pango metrics for a pango font description. Caches the metrics | 35 // Retrieves the pango metrics for a pango font description. Caches the metrics |
56 // and never frees them. The metrics objects are relatively small and | 36 // and never frees them. The metrics objects are relatively small and |
57 // very expensive to look up. | 37 // very expensive to look up. |
58 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { | 38 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { |
59 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; | 39 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; |
60 static PangoContext* context = NULL; | 40 static PangoContext* context = NULL; |
61 | 41 |
62 if (!context) { | 42 if (!context) { |
63 context = gfx::GetPangoContext(); | 43 context = gfx::GetPangoContext(); |
64 pango_context_set_language(context, pango_language_get_default()); | 44 pango_context_set_language(context, pango_language_get_default()); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 } | 138 } |
159 | 139 |
160 PlatformFontPango::PlatformFontPango(const Font& other) { | 140 PlatformFontPango::PlatformFontPango(const Font& other) { |
161 InitFromPlatformFont( | 141 InitFromPlatformFont( |
162 static_cast<PlatformFontPango*>(other.platform_font())); | 142 static_cast<PlatformFontPango*>(other.platform_font())); |
163 } | 143 } |
164 | 144 |
165 PlatformFontPango::PlatformFontPango(NativeFont native_font) { | 145 PlatformFontPango::PlatformFontPango(NativeFont native_font) { |
166 const char* family_name = pango_font_description_get_family(native_font); | 146 const char* family_name = pango_font_description_get_family(native_font); |
167 | 147 |
168 gint size_in_pixels = 0; | |
169 if (pango_font_description_get_size_is_absolute(native_font)) { | |
170 // If the size is absolute, then it's in Pango units rather than points. | |
171 // There are PANGO_SCALE Pango units in a device unit (pixel). | |
172 size_in_pixels = pango_font_description_get_size(native_font) / PANGO_SCALE; | |
173 } else { | |
174 // Otherwise, we need to convert from points. | |
175 size_in_pixels = | |
176 pango_font_description_get_size(native_font) * GetPixelsInPoint() / | |
177 PANGO_SCALE; | |
178 } | |
179 | |
180 // Find best match font for |family_name| to make sure we can get | 148 // Find best match font for |family_name| to make sure we can get |
181 // a SkTypeface for the default font. | 149 // a SkTypeface for the default font. |
182 // TODO(agl): remove this. | 150 // TODO(agl): remove this. |
183 std::string font_family = FindBestMatchFontFamilyName(family_name); | 151 std::string font_family = FindBestMatchFontFamilyName(family_name); |
184 | 152 |
185 InitWithNameAndSize(font_family, size_in_pixels); | 153 InitWithNameAndSize(font_family, gfx::GetPangoFontSizeInPixels(native_font)); |
186 int style = 0; | 154 int style = 0; |
187 if (pango_font_description_get_weight(native_font) == PANGO_WEIGHT_BOLD) { | 155 if (pango_font_description_get_weight(native_font) == PANGO_WEIGHT_BOLD) { |
188 // TODO(davemoore) What should we do about other weights? We currently | 156 // TODO(davemoore) What should we do about other weights? We currently |
189 // only support BOLD. | 157 // only support BOLD. |
190 style |= gfx::Font::BOLD; | 158 style |= gfx::Font::BOLD; |
191 } | 159 } |
192 if (pango_font_description_get_style(native_font) == PANGO_STYLE_ITALIC) { | 160 if (pango_font_description_get_style(native_font) == PANGO_STYLE_ITALIC) { |
193 // TODO(davemoore) What about PANGO_STYLE_OBLIQUE? | 161 // TODO(davemoore) What about PANGO_STYLE_OBLIQUE? |
194 style |= gfx::Font::ITALIC; | 162 style |= gfx::Font::ITALIC; |
195 } | 163 } |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 return new PlatformFontPango(native_font); | 431 return new PlatformFontPango(native_font); |
464 } | 432 } |
465 | 433 |
466 // static | 434 // static |
467 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 435 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
468 int font_size) { | 436 int font_size) { |
469 return new PlatformFontPango(font_name, font_size); | 437 return new PlatformFontPango(font_name, font_size); |
470 } | 438 } |
471 | 439 |
472 } // namespace gfx | 440 } // namespace gfx |
OLD | NEW |