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