summaryrefslogtreecommitdiffstats
path: root/app/gfx/font_mac.mm
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-15 07:37:29 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-15 07:37:29 +0000
commit82522511b7921e8c61742ebd80a4c674c56e7e48 (patch)
tree381d71de2b9c7b99675e6f2b4523ae0876c72ce8 /app/gfx/font_mac.mm
parent631cf822bd6e64cf469544b42c9975f5602c29a6 (diff)
downloadchromium_src-82522511b7921e8c61742ebd80a4c674c56e7e48.zip
chromium_src-82522511b7921e8c61742ebd80a4c674c56e7e48.tar.gz
chromium_src-82522511b7921e8c61742ebd80a4c674c56e7e48.tar.bz2
ChromeCanvas->gfx::Canvas
Rename files too. TBR=brettw http://crbug.com/11387 Review URL: http://codereview.chromium.org/113443 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16148 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/gfx/font_mac.mm')
-rw-r--r--app/gfx/font_mac.mm90
1 files changed, 90 insertions, 0 deletions
diff --git a/app/gfx/font_mac.mm b/app/gfx/font_mac.mm
new file mode 100644
index 0000000..b39ebea
--- /dev/null
+++ b/app/gfx/font_mac.mm
@@ -0,0 +1,90 @@
+// 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 "app/gfx/font.h"
+
+#include <Cocoa/Cocoa.h>
+
+#include "base/logging.h"
+#include "base/sys_string_conversions.h"
+
+namespace gfx {
+
+// static
+Font Font::CreateFont(const std::wstring& font_name, int font_size) {
+ return Font(font_name, font_size, NORMAL);
+}
+
+Font::Font(const std::wstring& font_name, int font_size, int style)
+ : font_name_(font_name),
+ font_size_(font_size),
+ style_(style) {
+ calculateMetrics();
+}
+
+Font::Font()
+ : font_size_([NSFont systemFontSize]),
+ style_(NORMAL) {
+ NSFont* system_font = [NSFont systemFontOfSize:font_size_];
+ font_name_ = base::SysNSStringToWide([system_font fontName]);
+ calculateMetrics();
+}
+
+void Font::calculateMetrics() {
+ NSFont* font = nativeFont();
+ height_ = [font xHeight];
+ ascent_ = [font ascender];
+ avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width;
+}
+
+Font Font::DeriveFont(int size_delta, int style) const {
+ return Font(font_name_, font_size_ + size_delta, style);
+}
+
+int Font::height() const {
+ return height_;
+}
+
+int Font::baseline() const {
+ return ascent_;
+}
+
+int Font::ave_char_width() const {
+ return avg_width_;
+}
+
+int Font::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 Font::GetExpectedTextWidth(int length) const {
+ return length * avg_width_;
+}
+
+int Font::style() const {
+ return style_;
+}
+
+std::wstring Font::FontName() {
+ return font_name_;
+}
+
+int Font::FontSize() {
+ return font_size_;
+}
+
+NativeFont Font::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_];
+}
+
+} // namespace gfx