-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRTFConsumer.m
1111 lines (932 loc) · 24 KB
/
RTFConsumer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* attributedStringConsumer.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Böhringer ([email protected])
Date: Dec 1999
Author: Fred Kiefer <[email protected]>
Date: June 2000
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <Foundation/Foundation.h>
#include <UIKit/UIKit.h>
#include "RTFConsumer.h"
#include "RTFConsumerFunctions.h"
#include "RTFProducer.h"
/* we have to satisfy the scanner with a stream reading function */
typedef struct {
NSString *string;
int position;
int length;
} StringContext;
static void
initStringContext (StringContext *ctxt, NSString *string)
{
ctxt->string = string;
ctxt->position = 0;
ctxt->length = [string length];
}
static int
readNSString (StringContext *ctxt)
{
return (ctxt->position < ctxt->length )
? [ctxt->string characterAtIndex:ctxt->position++]: EOF;
}
// Hold the attributs of the current run
@interface RTFAttribute: NSObject <NSCopying>
{
@public
BOOL changed;
BOOL tabChanged;
NSMutableParagraphStyle *paragraph;
NSColor *fgColour;
NSColor *bgColour;
NSString *fontName;
float fontSize;
BOOL bold;
BOOL italic;
BOOL underline;
int script;
float real_fi, real_li;
}
- (NSFont*) currentFont;
- (NSNumber*) script;
- (NSNumber*) underline;
- (void) resetParagraphStyle;
- (void) resetFont;
- (void) addTab: (float)location type: (NSTextTabType)type;
@end
@implementation RTFAttribute
- (id) init
{
[self resetFont];
[self resetParagraphStyle];
return self;
}
- (void) dealloc
{
RELEASE(paragraph);
RELEASE(fontName);
RELEASE(fgColour);
RELEASE(bgColour);
[super dealloc];
}
- (id) copyWithZone: (NSZone*)zone
{
RTFAttribute *new = (RTFAttribute *)NSCopyObject (self, 0, zone);
new->paragraph = [paragraph mutableCopyWithZone: zone];
RETAIN(new->fontName);
RETAIN(new->fgColour);
RETAIN(new->bgColour);
return new;
}
- (NSFont*) currentFont
{
NSFont *font;
NSFontTraitMask traits = 0;
int weight;
if (bold)
{
weight = 9;
traits |= NSBoldFontMask;
}
else
{
weight = 5;
traits |= NSUnboldFontMask;
}
if (italic)
{
traits |= NSItalicFontMask;
}
else
{
traits |= NSUnitalicFontMask;
}
font = [[NSFontManager sharedFontManager] fontWithFamily: fontName
traits: traits
weight: weight
size: fontSize];
if (font == nil)
{
/* Before giving up and using a default font, we try if this is
* not the case of a font with a composite name, such as
* 'Helvetica-Light'. In that case, even if we don't have
* exactly an 'Helvetica-Light' font family, we might have an
* 'Helvetica' one. */
NSRange range = [fontName rangeOfString:@"-"];
if (range.location != NSNotFound)
{
NSString *fontFamily = [fontName substringToIndex: range.location];
font = [[NSFontManager sharedFontManager] fontWithFamily: fontFamily
traits: traits
weight: weight
size: fontSize];
}
if (font == nil)
{
NSDebugMLLog(@"RTFParser",
@"Could not find font %@ size %f traits %d weight %d",
fontName, fontSize, traits, weight);
/* Last resort, default font. :-( */
font = [NSFont userFontOfSize: fontSize];
}
}
return font;
}
- (NSNumber*) script
{
return [NSNumber numberWithInt: script];
}
- (NSNumber*) underline
{
if (underline)
return [NSNumber numberWithInt: NSSingleUnderlineStyle];
else
return nil;
}
- (void) resetParagraphStyle
{
DESTROY(paragraph);
paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
real_fi = real_li = 0.0;
tabChanged = NO;
changed = YES;
}
- (void) resetFont
{
NSFont *font = [NSFont userFontOfSize:12];
ASSIGN(fontName, [font familyName]);
fontSize = 12.0;
italic = NO;
bold = NO;
underline = NO;
script = 0;
DESTROY(fgColour);
DESTROY(bgColour);
changed = YES;
}
- (void) addTab: (float) location type: (NSTextTabType) type
{
NSTextTab *tab = [[NSTextTab alloc] initWithType: NSLeftTabStopType
location: location];
if (!tabChanged)
{
NSArray *a;
a = [[NSArray alloc] initWithObjects: tab, nil];
// remove all tab stops
[paragraph setTabStops: a];
DESTROY(a);
tabChanged = YES;
}
else
{
[paragraph addTabStop: tab];
}
changed = YES;
RELEASE(tab);
}
@end
static BOOL classInheritsFromNSMutableAttributedString (Class c)
{
Class mutable = [NSMutableAttributedString class];
while (c != Nil)
{
if (c == mutable)
{
return YES;
}
c = [c superclass];
}
return NO;
}
@interface RTFConsumer (Private)
- (NSAttributedString*) parseRTF: (NSData *)rtfData
documentAttributes: (NSDictionary **)dict
class: (Class)class;
- (NSDictionary*) documentAttributes;
- (RTFAttribute*) attr;
- (void) push;
- (void) pop;
- (void) appendString: (NSString*)string;
@end
@implementation RTFConsumer
/* RTFConsumer is the principal class and thus implements this */
+ (Class) classForFormat: (NSString *)format producer: (BOOL)flag
{
Class cClass = Nil;
if (flag)
{
if ([format isEqual: @"RTFD"])
{
cClass = [RTFDProducer class];
}
else if ([format isEqual: @"RTF"])
{
cClass = [RTFProducer class];
}
}
else
{
if ([format isEqual: @"RTFD"])
{
cClass = [RTFDConsumer class];
}
else if ([format isEqual: @"RTF"])
{
cClass = [RTFConsumer class];
}
}
return cClass;
}
+ (NSAttributedString*) parseFile: (NSFileWrapper *)wrapper
documentAttributes: (NSDictionary **)dict
class: (Class)class
{
NSAttributedString *text = nil;
if ([wrapper isRegularFile])
{
RTFConsumer *consumer = [RTFConsumer new];
text = [consumer parseRTF: [wrapper regularFileContents]
documentAttributes: dict
class: class];
RELEASE(consumer);
}
else if ([wrapper isDirectory])
{
NSDictionary *files = [wrapper fileWrappers];
NSFileWrapper *contents;
RTFDConsumer* consumer = [RTFDConsumer new];
if ((contents = [files objectForKey: @"TXT.rtf"]) != nil)
{
[consumer setFiles: files];
text = [consumer parseRTF: [contents regularFileContents]
documentAttributes: dict
class: class];
}
RELEASE(consumer);
}
return text;
}
+ (NSAttributedString*) parseData: (NSData *)rtfData
documentAttributes: (NSDictionary **)dict
class: (Class)class
{
RTFConsumer *consumer = [RTFConsumer new];
NSAttributedString *text;
text = [consumer parseRTF: rtfData
documentAttributes: dict
class: class];
RELEASE(consumer);
return text;
}
- (id) init
{
ignore = 0;
result = nil;
documentAttributes = nil;
fonts = nil;
attrs = nil;
colours = nil;
_class = Nil;
return self;
}
- (void) dealloc
{
RELEASE(fonts);
RELEASE(attrs);
RELEASE(colours);
RELEASE(result);
RELEASE(documentAttributes);
[super dealloc];
}
@end
@interface RTFDConsumer (Private)
- (void) appendImage: (NSString*) string;
@end
@implementation RTFDConsumer
- (id) init
{
self = [super init];
files = nil;
return self;
}
- (void) dealloc
{
RELEASE(files);
[super dealloc];
}
- (void) setFiles: (NSDictionary*) theFiles
{
ASSIGN (files, theFiles);
}
+ (NSAttributedString*) parseData: (NSData *)rtfData
documentAttributes: (NSDictionary **)dict
class: (Class)class
{
NSAttributedString *str;
NSFileWrapper *wrapper = [[NSFileWrapper alloc]
initWithSerializedRepresentation: rtfData];
str = [self parseFile: wrapper documentAttributes: dict class: class];
RELEASE (wrapper);
return str;
}
- (void) appendImage: (NSString*)string
{
int oldPosition = [result length];
NSRange insertionRange = NSMakeRange(oldPosition,0);
if (!ignore)
{
NSString* fileName = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSFileWrapper* wrapper = [files objectForKey: fileName];
if (wrapper != nil)
{
NSImage* image = [[NSImage alloc] initWithData: [wrapper regularFileContents]];
NSTextAttachmentCell* attachedCell = [[NSTextAttachmentCell alloc] initImageCell: image];
NSTextAttachment* attachment = [[NSTextAttachment alloc] initWithFileWrapper: wrapper];
RTFAttribute* attr = [self attr];
NSMutableDictionary* attributes = nil;
NSMutableAttributedString* str = nil;
[attachment setAttachmentCell: attachedCell];
attributes = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[attr currentFont], NSFontAttributeName,
attr->paragraph, NSParagraphStyleAttributeName,
nil];
str = (NSMutableAttributedString*) [NSMutableAttributedString
attributedStringWithAttachment: attachment];
[str addAttributes: attributes range: NSMakeRange (0, [str length])];
[result replaceCharactersInRange: insertionRange withAttributedString: str];
RELEASE(attributes);
RELEASE(attachment);
RELEASE(attachedCell);
RELEASE(image);
}
}
}
@end
@implementation RTFConsumer (Private)
- (NSDictionary*) documentAttributes
{
RETAIN(documentAttributes);
return AUTORELEASE(documentAttributes);
}
- (void) reset
{
RTFAttribute *attr = [RTFAttribute new];
ignore = 0;
DESTROY(result);
if (classInheritsFromNSMutableAttributedString (_class))
{
result = [[_class alloc] init];
}
else
{
result = [[NSMutableAttributedString alloc] init];
}
ASSIGN(documentAttributes, [NSMutableDictionary dictionary]);
ASSIGN(fonts, [NSMutableDictionary dictionary]);
ASSIGN(attrs, [NSMutableArray array]);
ASSIGN(colours, [NSMutableArray array]);
[attrs addObject: attr];
RELEASE(attr);
}
- (RTFAttribute*) attr
{
return [attrs lastObject];
}
- (void) push
{
RTFAttribute *attr = [[attrs lastObject] copy];
[attrs addObject: attr];
RELEASE(attr);
}
- (void) pop
{
[attrs removeLastObject];
((RTFAttribute*)[attrs lastObject])->changed = YES;
}
- (NSAttributedString*) parseRTF: (NSData *)rtfData
documentAttributes: (NSDictionary **)dict
class: (Class)class
{
CREATE_AUTORELEASE_POOL(pool);
RTFscannerCtxt scanner;
StringContext stringCtxt;
// We should read in the first few characters to find out which
// encoding we have
NSString *rtfString = [[NSString alloc]
initWithData: rtfData
encoding: NSASCIIStringEncoding];
// Reset this RFTConsumer, as it might already have been used!
_class = class;
[self reset];
initStringContext(&stringCtxt, rtfString);
lexInitContext(&scanner, &stringCtxt, (int (*)(void*))readNSString);
[result beginEditing];
NS_DURING
GSRTFparse((void *)self, &scanner);
NS_HANDLER
NSLog(@"Problem during RTF Parsing: %@",
[localException reason]);
//[localException raise];
NS_ENDHANDLER
[result endEditing];
RELEASE(rtfString);
RELEASE(pool);
// document attributes
if (dict)
{
*dict = [self documentAttributes];
}
if (classInheritsFromNSMutableAttributedString (_class))
{
RETAIN (result);
AUTORELEASE (result);
return result;
}
else
{
return AUTORELEASE ([[_class alloc] initWithAttributedString: result]);
}
}
- (void) appendString: (NSString*)string
{
int oldPosition = [result length];
int textlen = [string length];
NSRange insertionRange = NSMakeRange(oldPosition,0);
NSMutableDictionary *attributes;
if (!ignore && textlen)
{
RTFAttribute* attr = [self attr];
[result replaceCharactersInRange: insertionRange
withString: string];
if (attr->changed)
{
NSParagraphStyle *ps = [attr->paragraph copy];
attributes = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[attr currentFont], NSFontAttributeName,
ps, NSParagraphStyleAttributeName,
nil];
DESTROY(ps);
if (attr->underline)
{
[attributes setObject: [attr underline]
forKey: NSUnderlineStyleAttributeName];
}
if (attr->script)
{
[attributes setObject: [attr script]
forKey: NSSuperscriptAttributeName];
}
if (attr->fgColour != nil)
{
[attributes setObject: attr->fgColour
forKey: NSForegroundColorAttributeName];
}
if (attr->bgColour != nil)
{
[attributes setObject: attr->bgColour
forKey: NSBackgroundColorAttributeName];
}
[result setAttributes: attributes
range: NSMakeRange(oldPosition, textlen)];
DESTROY(attributes);
attr->changed = NO;
}
}
}
@end
#undef IGNORE
#define FONTS ((RTFConsumer *)ctxt)->fonts
#define COLOURS ((RTFConsumer *)ctxt)->colours
#define RESULT ((RTFConsumer *)ctxt)->result
#define IGNORE ((RTFConsumer *)ctxt)->ignore
#define TEXTPOSITION [RESULT length]
#define DOCUMENTATTRIBUTES ((RTFConsumer*)ctxt)->documentAttributes
#define FILES ((RTFDConsumer*)ctxt)->files
#define CTXT [((RTFConsumer *)ctxt) attr]
#define CHANGED CTXT->changed
#define PARAGRAPH CTXT->paragraph
#define FONTNAME CTXT->fontName
#define SCRIPT CTXT->script
#define ITALIC CTXT->italic
#define BOLD CTXT->bold
#define UNDERLINE CTXT->underline
#define FGCOLOUR CTXT->fgColour
#define BGCOLOUR CTXT->bgColour
#define PAPERSIZE @"PaperSize"
#define LEFTMARGIN @"LeftMargin"
#define RIGHTMARGIN @"RightMargin"
#define TOPMARGIN @"TopMargin"
#define BUTTOMMARGIN @"ButtomMargin"
/*
we must implement from the rtfConsumerFunctions.h file (Supporting files)
this includes the yacc error handling and output
*/
/* handle errors (this is the yacc error mech) */
void GSRTFerror (const char *msg)
{
/* [NSException raise:NSInvalidArgumentException
format:@"Syntax error in RTF: %s", msg];*/
NSDebugLLog(@"RTFParser",@"Syntax error in RTF: %s", msg);
}
void GSRTFgenericRTFcommand (void *ctxt, RTFcmd cmd)
{
NSDebugLLog(@"RTFParser", @"encountered rtf cmd:%s", cmd.name);
if (!cmd.isEmpty)
NSDebugLLog(@"RTFParser", @" argument is %d\n", cmd.parameter);
}
//Start: we're doing some initialization
void GSRTFstart (void *ctxt)
{
NSDebugLLog(@"RTFParser", @"Start RTF parsing");
}
// Finished to parse one piece of RTF.
void GSRTFstop (void *ctxt)
{
//<!> close all open bolds et al.
NSDebugLLog(@"RTFParser", @"End RTF parsing");
}
void GSRTFopenBlock (void *ctxt, BOOL ignore)
{
if (!IGNORE)
{
[(RTFConsumer *)ctxt push];
}
// Switch off any output for ignored block statements
if (ignore)
{
IGNORE++;
}
}
void GSRTFcloseBlock (void *ctxt, BOOL ignore)
{
if (ignore)
{
IGNORE--;
}
if (!IGNORE)
{
[(RTFConsumer *)ctxt pop];
}
}
void GSRTFmangleText (void *ctxt, const char *text)
{
NSData *data = [[NSData alloc] initWithBytes: (void*)text
length: strlen(text)];
NSString *str = [[NSString alloc] initWithData: data
encoding: NSISOLatin1StringEncoding];
[(RTFConsumer *)ctxt appendString: str];
DESTROY(str);
DESTROY(data);
}
void GSRTFunicode (void *ctxt, int uchar)
{
unichar chars = uchar;
NSString *str = [[NSString alloc] initWithCharacters: &chars
length: 1];
[(RTFConsumer *)ctxt appendString: str];
DESTROY(str);
}
void GSRTFregisterFont (void *ctxt, const char *fontName,
RTFfontFamily family, int fontNumber)
{
NSString *fontNameString;
NSNumber *fontId = [NSNumber numberWithInt: fontNumber];
if (!fontName || !*fontName)
{
[NSException raise: NSInvalidArgumentException
format: @"Error in RTF (font omitted?), position:%d",
TEXTPOSITION];
}
// exclude trailing ';' from fontName
if (';' == fontName[strlen(fontName)-1])
{
fontNameString = [NSString stringWithCString: fontName
length: strlen(fontName)-1];
}
else
{
fontNameString = [NSString stringWithCString: fontName
length: strlen(fontName)];
}
[FONTS setObject: fontNameString forKey: fontId];
}
void GSRTFfontNumber (void *ctxt, int fontNumber)
{
NSNumber *fontId = [NSNumber numberWithInt: fontNumber];
NSString *fontName = [FONTS objectForKey: fontId];
if (fontName == nil)
{
/* we're about to set an unknown font */
[NSException raise: NSInvalidArgumentException
format: @"Error in RTF (referring to undefined font \\f%d), position:%d",
fontNumber,
TEXTPOSITION];
}
else
{
if (![fontName isEqual: FONTNAME])
{
ASSIGN(FONTNAME, fontName);
CHANGED = YES;
}
}
}
// <N> fontSize is in halfpoints according to spec
void GSRTFfontSize (void *ctxt, int fontSize)
{
float size = halfpoints2points(fontSize);
if (size != CTXT->fontSize)
{
CTXT->fontSize = size;
CHANGED = YES;
}
}
void GSRTFpaperWidth (void *ctxt, int width)
{
float fwidth = twips2points(width);
NSMutableDictionary *dict = DOCUMENTATTRIBUTES;
NSValue *val = [dict objectForKey: PAPERSIZE];
NSSize size;
if (val == nil)
{
size = NSMakeSize(fwidth, 792);
}
else
{
size = [val sizeValue];
size.width = fwidth;
}
[dict setObject: [NSValue valueWithSize: size] forKey: PAPERSIZE];
}
void GSRTFpaperHeight (void *ctxt, int height)
{
float fheight = twips2points(height);
NSMutableDictionary *dict = DOCUMENTATTRIBUTES;
NSValue *val = [dict objectForKey: PAPERSIZE];
NSSize size;
if (val == nil)
{
size = NSMakeSize(612, fheight);
}
else
{
size = [val sizeValue];
size.height = fheight;
}
[dict setObject: [NSValue valueWithSize: size] forKey: PAPERSIZE];
}
void GSRTFmarginLeft (void *ctxt, int margin)
{
float fmargin = twips2points(margin);
NSMutableDictionary *dict = DOCUMENTATTRIBUTES;
[dict setObject: [NSNumber numberWithFloat: fmargin] forKey: LEFTMARGIN];
}
void GSRTFmarginRight (void *ctxt, int margin)
{
float fmargin = twips2points(margin);
NSMutableDictionary *dict = DOCUMENTATTRIBUTES;
[dict setObject: [NSNumber numberWithFloat: fmargin] forKey: RIGHTMARGIN];
}
void GSRTFmarginTop (void *ctxt, int margin)
{
float fmargin = twips2points(margin);
NSMutableDictionary *dict = DOCUMENTATTRIBUTES;
[dict setObject: [NSNumber numberWithFloat: fmargin] forKey: TOPMARGIN];
}
void GSRTFmarginButtom (void *ctxt, int margin)
{
float fmargin = twips2points(margin);
NSMutableDictionary *dict = DOCUMENTATTRIBUTES;
[dict setObject: [NSNumber numberWithFloat: fmargin] forKey: BUTTOMMARGIN];
}
void GSRTFfirstLineIndent (void *ctxt, int indent)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float findent = twips2points(indent);
CTXT->real_fi = findent;
findent = CTXT->real_li + CTXT->real_fi;
// for attributed strings only positiv indent is allowed
if ((findent >= 0.0) && ([para firstLineHeadIndent] != findent))
{
[para setFirstLineHeadIndent: findent];
CHANGED = YES;
}
}
void GSRTFleftIndent (void *ctxt, int indent)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float findent = twips2points(indent);
CTXT->real_li = findent;
// for attributed strings only positiv indent is allowed
if ((findent >= 0.0) && ([para headIndent] != findent))
{
[para setHeadIndent: findent];
CHANGED = YES;
}
findent = CTXT->real_li + CTXT->real_fi;
if ((findent >= 0.0) && ([para firstLineHeadIndent] != findent))
{
[para setFirstLineHeadIndent: findent];
CHANGED = YES;
}
}
void GSRTFrightIndent (void *ctxt, int indent)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float findent = twips2points(indent);
// for attributed strings only positiv indent is allowed
if ((findent >= 0.0) && ([para tailIndent] != findent))
{
[para setTailIndent: -findent];
CHANGED = YES;
}
}
void GSRTFtabstop (void *ctxt, int location)
{
float flocation = twips2points(location);
if (flocation >= 0.0)
{
[CTXT addTab: flocation type: NSLeftTabStopType];
}
}
void GSRTFalignCenter (void *ctxt)
{
NSMutableParagraphStyle *para = PARAGRAPH;
if ([para alignment] != NSCenterTextAlignment)
{
[para setAlignment: NSCenterTextAlignment];
CHANGED = YES;
}
}
void GSRTFalignJustified (void *ctxt)
{
NSMutableParagraphStyle *para = PARAGRAPH;
if ([para alignment] != NSJustifiedTextAlignment)
{
[para setAlignment: NSJustifiedTextAlignment];
CHANGED = YES;
}
}
void GSRTFalignLeft (void *ctxt)
{
NSMutableParagraphStyle *para = PARAGRAPH;
if ([para alignment] != NSLeftTextAlignment)
{
[para setAlignment: NSLeftTextAlignment];
CHANGED = YES;
}
}
void GSRTFalignRight (void *ctxt)
{
NSMutableParagraphStyle *para = PARAGRAPH;
if ([para alignment] != NSRightTextAlignment)
{
[para setAlignment: NSRightTextAlignment];
CHANGED = YES;
}
}
void GSRTFspaceAbove (void *ctxt, int space)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float fspace = twips2points(space);
if (fspace >= 0.0)
{
[para setParagraphSpacing: fspace];
CHANGED = YES;
}
}
void GSRTFlineSpace (void *ctxt, int space)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float fspace = twips2points(space);
if (space == 1000)
{
[para setMinimumLineHeight: 0.0];
[para setMaximumLineHeight: 0.0];
CHANGED = YES;
}
else if (fspace < 0.0)
{
[para setMaximumLineHeight: -fspace];
CHANGED = YES;
}
else
{
[para setMinimumLineHeight: fspace];
CHANGED = YES;
}
}
void GSRTFdefaultParagraph (void *ctxt)
{
[CTXT resetParagraphStyle];
}