platform_font_mac_unittest.mm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/platform_font_mac.h"
  5. #include <Cocoa/Cocoa.h>
  6. #include <stddef.h>
  7. #import "base/mac/foundation_util.h"
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/gfx/font.h"
  11. namespace gfx {
  12. using Weight = Font::Weight;
  13. TEST(PlatformFontMacTest, DeriveFont) {
  14. // |weight_tri| is either -1, 0, or 1 meaning "light", "normal", or "bold".
  15. auto CheckExpected = [](const Font& font, int weight_tri, bool isItalic) {
  16. base::ScopedCFTypeRef<CFDictionaryRef> traits(
  17. CTFontCopyTraits(base::mac::NSToCFCast(font.GetNativeFont())));
  18. DCHECK(traits);
  19. CFNumberRef cf_slant = base::mac::GetValueFromDictionary<CFNumberRef>(
  20. traits, kCTFontSlantTrait);
  21. CGFloat slant;
  22. CFNumberGetValue(cf_slant, kCFNumberCGFloatType, &slant);
  23. if (isItalic)
  24. EXPECT_GT(slant, 0);
  25. else
  26. EXPECT_EQ(slant, 0);
  27. CFNumberRef cf_weight = base::mac::GetValueFromDictionary<CFNumberRef>(
  28. traits, kCTFontWeightTrait);
  29. CGFloat weight;
  30. CFNumberGetValue(cf_weight, kCFNumberCGFloatType, &weight);
  31. if (weight_tri < 0)
  32. EXPECT_LT(weight, 0);
  33. else if (weight_tri == 0)
  34. EXPECT_EQ(weight, 0);
  35. else
  36. EXPECT_GT(weight, 0);
  37. };
  38. // Use a base font that support all traits.
  39. Font base_font("Helvetica", 13);
  40. {
  41. SCOPED_TRACE("plain font");
  42. CheckExpected(base_font, 0, false);
  43. }
  44. // Italic
  45. Font italic_font(base_font.Derive(0, Font::ITALIC, Weight::NORMAL));
  46. {
  47. SCOPED_TRACE("italic font");
  48. CheckExpected(italic_font, 0, true);
  49. }
  50. // Bold
  51. Font bold_font(base_font.Derive(0, Font::NORMAL, Weight::BOLD));
  52. {
  53. SCOPED_TRACE("bold font");
  54. CheckExpected(bold_font, 1, false);
  55. }
  56. // Bold italic
  57. Font bold_italic_font(base_font.Derive(0, Font::ITALIC, Weight::BOLD));
  58. {
  59. SCOPED_TRACE("bold italic font");
  60. CheckExpected(bold_italic_font, 1, true);
  61. }
  62. // Non-existent thin will return the closest weight, light
  63. Font thin_font(base_font.Derive(0, Font::NORMAL, Weight::THIN));
  64. {
  65. SCOPED_TRACE("thin font");
  66. CheckExpected(thin_font, -1, false);
  67. }
  68. // Non-existent black will return the closest weight, bold
  69. Font black_font(base_font.Derive(0, Font::NORMAL, Weight::BLACK));
  70. {
  71. SCOPED_TRACE("black font");
  72. CheckExpected(black_font, 1, false);
  73. }
  74. }
  75. TEST(PlatformFontMacTest, DeriveFontUnderline) {
  76. // Create a default font.
  77. Font base_font;
  78. // Make the font underlined.
  79. Font derived_font(base_font.Derive(0, base_font.GetStyle() | Font::UNDERLINE,
  80. base_font.GetWeight()));
  81. // Validate the derived font properties against its native font instance.
  82. NSFontTraitMask traits = [[NSFontManager sharedFontManager]
  83. traitsOfFont:derived_font.GetNativeFont()];
  84. Weight actual_weight =
  85. (traits & NSFontBoldTrait) ? Weight::BOLD : Weight::NORMAL;
  86. int actual_style = Font::UNDERLINE;
  87. if (traits & NSFontItalicTrait)
  88. actual_style |= Font::ITALIC;
  89. EXPECT_TRUE(derived_font.GetStyle() & Font::UNDERLINE);
  90. EXPECT_EQ(derived_font.GetStyle(), actual_style);
  91. EXPECT_EQ(derived_font.GetWeight(), actual_weight);
  92. }
  93. // Tests internal methods for extracting Font properties from the
  94. // underlying CTFont representation.
  95. TEST(PlatformFontMacTest, ConstructFromNativeFont) {
  96. Font light_font([NSFont fontWithName:@"Helvetica-Light" size:12]);
  97. EXPECT_EQ(12, light_font.GetFontSize());
  98. EXPECT_EQ("Helvetica", light_font.GetFontName());
  99. EXPECT_EQ(Font::NORMAL, light_font.GetStyle());
  100. EXPECT_EQ(Weight::LIGHT, light_font.GetWeight());
  101. Font light_italic_font([NSFont fontWithName:@"Helvetica-LightOblique"
  102. size:14]);
  103. EXPECT_EQ(14, light_italic_font.GetFontSize());
  104. EXPECT_EQ("Helvetica", light_italic_font.GetFontName());
  105. EXPECT_EQ(Font::ITALIC, light_italic_font.GetStyle());
  106. EXPECT_EQ(Weight::LIGHT, light_italic_font.GetWeight());
  107. Font normal_font([NSFont fontWithName:@"Helvetica" size:12]);
  108. EXPECT_EQ(12, normal_font.GetFontSize());
  109. EXPECT_EQ("Helvetica", normal_font.GetFontName());
  110. EXPECT_EQ(Font::NORMAL, normal_font.GetStyle());
  111. EXPECT_EQ(Weight::NORMAL, normal_font.GetWeight());
  112. Font italic_font([NSFont fontWithName:@"Helvetica-Oblique" size:14]);
  113. EXPECT_EQ(14, italic_font.GetFontSize());
  114. EXPECT_EQ("Helvetica", italic_font.GetFontName());
  115. EXPECT_EQ(Font::ITALIC, italic_font.GetStyle());
  116. EXPECT_EQ(Weight::NORMAL, italic_font.GetWeight());
  117. Font bold_font([NSFont fontWithName:@"Helvetica-Bold" size:12]);
  118. EXPECT_EQ(12, bold_font.GetFontSize());
  119. EXPECT_EQ("Helvetica", bold_font.GetFontName());
  120. EXPECT_EQ(Font::NORMAL, bold_font.GetStyle());
  121. EXPECT_EQ(Weight::BOLD, bold_font.GetWeight());
  122. Font bold_italic_font([NSFont fontWithName:@"Helvetica-BoldOblique" size:14]);
  123. EXPECT_EQ(14, bold_italic_font.GetFontSize());
  124. EXPECT_EQ("Helvetica", bold_italic_font.GetFontName());
  125. EXPECT_EQ(Font::ITALIC, bold_italic_font.GetStyle());
  126. EXPECT_EQ(Weight::BOLD, bold_italic_font.GetWeight());
  127. }
  128. // Test font derivation for fine-grained font weights.
  129. TEST(PlatformFontMacTest, DerivedFineGrainedFonts) {
  130. // The resulting, actual font weight after deriving |weight| from |base|.
  131. auto DerivedIntWeight = [](Weight weight) {
  132. Font base; // The default system font.
  133. Font derived(base.Derive(0, 0, weight));
  134. // PlatformFont should always pass the requested weight, not what the OS
  135. // could provide. This just checks a constructor argument, so not very
  136. // interesting.
  137. EXPECT_EQ(static_cast<int>(weight), static_cast<int>(derived.GetWeight()));
  138. return static_cast<int>(PlatformFontMac::GetFontWeightFromNSFontForTesting(
  139. derived.GetNativeFont()));
  140. };
  141. EXPECT_EQ(static_cast<int>(Weight::THIN), DerivedIntWeight(Weight::THIN));
  142. EXPECT_EQ(static_cast<int>(Weight::EXTRA_LIGHT),
  143. DerivedIntWeight(Weight::EXTRA_LIGHT));
  144. EXPECT_EQ(static_cast<int>(Weight::LIGHT), DerivedIntWeight(Weight::LIGHT));
  145. EXPECT_EQ(static_cast<int>(Weight::NORMAL), DerivedIntWeight(Weight::NORMAL));
  146. EXPECT_EQ(static_cast<int>(Weight::MEDIUM), DerivedIntWeight(Weight::MEDIUM));
  147. EXPECT_EQ(static_cast<int>(Weight::SEMIBOLD),
  148. DerivedIntWeight(Weight::SEMIBOLD));
  149. EXPECT_EQ(static_cast<int>(Weight::BOLD), DerivedIntWeight(Weight::BOLD));
  150. EXPECT_EQ(static_cast<int>(Weight::EXTRA_BOLD),
  151. DerivedIntWeight(Weight::EXTRA_BOLD));
  152. EXPECT_EQ(static_cast<int>(Weight::BLACK), DerivedIntWeight(Weight::BLACK));
  153. }
  154. // Ensures that the Font's reported height is consistent with the native font's
  155. // ascender and descender metrics.
  156. TEST(PlatformFontMacTest, ValidateFontHeight) {
  157. // Use the default ResourceBundle system font (i.e. San Francisco).
  158. Font default_font;
  159. Font::FontStyle styles[] = {Font::NORMAL, Font::ITALIC, Font::UNDERLINE};
  160. for (auto& style : styles) {
  161. SCOPED_TRACE(testing::Message() << "Font::FontStyle: " << style);
  162. // Include the range of sizes used by ResourceBundle::FontStyle (-1 to +8).
  163. for (int delta = -1; delta <= 8; ++delta) {
  164. Font font = default_font.Derive(delta, style, Weight::NORMAL);
  165. SCOPED_TRACE(testing::Message() << "FontSize(): " << font.GetFontSize());
  166. NSFont* native_font = font.GetNativeFont();
  167. // Font height (an integer) should be the sum of these.
  168. CGFloat ascender = [native_font ascender];
  169. CGFloat descender = [native_font descender];
  170. CGFloat leading = [native_font leading];
  171. // NSFont always gives a negative value for descender. Others positive.
  172. EXPECT_GE(0, descender);
  173. EXPECT_LE(0, ascender);
  174. EXPECT_LE(0, leading);
  175. int sum = ceil(ascender - descender + leading);
  176. // Text layout is performed using an integral baseline offset derived from
  177. // the ascender. The height needs to be enough to fit the full descender
  178. // (plus baseline). So the height depends on the rounding of the ascender,
  179. // and can be as much as 1 greater than the simple sum of floats.
  180. EXPECT_LE(sum, font.GetHeight());
  181. EXPECT_GE(sum + 1, font.GetHeight());
  182. // Recreate the rounding performed for GetBaseLine().
  183. EXPECT_EQ(ceil(ceil(ascender) - descender + leading), font.GetHeight());
  184. }
  185. }
  186. }
  187. // Test to ensure we cater for the AppKit quirk that can make the font italic
  188. // when asking for a fine-grained weight. See http://crbug.com/742261. Note that
  189. // Appkit's bug was detected on macOS 10.10 which uses Helvetica Neue as the
  190. // system font.
  191. TEST(PlatformFontMacTest, DerivedSemiboldFontIsNotItalic) {
  192. Font base_font;
  193. {
  194. NSFontTraitMask traits = [[NSFontManager sharedFontManager]
  195. traitsOfFont:base_font.GetNativeFont()];
  196. ASSERT_FALSE(traits & NSItalicFontMask);
  197. }
  198. Font semibold_font(base_font.Derive(0, Font::NORMAL, Weight::SEMIBOLD));
  199. NSFontTraitMask traits = [[NSFontManager sharedFontManager]
  200. traitsOfFont:semibold_font.GetNativeFont()];
  201. EXPECT_FALSE(traits & NSItalicFontMask);
  202. }
  203. } // namespace gfx