Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Unified Diff: ui/gfx/decorated_text_mac.mm

Issue 2164483006: [MacViews] Implemented text context menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/decorated_text_mac.h ('k') | ui/gfx/render_text.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/decorated_text_mac.mm
diff --git a/ui/gfx/decorated_text_mac.mm b/ui/gfx/decorated_text_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..3a6ae10524fbda9932f41ec01e8105c27b729cf8
--- /dev/null
+++ b/ui/gfx/decorated_text_mac.mm
@@ -0,0 +1,51 @@
+// Copyright 2017 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.
+
+#import "ui/gfx/decorated_text_mac.h"
+
+#import <Cocoa/Cocoa.h>
+
+#import "base/mac/scoped_nsobject.h"
+#include "base/strings/sys_string_conversions.h"
+#include "ui/gfx/decorated_text.h"
+
+namespace gfx {
+
+NSAttributedString* GetAttributedStringFromDecoratedText(
+ const DecoratedText& decorated_text) {
+ base::scoped_nsobject<NSMutableAttributedString> str(
+ [[NSMutableAttributedString alloc]
+ initWithString:base::SysUTF16ToNSString(decorated_text.text)]);
+ [str beginEditing];
+
+ NSValue* const line_style =
+ @(NSUnderlineStyleSingle | NSUnderlinePatternSolid);
+
+ for (const auto& attribute : decorated_text.attributes) {
+ DCHECK(!attribute.range.is_reversed());
+ DCHECK_LE(attribute.range.end(), [str length]);
+
+ NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
+ NSRange range = attribute.range.ToNSRange();
+
+ if (attribute.font.GetNativeFont())
+ attrs[NSFontAttributeName] = attribute.font.GetNativeFont();
+
+ // NSFont does not have underline as an attribute. Hence handle it
+ // separately.
+ const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE;
+ if (underline)
+ attrs[NSUnderlineStyleAttributeName] = line_style;
+
+ if (attribute.strike)
+ attrs[NSStrikethroughStyleAttributeName] = line_style;
+
+ [str setAttributes:attrs range:range];
+ }
+
+ [str endEditing];
+ return str.autorelease();
+}
+
+} // namespace gfx
« no previous file with comments | « ui/gfx/decorated_text_mac.h ('k') | ui/gfx/render_text.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698