My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /AppKit/RTF.subproj/NSRichTextWriter.m
e5277a40a7e8 vs. 2470bd80abf4 Compare: vs.  Format:
Revision 2470bd80abf4
Go to: 
Project members, sign in to write a code review
/AppKit/RTF.subproj/NSRichTextWriter.m   e5277a40a7e8 /AppKit/RTF.subproj/NSRichTextWriter.m   2470bd80abf4
1 /* Copyright (c) 2006 Christopher J. W. Lloyd 1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
2 2
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: 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:
4 4
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 6
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. */ 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. */
8 8
9 // Original - Christopher Lloyd <cjwl@objc.net> 9 // Original - Christopher Lloyd <cjwl@objc.net>
10 #import <AppKit/NSRichTextWriter.h> 10 #import <AppKit/NSRichTextWriter.h>
11 #import <AppKit/NSFont.h> 11 #import <AppKit/NSFont.h>
12 #import <AppKit/NSFontManager.h> 12 #import <AppKit/NSFontManager.h>
13 #import <AppKit/NSAttributedString.h> 13 #import <AppKit/NSAttributedString.h>
14 #import <malloc.h> 14 #import <malloc.h>
15 15
16 @implementation NSRichTextWriter 16 @implementation NSRichTextWriter
17 17
18 -initWithAttributedString:(NSAttributedString *)attributedString range:(NSRange)range { 18 -initWithAttributedString:(NSAttributedString *)attributedString range:(NSRange)range {
19 _attributedString=[attributedString retain]; 19 _attributedString=[attributedString retain];
20 _string=[[_attributedString string] retain]; 20 _string=[[_attributedString string] retain];
21 _range=range; 21 _range=range;
22 _data=[NSMutableData new]; 22 _data=[NSMutableData new];
23 return self; 23 return self;
24 } 24 }
25 25
26 -(void)dealloc { 26 -(void)dealloc {
27 [_attributedString release]; 27 [_attributedString release];
28 [_string release]; 28 [_string release];
29 [_data release]; 29 [_data release];
30 [super dealloc]; 30 [super dealloc];
31 } 31 }
32 32
33 +(NSData *)dataWithAttributedString:(NSAttributedString *)attributedString range:(NSRange)range { 33 +(NSData *)dataWithAttributedString:(NSAttributedString *)attributedString range:(NSRange)range {
34 NSRichTextWriter *writer=[[self alloc] initWithAttributedString:attributedString range:range]; 34 NSRichTextWriter *writer=[[self alloc] initWithAttributedString:attributedString range:range];
35 NSData *result=[[[writer generateData] retain] autorelease]; 35 NSData *result=[[[writer generateData] retain] autorelease];
36 36
37 [writer release]; 37 [writer release];
38 38
39 return result; 39 return result;
40 } 40 }
41 41
42 -(void)appendCString:(const char *)cString { 42 -(void)appendCString:(const char *)cString {
43 [_data appendBytes:cString length:strlen(cString)]; 43 [_data appendBytes:cString length:strlen(cString)];
44 } 44 }
45 45
46 -(void)appendStringFromRange:(NSRange)range { 46 -(void)appendStringFromRange:(NSRange)range {
47 unichar buffer[range.length]; 47 unichar buffer[range.length];
48 unsigned char *ansi; 48 unsigned char *ansi;
49 int i,ansiLength=0; 49 int i,ansiLength=0;
50 50
51 [_string getCharacters:buffer range:range]; 51 [_string getCharacters:buffer range:range];
52 for(i=0;i<range.length;i++){ 52 for(i=0;i<range.length;i++){
53 if(buffer[i]=='\n') 53 if(buffer[i]=='\n')
54 ansiLength+=2; 54 ansiLength+=2;
55 else if(buffer[i]>127) 55 else if(buffer[i]>127)
56 ansiLength+=6; 56 ansiLength+=6;
57 } 57 }
58 58
59 ansi=alloca(sizeof(unsigned char)*ansiLength); 59 ansi=alloca(sizeof(unsigned char)*ansiLength);
60 ansiLength=0; 60 ansiLength=0;
61 for(i=0;i<range.length;i++){ 61 for(i=0;i<range.length;i++){
62 unichar code=buffer[i]; 62 unichar code=buffer[i];
63 63
64 if(code=='\n'){ 64 if(code=='\n'){
65 ansi[ansiLength++]='\\'; 65 ansi[ansiLength++]='\\';
66 ansi[ansiLength++]='\n'; 66 ansi[ansiLength++]='\n';
67 } 67 }
68 else if(code>127){ 68 else if(code>127){
69 char *hex="0123456789ABCDEF"; 69 char *hex="0123456789ABCDEF";
70 70
71 ansi[ansiLength++]='\\'; 71 ansi[ansiLength++]='\\';
72 ansi[ansiLength++]='U'; 72 ansi[ansiLength++]='U';
73 ansi[ansiLength++]=hex[(code>>12)&0x0F]; 73 ansi[ansiLength++]=hex[(code>>12)&0x0F];
74 ansi[ansiLength++]=hex[(code>>8)&0x0F]; 74 ansi[ansiLength++]=hex[(code>>8)&0x0F];
75 ansi[ansiLength++]=hex[(code>>4)&0x0F]; 75 ansi[ansiLength++]=hex[(code>>4)&0x0F];
76 ansi[ansiLength++]=hex[code&0x0F]; 76 ansi[ansiLength++]=hex[code&0x0F];
77 } 77 }
78 else { 78 else {
79 ansi[ansiLength++]=code; 79 ansi[ansiLength++]=code;
80 } 80 }
81 } 81 }
82 82
83 [_data appendBytes:ansi length:ansiLength]; 83 [_data appendBytes:ansi length:ansiLength];
84 } 84 }
85 85
86 -(void)writeRichText { 86 -(void)writeRichText {
87 unsigned location=_range.location; 87 unsigned location=_range.location;
88 unsigned limit=NSMaxRange(_range); 88 unsigned limit=NSMaxRange(_range);
89 89
90 [self appendCString:"{\\rtf0\\ansi"]; 90 [self appendCString:"{\\rtf0\\ansi"];
91 91
92 while(location<limit){ 92 while(location<limit){
93 NSRange effectiveRange; 93 NSRange effectiveRange;
94 NSDictionary *attributes=[_attributedString attributesAtIndex:location effectiveRange:&effectiveRange]; 94 NSDictionary *attributes=[_attributedString attributesAtIndex:location effectiveRange:&effectiveRange];
95 NSFont *font=NSFontAttributeInDictionary(attributes); 95 NSFont *font=NSFontAttributeInDictionary(attributes);
96 NSFontTraitMask traits=[[NSFontManager sharedFontManager] traitsOfFont:font]; 96 NSFontTraitMask traits=[[NSFontManager sharedFontManager] traitsOfFont:font];
97 97
98 if(effectiveRange.location<location){ 98 if(effectiveRange.location<location){
99 effectiveRange.length=NSMaxRange(effectiveRange)-location; 99 effectiveRange.length=NSMaxRange(effectiveRange)-location;
100 effectiveRange.location=location; 100 effectiveRange.location=location;
101 } 101 }
102 if(NSMaxRange(effectiveRange)>limit) 102 if(NSMaxRange(effectiveRange)>limit)
103 effectiveRange.length=limit-effectiveRange.location; 103 effectiveRange.length=limit-effectiveRange.location;
104 104
105 if(traits&NSBoldFontMask) 105 if(traits&NSBoldFontMask)
106 [self appendCString:"\\b "]; 106 [self appendCString:"\\b "];
107 107
108 [self appendStringFromRange:effectiveRange]; 108 [self appendStringFromRange:effectiveRange];
109 109
110 if(traits&NSBoldFontMask) 110 if(traits&NSBoldFontMask)
111 [self appendCString:"\\b0 "]; 111 [self appendCString:"\\b0 "];
112 112
113 location=NSMaxRange(effectiveRange); 113 location=NSMaxRange(effectiveRange);
114 } 114 }
115 [self appendCString:"}"]; 115 [self appendCString:"}"];
116 } 116 }
117 117
118 -(NSData *)generateData { 118 -(NSData *)generateData {
119 [self writeRichText]; 119 [self writeRichText];
120 return _data; 120 return _data;
121 } 121 }
122 122
123 @end 123 @end
Powered by Google Project Hosting