summaryrefslogtreecommitdiffstats
path: root/ui/gfx
diff options
context:
space:
mode:
authordharcourt@chromium.org <dharcourt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-19 03:39:58 +0000
committerdharcourt@chromium.org <dharcourt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-19 03:39:58 +0000
commit561f748a646edd1624ed4c07737a1987c82d1469 (patch)
tree6e12cd8559c501f75523b29829a3f09e37c79d8a /ui/gfx
parenta1c0601dc65636bb6dca44dceef36b8823791fa0 (diff)
downloadchromium_src-561f748a646edd1624ed4c07737a1987c82d1469.zip
chromium_src-561f748a646edd1624ed4c07737a1987c82d1469.tar.gz
chromium_src-561f748a646edd1624ed4c07737a1987c82d1469.tar.bz2
Add line height setting to views::Label & use it for notifications.
This fixes the line spacing of notification so it's 18px on all platforms (instead of the previous 17px on Chrome OS and 15px on Windows). To do this, support for a line height property was added to the BoundedLabel that NotificationView uses, to the Label that BoundedLabel uses, and to the Canvas that Label uses. BUG=225871 R=mukai@chromium.org,msw@chromium.org Review URL: https://chromiumcodereview.appspot.com/14322007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195076 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r--ui/gfx/canvas.cc3
-rw-r--r--ui/gfx/canvas.h8
-rw-r--r--ui/gfx/canvas_android.cc6
-rw-r--r--ui/gfx/canvas_mac.mm18
-rw-r--r--ui/gfx/canvas_skia.cc22
-rw-r--r--ui/gfx/canvas_unittest.cc59
6 files changed, 80 insertions, 36 deletions
diff --git a/ui/gfx/canvas.cc b/ui/gfx/canvas.cc
index 134c145..aac2c035 100644
--- a/ui/gfx/canvas.cc
+++ b/ui/gfx/canvas.cc
@@ -89,7 +89,7 @@ void Canvas::RecreateBackingCanvas(const gfx::Size& size,
// static
int Canvas::GetStringWidth(const string16& text, const gfx::Font& font) {
int width = 0, height = 0;
- Canvas::SizeStringInt(text, font, &width, &height, NO_ELLIPSIS);
+ Canvas::SizeStringInt(text, font, &width, &height, 0, NO_ELLIPSIS);
return width;
}
@@ -433,6 +433,7 @@ void Canvas::DrawStringInt(const string16& text,
font,
color,
gfx::Rect(x, y, w, h),
+ 0,
flags,
ShadowValues());
}
diff --git a/ui/gfx/canvas.h b/ui/gfx/canvas.h
index c7673bc..65fc156 100644
--- a/ui/gfx/canvas.h
+++ b/ui/gfx/canvas.h
@@ -124,10 +124,12 @@ class UI_EXPORT Canvas {
// Compute the size required to draw some text with the provided font.
// Attempts to fit the text with the provided width and height. Increases
// height and then width as needed to make the text fit. This method
- // supports multiple lines.
+ // supports multiple lines. On Skia only a line_height can be specified and
+ // specifying a 0 value for it will cause the default height to be used.
static void SizeStringInt(const string16& text,
const gfx::Font& font,
int* width, int* height,
+ int line_height,
int flags);
// Returns the number of horizontal pixels needed to display the specified
@@ -322,11 +324,13 @@ class UI_EXPORT Canvas {
int flags);
// Similar to above DrawStringInt method but with text shadows support.
- // Currently it's only implemented for canvas skia.
+ // Currently it's only implemented for canvas skia. Specifying a 0 line_height
+ // will cause the default height to be used.
void DrawStringWithShadows(const string16& text,
const gfx::Font& font,
SkColor color,
const gfx::Rect& text_bounds,
+ int line_height,
int flags,
const ShadowValues& shadows);
diff --git a/ui/gfx/canvas_android.cc b/ui/gfx/canvas_android.cc
index 73ddee8..0c339f9dc 100644
--- a/ui/gfx/canvas_android.cc
+++ b/ui/gfx/canvas_android.cc
@@ -12,7 +12,10 @@ namespace gfx {
// static
void Canvas::SizeStringInt(const string16& text,
const gfx::Font& font,
- int* width, int* height, int flags) {
+ int* width,
+ int* height,
+ int line_height,
+ int flags) {
NOTIMPLEMENTED();
}
@@ -20,6 +23,7 @@ void Canvas::DrawStringWithShadows(const string16& text,
const gfx::Font& font,
SkColor color,
const gfx::Rect& text_bounds,
+ int line_height,
int flags,
const ShadowValues& shadows) {
NOTIMPLEMENTED();
diff --git a/ui/gfx/canvas_mac.mm b/ui/gfx/canvas_mac.mm
index bb99c06..f1ad6a8 100644
--- a/ui/gfx/canvas_mac.mm
+++ b/ui/gfx/canvas_mac.mm
@@ -13,11 +13,10 @@
#include "ui/gfx/rect.h"
// Note: This is a temporary Skia-based implementation of the ui/gfx text
-// rendering routines for views/aura. It replaces the stale Cocoa-based
-// implementation. A future |canvas_skia.cc| implementation will supersede
-// this and the other platform-specific implmenentations.
-// Most drawing options, such as alignment and multi-line, are not implemented
-// here.
+// rendering routines for views/aura. It replaces the stale Cocoa-based
+// implementation. A future |canvas_skia.cc| implementation will supersede
+// this and the other platform-specific implmenentations. Most drawing options,
+// such as alignment, multi-line, and line heights are not implemented here.
namespace {
@@ -40,7 +39,11 @@ void Canvas::SizeStringInt(const string16& text,
const gfx::Font& font,
int* width,
int* height,
+ int line_height,
int flags) {
+ DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented.";
+ DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented.";
+
NSFont* native_font = font.GetNativeFont();
NSString* ns_string = base::SysUTF16ToNSString(text);
NSDictionary* attributes =
@@ -55,9 +58,12 @@ void Canvas::DrawStringWithShadows(const string16& text,
const gfx::Font& font,
SkColor color,
const gfx::Rect& text_bounds,
+ int line_height,
int flags,
const ShadowValues& shadows) {
- DLOG_IF(WARNING, !shadows.empty()) << "Text shadow not implemented.";
+ DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented.";
+ DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented.";
+ DLOG_IF(WARNING, !shadows.empty()) << "Text shadows not implemented.";
skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(
SkTypeface::CreateFromName(
diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc
index 2d995cf..baed1dd 100644
--- a/ui/gfx/canvas_skia.cc
+++ b/ui/gfx/canvas_skia.cc
@@ -169,6 +169,7 @@ int AdjustPlatformSpecificFlags(const string16& text, int flags) {
void Canvas::SizeStringInt(const string16& text,
const Font& font,
int* width, int* height,
+ int line_height,
int flags) {
DCHECK_GE(*width, 0);
DCHECK_GE(*height, 0);
@@ -201,7 +202,7 @@ void Canvas::SizeStringInt(const string16& text,
render_text->SetText(strings[i]);
const Size string_size = render_text->GetStringSize();
w = std::max(w, string_size.width());
- h += string_size.height();
+ h += (i > 0 && line_height > 0) ? line_height : string_size.height();
}
*width = w;
*height = h;
@@ -228,6 +229,7 @@ void Canvas::DrawStringWithShadows(const string16& text,
const Font& font,
SkColor color,
const Rect& text_bounds,
+ int line_height,
int flags,
const ShadowValues& shadows) {
if (!IntersectsClipRect(text_bounds))
@@ -268,18 +270,22 @@ void Canvas::DrawStringWithShadows(const string16& text,
for (size_t i = 0; i < strings.size(); i++) {
ui::Range range = StripAcceleratorChars(flags, &strings[i]);
UpdateRenderText(rect, strings[i], font, flags, color, render_text.get());
- const int line_height = render_text->GetStringSize().height();
+ int line_padding = 0;
+ if (line_height > 0)
+ line_padding = line_height - render_text->GetStringSize().height();
+ else
+ line_height = render_text->GetStringSize().height();
// TODO(msw|asvitkine): Center Windows multi-line text: crbug.com/107357
#if !defined(OS_WIN)
if (i == 0) {
// TODO(msw|asvitkine): Support multi-line text with varied heights.
- const int aggregate_height = strings.size() * line_height;
- rect += Vector2d(0, (text_bounds.height() - aggregate_height) / 2);
+ const int text_height = strings.size() * line_height - line_padding;
+ rect += Vector2d(0, (text_bounds.height() - text_height) / 2);
}
#endif
- rect.set_height(line_height);
+ rect.set_height(line_height - line_padding);
if (range.IsValid())
render_text->ApplyStyle(UNDERLINE, true, range);
@@ -313,10 +319,10 @@ void Canvas::DrawStringWithShadows(const string16& text,
UpdateRenderText(rect, adjusted_text, font, flags, color,
render_text.get());
- const int line_height = render_text->GetStringSize().height();
+ const int text_height = render_text->GetStringSize().height();
// Center the text vertically.
- rect += Vector2d(0, (text_bounds.height() - line_height) / 2);
- rect.set_height(line_height);
+ rect += Vector2d(0, (text_bounds.height() - text_height) / 2);
+ rect.set_height(text_height);
render_text->SetDisplayRect(rect);
if (range.IsValid())
render_text->ApplyStyle(UNDERLINE, true, range);
diff --git a/ui/gfx/canvas_unittest.cc b/ui/gfx/canvas_unittest.cc
index ba0a0bc..2d02725 100644
--- a/ui/gfx/canvas_unittest.cc
+++ b/ui/gfx/canvas_unittest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <limits>
+
#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/canvas.h"
@@ -9,29 +11,50 @@
namespace gfx {
-TEST(CanvasTest, StringWidth) {
- const string16 text = UTF8ToUTF16("Test");
- const int width = Canvas::GetStringWidth(text, Font());
-
- EXPECT_GT(width, 0);
+class CanvasTest : public testing::Test {
+ protected:
+ int GetStringWidth(const char *text) {
+ return Canvas::GetStringWidth(UTF8ToUTF16(text), font_);
+ }
+
+ gfx::Size SizeStringInt(const char *text, int width, int line_height) {
+ string16 text16 = UTF8ToUTF16(text);
+ int height = 0;
+ int flags = (text16.find('\n') != string16::npos) ? Canvas::MULTI_LINE : 0;
+ Canvas::SizeStringInt(text16, font_, &width, &height, line_height, flags);
+ return gfx::Size(width, height);
+ }
+
+ private:
+ gfx::Font font_;
+};
+
+TEST_F(CanvasTest, StringWidth) {
+ EXPECT_GT(GetStringWidth("Test"), 0);
}
-TEST(CanvasTest, StringWidthEmptyString) {
- const string16 text = UTF8ToUTF16("");
- const int width = Canvas::GetStringWidth(text, Font());
-
- EXPECT_EQ(0, width);
+TEST_F(CanvasTest, StringWidthEmptyString) {
+ EXPECT_EQ(0, GetStringWidth(""));
}
-TEST(CanvasTest, StringSizeEmptyString) {
- const Font font;
- const string16 text = UTF8ToUTF16("");
- int width = 0;
- int height = 0;
- Canvas::SizeStringInt(text, font, &width, &height, 0);
+TEST_F(CanvasTest, StringSizeEmptyString) {
+ gfx::Size size = SizeStringInt("", 0, 0);
+ EXPECT_EQ(0, size.width());
+ EXPECT_GT(size.height(), 0);
+}
- EXPECT_EQ(0, width);
- EXPECT_GT(height, 0);
+// Line height is only supported on Skia.
+#if defined(OS_MACOSX) || defined(OS_ANDROID)
+#define MAYBE_StringSizeWithLineHeight DISABLED_StringSizeWithLineHeight
+#else
+#define MAYBE_StringSizeWithLineHeight StringSizeWithLineHeight
+#endif
+
+TEST_F(CanvasTest, MAYBE_StringSizeWithLineHeight) {
+ gfx::Size one_line_size = SizeStringInt("Q", 0, 0);
+ gfx::Size four_line_size = SizeStringInt("Q\nQ\nQ\nQ", 1000000, 1000);
+ EXPECT_EQ(one_line_size.width(), four_line_size.width());
+ EXPECT_EQ(3 * 1000 + one_line_size.height(), four_line_size.height());
}
} // namespace gfx