Commit b0e19202 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.

This re-lands c2e39ede.
It turns out that DCHECKs do evaluate everything in them
so wrap it with DCHECK_IS_ON().

Bug: 1101426
Change-Id: I1194b4aa127223456b432d12c47d248264d87c85
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2342387Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796478}
parent aeac4677
......@@ -8,18 +8,39 @@
#include "base/compiler_specific.h"
#include "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "base/optional.h"
#include "ui/gfx/font_render_params.h"
#include "ui/gfx/platform_font.h"
namespace gfx {
class PlatformFontMac : public PlatformFont {
class GFX_EXPORT PlatformFontMac : public PlatformFont {
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);
// 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,
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,
int font_size_pixels,
const base::Optional<FontRenderParams>& params);
......@@ -41,35 +62,44 @@ class PlatformFontMac : public PlatformFont {
NativeFont GetNativeFont() 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:
PlatformFontMac(const std::string& font_name,
int font_size,
int font_style,
Font::Weight font_weight);
struct FontSpec {
std::string name; // Corresponds to -[NSFont fontFamily].
int size;
int style;
Font::Weight weight;
};
PlatformFontMac(NativeFont font,
const std::string& font_name,
int font_size,
int font_style,
Font::Weight font_weight);
base::Optional<SystemFontType> system_font_type);
PlatformFontMac(NativeFont font,
base::Optional<SystemFontType> system_font_type,
FontSpec spec);
~PlatformFontMac() override;
// Calculates and caches the font metrics and inits |render_params_|.
// Calculates and caches the font metrics and initializes |render_params_|.
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
// NSFont instance, this holds that NSFont instance. Otherwise this NSFont
// instance is constructed from the name, size, and style. If there is no
// active font that matched those criteria a default font is used.
base::scoped_nsobject<NSFont> native_font_;
// The name/size/style trio that specify the font. Initialized in the
// constructors.
const std::string font_name_; // Corresponds to -[NSFont fontFamily].
const int font_size_;
const int font_style_;
const Font::Weight font_weight_;
// If the font is a system font, and if so, what kind.
const base::Optional<SystemFontType> system_font_type_;
// The name/size/style/weight quartet that specify the font. Initialized in
// the constructors.
const FontSpec font_spec_;
// Cached metrics, generated in CalculateMetrics().
int height_;
......
This diff is collapsed.
This diff is collapsed.
......@@ -10,6 +10,7 @@
#include "ui/base/cocoa/cocoa_base_utils.h"
#include "ui/gfx/font_list.h"
#import "ui/gfx/mac/coordinate_conversion.h"
#include "ui/gfx/platform_font_mac.h"
namespace {
......@@ -32,8 +33,10 @@ int TooltipManagerMac::GetMaxWidth(const gfx::Point& location) const {
}
const gfx::FontList& TooltipManagerMac::GetFontList() const {
static base::NoDestructor<gfx::FontList> font_list(
[]() { return gfx::Font([NSFont toolTipsFontOfSize:0]); }());
static base::NoDestructor<gfx::FontList> font_list([]() {
return gfx::Font(new gfx::PlatformFontMac(
gfx::PlatformFontMac::SystemFontType::kToolTip));
}());
return *font_list;
}
......
......@@ -7,6 +7,7 @@
#import <AppKit/AppKit.h>
#include "base/mac/mac_util.h"
#include "ui/gfx/platform_font_mac.h"
namespace {
......@@ -42,7 +43,8 @@ void InitMaterialMenuConfig(views::MenuConfig* config) {
namespace views {
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;
arrow_key_selection_wraps = 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