Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Unified Diff: ui/gfx/platform_font_mac.mm

Issue 2222483002: Mac: Fix PlatformFontMac::DeriveFont. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@font_mac
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/platform_font_mac.h ('k') | ui/gfx/platform_font_mac_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/platform_font_mac.mm
diff --git a/ui/gfx/platform_font_mac.mm b/ui/gfx/platform_font_mac.mm
index 1df9e6922196b6df791b3a67db9e676490783a2e..53781f8e2309e74b7c72fb319b85c430132c83aa 100644
--- a/ui/gfx/platform_font_mac.mm
+++ b/ui/gfx/platform_font_mac.mm
@@ -19,6 +19,22 @@
namespace {
+// Returns the font style for |font|. Disregards Font::UNDERLINE, since NSFont
+// does not support it as a trait.
+int GetFontStyleFromNSFont(NSFont* font) {
+ int font_style = Font::NORMAL;
+ NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits];
+ if (traits & NSFontItalicTrait)
+ font_style |= Font::ITALIC;
+ return font_style;
+}
+
+// Returns the Font weight for |font|.
+Font::Weight GetFontWeightFromNSFont(NSFont* font) {
+ NSFontSymbolicTraits traits = [[font fontDescriptor] symbolicTraits];
+ return (traits & NSFontBoldTrait) ? Font::Weight::BOLD : Font::Weight::NORMAL;
+}
+
// Returns an autoreleased NSFont created with the passed-in specifications.
NSFont* NSFontWithSpec(const std::string& font_name,
int font_size,
@@ -64,31 +80,17 @@
}
PlatformFontMac::PlatformFontMac(NativeFont native_font)
- : native_font_([native_font retain]),
- font_name_(base::SysNSStringToUTF8([native_font_ familyName])),
- font_size_([native_font_ pointSize]),
- font_style_(Font::NORMAL),
- font_weight_(Font::Weight::NORMAL) {
- NSFontSymbolicTraits traits = [[native_font fontDescriptor] symbolicTraits];
- if (traits & NSFontItalicTrait)
- font_style_ |= Font::ITALIC;
- if (traits & NSFontBoldTrait)
- font_weight_ = Font::Weight::BOLD;
-
- CalculateMetricsAndInitRenderParams();
-}
+ : PlatformFontMac(native_font,
+ base::SysNSStringToUTF8([native_font familyName]),
+ [native_font pointSize],
+ GetFontStyleFromNSFont(native_font),
+ GetFontWeightFromNSFont(native_font)) {}
PlatformFontMac::PlatformFontMac(const std::string& font_name, int font_size)
- : native_font_([NSFontWithSpec(font_name,
- font_size,
- Font::NORMAL,
- Font::Weight::NORMAL) retain]),
- font_name_(font_name),
- font_size_(font_size),
- font_style_(Font::NORMAL),
- font_weight_(Font::Weight::NORMAL) {
- CalculateMetricsAndInitRenderParams();
-}
+ : PlatformFontMac(font_name,
+ font_size,
+ Font::NORMAL,
+ Font::Weight::NORMAL) {}
////////////////////////////////////////////////////////////////////////////////
// PlatformFontMac, PlatformFont implementation:
@@ -96,16 +98,26 @@
Font PlatformFontMac::DeriveFont(int size_delta,
int style,
Font::Weight weight) const {
- if (native_font_ && style == font_style_ && weight == font_weight_) {
- // System fonts have special attributes starting with 10.11. They should be
- // requested using the same descriptor to preserve these attributes.
- return Font(new PlatformFontMac([NSFont
- fontWithDescriptor:[native_font_ fontDescriptor]
- size:font_size_ + size_delta]));
- }
+ // For some reason, creating fonts using the NSFontDescriptor API's seem to be
+ // unreliable. Hence use the NSFontManager.
+ NSFont* derived_font = native_font_;
+ NSFontManager* font_manager = [NSFontManager sharedFontManager];
+
+ NSFontTraitMask bold_trait_mask =
+ weight >= Font::Weight::BOLD ? NSBoldFontMask : NSUnboldFontMask;
+ derived_font =
+ [font_manager convertFont:derived_font toHaveTrait:bold_trait_mask];
- return Font(
- new PlatformFontMac(font_name_, font_size_ + size_delta, style, weight));
+ NSFontTraitMask italic_trait_mask =
+ (style & Font::ITALIC) ? NSItalicFontMask : NSUnitalicFontMask;
+ derived_font =
+ [font_manager convertFont:derived_font toHaveTrait:italic_trait_mask];
+
+ derived_font =
+ [font_manager convertFont:derived_font toSize:font_size_ + size_delta];
+
+ return Font(new PlatformFontMac(derived_font, font_name_,
+ font_size_ + size_delta, style, weight));
}
int PlatformFontMac::GetHeight() {
@@ -159,9 +171,19 @@
int font_size,
int font_style,
Font::Weight font_weight)
- : native_font_(
- [NSFontWithSpec(font_name, font_size, font_style, font_weight)
- retain]),
+ : PlatformFontMac(
+ NSFontWithSpec(font_name, font_size, font_style, font_weight),
+ font_name,
+ font_size,
+ font_style,
+ font_weight) {}
+
+PlatformFontMac::PlatformFontMac(NativeFont font,
+ const std::string& font_name,
+ int font_size,
+ int font_style,
+ Font::Weight font_weight)
+ : native_font_([font retain]),
font_name_(font_name),
font_size_(font_size),
font_style_(font_style),
« no previous file with comments | « ui/gfx/platform_font_mac.h ('k') | ui/gfx/platform_font_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698