Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / platform_font_pango.cc
1 // Copyright (c) 2012 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 "ui/gfx/platform_font_pango.h"
6
7 #include <fontconfig/fontconfig.h>
8 #include <pango/pango.h>
9
10 #include <algorithm>
11 #include <string>
12
13 #include "base/logging.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "third_party/skia/include/core/SkPaint.h"
18 #include "third_party/skia/include/core/SkString.h"
19 #include "third_party/skia/include/core/SkTypeface.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/font.h"
22 #include "ui/gfx/font_list.h"
23 #include "ui/gfx/linux_font_delegate.h"
24 #include "ui/gfx/pango_util.h"
25 #include "ui/gfx/text_utils.h"
26
27 namespace {
28
29 // 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 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp.
32 const char* kFallbackFontFamilyName = "sans";
33
34 // Creates a SkTypeface for the passed-in Font::FontStyle and family. If a
35 // fallback typeface is used instead of the requested family, |family| will be
36 // updated to contain the fallback's family name.
37 skia::RefPtr<SkTypeface> CreateSkTypeface(int style, std::string* family) {
38   DCHECK(family);
39
40   int skia_style = SkTypeface::kNormal;
41   if (gfx::Font::BOLD & style)
42     skia_style |= SkTypeface::kBold;
43   if (gfx::Font::ITALIC & style)
44     skia_style |= SkTypeface::kItalic;
45
46   skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(SkTypeface::CreateFromName(
47       family->c_str(), static_cast<SkTypeface::Style>(skia_style)));
48   if (!typeface) {
49     // A non-scalable font such as .pcf is specified. Fall back to a default
50     // scalable font.
51     typeface = skia::AdoptRef(SkTypeface::CreateFromName(
52         kFallbackFontFamilyName, static_cast<SkTypeface::Style>(skia_style)));
53     CHECK(typeface) << "Could not find any font: " << family << ", "
54                     << kFallbackFontFamilyName;
55     *family = kFallbackFontFamilyName;
56   }
57   return typeface;
58 }
59
60 }  // namespace
61
62 namespace gfx {
63
64 // static
65 Font* PlatformFontPango::default_font_ = NULL;
66
67 #if defined(OS_CHROMEOS)
68 // static
69 std::string* PlatformFontPango::default_font_description_ = NULL;
70 #endif
71
72 ////////////////////////////////////////////////////////////////////////////////
73 // PlatformFontPango, public:
74
75 PlatformFontPango::PlatformFontPango() {
76   if (!default_font_) {
77     scoped_ptr<ScopedPangoFontDescription> description;
78 #if defined(OS_CHROMEOS)
79     CHECK(default_font_description_);
80     description.reset(
81         new ScopedPangoFontDescription(*default_font_description_));
82 #else
83     const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
84     if (delegate)
85       description = delegate->GetDefaultPangoFontDescription();
86 #endif
87     if (!description || !description->get())
88       description.reset(new ScopedPangoFontDescription("sans 10"));
89     default_font_ = new Font(description->get());
90   }
91
92   InitFromPlatformFont(
93       static_cast<PlatformFontPango*>(default_font_->platform_font()));
94 }
95
96 PlatformFontPango::PlatformFontPango(NativeFont native_font) {
97   FontRenderParamsQuery query(false);
98   base::SplitString(pango_font_description_get_family(native_font), ',',
99                     &query.families);
100
101   const int pango_size =
102       pango_font_description_get_size(native_font) / PANGO_SCALE;
103   if (pango_font_description_get_size_is_absolute(native_font))
104     query.pixel_size = pango_size;
105   else
106     query.point_size = pango_size;
107
108   query.style = gfx::Font::NORMAL;
109   // TODO(davemoore): Support weights other than bold?
110   if (pango_font_description_get_weight(native_font) == PANGO_WEIGHT_BOLD)
111     query.style |= gfx::Font::BOLD;
112   // TODO(davemoore): What about PANGO_STYLE_OBLIQUE?
113   if (pango_font_description_get_style(native_font) == PANGO_STYLE_ITALIC)
114     query.style |= gfx::Font::ITALIC;
115
116   std::string font_family;
117   const FontRenderParams params = gfx::GetFontRenderParams(query, &font_family);
118   InitFromDetails(skia::RefPtr<SkTypeface>(), font_family,
119                   gfx::GetPangoFontSizeInPixels(native_font),
120                   query.style, params);
121 }
122
123 PlatformFontPango::PlatformFontPango(const std::string& font_name,
124                                      int font_size_pixels) {
125   FontRenderParamsQuery query(false);
126   query.families.push_back(font_name);
127   query.pixel_size = font_size_pixels;
128   query.style = gfx::Font::NORMAL;
129   InitFromDetails(skia::RefPtr<SkTypeface>(), font_name, font_size_pixels,
130                   query.style, gfx::GetFontRenderParams(query, NULL));
131 }
132
133 ////////////////////////////////////////////////////////////////////////////////
134 // PlatformFontPango, PlatformFont implementation:
135
136 // static
137 void PlatformFontPango::ReloadDefaultFont() {
138   delete default_font_;
139   default_font_ = NULL;
140 }
141
142 #if defined(OS_CHROMEOS)
143 // static
144 void PlatformFontPango::SetDefaultFontDescription(
145     const std::string& font_description) {
146   delete default_font_description_;
147   default_font_description_ = new std::string(font_description);
148 }
149
150 #endif
151
152 Font PlatformFontPango::DeriveFont(int size_delta, int style) const {
153   const int new_size = font_size_pixels_ + size_delta;
154   DCHECK_GT(new_size, 0);
155
156   // If the style changed, we may need to load a new face.
157   std::string new_family = font_family_;
158   skia::RefPtr<SkTypeface> typeface =
159       (style == style_) ? typeface_ : CreateSkTypeface(style, &new_family);
160
161   FontRenderParamsQuery query(false);
162   query.families.push_back(new_family);
163   query.pixel_size = new_size;
164   query.style = style;
165
166   return Font(new PlatformFontPango(typeface, new_family, new_size, style,
167                                     gfx::GetFontRenderParams(query, NULL)));
168 }
169
170 int PlatformFontPango::GetHeight() const {
171   return height_pixels_;
172 }
173
174 int PlatformFontPango::GetBaseline() const {
175   return ascent_pixels_;
176 }
177
178 int PlatformFontPango::GetCapHeight() const {
179   return cap_height_pixels_;
180 }
181
182 int PlatformFontPango::GetExpectedTextWidth(int length) const {
183   double char_width = const_cast<PlatformFontPango*>(this)->GetAverageWidth();
184   return round(static_cast<float>(length) * char_width);
185 }
186
187 int PlatformFontPango::GetStyle() const {
188   return style_;
189 }
190
191 std::string PlatformFontPango::GetFontName() const {
192   return font_family_;
193 }
194
195 std::string PlatformFontPango::GetActualFontNameForTesting() const {
196   SkString family_name;
197   typeface_->getFamilyName(&family_name);
198   return family_name.c_str();
199 }
200
201 int PlatformFontPango::GetFontSize() const {
202   return font_size_pixels_;
203 }
204
205 const FontRenderParams& PlatformFontPango::GetFontRenderParams() const {
206   return font_render_params_;
207 }
208
209 NativeFont PlatformFontPango::GetNativeFont() const {
210   PangoFontDescription* pfd = pango_font_description_new();
211   pango_font_description_set_family(pfd, GetFontName().c_str());
212   // Set the absolute size to avoid overflowing UI elements.
213   // pango_font_description_set_absolute_size() takes a size in Pango units.
214   // There are PANGO_SCALE Pango units in one device unit.  Screen output
215   // devices use pixels as their device units.
216   pango_font_description_set_absolute_size(
217       pfd, font_size_pixels_ * PANGO_SCALE);
218
219   switch (GetStyle()) {
220     case gfx::Font::NORMAL:
221       // Nothing to do, should already be PANGO_STYLE_NORMAL.
222       break;
223     case gfx::Font::BOLD:
224       pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD);
225       break;
226     case gfx::Font::ITALIC:
227       pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC);
228       break;
229     case gfx::Font::UNDERLINE:
230       // TODO(deanm): How to do underline?  Where do we use it?  Probably have
231       // to paint it ourselves, see pango_font_metrics_get_underline_position.
232       break;
233   }
234
235   return pfd;
236 }
237
238 ////////////////////////////////////////////////////////////////////////////////
239 // PlatformFontPango, private:
240
241 PlatformFontPango::PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
242                                      const std::string& name,
243                                      int size_pixels,
244                                      int style,
245                                      const FontRenderParams& render_params) {
246   InitFromDetails(typeface, name, size_pixels, style, render_params);
247 }
248
249 PlatformFontPango::~PlatformFontPango() {}
250
251 void PlatformFontPango::InitFromDetails(
252     const skia::RefPtr<SkTypeface>& typeface,
253     const std::string& font_family,
254     int font_size_pixels,
255     int style,
256     const FontRenderParams& render_params) {
257   DCHECK_GT(font_size_pixels, 0);
258
259   font_family_ = font_family;
260   typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_);
261
262   font_size_pixels_ = font_size_pixels;
263   style_ = style;
264   font_render_params_ = render_params;
265
266   SkPaint paint;
267   SkPaint::FontMetrics metrics;
268   PaintSetup(&paint);
269   paint.getFontMetrics(&metrics);
270   ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent);
271   height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent);
272   cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight);
273
274   pango_metrics_inited_ = false;
275   average_width_pixels_ = 0.0f;
276 }
277
278 void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) {
279   typeface_ = other->typeface_;
280   font_family_ = other->font_family_;
281   font_size_pixels_ = other->font_size_pixels_;
282   style_ = other->style_;
283   font_render_params_ = other->font_render_params_;
284   ascent_pixels_ = other->ascent_pixels_;
285   height_pixels_ = other->height_pixels_;
286   cap_height_pixels_ = other->cap_height_pixels_;
287   pango_metrics_inited_ = other->pango_metrics_inited_;
288   average_width_pixels_ = other->average_width_pixels_;
289 }
290
291 void PlatformFontPango::PaintSetup(SkPaint* paint) const {
292   paint->setAntiAlias(false);
293   paint->setSubpixelText(false);
294   paint->setTextSize(font_size_pixels_);
295   paint->setTypeface(typeface_.get());
296   paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold());
297   paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ?
298                       -SK_Scalar1/4 : 0);
299 }
300
301 void PlatformFontPango::InitPangoMetrics() {
302   if (!pango_metrics_inited_) {
303     pango_metrics_inited_ = true;
304     ScopedPangoFontDescription pango_desc(GetNativeFont());
305     PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get());
306
307     // First get the Pango-based width (converting from Pango units to pixels).
308     const double pango_width_pixels =
309         pango_font_metrics_get_approximate_char_width(pango_metrics) /
310         PANGO_SCALE;
311
312     // Yes, this is how Microsoft recommends calculating the dialog unit
313     // conversions.
314     const int text_width_pixels = GetStringWidth(
315         base::ASCIIToUTF16(
316             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
317         FontList(Font(this)));
318     const double dialog_units_pixels = (text_width_pixels / 26 + 1) / 2;
319     average_width_pixels_ = std::min(pango_width_pixels, dialog_units_pixels);
320   }
321 }
322
323 double PlatformFontPango::GetAverageWidth() const {
324   const_cast<PlatformFontPango*>(this)->InitPangoMetrics();
325   return average_width_pixels_;
326 }
327
328 ////////////////////////////////////////////////////////////////////////////////
329 // PlatformFont, public:
330
331 // static
332 PlatformFont* PlatformFont::CreateDefault() {
333   return new PlatformFontPango;
334 }
335
336 // static
337 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
338   return new PlatformFontPango(native_font);
339 }
340
341 // static
342 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
343                                                   int font_size) {
344   return new PlatformFontPango(font_name, font_size);
345 }
346
347 }  // namespace gfx