OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "ui/gfx/decorated_text_mac.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #import "base/mac/scoped_nsobject.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "ui/gfx/decorated_text.h" |
| 12 |
| 13 namespace gfx { |
| 14 |
| 15 NSAttributedString* GetAttributedStringFromDecoratedText( |
| 16 const DecoratedText& decorated_text) { |
| 17 base::scoped_nsobject<NSMutableAttributedString> str( |
| 18 [[NSMutableAttributedString alloc] |
| 19 initWithString:base::SysUTF16ToNSString(decorated_text.text)]); |
| 20 [str beginEditing]; |
| 21 |
| 22 NSValue* const line_style = |
| 23 @(NSUnderlineStyleSingle | NSUnderlinePatternSolid); |
| 24 |
| 25 for (const auto& attribute : decorated_text.attributes) { |
| 26 DCHECK(!attribute.range.is_reversed()); |
| 27 DCHECK_LE(attribute.range.end(), [str length]); |
| 28 |
| 29 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
| 30 NSRange range = attribute.range.ToNSRange(); |
| 31 |
| 32 if (attribute.font.GetNativeFont()) |
| 33 attrs[NSFontAttributeName] = attribute.font.GetNativeFont(); |
| 34 |
| 35 // NSFont does not have underline as an attribute. Hence handle it |
| 36 // separately. |
| 37 const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE; |
| 38 if (underline) |
| 39 attrs[NSUnderlineStyleAttributeName] = line_style; |
| 40 |
| 41 if (attribute.strike) |
| 42 attrs[NSStrikethroughStyleAttributeName] = line_style; |
| 43 |
| 44 [str setAttributes:attrs range:range]; |
| 45 } |
| 46 |
| 47 [str endEditing]; |
| 48 return str.autorelease(); |
| 49 } |
| 50 |
| 51 } // namespace gfx |
OLD | NEW |