Commit 515f516d authored by Mariia Leliuk's avatar Mariia Leliuk Committed by Commit Bot

Fix for Appkit sometimes making the font italic when changing font's weight

When trying to get Semibold font using cocoa's convertWeight it somehow becomes
Italic.
It looks like there is a problem in cocoa. I have tried the following test
(which follows the steps to get Semibold font in platform_font_mac.mm) and it
fails on the last line:

  NSFont* ns_font = [NSFont systemFontOfSize:13];
  NSFontManager* font_manager = [NSFontManager sharedFontManager];

  ns_font = [font_manager convertFont:ns_font toHaveTrait:NSBoldFontMask];
  {
    NSFontTraitMask traits = [font_manager traitsOfFont:ns_font];
    EXPECT_FALSE(traits & NSItalicFontMask);
  }

  ns_font = [font_manager convertWeight:NO ofFont:ns_font];
  {
    NSFontTraitMask traits = [font_manager traitsOfFont:ns_font];
    EXPECT_FALSE(traits & NSItalicFontMask);
  }

So we should better apply NSFontTraitMask to the derived font unconditionally.

BUG=742261

Change-Id: If7d6c93cd95a3afce0909d65a8668f7a111eb429
Reviewed-on: https://chromium-review.googlesource.com/570041Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Reviewed-by: default avatarAlexei Svitkine (slow) <asvitkine@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487416}
parent 3061637c
......@@ -236,7 +236,10 @@ Font PlatformFontMac::DeriveFont(int size_delta,
derived = [font_manager convertWeight:NO ofFont:derived];
}
if (style != font_style_) {
// Always apply the italic trait, even if the italic trait is not changing.
// it's possible for a change in the weight to trigger the font to go italic.
// This is due to an AppKit bug. See http://crbug.com/742261.
if (style != font_style_ || weight != font_weight_) {
NSFontTraitMask italic_trait_mask =
(style & Font::ITALIC) ? NSItalicFontMask : NSUnitalicFontMask;
derived = [font_manager convertFont:derived toHaveTrait:italic_trait_mask];
......
......@@ -330,4 +330,23 @@ TEST(PlatformFontMacTest, ValidateFontHeight) {
}
}
// Test to ensure we cater for the AppKit quirk that can make the font italic
// when asking for a fine-grained weight. See http://crbug.com/742261. Note that
// Appkit's bug was detected on macOS 10.10 which uses Helvetica Neue as the
// system font.
TEST(PlatformFontMacTest, DerivedSemiboldFontIsNotItalic) {
gfx::Font base_font;
{
NSFontTraitMask traits = [[NSFontManager sharedFontManager]
traitsOfFont:base_font.GetNativeFont()];
ASSERT_FALSE(traits & NSItalicFontMask);
}
gfx::Font semibold_font(
base_font.Derive(0, gfx::Font::NORMAL, gfx::Font::Weight::SEMIBOLD));
NSFontTraitMask traits = [[NSFontManager sharedFontManager]
traitsOfFont:semibold_font.GetNativeFont()];
EXPECT_FALSE(traits & NSItalicFontMask);
}
} // namespace gfx
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment