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 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #include "base/strings/sys_string_conversions.h" | |
9 #include "third_party/skia/include/ports/SkTypeface_mac.h" | |
10 #include "ui/gfx/font.h" | |
11 #include "ui/gfx/render_text_harfbuzz.h" | |
12 | |
13 namespace gfx { | |
14 | |
15 namespace internal { | |
16 | |
17 sk_sp<SkTypeface> CreateSkiaTypeface(const Font& font, | |
18 bool italic, | |
19 Font::Weight weight) { | |
20 const Font::FontStyle style = italic ? Font::ITALIC : Font::NORMAL; | |
21 Font font_with_style = font.Derive(0, style, weight); | |
22 if (!font_with_style.GetNativeFont()) | |
23 return nullptr; | |
24 | |
25 return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont( | |
26 base::mac::NSToCFCast(font_with_style.GetNativeFont()))); | |
27 } | |
28 | |
29 } // namespace internal | |
30 | |
31 bool RenderTextHarfBuzz::GetStyledStringForRange( | |
32 const Range& range, | |
33 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
| |
34 if (obscured()) | |
35 return false; | |
36 | |
37 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; | |
38 | |
39 [str beginEditing]; | |
40 [[str mutableString] | |
41 setString:base::SysUTF16ToNSString(GetTextFromRange(range))]; | |
42 | |
43 const internal::TextRunList* run_list = GetRunList(); | |
44 for (size_t i = 0; i < run_list->size(); i++) { | |
45 const internal::TextRunHarfBuzz& run = *run_list->runs()[i]; | |
46 | |
47 const Range& intersection = range.Intersect(run.range); | |
48 DCHECK(!intersection.is_reversed()); | |
49 | |
50 if (!intersection.is_empty()) { | |
51 // Get range relative to |str|. | |
52 NSRange strRange = NSMakeRange(intersection.start() - range.GetMin(), | |
53 intersection.length()); | |
54 | |
55 // Font. | |
56 if (run.font.GetNativeFont()) | |
57 [attrs setObject:run.font.GetNativeFont() forKey:NSFontAttributeName]; | |
58 else | |
59 [attrs removeObjectForKey:NSFontAttributeName]; | |
60 | |
61 // Underline. | |
62 if (run.underline) { | |
63 [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) | |
64 forKey:NSUnderlineStyleAttributeName]; | |
65 } else { | |
66 [attrs removeObjectForKey:NSUnderlineStyleAttributeName]; | |
67 } | |
68 | |
69 // Strikethrough. | |
70 if (run.strike) { | |
71 [attrs setObject:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) | |
72 forKey:NSStrikethroughStyleAttributeName]; | |
73 } else { | |
74 [attrs removeObjectForKey:NSStrikethroughStyleAttributeName]; | |
75 } | |
76 | |
77 // Apply attributes. | |
78 [str setAttributes:attrs range:strRange]; | |
79 | |
80 // Bold. | |
81 if (run.weight >= Font::Weight::BOLD) | |
82 [str applyFontTraits:NSBoldFontMask range:strRange]; | |
83 | |
84 // Italics. | |
85 if (run.italic) | |
86 [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.
| |
87 } | |
88 } | |
89 | |
90 [str endEditing]; | |
91 return true; | |
92 } | |
93 | |
94 } // namespace gfx | |
OLD | NEW |