Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/common/attributed_string_coder.h" | |
| 6 | |
| 7 #include <AppKit/AppKit.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_nsobject.h" | |
| 11 #include "base/sys_string_conversions.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "content/common/common_param_traits.h" | |
| 14 #include "ipc/ipc_message_utils.h" | |
| 15 | |
| 16 namespace mac { | |
| 17 | |
| 18 // static | |
| 19 const AttributedStringCoder::EncodedString* AttributedStringCoder::Encode( | |
| 20 NSAttributedString* str) { | |
| 21 // Create the return value. | |
| 22 EncodedString* encoded_string = | |
| 23 new EncodedString(base::SysNSStringToUTF16([str string])); | |
| 24 // Iterate over all the attributes in the string. | |
| 25 NSUInteger length = [str length]; | |
| 26 for (NSUInteger i = 0; i < length; ) { | |
| 27 NSRange effective_range; | |
| 28 NSDictionary* ns_attributes = [str attributesAtIndex:i | |
| 29 effectiveRange:&effective_range]; | |
| 30 // Convert the attributes to IPC-friendly types. | |
| 31 FontAttribute attrs(ns_attributes, ui::Range(effective_range)); | |
| 32 // Only encode the attributes if the filtered set contains font information. | |
| 33 if (attrs.ShouldEncode()) { | |
| 34 encoded_string->attributes()->push_back(attrs); | |
| 35 } | |
| 36 // Advance the iterator to the position outside of the effective range. | |
| 37 i = NSMaxRange(effective_range); | |
| 38 } | |
| 39 return encoded_string; | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 NSAttributedString* AttributedStringCoder::Decode( | |
| 44 const AttributedStringCoder::EncodedString* str) { | |
| 45 // Create the return value. | |
| 46 NSString* plain_text = base::SysUTF16ToNSString(str->string()); | |
| 47 scoped_nsobject<NSMutableAttributedString> decoded_string( | |
| 48 [[NSMutableAttributedString alloc] initWithString:plain_text]); | |
| 49 // Iterate over all the encoded attributes, attaching each to the string. | |
| 50 const std::vector<FontAttribute> attributes = str->attributes(); | |
| 51 for (std::vector<FontAttribute>::const_iterator it = attributes.begin(); | |
| 52 it != attributes.end(); ++it) { | |
| 53 [decoded_string addAttributes:it->ToAttributesDictionary() | |
| 54 range:it->effective_range().ToNSRange()]; | |
| 55 } | |
| 56 return [decoded_string.release() autorelease]; | |
| 57 } | |
| 58 | |
| 59 // Data Types ////////////////////////////////////////////////////////////////// | |
| 60 | |
| 61 AttributedStringCoder::EncodedString::EncodedString(string16 string) | |
| 62 : string_(string) { | |
| 63 } | |
| 64 | |
| 65 AttributedStringCoder::EncodedString::EncodedString() | |
| 66 : string_() { | |
| 67 } | |
| 68 | |
| 69 AttributedStringCoder::FontAttribute::FontAttribute(NSDictionary* dict, | |
| 70 ui::Range effective_range) | |
| 71 : font_name_(), | |
| 72 font_size_(0), | |
| 73 effective_range_(effective_range) { | |
| 74 NSFont* font = [dict objectForKey:NSFontAttributeName]; | |
| 75 if (font) { | |
| 76 font_name_ = base::SysNSStringToUTF8([font fontName]); | |
|
jeremy
2011/04/12 09:44:06
Could you use a FontDescriptor (content/common/fon
Robert Sesek
2011/04/25 20:58:18
Done.
| |
| 77 font_size_ = [font pointSize]; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 AttributedStringCoder::FontAttribute::FontAttribute(std::string name, | |
| 82 float size, | |
| 83 ui::Range range) | |
| 84 : font_name_(name), | |
| 85 font_size_(size), | |
| 86 effective_range_(range) { | |
| 87 } | |
| 88 | |
| 89 AttributedStringCoder::FontAttribute::FontAttribute() | |
| 90 : font_name_(), | |
| 91 font_size_(0), | |
| 92 effective_range_() { | |
| 93 } | |
| 94 | |
| 95 NSDictionary* AttributedStringCoder::FontAttribute::ToAttributesDictionary( | |
| 96 void) const { | |
| 97 DCHECK(ShouldEncode()); | |
| 98 NSFont* font = [NSFont fontWithName:base::SysUTF8ToNSString(font_name_) | |
| 99 size:font_size_]; | |
| 100 return [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; | |
| 101 } | |
| 102 | |
| 103 bool AttributedStringCoder::FontAttribute::ShouldEncode() const { | |
| 104 return !font_name_.empty(); | |
| 105 } | |
| 106 | |
| 107 } // namespace mac | |
| 108 | |
| 109 // IPC ParamTraits specialization ////////////////////////////////////////////// | |
|
jeremy
2011/04/12 09:44:06
When deserializing values here, could you add some
Robert Sesek
2011/04/25 20:58:18
ui::Range protects against this in its own ParamTr
jeremy
2011/05/01 09:25:56
FontDescriptor's code uses the standard macros and
Robert Sesek
2011/05/02 15:43:31
Added a check that Range::GetMin() and GetMax() ar
| |
| 110 | |
| 111 namespace IPC { | |
| 112 | |
| 113 using mac::AttributedStringCoder; | |
| 114 | |
| 115 void ParamTraits<AttributedStringCoder::EncodedString>::Write( | |
| 116 Message* m, const param_type& p) { | |
| 117 WriteParam(m, p.string()); | |
| 118 WriteParam(m, p.attributes()); | |
| 119 } | |
| 120 | |
| 121 bool ParamTraits<AttributedStringCoder::EncodedString>::Read( | |
| 122 const Message* m, void** iter, param_type* p) { | |
| 123 bool success = true; | |
| 124 | |
| 125 string16 result; | |
| 126 success &= ReadParam(m, iter, &result); | |
| 127 *p = AttributedStringCoder::EncodedString(result); | |
| 128 | |
| 129 success &= ReadParam(m, iter, p->attributes()); | |
| 130 return success; | |
| 131 } | |
| 132 | |
| 133 void ParamTraits<AttributedStringCoder::EncodedString>::Log( | |
| 134 const param_type& p, std::string* l) { | |
| 135 l->append(UTF16ToUTF8(p.string())); | |
| 136 } | |
| 137 | |
| 138 void ParamTraits<AttributedStringCoder::FontAttribute>::Write( | |
| 139 Message* m, const param_type& p) { | |
| 140 WriteParam(m, p.font_name()); | |
| 141 WriteParam(m, p.font_size()); | |
| 142 WriteParam(m, p.effective_range()); | |
| 143 } | |
| 144 | |
| 145 bool ParamTraits<AttributedStringCoder::FontAttribute>::Read( | |
| 146 const Message* m, void** iter, param_type* p) { | |
| 147 bool success = true; | |
| 148 | |
| 149 std::string name; | |
| 150 success &= ReadParam(m, iter, &name); | |
| 151 | |
| 152 float size; | |
| 153 success &= ReadParam(m, iter, &size); | |
| 154 | |
| 155 ui::Range range; | |
| 156 success &= ReadParam(m, iter, &range); | |
| 157 | |
| 158 if (success) { | |
| 159 *p = AttributedStringCoder::FontAttribute(name, size, range); | |
| 160 } | |
| 161 return success; | |
| 162 } | |
| 163 | |
| 164 void ParamTraits<AttributedStringCoder::FontAttribute>::Log( | |
| 165 const param_type& p, std::string* l) { | |
| 166 } | |
| 167 | |
| 168 } // namespace IPC | |
| OLD | NEW |