1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSKeyedArchiver.h>
11 #import <AppKit/NSParagraphStyle.h>
12 #import <AppKit/NSRaise.h>
13 #import <AppKit/NSTextTab.h>
15 @implementation NSParagraphStyle
17 +(NSParagraphStyle *)defaultParagraphStyle {
18 static NSParagraphStyle *shared=nil;
26 +(NSArray *)_defaultTabStops {
27 static NSArray *shared=nil;
31 NSTextTab *tabs[count];
34 tabs[i]=[[[NSTextTab alloc] initWithType:NSLeftTabStopType location:(i+1)*28.0] autorelease];
36 shared=[[NSArray alloc] initWithObjects:tabs count:count];
42 -(void)_initWithDefaults {
43 _writingDirection=NSWritingDirectionLeftToRight;
45 _paragraphSpacingBefore=0;
49 _firstLineHeadIndent=0;
52 _alignment=NSNaturalTextAlignment;
53 _lineBreakMode=NSLineBreakByWordWrapping;
56 _lineHeightMultiple=0;
58 _defaultTabInterval=0;
59 _tabStops=[[isa _defaultTabStops] retain];
61 _tighteningFactorForTruncation=0;
64 -initWithCoder:(NSCoder *)coder {
65 if([coder allowsKeyedCoding]){
66 _writingDirection = [coder decodeIntForKey: @"BaseWritingDirection"];
67 _paragraphSpacing = [coder decodeFloatForKey: @"ParagraphSpacing"];
68 _paragraphSpacingBefore = [coder decodeFloatForKey: @"ParagraphSpacingBefore"];
69 _textBlocks = [[coder decodeObjectForKey: @"Blocks"] retain];
70 _textLists = [coder decodeObjectForKey: @"Lists"];
71 _headerLevel = [coder decodeIntForKey: @"HeaderLevel"];
72 _firstLineHeadIndent = [coder decodeFloatForKey: @"FirstLineHeadIndent"];
73 _headIndent = [coder decodeFloatForKey: @"HeadIndent"];
74 _tailIndent = [coder decodeFloatForKey: @"TailIndent"];
75 _alignment = [coder decodeIntForKey: @"Alignment"];
76 _lineBreakMode = [coder decodeIntForKey: @"LineBreakMode"];
77 _minimumLineHeight = [coder decodeFloatForKey: @"MinimumLineHeight"];
78 _maximumLineHeight = [coder decodeFloatForKey: @"MaximumLineHeight"];
79 _lineHeightMultiple = [coder decodeFloatForKey: @"LineHeightMultiple"];
80 _lineSpacing = [coder decodeFloatForKey: @"LineSpacing"];
81 _defaultTabInterval = [coder decodeFloatForKey: @"DefaultTabInterval"];
82 _tabStops = [[coder decodeObjectForKey: @"Tabs"] retain];
83 _hyphenationFactor = [coder decodeFloatForKey: @"HyphenationFactor"];
84 _tighteningFactorForTruncation = [coder decodeFloatForKey: @"TighteningFactor"];
87 [NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
94 [self _initWithDefaults];
98 -initWithParagraphStyle:(NSParagraphStyle *)other {
99 _writingDirection=other->_writingDirection;
100 _paragraphSpacing=other->_paragraphSpacing;
101 _paragraphSpacingBefore=other->_paragraphSpacingBefore;
102 _textBlocks=[other->_textBlocks copy];
103 _textLists=[other->_textLists copy];
104 _headerLevel=other->_headerLevel;
105 _firstLineHeadIndent=other->_firstLineHeadIndent;
106 _headIndent=other->_headIndent;
107 _tailIndent=other->_tailIndent;
108 _alignment=other->_alignment;
109 _lineBreakMode=other->_lineBreakMode;
110 _minimumLineHeight=other->_minimumLineHeight;
111 _maximumLineHeight=other->_maximumLineHeight;
112 _lineHeightMultiple=other->_lineHeightMultiple;
113 _lineSpacing=other->_lineSpacing;
114 _defaultTabInterval=other->_defaultTabInterval;
115 _tabStops=[other->_tabStops copy];
116 _hyphenationFactor=other->_hyphenationFactor;
117 _tighteningFactorForTruncation=other->_tighteningFactorForTruncation;
121 - (void)encodeWithCoder:(NSCoder *)coder
123 if ([coder isKindOfClass: [NSKeyedArchiver class]]) {
124 [coder encodeInt: _writingDirection forKey: @"BaseWritingDirection"];
125 [coder encodeFloat: _paragraphSpacing forKey: @"ParagraphSpacing"];
126 [coder encodeFloat: _paragraphSpacingBefore forKey: @"ParagraphSpacingBefore"];
127 [coder encodeObject: _textBlocks forKey: @"Blocks"];
128 [coder encodeObject: _textLists forKey: @"Lists"];
129 [coder encodeInt: _headerLevel forKey: @"HeaderLevel"];
130 [coder encodeFloat: _firstLineHeadIndent forKey: @"FirstLineHeadIndent"];
131 [coder encodeFloat: _headIndent forKey: @"HeadIndent"];
132 [coder encodeFloat: _tailIndent forKey: @"TailIndent"];
133 [coder encodeInt: _alignment forKey: @"Alignment"];
134 [coder encodeInt: _lineBreakMode forKey: @"LineBreakMode"];
135 [coder encodeFloat: _minimumLineHeight forKey: @"MinimumLineHeight"];
136 [coder encodeFloat: _maximumLineHeight forKey: @"MaximumLineHeight"];
137 [coder encodeFloat: _lineHeightMultiple forKey: @"LineHeightMultiple"];
138 [coder encodeFloat: _lineSpacing forKey: @"LineSpacing"];
139 [coder encodeFloat: _defaultTabInterval forKey: @"DefaultTabInterval"];
140 [coder encodeObject: _tabStops forKey: @"Tabs"];
141 [coder encodeFloat: _hyphenationFactor forKey: @"HyphenationFactor"];
142 [coder encodeFloat: _tighteningFactorForTruncation forKey: @"TighteningFactor"];
144 NSUnimplementedMethod();
149 [_textBlocks release];
150 [_textLists release];
156 return [self retain];
159 -copyWithZone:(NSZone *)zone {
160 return [self retain];
163 static inline id mutableCopyWithZone(NSParagraphStyle *self,NSZone *zone){
164 return [[NSMutableParagraphStyle allocWithZone:zone] initWithParagraphStyle:self];
168 return mutableCopyWithZone(self,NULL);
171 -mutableCopyWithZone:(NSZone *)zone {
172 return mutableCopyWithZone(self,zone);
175 -(NSWritingDirection)baseWritingDirection {
176 return _writingDirection;
179 -(float)paragraphSpacing {
180 return _paragraphSpacing;
183 -(float)paragraphSpacingBefore {
184 return _paragraphSpacingBefore;
187 -(NSArray *)textBlocks {
191 -(NSArray *)textLists {
199 -(float)firstLineHeadIndent {
200 return _firstLineHeadIndent;
211 -(NSTextAlignment)alignment {
215 -(NSLineBreakMode)lineBreakMode {
216 return _lineBreakMode;
219 -(float)minimumLineHeight {
220 return _minimumLineHeight;
223 -(float)maximumLineHeight {
224 return _maximumLineHeight;
227 -(float)lineHeightMultiple {
228 return _lineHeightMultiple;
231 -(float)lineSpacing {
235 -(float)defaultTabInterval {
236 return _defaultTabInterval;
239 -(NSArray *)tabStops {
243 -(float)hyphenationFactor {
244 return _hyphenationFactor;
247 -(float)tighteningFactorForTruncation {
248 return _tighteningFactorForTruncation;
251 - (BOOL)isEqual:(id)object
253 if (self == object) {
256 if (![object isKindOfClass:[NSParagraphStyle class]]) {
259 NSParagraphStyle *other = (NSParagraphStyle *)object;
260 return (_writingDirection == other->_writingDirection &&
261 _paragraphSpacing == other->_paragraphSpacing &&
262 _paragraphSpacingBefore == other->_paragraphSpacingBefore &&
263 _headerLevel == other->_headerLevel &&
264 _firstLineHeadIndent == other->_firstLineHeadIndent &&
265 _headIndent == other->_headIndent &&
266 _tailIndent == other->_tailIndent &&
267 _alignment == other->_alignment &&
268 _lineBreakMode == other->_lineBreakMode &&
269 _minimumLineHeight == other->_minimumLineHeight &&
270 _maximumLineHeight == other->_maximumLineHeight &&
271 _lineHeightMultiple == other->_lineHeightMultiple &&
272 _lineSpacing == other->_lineSpacing &&
273 _defaultTabInterval == other->_defaultTabInterval &&
274 _hyphenationFactor == other->_hyphenationFactor &&
275 _tighteningFactorForTruncation == other->_tighteningFactorForTruncation &&
276 (_tabStops == other->_tabStops || [_tabStops isEqual:other->_tabStops]) &&
277 (_textBlocks == other->_textBlocks || [_textBlocks isEqual:other->_textBlocks]) &&
278 (_textLists == other->_textLists || [_textLists isEqual:other->_textLists]));
281 - (NSString*)description
283 NSMutableString* tabsString = [NSMutableString stringWithString: @" (\n"];
285 for (NSTextTab* tab in _tabStops) {
286 switch ([tab tabStopType]) {
287 case NSLeftTabStopType:
288 [tabsString appendString: [NSString stringWithFormat: @"%fL", [tab location]]];
290 case NSRightTabStopType:
291 [tabsString appendString: [NSString stringWithFormat: @"%fR", [tab location]]];
293 case NSCenterTabStopType:
294 [tabsString appendString: [NSString stringWithFormat: @"%fC", [tab location]]];
296 case NSDecimalTabStopType:
297 [tabsString appendString: [NSString stringWithFormat: @"%fD", [tab location]]];
301 [tabsString appendString: @")"];
303 return [NSString stringWithFormat: @"Alignment %d, LineSpacing %f, ParagraphSpacing %f, "
304 @"ParagraphSpacingBefore %f, HeadIndent %f, TailIndent %f, FirstLineHeadIndent %f, "
305 @"LineHeight %f/%f, LineHeightMultiple %f, LineBreakMode %d, Tabs %@, "
306 @"DefaultTabInterval %f, Blocks %@, Lists %@, BaseWritingDirection %d, "
307 @"HyphenationFactor %f, TighteningFactor %f, HeaderLevel %d",
308 _alignment, _lineSpacing, _paragraphSpacing,
309 _paragraphSpacingBefore, _headIndent, _tailIndent, _firstLineHeadIndent,
310 _minimumLineHeight, _maximumLineHeight, _lineHeightMultiple, _lineBreakMode, tabsString,
311 _defaultTabInterval, _textBlocks, _textLists, _writingDirection,
312 _hyphenationFactor, _tighteningFactorForTruncation, _headerLevel];