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 <gdk/gdk.h> | 7 #include <gdk/gdk.h> |
8 #include <map> | 8 #include <map> |
9 #include <pango/pango.h> | 9 #include <pango/pango.h> |
10 | 10 |
11 #include "app/gfx/canvas.h" | 11 #include "app/gfx/canvas.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
15 #include "third_party/skia/include/core/SkTypeface.h" | 15 #include "third_party/skia/include/core/SkTypeface.h" |
16 #include "third_party/skia/include/core/SkPaint.h" | 16 #include "third_party/skia/include/core/SkPaint.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // The font family name which is used when a user's application font for | 20 // The font family name which is used when a user's application font for |
21 // GNOME/KDE is a non-scalable one. The name should be listed in the | 21 // GNOME/KDE is a non-scalable one. The name should be listed in the |
22 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. | 22 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. |
23 const char* kFallbackFontFamilyName = "sans"; | 23 const char* kFallbackFontFamilyName = "sans"; |
24 | 24 |
25 // Pango scales font sizes. This returns the scale factor. See | |
26 // pango_cairo_context_set_resolution for details. | |
27 // NOTE: this isn't entirely accurate, in that Pango also consults the | |
28 // FC_PIXEL_SIZE first (see get_font_size in pangocairo-fcfont), but this | |
29 // seems to give us the same sizes as used by Pango for all our fonts in both | |
30 // English and Thia. | |
31 static double GetPangoScaleFactor() { | |
32 static float scale_factor = 0; | |
33 static bool determined_scale = false; | |
34 if (!determined_scale) { | |
35 PangoContext* context = gdk_pango_context_get(); | |
36 scale_factor = pango_cairo_context_get_resolution(context); | |
37 g_object_unref(context); | |
38 if (scale_factor <= 0) | |
39 scale_factor = 1; | |
40 else | |
41 scale_factor /= 72.0; | |
42 determined_scale = true; | |
43 } | |
44 return scale_factor; | |
45 } | |
46 | |
47 // Retrieves the pango metrics for a pango font description. Caches the metrics | 25 // Retrieves the pango metrics for a pango font description. Caches the metrics |
48 // and never frees them. The metrics objects are relatively small and | 26 // and never frees them. The metrics objects are relatively small and |
49 // very expensive to look up. | 27 // very expensive to look up. |
50 static PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { | 28 static PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { |
51 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; | 29 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; |
52 static PangoContext* context = NULL; | 30 static PangoContext* context = NULL; |
53 | 31 |
54 if (!context) { | 32 if (!context) { |
55 context = gdk_pango_context_get_for_screen(gdk_screen_get_default()); | 33 context = gdk_pango_context_get_for_screen(gdk_screen_get_default()); |
56 pango_context_set_language(context, pango_language_get_default()); | 34 pango_context_set_language(context, pango_language_get_default()); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 base::SysWideToUTF8(font_family_).c_str(), | 161 base::SysWideToUTF8(font_family_).c_str(), |
184 static_cast<SkTypeface::Style>(skstyle)); | 162 static_cast<SkTypeface::Style>(skstyle)); |
185 SkAutoUnref tf_helper(tf); | 163 SkAutoUnref tf_helper(tf); |
186 | 164 |
187 return Font(tf, font_family_, font_size_ + size_delta, style); | 165 return Font(tf, font_family_, font_size_ + size_delta, style); |
188 } | 166 } |
189 | 167 |
190 void Font::PaintSetup(SkPaint* paint) const { | 168 void Font::PaintSetup(SkPaint* paint) const { |
191 paint->setAntiAlias(false); | 169 paint->setAntiAlias(false); |
192 paint->setSubpixelText(false); | 170 paint->setSubpixelText(false); |
193 paint->setTextSize(SkFloatToScalar(font_size_ * GetPangoScaleFactor())); | 171 paint->setTextSize(SkFloatToScalar(font_size_ * Font::GetPangoScaleFactor())); |
194 paint->setTypeface(typeface_); | 172 paint->setTypeface(typeface_); |
195 paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold()); | 173 paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold()); |
196 paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ? | 174 paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ? |
197 -SK_Scalar1/4 : 0); | 175 -SK_Scalar1/4 : 0); |
198 } | 176 } |
199 | 177 |
200 int Font::GetStringWidth(const std::wstring& text) const { | 178 int Font::GetStringWidth(const std::wstring& text) const { |
201 int width = 0, height = 0; | 179 int width = 0, height = 0; |
202 | 180 |
203 Canvas::SizeStringInt(text, *this, &width, &height, 0); | 181 Canvas::SizeStringInt(text, *this, &width, &height, 0); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 246 |
269 int Font::FontSize() { | 247 int Font::FontSize() { |
270 return font_size_; | 248 return font_size_; |
271 } | 249 } |
272 | 250 |
273 NativeFont Font::nativeFont() const { | 251 NativeFont Font::nativeFont() const { |
274 return typeface_; | 252 return typeface_; |
275 } | 253 } |
276 | 254 |
277 } // namespace gfx | 255 } // namespace gfx |
OLD | NEW |