Allow single-line text to be top or bottom aligned.

This is a follow-up to
https://chromium-review.googlesource.com/c/chromium/src/+/1764942
which restricted vertical alignment of labels to multiline only.

It relaxes the restriction, allowing all render text to be top or bottom
aligned, though at the expense of the ability to guarantee baseline
alignment when mixed fonts are required to render a line (still a
feature of the default ALIGN_MIDDLE; a note has been put on the new
Label::SetVerticalAlignment() method to this effect).

Bug: 996905
Change-Id: I46d60902e91ea202f48ae2952b4c0cb118d80650
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1772449
Reviewed-by: Michael Wasserman <msw@chromium.org>
Commit-Queue: Dana Fried <dfried@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690877}
diff --git a/chrome/browser/ui/views/tabs/tab_hover_card_bubble_view.cc b/chrome/browser/ui/views/tabs/tab_hover_card_bubble_view.cc
index c781d7ce..d7d7c2f 100644
--- a/chrome/browser/ui/views/tabs/tab_hover_card_bubble_view.cc
+++ b/chrome/browser/ui/views/tabs/tab_hover_card_bubble_view.cc
@@ -306,6 +306,7 @@
       new views::Label(base::string16(), CONTEXT_TAB_HOVER_CARD_TITLE,
                        views::style::STYLE_PRIMARY);
   title_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
+  title_label_->SetVerticalAlignment(gfx::ALIGN_TOP);
   title_label_->SetMultiLine(true);
   title_label_->SetMaxLines(kTitleMaxLines);
   title_label_->SetProperty(views::kFlexBehaviorKey,
@@ -557,14 +558,12 @@
   }
   base::string16 domain;
   if (domain_url.SchemeIsFile()) {
-    title_label_->SetVerticalAlignment(gfx::ALIGN_MIDDLE);
     title_label_->SetMultiLine(false);
     title_label_->SetElideBehavior(gfx::ELIDE_MIDDLE);
     domain = l10n_util::GetStringUTF16(IDS_HOVER_CARD_FILE_URL_SOURCE);
   } else {
     title_label_->SetElideBehavior(gfx::ELIDE_TAIL);
     title_label_->SetMultiLine(true);
-    title_label_->SetVerticalAlignment(gfx::ALIGN_TOP);
     domain = url_formatter::FormatUrl(
         domain_url,
         url_formatter::kFormatUrlOmitDefaults |
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index 2ed814d..8aedc0e 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -1020,7 +1020,6 @@
   Vector2d offset = display_rect().OffsetFromOrigin();
   // TODO(ckocagil): Apply the display offset for multiline scrolling.
   if (!multiline()) {
-    DCHECK_EQ(ALIGN_MIDDLE, vertical_alignment_);
     offset.Add(GetUpdatedDisplayOffset());
   } else {
     DCHECK_LT(line_number, lines().size());
@@ -1417,14 +1416,20 @@
       offset.set_x((offset.x() + 1) / 2);
   }
 
-  if (!multiline_)
-    offset.set_y(GetBaseline() - GetDisplayTextBaseline());
-  else if (vertical_alignment_ == ALIGN_TOP)
-    offset.set_y(0);
-  else if (vertical_alignment_ == ALIGN_MIDDLE)
-    offset.set_y((display_rect_.height() - GetStringSize().height()) / 2);
-  else
-    offset.set_y(display_rect_.height() - GetStringSize().height());
+  switch (vertical_alignment_) {
+    case ALIGN_TOP:
+      offset.set_y(0);
+      break;
+    case ALIGN_MIDDLE:
+      if (multiline_)
+        offset.set_y((display_rect_.height() - GetStringSize().height()) / 2);
+      else
+        offset.set_y(GetBaseline() - GetDisplayTextBaseline());
+      break;
+    case ALIGN_BOTTOM:
+      offset.set_y(display_rect_.height() - GetStringSize().height());
+      break;
+  }
 
   return offset;
 }
diff --git a/ui/views/controls/label.cc b/ui/views/controls/label.cc
index 5bfdab55..a4f20420 100644
--- a/ui/views/controls/label.cc
+++ b/ui/views/controls/label.cc
@@ -208,9 +208,6 @@
 }
 
 void Label::SetVerticalAlignment(gfx::VerticalAlignment alignment) {
-  // TODO(crbug.com/996905): remove once single-line vertical alignment is
-  // supported.
-  DCHECK(GetMultiLine() || alignment == gfx::ALIGN_MIDDLE);
   if (GetVerticalAlignment() == alignment)
     return;
   full_text_->SetVerticalAlignment(alignment);
@@ -237,9 +234,6 @@
 void Label::SetMultiLine(bool multi_line) {
   DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL ||
                          elide_behavior_ == gfx::NO_ELIDE));
-  // TODO(crbug.com/996905): remove once single-line vertical alignment is
-  // supported.
-  DCHECK(multi_line || GetVerticalAlignment() == gfx::ALIGN_MIDDLE);
   if (this->GetMultiLine() == multi_line)
     return;
   multi_line_ = multi_line;
diff --git a/ui/views/controls/label.h b/ui/views/controls/label.h
index 47cec8b..bc90b6f 100644
--- a/ui/views/controls/label.h
+++ b/ui/views/controls/label.h
@@ -131,10 +131,9 @@
 
   // Gets/Sets the vertical alignment. Affects how whitespace is distributed
   // vertically around the label text, or if the label is not tall enough to
-  // render all of the text, what gets cut off.
-  //
-  // Currently, this must be ALIGN_MIDDLE (default) for non-multiline labels.
-  // TODO(crbug.com/996905): support single-line vertical alignment.
+  // render all of the text, what gets cut off. ALIGN_MIDDLE is default and is
+  // strongly suggested for single-line labels because it produces a consistent
+  // baseline even when rendering with mixed fonts.
   gfx::VerticalAlignment GetVerticalAlignment() const;
   void SetVerticalAlignment(gfx::VerticalAlignment alignment);