OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/mac/attributed_string_coder.h" | 5 #include "content/common/mac/attributed_string_coder.h" |
6 | 6 |
7 #include <AppKit/AppKit.h> | 7 #include <AppKit/AppKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // Create the return value. | 22 // Create the return value. |
23 EncodedString* encoded_string = | 23 EncodedString* encoded_string = |
24 new EncodedString(base::SysNSStringToUTF16([str string])); | 24 new EncodedString(base::SysNSStringToUTF16([str string])); |
25 // Iterate over all the attributes in the string. | 25 // Iterate over all the attributes in the string. |
26 NSUInteger length = [str length]; | 26 NSUInteger length = [str length]; |
27 for (NSUInteger i = 0; i < length; ) { | 27 for (NSUInteger i = 0; i < length; ) { |
28 NSRange effective_range; | 28 NSRange effective_range; |
29 NSDictionary* ns_attributes = [str attributesAtIndex:i | 29 NSDictionary* ns_attributes = [str attributesAtIndex:i |
30 effectiveRange:&effective_range]; | 30 effectiveRange:&effective_range]; |
31 // Convert the attributes to IPC-friendly types. | 31 // Convert the attributes to IPC-friendly types. |
32 FontAttribute attrs(ns_attributes, ui::Range(effective_range)); | 32 FontAttribute attrs(ns_attributes, gfx::Range(effective_range)); |
33 // Only encode the attributes if the filtered set contains font information. | 33 // Only encode the attributes if the filtered set contains font information. |
34 if (attrs.ShouldEncode()) { | 34 if (attrs.ShouldEncode()) { |
35 encoded_string->attributes()->push_back(attrs); | 35 encoded_string->attributes()->push_back(attrs); |
36 } | 36 } |
37 // Advance the iterator to the position outside of the effective range. | 37 // Advance the iterator to the position outside of the effective range. |
38 i = NSMaxRange(effective_range); | 38 i = NSMaxRange(effective_range); |
39 } | 39 } |
40 return encoded_string; | 40 return encoded_string; |
41 } | 41 } |
42 | 42 |
43 // static | 43 // static |
44 NSAttributedString* AttributedStringCoder::Decode( | 44 NSAttributedString* AttributedStringCoder::Decode( |
45 const AttributedStringCoder::EncodedString* str) { | 45 const AttributedStringCoder::EncodedString* str) { |
46 // Create the return value. | 46 // Create the return value. |
47 NSString* plain_text = base::SysUTF16ToNSString(str->string()); | 47 NSString* plain_text = base::SysUTF16ToNSString(str->string()); |
48 base::scoped_nsobject<NSMutableAttributedString> decoded_string( | 48 base::scoped_nsobject<NSMutableAttributedString> decoded_string( |
49 [[NSMutableAttributedString alloc] initWithString:plain_text]); | 49 [[NSMutableAttributedString alloc] initWithString:plain_text]); |
50 // Iterate over all the encoded attributes, attaching each to the string. | 50 // Iterate over all the encoded attributes, attaching each to the string. |
51 const std::vector<FontAttribute> attributes = str->attributes(); | 51 const std::vector<FontAttribute> attributes = str->attributes(); |
52 for (std::vector<FontAttribute>::const_iterator it = attributes.begin(); | 52 for (std::vector<FontAttribute>::const_iterator it = attributes.begin(); |
53 it != attributes.end(); ++it) { | 53 it != attributes.end(); ++it) { |
54 // Protect against ranges that are outside the range of the string. | 54 // Protect against ranges that are outside the range of the string. |
55 const ui::Range& range = it->effective_range(); | 55 const gfx::Range& range = it->effective_range(); |
56 if (range.GetMin() > [plain_text length] || | 56 if (range.GetMin() > [plain_text length] || |
57 range.GetMax() > [plain_text length]) { | 57 range.GetMax() > [plain_text length]) { |
58 continue; | 58 continue; |
59 } | 59 } |
60 [decoded_string addAttributes:it->ToAttributesDictionary() | 60 [decoded_string addAttributes:it->ToAttributesDictionary() |
61 range:range.ToNSRange()]; | 61 range:range.ToNSRange()]; |
62 } | 62 } |
63 return [decoded_string.release() autorelease]; | 63 return [decoded_string.release() autorelease]; |
64 } | 64 } |
65 | 65 |
66 // Data Types ////////////////////////////////////////////////////////////////// | 66 // Data Types ////////////////////////////////////////////////////////////////// |
67 | 67 |
68 AttributedStringCoder::EncodedString::EncodedString(string16 string) | 68 AttributedStringCoder::EncodedString::EncodedString(string16 string) |
69 : string_(string) { | 69 : string_(string) { |
70 } | 70 } |
71 | 71 |
72 AttributedStringCoder::EncodedString::EncodedString() | 72 AttributedStringCoder::EncodedString::EncodedString() |
73 : string_() { | 73 : string_() { |
74 } | 74 } |
75 | 75 |
76 AttributedStringCoder::EncodedString::~EncodedString() { | 76 AttributedStringCoder::EncodedString::~EncodedString() { |
77 } | 77 } |
78 | 78 |
79 AttributedStringCoder::FontAttribute::FontAttribute(NSDictionary* dict, | 79 AttributedStringCoder::FontAttribute::FontAttribute(NSDictionary* dict, |
80 ui::Range effective_range) | 80 gfx::Range effective_range) |
81 : font_descriptor_(), | 81 : font_descriptor_(), |
82 effective_range_(effective_range) { | 82 effective_range_(effective_range) { |
83 NSFont* font = [dict objectForKey:NSFontAttributeName]; | 83 NSFont* font = [dict objectForKey:NSFontAttributeName]; |
84 if (font) { | 84 if (font) { |
85 font_descriptor_ = FontDescriptor(font); | 85 font_descriptor_ = FontDescriptor(font); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 AttributedStringCoder::FontAttribute::FontAttribute(FontDescriptor font, | 89 AttributedStringCoder::FontAttribute::FontAttribute(FontDescriptor font, |
90 ui::Range range) | 90 gfx::Range range) |
91 : font_descriptor_(font), | 91 : font_descriptor_(font), |
92 effective_range_(range) { | 92 effective_range_(range) { |
93 } | 93 } |
94 | 94 |
95 AttributedStringCoder::FontAttribute::FontAttribute() | 95 AttributedStringCoder::FontAttribute::FontAttribute() |
96 : font_descriptor_(), | 96 : font_descriptor_(), |
97 effective_range_() { | 97 effective_range_() { |
98 } | 98 } |
99 | 99 |
100 AttributedStringCoder::FontAttribute::~FontAttribute() { | 100 AttributedStringCoder::FontAttribute::~FontAttribute() { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 WriteParam(m, p.effective_range()); | 148 WriteParam(m, p.effective_range()); |
149 } | 149 } |
150 | 150 |
151 bool ParamTraits<AttributedStringCoder::FontAttribute>::Read( | 151 bool ParamTraits<AttributedStringCoder::FontAttribute>::Read( |
152 const Message* m, PickleIterator* iter, param_type* p) { | 152 const Message* m, PickleIterator* iter, param_type* p) { |
153 bool success = true; | 153 bool success = true; |
154 | 154 |
155 FontDescriptor font; | 155 FontDescriptor font; |
156 success &= ReadParam(m, iter, &font); | 156 success &= ReadParam(m, iter, &font); |
157 | 157 |
158 ui::Range range; | 158 gfx::Range range; |
159 success &= ReadParam(m, iter, &range); | 159 success &= ReadParam(m, iter, &range); |
160 | 160 |
161 if (success) { | 161 if (success) { |
162 *p = AttributedStringCoder::FontAttribute(font, range); | 162 *p = AttributedStringCoder::FontAttribute(font, range); |
163 } | 163 } |
164 return success; | 164 return success; |
165 } | 165 } |
166 | 166 |
167 void ParamTraits<AttributedStringCoder::FontAttribute>::Log( | 167 void ParamTraits<AttributedStringCoder::FontAttribute>::Log( |
168 const param_type& p, std::string* l) { | 168 const param_type& p, std::string* l) { |
169 } | 169 } |
170 | 170 |
171 } // namespace IPC | 171 } // namespace IPC |
OLD | NEW |