| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "app/gfx/font.h" | |
| 6 | |
| 7 #include <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/scoped_nsobject.h" | |
| 11 #include "base/sys_string_conversions.h" | |
| 12 | |
| 13 namespace gfx { | |
| 14 | |
| 15 // static | |
| 16 Font Font::CreateFont(const std::wstring& font_name, int font_size) { | |
| 17 return Font(font_name, font_size, NORMAL); | |
| 18 } | |
| 19 | |
| 20 Font::Font(const std::wstring& font_name, int font_size, int style) | |
| 21 : font_name_(font_name), | |
| 22 font_size_(font_size), | |
| 23 style_(style) { | |
| 24 calculateMetrics(); | |
| 25 } | |
| 26 | |
| 27 Font::Font() | |
| 28 : font_size_([NSFont systemFontSize]), | |
| 29 style_(NORMAL) { | |
| 30 NSFont* system_font = [NSFont systemFontOfSize:font_size_]; | |
| 31 font_name_ = base::SysNSStringToWide([system_font fontName]); | |
| 32 calculateMetrics(); | |
| 33 } | |
| 34 | |
| 35 void Font::calculateMetrics() { | |
| 36 NSFont* font = nativeFont(); | |
| 37 scoped_nsobject<NSLayoutManager> layout_manager( | |
| 38 [[NSLayoutManager alloc] init]); | |
| 39 height_ = [layout_manager defaultLineHeightForFont:font]; | |
| 40 ascent_ = [font ascender]; | |
| 41 avg_width_ = [font boundingRectForGlyph:[font glyphWithName:@"x"]].size.width; | |
| 42 } | |
| 43 | |
| 44 Font Font::DeriveFont(int size_delta, int style) const { | |
| 45 return Font(font_name_, font_size_ + size_delta, style); | |
| 46 } | |
| 47 | |
| 48 int Font::height() const { | |
| 49 return height_; | |
| 50 } | |
| 51 | |
| 52 int Font::baseline() const { | |
| 53 return ascent_; | |
| 54 } | |
| 55 | |
| 56 int Font::ave_char_width() const { | |
| 57 return avg_width_; | |
| 58 } | |
| 59 | |
| 60 int Font::GetStringWidth(const std::wstring& text) const { | |
| 61 NSFont* font = nativeFont(); | |
| 62 NSString* ns_string = base::SysWideToNSString(text); | |
| 63 NSDictionary* attributes = | |
| 64 [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; | |
| 65 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | |
| 66 return string_size.width; | |
| 67 } | |
| 68 | |
| 69 int Font::GetExpectedTextWidth(int length) const { | |
| 70 return length * avg_width_; | |
| 71 } | |
| 72 | |
| 73 int Font::style() const { | |
| 74 return style_; | |
| 75 } | |
| 76 | |
| 77 const std::wstring& Font::FontName() const { | |
| 78 return font_name_; | |
| 79 } | |
| 80 | |
| 81 int Font::FontSize() { | |
| 82 return font_size_; | |
| 83 } | |
| 84 | |
| 85 NativeFont Font::nativeFont() const { | |
| 86 // TODO(pinkerton): apply |style_| to font. http://crbug.com/34667 | |
| 87 // We could cache this, but then we'd have to conditionally change the | |
| 88 // dtor just for MacOS. Not sure if we want to/need to do that. | |
| 89 return [NSFont fontWithName:base::SysWideToNSString(font_name_) | |
| 90 size:font_size_]; | |
| 91 } | |
| 92 | |
| 93 } // namespace gfx | |
| OLD | NEW |