1 | /*-------------------------------------------------------------------------------------------------------*\ |
---|
2 | | Adium, Copyright (C) 2001-2005, Adam Iser (adamiser@mac.com | http://www.adiumx.com) | |
---|
3 | \---------------------------------------------------------------------------------------------------------/ |
---|
4 | | This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
---|
5 | | General Public License as published by the Free Software Foundation; either version 2 of the License, |
---|
6 | | or (at your option) any later version. |
---|
7 | | |
---|
8 | | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
---|
9 | | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
10 | | Public License for more details. |
---|
11 | | |
---|
12 | | You should have received a copy of the GNU General Public License along with this program; if not, |
---|
13 | | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
14 | \------------------------------------------------------------------------------------------------------ */ |
---|
15 | |
---|
16 | extern NSString *AIFontFamilyAttributeName; |
---|
17 | extern NSString *AIFontSizeAttributeName; |
---|
18 | extern NSString *AIFontWeightAttributeName; |
---|
19 | extern NSString *AIFontStyleAttributeName; |
---|
20 | |
---|
21 | /*! |
---|
22 | * @category NSMutableAttributedString(AIAttributedStringAdditions) |
---|
23 | * @brief Additions to NSMutableAttributedString |
---|
24 | * |
---|
25 | * These methods add string replacement, <tt>NSData</tt> conversion, color adjustment, and more. |
---|
26 | */ |
---|
27 | @interface NSMutableAttributedString (AIAttributedStringAdditions) |
---|
28 | /*! |
---|
29 | * @brief Append a string and set its attributes |
---|
30 | * |
---|
31 | * Appends <b>aString</b>, setting its attributes to <b>attributes</b> |
---|
32 | * @param aString The string to append |
---|
33 | * @param attributes The attributes to use |
---|
34 | */ |
---|
35 | - (void)appendString:(NSString *)aString withAttributes:(NSDictionary *)attributes; |
---|
36 | |
---|
37 | /*! |
---|
38 | * @brief Find and replace on an attributed string |
---|
39 | * |
---|
40 | * Operation is identical to <tt>NSMutableString</tt>'s method of the same name. The replacement string has the attributes of the string it replaced. |
---|
41 | * @param target The string to search for |
---|
42 | * @param replacement The string with which to replace <b>target</b> |
---|
43 | * @param opts Search options, as with NSMutableString's method |
---|
44 | * @param searchRange The range in which to search |
---|
45 | * @return Returns the number of replacements made |
---|
46 | */ |
---|
47 | - (unsigned int)replaceOccurrencesOfString:(NSString *)target withString:(NSString*)replacement options:(unsigned)opts range:(NSRange)searchRange; |
---|
48 | |
---|
49 | /*! |
---|
50 | * @brief Find and replace on an attributed string setting the attributes of the replacements |
---|
51 | * |
---|
52 | * Operation is identical to <tt>NSMutableString</tt>'s method of the same name. The replacement string has the specified attributes. |
---|
53 | * @param target The string to search for |
---|
54 | * @param replacement The string with which to replace <b>target</b> |
---|
55 | * @param attributes The attributes to apply to <b>replacement</b> for each replacement |
---|
56 | * @param opts Search options, as with NSMutableString's method |
---|
57 | * @param searchRange The range in which to search |
---|
58 | * @return Returns the number of replacements made |
---|
59 | */ |
---|
60 | - (unsigned int)replaceOccurrencesOfString:(NSString *)target withString:(NSString*)replacement attributes:(NSDictionary*)attributes options:(unsigned)opts range:(NSRange)searchRange; |
---|
61 | |
---|
62 | /*! |
---|
63 | * @brief Apply color adjustments for a background |
---|
64 | * |
---|
65 | * Adjust all colors in the attributed string so they are visible on the specified background |
---|
66 | * @param backgroundColor The background color |
---|
67 | */ |
---|
68 | - (void)adjustColorsToShowOnBackground:(NSColor *)backgroundColor; |
---|
69 | |
---|
70 | /*! |
---|
71 | * @brief Apply color adjustments for a background |
---|
72 | * |
---|
73 | * Adjust all colors in the attributed string so they are visible on the background, adjusting brightness in a manner proportional to the original background |
---|
74 | * @param backgroundColor The background color |
---|
75 | */ |
---|
76 | - (void)adjustColorsToShowOnBackgroundRelativeToOriginalBackground:(NSColor *)backgroundColor; |
---|
77 | |
---|
78 | /*! |
---|
79 | * @brief Apply link appearance attributes where appropriate |
---|
80 | * |
---|
81 | * Sets color and underline attributes for any areas with NSLinkAttributeName set |
---|
82 | */ |
---|
83 | - (void)addFormattingForLinks; |
---|
84 | |
---|
85 | /*! |
---|
86 | * @brief Convert attachments to strings |
---|
87 | * |
---|
88 | * Generate an NSAttributedString without attachments by substituting their string value if possible |
---|
89 | * (if the attachment responds to @selector(string)), and if not, substituting a characteristic string. |
---|
90 | * @param inPlaceholder The string to use in place of attachments if a string value can not be found |
---|
91 | */ |
---|
92 | - (void)convertAttachmentsToStringsUsingPlaceholder:(NSString *)inPlaceholder; |
---|
93 | @end |
---|
94 | |
---|
95 | /*! |
---|
96 | * @category NSData(AIAppleScriptAdditions) |
---|
97 | * @brief Adds the ability to obtain an <tt>NSAttributedString</tt> from data. |
---|
98 | * |
---|
99 | * This category on <tt>NSData</tt> complements a method in the NSAttributedString(AIAttributedStringAdditions) category. |
---|
100 | */ |
---|
101 | @interface NSData (AIAttributedStringAdditions) |
---|
102 | |
---|
103 | /*! |
---|
104 | * @brief Return an <tt>NSAttributedString</tt> from this data |
---|
105 | * |
---|
106 | * Return an <tt>NSAttributedString</tt> from this data. The data should have been created via -[NSAttributedString dataRepresentation]. |
---|
107 | * @return An <tt>NSAttributedString</tt> |
---|
108 | */ |
---|
109 | - (NSAttributedString *)attributedString; |
---|
110 | @end |
---|
111 | |
---|
112 | @interface NSAttributedString (AIAttributedStringAdditions) |
---|
113 | /*! |
---|
114 | * @brief Returns the set of \c NSAttributedString attributes that can be represented in CSS by \c +CSSStringForTextAttributes:. |
---|
115 | * @return An \c NSSet. |
---|
116 | * |
---|
117 | */ |
---|
118 | + (NSSet *)CSSCapableAttributesSet; |
---|
119 | |
---|
120 | /*! |
---|
121 | * @brief Create a string of CSS attributes for AppKit text attributes. |
---|
122 | * |
---|
123 | * Methods used by this method: |
---|
124 | -[NSColor CSSRepresentation]: Returns color name when color space is named; else, # + hexString |
---|
125 | -[NSShadow CSSRepresentation]: Color, space, offset, space, blur radius |
---|
126 | -[NSCursor CSSRepresentation]: Cursor name |
---|
127 | -[NSFont CSSRepresentation]: [Font weight, space, ]size, space, family(?) name (name in quotes if >1 words); example: @"12pt \"Lucida Grande\"" |
---|
128 | |
---|
129 | Not currently used: |
---|
130 | -[NSURL CSSRepresentation]: @url |
---|
131 | -[NSString CSSRepresentation]: Quoted form, unless it's a URL, in which case @url |
---|
132 | -[NSValue CSSRepresentation]: If NSRect, @"top right bottom left". Else, nil. |
---|
133 | |
---|
134 | Note that NSLinkAttributeName and NSAttachmentAttributeName will not be included in the CSS. Those must be implemented as HTML (\<a\> and \<img\>). |
---|
135 | |
---|
136 | Attribute CSS property |
---|
137 | NSFontAttributeName font (use -CSSRepresentation) |
---|
138 | NSForegroundColorAttributeName color (use -CSSRepresentation) |
---|
139 | NSBackgroundColorAttributeName background-color (use -CSSRepresentation) |
---|
140 | NSStrikethroughStyleAttributeName text-decoration |
---|
141 | NSUnderlineStyleAttributeName text-decoration |
---|
142 | NSSuperscriptAttributeName vertical-align |
---|
143 | NSShadowAttributeName text-shadow (use -CSSRepresentation) |
---|
144 | NSCursorAttributeName cursor (use -CSSRepresentation) |
---|
145 | * |
---|
146 | * @return A \c NSString of CSS code. |
---|
147 | */ |
---|
148 | + (NSString *)CSSStringForTextAttributes:(NSDictionary *)attrs; |
---|
149 | |
---|
150 | /*! |
---|
151 | * @brief Determine the height needed to display an NSAttributedString with certain attributes |
---|
152 | * |
---|
153 | * Returns the height which a string with <b>attributes</b> will require for drawing purposes |
---|
154 | * @param attributes An <tt>NSDictionary</tt> of attributes |
---|
155 | * @return The needed height, as a float |
---|
156 | */ |
---|
157 | + (float)stringHeightForAttributes:(NSDictionary *)attributes; |
---|
158 | |
---|
159 | /*! |
---|
160 | * @brief Determine the height needed for display at a width |
---|
161 | * |
---|
162 | * Returns the height need to display at the passed width |
---|
163 | * @param width The available width for display |
---|
164 | * @return The needed height, as a float |
---|
165 | */ |
---|
166 | - (float)heightWithWidth:(float)width; |
---|
167 | |
---|
168 | /*! |
---|
169 | * @brief Encode to <tt>NSData</tt> |
---|
170 | * |
---|
171 | * Archives the <tt>NSAttributedString</tt> and returns <tt>NSData</tt> suitable for storage |
---|
172 | * @return The attributed string represented as <tt>NSData</tt> |
---|
173 | */ |
---|
174 | - (NSData *)dataRepresentation; |
---|
175 | |
---|
176 | /*! |
---|
177 | * @brief Obtain an <tt>NSAttributedString</tt> from encoded data |
---|
178 | * |
---|
179 | * Retrieves an <tt>NSAttributedString</tt> from <tt>NSData</tt> created with -[NSAttributedString dataRepresentation] |
---|
180 | * @param inData The source <tt>NSData</tt> |
---|
181 | * @return The decoded <tt>NSAttributedString</tt> |
---|
182 | */ |
---|
183 | + (NSAttributedString *)stringWithData:(NSData *)inData; |
---|
184 | |
---|
185 | /*! |
---|
186 | * @brief Generate an NSAttributedString without attachments |
---|
187 | * |
---|
188 | * Generate an NSAttributedString without attachments by substituting their string value if possible |
---|
189 | * (if the attachment responds to @selector(string)), and if not, substituting a characteristic string. |
---|
190 | * @return An <tt>NSAttributedString</tt> without attachments; it may be identical to the original object. |
---|
191 | */ |
---|
192 | - (NSAttributedString *)attributedStringByConvertingAttachmentsToStrings; |
---|
193 | |
---|
194 | /*! |
---|
195 | * @brief Generate an NSAttributedString without links |
---|
196 | * |
---|
197 | * @return An autoreleased copy of the receiver with each link expanded to its URI. |
---|
198 | */ |
---|
199 | - (NSAttributedString *)attributedStringByConvertingLinksToStrings; |
---|
200 | |
---|
201 | /*! |
---|
202 | * @brief Create a new NSAttributedString, apply link appearance attributes where appropriate |
---|
203 | * |
---|
204 | * Sets color and underline attributes for any areas with NSLinkAttributeName set and returns the resulting <tt>NSAttributedString</tt> |
---|
205 | * @return A formatted <tt>NSAttributedString</tt> |
---|
206 | */ |
---|
207 | - (NSAttributedString *)stringByAddingFormattingForLinks; |
---|
208 | |
---|
209 | /*! |
---|
210 | * @brief Create a new NSAttributedString from an NSString |
---|
211 | * |
---|
212 | * @return An autoreleased <tt>NSAttributedString</tt>. |
---|
213 | */ |
---|
214 | + (NSAttributedString *)stringWithString:(NSString *)inString; |
---|
215 | |
---|
216 | @end |
---|
217 | |
---|
218 | |
---|