diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-05 21:07:17 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-05 21:07:17 +0000 |
commit | bd0979609230cc9b1026e98f17f629bf0cfb6b8c (patch) | |
tree | 033fffdd50b98823f70a174e66dbe7af17ef99e1 /gfx | |
parent | 26ef58b74eb86e3520542ecd1eb768d71a374601 (diff) | |
download | chromium_src-bd0979609230cc9b1026e98f17f629bf0cfb6b8c.zip chromium_src-bd0979609230cc9b1026e98f17f629bf0cfb6b8c.tar.gz chromium_src-bd0979609230cc9b1026e98f17f629bf0cfb6b8c.tar.bz2 |
Make Font::GetStringWidth() a shortcut for gfx::Canvas::SizeStringInt() on Windows. These two functions already did the same thing on Mac and Linux. If they ever returned different results on Windows, it would lead to subtle bugs, since measurement and drawing would slightly disagree.
Clean up a few other things so that all three implementations of GetStringWidth() are identical.
BUG=38717
TEST=none
Review URL: http://codereview.chromium.org/1928003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46492 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/canvas_mac.mm | 10 | ||||
-rw-r--r-- | gfx/font_mac.mm | 11 | ||||
-rw-r--r-- | gfx/font_skia.cc | 5 | ||||
-rw-r--r-- | gfx/font_win.cc | 17 |
4 files changed, 18 insertions, 25 deletions
diff --git a/gfx/canvas_mac.mm b/gfx/canvas_mac.mm index bffa91f..87ff09b 100644 --- a/gfx/canvas_mac.mm +++ b/gfx/canvas_mac.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -28,7 +28,13 @@ Canvas::~Canvas() { void Canvas::SizeStringInt(const std::wstring& text, const gfx::Font& font, int *width, int *height, int flags) { - *width = font.GetStringWidth(text); + NSFont* native_font = font.nativeFont(); + NSString* ns_string = base::SysWideToNSString(text); + NSDictionary* attributes = + [NSDictionary dictionaryWithObject:native_font + forKey:NSFontAttributeName]; + NSSize string_size = [ns_string sizeWithAttributes:attributes]; + *width = string_size.width; *height = font.height(); } diff --git a/gfx/font_mac.mm b/gfx/font_mac.mm index fcc85833..9a1695b 100644 --- a/gfx/font_mac.mm +++ b/gfx/font_mac.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -58,12 +58,9 @@ int Font::ave_char_width() const { } int Font::GetStringWidth(const std::wstring& text) const { - NSFont* font = nativeFont(); - NSString* ns_string = base::SysWideToNSString(text); - NSDictionary* attributes = - [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; - NSSize string_size = [ns_string sizeWithAttributes:attributes]; - return string_size.width; + int width = 0, height = 0; + Canvas::SizeStringInt(text, *this, &width, &height, gfx::Canvas::NO_ELLIPSIS); + return width; } int Font::GetExpectedTextWidth(int length) const { diff --git a/gfx/font_skia.cc b/gfx/font_skia.cc index fffc53e..97a3c85 100644 --- a/gfx/font_skia.cc +++ b/gfx/font_skia.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -177,8 +177,7 @@ void Font::PaintSetup(SkPaint* paint) const { int Font::GetStringWidth(const std::wstring& text) const { int width = 0, height = 0; - - Canvas::SizeStringInt(text, *this, &width, &height, 0); + Canvas::SizeStringInt(text, *this, &width, &height, gfx::Canvas::NO_ELLIPSIS); return width; } diff --git a/gfx/font_win.cc b/gfx/font_win.cc index b55057c..4d4d28e 100644 --- a/gfx/font_win.cc +++ b/gfx/font_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,6 +12,7 @@ #include "base/logging.h" #include "base/string_util.h" #include "base/win_util.h" +#include "gfx/canvas.h" namespace gfx { @@ -169,18 +170,8 @@ Font Font::DeriveFont(int size_delta, int style) const { } int Font::GetStringWidth(const std::wstring& text) const { - int width = 0; - HDC dc = GetDC(NULL); - HFONT previous_font = static_cast<HFONT>(SelectObject(dc, hfont())); - SIZE size; - if (GetTextExtentPoint32(dc, text.c_str(), static_cast<int>(text.size()), - &size)) { - width = size.cx; - } else { - width = 0; - } - SelectObject(dc, previous_font); - ReleaseDC(NULL, dc); + int width = 0, height = 0; + Canvas::SizeStringInt(text, *this, &width, &height, gfx::Canvas::NO_ELLIPSIS); return width; } |