diff options
author | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-25 20:33:23 +0000 |
---|---|---|
committer | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-25 20:33:23 +0000 |
commit | c98fe6fa60b3262364158809ca0d462f1009709f (patch) | |
tree | ced35f47a361eb85b6db911c066be70e9c77a2b1 /chrome/common/gfx | |
parent | 309c8ff514d506364567d9e6095a295b8d5c3d3e (diff) | |
download | chromium_src-c98fe6fa60b3262364158809ca0d462f1009709f.zip chromium_src-c98fe6fa60b3262364158809ca0d462f1009709f.tar.gz chromium_src-c98fe6fa60b3262364158809ca0d462f1009709f.tar.bz2 |
Enable the ToolbarModel on Mac and Linux. Implement ChromeFont on Mac.
Review URL: http://codereview.chromium.org/28124
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10368 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/gfx')
-rw-r--r-- | chrome/common/gfx/chrome_font.h | 21 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_font_mac.mm | 87 |
2 files changed, 107 insertions, 1 deletions
diff --git a/chrome/common/gfx/chrome_font.h b/chrome/common/gfx/chrome_font.h index f4b2a9a..8ac225c 100644 --- a/chrome/common/gfx/chrome_font.h +++ b/chrome/common/gfx/chrome_font.h @@ -20,7 +20,12 @@ class SkTypeface; #if defined(OS_WIN) typedef struct HFONT__* NativeFont; #elif defined(OS_MACOSX) -typedef void* NativeFont; // TODO(port): set the correct type here +#ifdef __OBJC__ +@class NSFont; +#else +class NSFont; +#endif +typedef NSFont* NativeFont; #elif defined(OS_LINUX) class SkTypeface; typedef SkTypeface* NativeFont; @@ -208,6 +213,20 @@ class ChromeFont { int height_; int ascent_; int avg_width_; +#elif defined(OS_MACOSX) + explicit ChromeFont(const std::wstring& font_name, int font_size, int style); + + // Calculate and cache the font metrics. + void calculateMetrics(); + + std::wstring font_name_; + int font_size_; + int style_; + + // Cached metrics, generated at construction + int height_; + int ascent_; + int avg_width_; #endif }; diff --git a/chrome/common/gfx/chrome_font_mac.mm b/chrome/common/gfx/chrome_font_mac.mm new file mode 100644 index 0000000..a553be6 --- /dev/null +++ b/chrome/common/gfx/chrome_font_mac.mm @@ -0,0 +1,87 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/gfx/chrome_font.h" + +#include <Cocoa/Cocoa.h> + +#include "base/logging.h" +#include "base/sys_string_conversions.h" + +// static +ChromeFont ChromeFont::CreateFont(const std::wstring& font_name, + int font_size) { + return ChromeFont(font_name, font_size, NORMAL); +} + +ChromeFont::ChromeFont(const std::wstring& font_name, int font_size, int style) + : font_name_(font_name), + font_size_(font_size), + style_(style) { + calculateMetrics(); +} + +ChromeFont::ChromeFont() + : font_size_([NSFont systemFontSize]), + style_(NORMAL) { + NSFont* system_font = [NSFont systemFontOfSize:font_size_]; + font_name_ = base::SysNSStringToWide([system_font fontName]); + calculateMetrics(); +} + +void ChromeFont::calculateMetrics() { + NSFont* font = nativeFont(); + height_ = [font xHeight]; + ascent_ = [font ascender]; + avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; +} + +ChromeFont ChromeFont::DeriveFont(int size_delta, int style) const { + return ChromeFont(font_name_, font_size_ + size_delta, style); +} + +int ChromeFont::height() const { + return height_; +} + +int ChromeFont::baseline() const { + return ascent_; +} + +int ChromeFont::ave_char_width() const { + return avg_width_; +} + +int ChromeFont::GetStringWidth(const std::wstring& text) const { + NSFont* font = nativeFont(); + NSString* ns_string = base::SysWideToNSString(text); + NSDictionary* attributes = + [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; + NSSize string_size = [ns_string sizeWithAttributes:attributes]; + return string_size.width; +} + +int ChromeFont::GetExpectedTextWidth(int length) const { + return length * avg_width_; +} + +int ChromeFont::style() const { + return style_; +} + +std::wstring ChromeFont::FontName() { + return font_name_; +} + +int ChromeFont::FontSize() { + return font_size_; +} + +NativeFont ChromeFont::nativeFont() const { + // TODO(pinkerton): apply |style_| to font. + // We could cache this, but then we'd have to conditionally change the + // dtor just for MacOS. Not sure if we want to/need to do that. + return [NSFont fontWithName:base::SysWideToNSString(font_name_) + size:font_size_]; +} |