font.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  3. // found in the LICENSE file.
  4. #include "ui/gfx/font.h"
  5. #include <algorithm>
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "build/build_config.h"
  8. #include "ui/gfx/platform_font.h"
  9. #ifndef NDEBUG
  10. #include <ostream>
  11. #endif
  12. namespace gfx {
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // Font, public:
  15. Font::Font() : platform_font_(PlatformFont::CreateDefault()) {
  16. }
  17. Font::Font(const Font& other) : platform_font_(other.platform_font_) {
  18. }
  19. Font& Font::operator=(const Font& other) {
  20. platform_font_ = other.platform_font_;
  21. return *this;
  22. }
  23. #if BUILDFLAG(IS_APPLE)
  24. Font::Font(NativeFont native_font)
  25. : platform_font_(PlatformFont::CreateFromNativeFont(native_font)) {
  26. }
  27. #endif
  28. Font::Font(PlatformFont* platform_font) : platform_font_(platform_font) {
  29. }
  30. Font::Font(const std::string& font_name, int font_size)
  31. : platform_font_(PlatformFont::CreateFromNameAndSize(font_name,
  32. font_size)) {
  33. }
  34. Font::~Font() {
  35. }
  36. Font Font::Derive(int size_delta, int style, Font::Weight weight) const {
  37. if (size_delta == 0 && style == GetStyle() && weight == GetWeight())
  38. return *this;
  39. return platform_font_->DeriveFont(size_delta, style, weight);
  40. }
  41. int Font::GetHeight() const {
  42. return platform_font_->GetHeight();
  43. }
  44. int Font::GetBaseline() const {
  45. return platform_font_->GetBaseline();
  46. }
  47. int Font::GetCapHeight() const {
  48. return platform_font_->GetCapHeight();
  49. }
  50. int Font::GetExpectedTextWidth(int length) const {
  51. return platform_font_->GetExpectedTextWidth(length);
  52. }
  53. int Font::GetStyle() const {
  54. return platform_font_->GetStyle();
  55. }
  56. const std::string& Font::GetFontName() const {
  57. return platform_font_->GetFontName();
  58. }
  59. std::string Font::GetActualFontName() const {
  60. return platform_font_->GetActualFontName();
  61. }
  62. int Font::GetFontSize() const {
  63. return platform_font_->GetFontSize();
  64. }
  65. Font::Weight Font::GetWeight() const {
  66. return platform_font_->GetWeight();
  67. }
  68. const FontRenderParams& Font::GetFontRenderParams() const {
  69. return platform_font_->GetFontRenderParams();
  70. }
  71. #if BUILDFLAG(IS_APPLE)
  72. NativeFont Font::GetNativeFont() const {
  73. return platform_font_->GetNativeFont();
  74. }
  75. #endif
  76. #ifndef NDEBUG
  77. std::ostream& operator<<(std::ostream& stream, const Font::Weight weight) {
  78. return stream << static_cast<int>(weight);
  79. }
  80. #endif
  81. Font::Weight FontWeightFromInt(int weight) {
  82. static const Font::Weight weights[] = {
  83. Font::Weight::INVALID, Font::Weight::THIN, Font::Weight::EXTRA_LIGHT,
  84. Font::Weight::LIGHT, Font::Weight::NORMAL, Font::Weight::MEDIUM,
  85. Font::Weight::SEMIBOLD, Font::Weight::BOLD, Font::Weight::EXTRA_BOLD,
  86. Font::Weight::BLACK};
  87. const Font::Weight* next_bigger_weight = std::lower_bound(
  88. std::begin(weights), std::end(weights), weight,
  89. [](const Font::Weight& a, const int& b) {
  90. return static_cast<std::underlying_type<Font::Weight>::type>(a) < b;
  91. });
  92. if (next_bigger_weight != std::end(weights))
  93. return *next_bigger_weight;
  94. return Font::Weight::INVALID;
  95. }
  96. } // namespace gfx