Chromium Code Reviews| Index: ui/gfx/render_text_harfbuzz_mac.mm |
| diff --git a/ui/gfx/render_text_harfbuzz_mac.mm b/ui/gfx/render_text_harfbuzz_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..50f440101f58c45673528cb90d06a8e77cecc78f |
| --- /dev/null |
| +++ b/ui/gfx/render_text_harfbuzz_mac.mm |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2016 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 <Cocoa/Cocoa.h> |
| + |
| +#include "base/mac/foundation_util.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "third_party/skia/include/ports/SkTypeface_mac.h" |
| +#include "ui/gfx/font.h" |
| +#include "ui/gfx/render_text_harfbuzz.h" |
| + |
| +namespace gfx { |
| + |
| +namespace internal { |
| + |
| +sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font, |
| + bool italic, |
| + Font::Weight weight) { |
| + const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL; |
| + Font font_with_style = font.Derive(0, style, weight); |
| + if (!font_with_style.GetNativeFont()) |
| + return nullptr; |
| + |
| + return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont( |
| + base::mac::NSToCFCast(font_with_style.GetNativeFont()))); |
| +} |
| + |
| +} // namespace internal |
| + |
| +bool RenderTextHarfBuzz::GetStyledStringForRange( |
| + const Range& range, |
| + NSMutableAttributedString* str) const { |
|
tapted
2016/09/21 04:06:56
I'm not sure I like the idea of plumbing through a
karandeepb
2016/09/22 08:17:39
Yeah the typedef looked ugly. Encapsulating all th
|
| + if (obscured()) |
| + return false; |
| + |
| + NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
| + |
| + [str beginEditing]; |
| + [[str mutableString] |
| + setString:base::SysUTF16ToNSString(GetTextFromRange(range))]; |
| + |
| + const internal::TextRunList* run_list = GetRunList(); |
| + for (size_t i = 0; i < run_list->size(); i++) { |
| + const internal::TextRunHarfBuzz& run = *run_list->runs()[i]; |
| + |
| + const Range& intersection = range.Intersect(run.range); |
| + DCHECK(!intersection.is_reversed()); |
| + |
| + if (!intersection.is_empty()) { |
| + // Get range relative to |str|. |
| + NSRange strRange = NSMakeRange(intersection.start() - range.GetMin(), |
| + intersection.length()); |
| + |
| + // Font. |
| + if (run.font.GetNativeFont()) |
| + [attrs setObject:run.font.GetNativeFont() forKey:NSFontAttributeName]; |
| + else |
| + [attrs removeObjectForKey:NSFontAttributeName]; |
| + |
| + // Underline. |
| + if (run.underline) { |
| + [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) |
| + forKey:NSUnderlineStyleAttributeName]; |
| + } else { |
| + [attrs removeObjectForKey:NSUnderlineStyleAttributeName]; |
| + } |
| + |
| + // Strikethrough. |
| + if (run.strike) { |
| + [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) |
| + forKey:NSStrikethroughStyleAttributeName]; |
| + } else { |
| + [attrs removeObjectForKey:NSStrikethroughStyleAttributeName]; |
| + } |
| + |
| + // Apply attributes. |
| + [str setAttributes:attrs range:strRange]; |
| + |
| + // Bold. |
| + if (run.weight >= Font::Weight::BOLD) |
| + [str applyFontTraits:NSBoldFontMask range:strRange]; |
| + |
| + // Italics. |
| + if (run.italic) |
| + [str applyFontTraits:NSItalicFontMask range:strRange]; |
|
tapted
2016/09/21 04:06:55
does this work for bold+italic (i.e. should the tr
karandeepb
2016/09/22 08:17:39
Thanks for the catch. Done.
|
| + } |
| + } |
| + |
| + [str endEditing]; |
| + return true; |
| +} |
| + |
| +} // namespace gfx |