typography_provider.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2017 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. #include "ui/views/style/typography_provider.h"
  5. #include <string>
  6. #include "base/logging.h"
  7. #include "build/build_config.h"
  8. #include "ui/base/default_style.h"
  9. #include "ui/base/resource/resource_bundle.h"
  10. #include "ui/color/color_id.h"
  11. #include "ui/color/color_provider.h"
  12. #include "ui/views/style/typography.h"
  13. #include "ui/views/view.h"
  14. #if BUILDFLAG(IS_MAC)
  15. #include "base/mac/mac_util.h"
  16. #endif
  17. namespace views {
  18. namespace {
  19. gfx::Font::Weight GetValueBolderThan(gfx::Font::Weight weight) {
  20. switch (weight) {
  21. case gfx::Font::Weight::BOLD:
  22. return gfx::Font::Weight::EXTRA_BOLD;
  23. case gfx::Font::Weight::EXTRA_BOLD:
  24. case gfx::Font::Weight::BLACK:
  25. return gfx::Font::Weight::BLACK;
  26. default:
  27. return gfx::Font::Weight::BOLD;
  28. }
  29. }
  30. ui::ColorId GetDisabledColorId(int context) {
  31. switch (context) {
  32. case style::CONTEXT_BUTTON_MD:
  33. return ui::kColorButtonForegroundDisabled;
  34. case style::CONTEXT_TEXTFIELD:
  35. return ui::kColorTextfieldForegroundDisabled;
  36. case style::CONTEXT_MENU:
  37. case style::CONTEXT_TOUCH_MENU:
  38. return ui::kColorMenuItemForegroundDisabled;
  39. default:
  40. return ui::kColorLabelForegroundDisabled;
  41. }
  42. }
  43. ui::ColorId GetMenuColorId(int style) {
  44. switch (style) {
  45. case style::STYLE_SECONDARY:
  46. return ui::kColorMenuItemForegroundSecondary;
  47. case style::STYLE_SELECTED:
  48. return ui::kColorMenuItemForegroundSelected;
  49. case style::STYLE_HIGHLIGHTED:
  50. return ui::kColorMenuItemForegroundHighlighted;
  51. default:
  52. return ui::kColorMenuItemForeground;
  53. }
  54. }
  55. ui::ColorId GetHintColorId(int context) {
  56. return (context == style::CONTEXT_TEXTFIELD)
  57. ? ui::kColorTextfieldForegroundPlaceholder
  58. : ui::kColorLabelForegroundSecondary;
  59. }
  60. ui::ColorId GetColorId(int context, int style) {
  61. if (style == style::STYLE_DIALOG_BUTTON_DEFAULT)
  62. return ui::kColorButtonForegroundProminent;
  63. if (style == style::STYLE_DISABLED)
  64. return GetDisabledColorId(context);
  65. if (style == style::STYLE_LINK)
  66. return ui::kColorLinkForeground;
  67. if (style == style::STYLE_HINT)
  68. return GetHintColorId(context);
  69. if (context == style::CONTEXT_BUTTON_MD)
  70. return ui::kColorButtonForeground;
  71. if (context == style::CONTEXT_LABEL && style == style::STYLE_SECONDARY)
  72. return ui::kColorLabelForegroundSecondary;
  73. if (context == style::CONTEXT_DIALOG_BODY_TEXT &&
  74. (style == style::STYLE_PRIMARY || style == style::STYLE_SECONDARY))
  75. return ui::kColorDialogForeground;
  76. if (context == style::CONTEXT_TEXTFIELD)
  77. return ui::kColorTextfieldForeground;
  78. if (context == style::CONTEXT_MENU || context == style::CONTEXT_TOUCH_MENU)
  79. return GetMenuColorId(style);
  80. return ui::kColorLabelForeground;
  81. }
  82. } // namespace
  83. ui::ResourceBundle::FontDetails TypographyProvider::GetFontDetails(
  84. int context,
  85. int style) const {
  86. DCHECK(StyleAllowedForContext(context, style))
  87. << "context: " << context << " style: " << style;
  88. ui::ResourceBundle::FontDetails details;
  89. switch (context) {
  90. case style::CONTEXT_BUTTON_MD:
  91. details.size_delta = ui::kLabelFontSizeDelta;
  92. details.weight = TypographyProvider::MediumWeightForUI();
  93. break;
  94. case style::CONTEXT_DIALOG_TITLE:
  95. details.size_delta = ui::kTitleFontSizeDelta;
  96. break;
  97. case style::CONTEXT_TOUCH_MENU:
  98. details.size_delta = 2;
  99. break;
  100. default:
  101. details.size_delta = ui::kLabelFontSizeDelta;
  102. break;
  103. }
  104. switch (style) {
  105. case style::STYLE_TAB_ACTIVE:
  106. details.weight = gfx::Font::Weight::BOLD;
  107. break;
  108. case style::STYLE_DIALOG_BUTTON_DEFAULT:
  109. // Only non-MD default buttons should "increase" in boldness.
  110. if (context == style::CONTEXT_BUTTON) {
  111. details.weight =
  112. GetValueBolderThan(ui::ResourceBundle::GetSharedInstance()
  113. .GetFontListForDetails(details)
  114. .GetFontWeight());
  115. }
  116. break;
  117. case style::STYLE_EMPHASIZED:
  118. case style::STYLE_EMPHASIZED_SECONDARY:
  119. details.weight = gfx::Font::Weight::SEMIBOLD;
  120. break;
  121. }
  122. return details;
  123. }
  124. const gfx::FontList& TypographyProvider::GetFont(int context, int style) const {
  125. return ui::ResourceBundle::GetSharedInstance().GetFontListForDetails(
  126. GetFontDetails(context, style));
  127. }
  128. SkColor TypographyProvider::GetColor(const View& view,
  129. int context,
  130. int style) const {
  131. return view.GetColorProvider()->GetColor(GetColorId(context, style));
  132. }
  133. int TypographyProvider::GetLineHeight(int context, int style) const {
  134. return GetFont(context, style).GetHeight();
  135. }
  136. bool TypographyProvider::StyleAllowedForContext(int context, int style) const {
  137. // TODO(https://crbug.com/1352340): Limit emphasizing text to contexts where
  138. // it's obviously correct. chrome_typography_provider.cc implements this
  139. // correctly, but that does not cover uses outside of //chrome or //ash.
  140. return true;
  141. }
  142. // static
  143. gfx::Font::Weight TypographyProvider::MediumWeightForUI() {
  144. #if BUILDFLAG(IS_MAC)
  145. // System fonts are not user-configurable on Mac, so it's simpler.
  146. return gfx::Font::Weight::MEDIUM;
  147. #else
  148. // NORMAL may already have at least MEDIUM weight. Return NORMAL in that case
  149. // since trying to return MEDIUM would actually make the font lighter-weight
  150. // than the surrounding text. For example, Windows can be configured to use a
  151. // BOLD font for dialog text; deriving MEDIUM from that would replace the BOLD
  152. // attribute with something lighter.
  153. if (ui::ResourceBundle::GetSharedInstance()
  154. .GetFontListForDetails(ui::ResourceBundle::FontDetails())
  155. .GetFontWeight() < gfx::Font::Weight::MEDIUM)
  156. return gfx::Font::Weight::MEDIUM;
  157. return gfx::Font::Weight::NORMAL;
  158. #endif
  159. }
  160. } // namespace views