Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / AppKit / NSParagraphStyle.m
blob29927b8af286e501b76de6fbfb4947a930fa404b
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;
20    if(shared==nil)
21     shared=[self new];
23    return shared;
26 +(NSArray *)_defaultTabStops {
27    static NSArray *shared=nil;
29    if(shared==nil){
30     int        i,count=12;
31     NSTextTab *tabs[count];
33     for(i=0;i<count;i++){
34      tabs[i]=[[[NSTextTab alloc] initWithType:NSLeftTabStopType location:(i+1)*28.0] autorelease];
35     }
36     shared=[[NSArray alloc] initWithObjects:tabs count:count];
37    }
39    return shared;
42 -(void)_initWithDefaults {
43    _writingDirection=NSWritingDirectionLeftToRight;
44    _paragraphSpacing=0;
45    _paragraphSpacingBefore=0;
46    _textBlocks=nil;
47    _textLists=nil;
48    _headerLevel=0;
49    _firstLineHeadIndent=0;
50    _headIndent=0;
51    _tailIndent=0;
52    _alignment=NSNaturalTextAlignment;
53    _lineBreakMode=NSLineBreakByWordWrapping;
54    _minimumLineHeight=0;
55    _maximumLineHeight=0;
56    _lineHeightMultiple=0;
57    _lineSpacing=0;
58    _defaultTabInterval=0;
59    _tabStops=[[isa _defaultTabStops] retain];
60    _hyphenationFactor=0;
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"];
85    }
86    else {
87     [NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
88    }
89    
90    return self;
93 -init {
94    [self _initWithDefaults];
95    return self;
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;
118    return self;
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"];
143         } else {
144                 NSUnimplementedMethod();
145         }
148 -(void)dealloc {
149    [_textBlocks release];
150    [_textLists release];
151    [_tabStops release];
152    [super dealloc];
155 -copy {
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];
167 -mutableCopy {
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 {
188    return _textBlocks;
191 -(NSArray *)textLists {
192    return _textLists;
195 -(int)headerLevel {
196    return _headerLevel;
199 -(float)firstLineHeadIndent {
200    return _firstLineHeadIndent;
203 -(float)headIndent {
204    return _headIndent;
207 -(float)tailIndent {
208    return _tailIndent;
211 -(NSTextAlignment)alignment {
212    return _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 {
232    return _lineSpacing;
235 -(float)defaultTabInterval {
236    return _defaultTabInterval;
239 -(NSArray *)tabStops {
240    return _tabStops;
243 -(float)hyphenationFactor {
244    return _hyphenationFactor;
247 -(float)tighteningFactorForTruncation {
248    return _tighteningFactorForTruncation;
251 - (BOOL)isEqual:(id)object
253     if (self == object)  {
254         return YES;
255     }
256     if (![object isKindOfClass:[NSParagraphStyle class]]) {
257         return NO;
258     }
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"];
284         
285         for (NSTextTab* tab in _tabStops) {
286                 switch ([tab tabStopType]) {
287                         case NSLeftTabStopType:
288                                 [tabsString appendString: [NSString stringWithFormat: @"%fL", [tab location]]];
289                                 break;
290                         case NSRightTabStopType:
291                                 [tabsString appendString: [NSString stringWithFormat: @"%fR", [tab location]]];
292                                 break;
293                         case NSCenterTabStopType:
294                                 [tabsString appendString: [NSString stringWithFormat: @"%fC", [tab location]]];
295                                 break;
296                         case NSDecimalTabStopType:
297                                 [tabsString appendString: [NSString stringWithFormat: @"%fD", [tab location]]];
298                                 break;
299                 }
300         }
301         [tabsString appendString: @")"];
302                 
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];
315 @end