Commit c2e39ede authored by Avi Drissman's avatar Avi Drissman Committed by Commit Bot

Do clean up in PlatformFontMac

Do not try to derive one weight of font from another.
Use the API to directly generate them.

PlatformFontMacTest.FontWeightAPIConsistency tested that
our understanding of shifting weights up and down was
correct. The code no longer shifts weights up and down
as that is unreliable and version-specific, so there is
no need to test weight shifting and it is removed.

This also fixes Big Sur issues.

Bug: 1101426
Change-Id: I2165c3550ad295b3ee7742f3289874ed2bb89c68
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2340262
Commit-Queue: Avi Drissman <avi@chromium.org>
Auto-Submit: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796044}
parent 8c3ca5eb
...@@ -8,18 +8,39 @@ ...@@ -8,18 +8,39 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h"
#include "ui/gfx/font_render_params.h" #include "ui/gfx/font_render_params.h"
#include "ui/gfx/platform_font.h" #include "ui/gfx/platform_font.h"
namespace gfx { namespace gfx {
class PlatformFontMac : public PlatformFont { class GFX_EXPORT PlatformFontMac : public PlatformFont {
public: public:
PlatformFontMac(); // An enum indicating a type of system-specified font.
// - kGeneral: +[NSFont systemFontOfSize:(weight:)]
// - kMenu: +[NSFont menuFontOfSize:]
// - kToolTip: +[NSFont toolTipsFontOfSize:]
enum class SystemFontType { kGeneral, kMenu, kToolTip };
// Constructs a PlatformFontMac for a system-specified font of
// |system_font_type| type. For a non-system-specified font, use any other
// constructor.
explicit PlatformFontMac(SystemFontType system_font_type);
// Constructs a PlatformFontMac for containing the NSFont* |native_font|. Do
// not call this for a system-specified font; use the |SystemFontType|
// constructor for that. |native_font| must not be null.
explicit PlatformFontMac(NativeFont native_font); explicit PlatformFontMac(NativeFont native_font);
// Constructs a PlatformFontMac representing the font with name |font_name|
// and the size |font_size|. Do not call this for a system-specified font; use
// the |SystemFontType| constructor for that.
PlatformFontMac(const std::string& font_name, PlatformFontMac(const std::string& font_name,
int font_size); int font_size);
// Constructs a PlatformFontMac representing the font specified by |typeface|
// and the size |font_size_pixels|. Do not call this for a system-specified
// font; use the |SystemFontType| constructor for that.
PlatformFontMac(sk_sp<SkTypeface> typeface, PlatformFontMac(sk_sp<SkTypeface> typeface,
int font_size_pixels, int font_size_pixels,
const base::Optional<FontRenderParams>& params); const base::Optional<FontRenderParams>& params);
...@@ -41,35 +62,44 @@ class PlatformFontMac : public PlatformFont { ...@@ -41,35 +62,44 @@ class PlatformFontMac : public PlatformFont {
NativeFont GetNativeFont() const override; NativeFont GetNativeFont() const override;
sk_sp<SkTypeface> GetNativeSkTypeface() const override; sk_sp<SkTypeface> GetNativeSkTypeface() const override;
// A utility function to get the weight of an NSFont. Used by the unit test.
static Font::Weight GetFontWeightFromNSFontForTesting(NSFont* font);
private: private:
PlatformFontMac(const std::string& font_name, struct FontSpec {
int font_size, std::string name; // Corresponds to -[NSFont fontFamily].
int font_style, int size;
Font::Weight font_weight); int style;
Font::Weight weight;
};
PlatformFontMac(NativeFont font, PlatformFontMac(NativeFont font,
const std::string& font_name, base::Optional<SystemFontType> system_font_type);
int font_size,
int font_style, PlatformFontMac(NativeFont font,
Font::Weight font_weight); base::Optional<SystemFontType> system_font_type,
FontSpec spec);
~PlatformFontMac() override; ~PlatformFontMac() override;
// Calculates and caches the font metrics and inits |render_params_|. // Calculates and caches the font metrics and initializes |render_params_|.
void CalculateMetricsAndInitRenderParams(); void CalculateMetricsAndInitRenderParams();
// Returns an autoreleased NSFont created with the passed-in specifications.
NSFont* NSFontWithSpec(FontSpec font_spec) const;
// The NSFont instance for this object. If this object was constructed from an // The NSFont instance for this object. If this object was constructed from an
// NSFont instance, this holds that NSFont instance. Otherwise this NSFont // NSFont instance, this holds that NSFont instance. Otherwise this NSFont
// instance is constructed from the name, size, and style. If there is no // instance is constructed from the name, size, and style. If there is no
// active font that matched those criteria a default font is used. // active font that matched those criteria a default font is used.
base::scoped_nsobject<NSFont> native_font_; base::scoped_nsobject<NSFont> native_font_;
// The name/size/style trio that specify the font. Initialized in the // If the font is a system font, and if so, what kind.
// constructors. const base::Optional<SystemFontType> system_font_type_;
const std::string font_name_; // Corresponds to -[NSFont fontFamily].
const int font_size_; // The name/size/style/weight quartet that specify the font. Initialized in
const int font_style_; // the constructors.
const Font::Weight font_weight_; const FontSpec font_spec_;
// Cached metrics, generated in CalculateMetrics(). // Cached metrics, generated in CalculateMetrics().
int height_; int height_;
......
This diff is collapsed.
This diff is collapsed.
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "ui/base/cocoa/cocoa_base_utils.h" #include "ui/base/cocoa/cocoa_base_utils.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#import "ui/gfx/mac/coordinate_conversion.h" #import "ui/gfx/mac/coordinate_conversion.h"
#include "ui/gfx/platform_font_mac.h"
namespace { namespace {
...@@ -32,8 +33,10 @@ int TooltipManagerMac::GetMaxWidth(const gfx::Point& location) const { ...@@ -32,8 +33,10 @@ int TooltipManagerMac::GetMaxWidth(const gfx::Point& location) const {
} }
const gfx::FontList& TooltipManagerMac::GetFontList() const { const gfx::FontList& TooltipManagerMac::GetFontList() const {
static base::NoDestructor<gfx::FontList> font_list( static base::NoDestructor<gfx::FontList> font_list([]() {
[]() { return gfx::Font([NSFont toolTipsFontOfSize:0]); }()); return gfx::Font(new gfx::PlatformFontMac(
gfx::PlatformFontMac::SystemFontType::kToolTip));
}());
return *font_list; return *font_list;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#import <AppKit/AppKit.h> #import <AppKit/AppKit.h>
#include "base/mac/mac_util.h" #include "base/mac/mac_util.h"
#include "ui/gfx/platform_font_mac.h"
namespace { namespace {
...@@ -42,7 +43,8 @@ void InitMaterialMenuConfig(views::MenuConfig* config) { ...@@ -42,7 +43,8 @@ void InitMaterialMenuConfig(views::MenuConfig* config) {
namespace views { namespace views {
void MenuConfig::Init() { void MenuConfig::Init() {
font_list = gfx::FontList(gfx::Font([NSFont menuFontOfSize:0.0])); font_list = gfx::FontList(gfx::Font(
new gfx::PlatformFontMac(gfx::PlatformFontMac::SystemFontType::kMenu)));
check_selected_combobox_item = true; check_selected_combobox_item = true;
arrow_key_selection_wraps = false; arrow_key_selection_wraps = false;
use_mnemonics = false; use_mnemonics = false;
......
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