Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: ui/gfx/platform_font_pango.cc

Issue 10406014: Add a ScopedPangoFontDescription helper class and use it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/pango_util.cc ('k') | ui/gfx/platform_font_pango_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <fontconfig/fontconfig.h> 7 #include <fontconfig/fontconfig.h>
8 #include <pango/pango.h> 8 #include <pango/pango.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 91
92 Font* PlatformFontPango::default_font_ = NULL; 92 Font* PlatformFontPango::default_font_ = NULL;
93 93
94 //////////////////////////////////////////////////////////////////////////////// 94 ////////////////////////////////////////////////////////////////////////////////
95 // PlatformFontPango, public: 95 // PlatformFontPango, public:
96 96
97 PlatformFontPango::PlatformFontPango() { 97 PlatformFontPango::PlatformFontPango() {
98 if (default_font_ == NULL) { 98 if (default_font_ == NULL) {
99 std::string font_name = GetDefaultFont(); 99 std::string font_name = GetDefaultFont();
100 100
101 PangoFontDescription* desc = 101 ScopedPangoFontDescription desc(
102 pango_font_description_from_string(font_name.c_str()); 102 pango_font_description_from_string(font_name.c_str()));
103 default_font_ = new Font(desc); 103 default_font_ = new Font(desc.get());
104 pango_font_description_free(desc);
105 104
106 DCHECK(default_font_); 105 DCHECK(default_font_);
107 } 106 }
108 107
109 InitFromPlatformFont( 108 InitFromPlatformFont(
110 static_cast<PlatformFontPango*>(default_font_->platform_font())); 109 static_cast<PlatformFontPango*>(default_font_->platform_font()));
111 } 110 }
112 111
113 PlatformFontPango::PlatformFontPango(NativeFont native_font) { 112 PlatformFontPango::PlatformFontPango(NativeFont native_font) {
114 std::vector<std::string> family_names; 113 std::vector<std::string> family_names;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 paint->setTextSize(font_size_pixels_); 332 paint->setTextSize(font_size_pixels_);
334 paint->setTypeface(typeface_); 333 paint->setTypeface(typeface_);
335 paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); 334 paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold());
336 paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? 335 paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ?
337 -SK_Scalar1/4 : 0); 336 -SK_Scalar1/4 : 0);
338 } 337 }
339 338
340 void PlatformFontPango::InitPangoMetrics() { 339 void PlatformFontPango::InitPangoMetrics() {
341 if (!pango_metrics_inited_) { 340 if (!pango_metrics_inited_) {
342 pango_metrics_inited_ = true; 341 pango_metrics_inited_ = true;
343 PangoFontDescription* pango_desc = GetNativeFont(); 342 ScopedPangoFontDescription pango_desc(GetNativeFont());
344 PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc); 343 PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get());
345 344
346 underline_position_pixels_ = 345 underline_position_pixels_ =
347 pango_font_metrics_get_underline_position(pango_metrics) / 346 pango_font_metrics_get_underline_position(pango_metrics) /
348 PANGO_SCALE; 347 PANGO_SCALE;
349 348
350 // TODO(davemoore): Come up with a better solution. 349 // TODO(davemoore): Come up with a better solution.
351 // This is a hack, but without doing this the underlines 350 // This is a hack, but without doing this the underlines
352 // we get end up fuzzy. So we align to the midpoint of a pixel. 351 // we get end up fuzzy. So we align to the midpoint of a pixel.
353 underline_position_pixels_ /= 2; 352 underline_position_pixels_ /= 2;
354 353
355 underline_thickness_pixels_ = 354 underline_thickness_pixels_ =
356 pango_font_metrics_get_underline_thickness(pango_metrics) / 355 pango_font_metrics_get_underline_thickness(pango_metrics) /
357 PANGO_SCALE; 356 PANGO_SCALE;
358 357
359 // First get the Pango-based width (converting from Pango units to pixels). 358 // First get the Pango-based width (converting from Pango units to pixels).
360 const double pango_width_pixels = 359 const double pango_width_pixels =
361 pango_font_metrics_get_approximate_char_width(pango_metrics) / 360 pango_font_metrics_get_approximate_char_width(pango_metrics) /
362 PANGO_SCALE; 361 PANGO_SCALE;
363 362
364 // Yes, this is how Microsoft recommends calculating the dialog unit 363 // Yes, this is how Microsoft recommends calculating the dialog unit
365 // conversions. 364 // conversions.
366 const int text_width_pixels = GetStringWidth( 365 const int text_width_pixels = GetStringWidth(
367 ASCIIToUTF16("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 366 ASCIIToUTF16("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
368 const double dialog_units_pixels = (text_width_pixels / 26 + 1) / 2; 367 const double dialog_units_pixels = (text_width_pixels / 26 + 1) / 2;
369 average_width_pixels_ = std::min(pango_width_pixels, dialog_units_pixels); 368 average_width_pixels_ = std::min(pango_width_pixels, dialog_units_pixels);
370 pango_font_description_free(pango_desc);
371 } 369 }
372 } 370 }
373 371
374 372
375 double PlatformFontPango::GetAverageWidth() const { 373 double PlatformFontPango::GetAverageWidth() const {
376 const_cast<PlatformFontPango*>(this)->InitPangoMetrics(); 374 const_cast<PlatformFontPango*>(this)->InitPangoMetrics();
377 return average_width_pixels_; 375 return average_width_pixels_;
378 } 376 }
379 377
380 //////////////////////////////////////////////////////////////////////////////// 378 ////////////////////////////////////////////////////////////////////////////////
381 // PlatformFont, public: 379 // PlatformFont, public:
382 380
383 // static 381 // static
384 PlatformFont* PlatformFont::CreateDefault() { 382 PlatformFont* PlatformFont::CreateDefault() {
385 return new PlatformFontPango; 383 return new PlatformFontPango;
386 } 384 }
387 385
388 // static 386 // static
389 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { 387 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
390 return new PlatformFontPango(native_font); 388 return new PlatformFontPango(native_font);
391 } 389 }
392 390
393 // static 391 // static
394 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 392 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
395 int font_size) { 393 int font_size) {
396 return new PlatformFontPango(font_name, font_size); 394 return new PlatformFontPango(font_name, font_size);
397 } 395 }
398 396
399 } // namespace gfx 397 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/pango_util.cc ('k') | ui/gfx/platform_font_pango_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698