summaryrefslogtreecommitdiff
path: root/tikzit-old/src/common/Preambles.m
blob: 922fc3094665db98db0016da83ece3b4ea1dc708 (plain)
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
//
//  Preambles.m
//  TikZiT
//  
//  Copyright 2010 Aleks Kissinger. All rights reserved.
//  
//  
//  This file is part of TikZiT.
//  
//  TikZiT is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  TikZiT 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 General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with TikZiT.  If not, see <http://www.gnu.org/licenses/>.
//  

#import "Preambles.h"
#import "NodeStyle.h"
#import "EdgeStyle.h"
#import "Graph.h"

static NSString *DEF_PREAMBLE_START =
@"\\usepackage[svgnames]{xcolor}\n"
@"\\usepackage{tikz}\n"
@"\\usetikzlibrary{decorations.markings}\n"
@"\\usetikzlibrary{shapes.geometric}\n"
@"\n"
@"\\pgfdeclarelayer{edgelayer}\n"
@"\\pgfdeclarelayer{nodelayer}\n"
@"\\pgfsetlayers{edgelayer,nodelayer,main}\n"
@"\n"
@"\\tikzstyle{none}=[inner sep=0pt]\n";

static NSString *PREAMBLE_TAIL =
@"\n"
@"\\pagestyle{empty}\n"
@"\\usepackage[graphics,tightpage,active]{preview}\n"
@"\\PreviewEnvironment{tikzpicture}\n"
@"\\newlength{\\imagewidth}\n"
@"\\newlength{\\imagescale}\n"
@"\n"
@"\\begin{document}\n";

static NSString *POSTAMBLE =
@"\n"
@"\\end{document}\n";

@implementation Preambles

+ (Preambles*)preambles {
#if __has_feature(objc_arc)
    return [[self alloc] init];
#else
	return [[[self alloc] init] autorelease];
#endif
}

- (id)init {
	self = [super init];
	if (self) {
		selectedPreambleName = @"default";
		preambleDict = [[NSMutableDictionary alloc] initWithCapacity:1];
		[preambleDict setObject:[self defaultPreamble] forKey:@"custom"];
		styles = nil;
		edges = nil;
		styleManager = nil;
	}
	return self;
}

- (void)dealloc {
#if ! __has_feature(objc_arc)
	[selectedPreambleName release];
	[styles release];
	[styleManager release];
	[super dealloc];
#endif
}

- (NSString*)preambleForName:(NSString*)name {
	if ([name isEqualToString:@"default"])
		return [self defaultPreamble];
	else
		return [preambleDict objectForKey:name];
}

- (BOOL)setPreamble:(NSString*)content forName:(NSString*)name {
	if ([name isEqualToString:@"default"])
		return NO;
	[preambleDict setObject:content forKey:name];
	return YES;
}

- (void)removeAllPreambles {
	[preambleDict removeAllObjects];
}

- (NSEnumerator*)customPreambleNameEnumerator {
	return [preambleDict keyEnumerator];
}

- (void)setStyles:(NSArray*)sty {
#if ! __has_feature(objc_arc)
	[sty retain];
	[styles release];
#endif
	styles = sty;
}

- (void)setEdges:(NSArray*)edg {
#if ! __has_feature(objc_arc)
	[edg retain];
	[edges release];
#endif
	edges = edg;
}

- (NSString*)styleDefinitions {
	if (styleManager != nil) {
		[self setStyles:[styleManager nodeStyles]];
	}
#if ! __has_feature(objc_arc)
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#endif
	NSMutableString *buf = [NSMutableString string];
	NSMutableString *colbuf = [NSMutableString string];
	NSMutableSet *colors = [NSMutableSet setWithCapacity:2*[styles count]];
	for (NodeStyle *st in styles) {
		[buf appendFormat:@"%@\n", [st tikz]];
		ColorRGB *fill = [st fillColorRGB];
		ColorRGB *stroke = [st strokeColorRGB];
		if ([fill name] == nil && ![colors containsObject:fill]) {
			[colors addObject:fill];
			[colbuf appendFormat:@"\\definecolor{%@}{rgb}{%.3f,%.3f,%.3f}\n",
			 [fill hexName], [fill redFloat], [fill greenFloat], [fill blueFloat]];
		}
		
		if ([stroke name] == nil && ![colors containsObject:stroke]) {
			[colors addObject:stroke];
			[colbuf appendFormat:@"\\definecolor{%@}{rgb}{%.3f,%.3f,%.3f}\n",
			 [stroke hexName], [stroke redFloat], [stroke greenFloat], [stroke blueFloat]];
		}
	}
    
	if (styleManager != nil) {
		[self setEdges:[styleManager edgeStyles]];
	}
    
	[buf appendString:@"\n"];
	for (EdgeStyle *st in edges) {
		[buf appendFormat:@"%@\n", [st tikz]];
		ColorRGB *color = [st colorRGB];
		if (color != nil && [color name] == nil && ![colors containsObject:color]) {
			[colors addObject:color];
			[colbuf appendFormat:@"\\definecolor{%@}{rgb}{%.3f,%.3f,%.3f}\n",
				[color hexName], [color redFloat], [color greenFloat], [color blueFloat]];
		}
	}
	
	NSString *defs = [[NSString alloc] initWithFormat:@"%@\n%@", colbuf, buf];
	
#if __has_feature(objc_arc)
    return defs;
#else
	[pool drain];
	return [defs autorelease];
#endif
}

- (NSString*)defaultPreamble {
	return [NSString stringWithFormat:@"%@%@",
			DEF_PREAMBLE_START, [self styleDefinitions]];
}

- (BOOL)selectedPreambleIsDefault {
	return [selectedPreambleName isEqualToString:@"default"];
}

- (NSString*)selectedPreambleName { return selectedPreambleName; }
- (void)setSelectedPreambleName:(NSString *)sel {
	if (sel != selectedPreambleName) {
#if ! __has_feature(objc_arc)
		[selectedPreambleName release];
#endif
		selectedPreambleName = [sel copy];
	}
}

- (NSString*)currentPreamble {
	NSString *pre = [self preambleForName:selectedPreambleName];
	return (pre == nil) ? [self defaultPreamble] : pre;
}

- (void)setCurrentPreamble:(NSString*)str {
	if (![selectedPreambleName isEqualToString:@"default"])
		[preambleDict setObject:str forKey:selectedPreambleName];
}

- (StyleManager*)styleManager {
	return styleManager;
}

- (void)setStyleManager:(StyleManager *)manager {
#if ! __has_feature(objc_arc)
	[manager retain];
	[styleManager release];
#endif
	styleManager = manager;
}

- (NSString*)currentPostamble {
	return POSTAMBLE;
}

- (NSMutableDictionary*)preambleDict {
	return preambleDict;
}

- (NSString*)defaultPreambleName {
	return @"default";
}

- (NSString*)addPreamble {
	return [self addPreambleWithNameBase:@"new preamble"];
}

- (NSString*)addPreambleWithNameBase:(NSString*)base {
	if ([preambleDict objectForKey:base] == nil) {
		[self setPreamble:[self defaultPreamble] forName:base];
		return base;
	}
	int i = 0;
	NSString *tryName = nil;
	do {
		++i;
		tryName = [NSString stringWithFormat:@"%@ %d", base, i];
	} while ([preambleDict objectForKey:tryName] != nil);

	[self setPreamble:[self defaultPreamble] forName:tryName];
	return tryName;
}

- (BOOL)renamePreambleFrom:(NSString*)old to:(NSString*)new {
	if ([old isEqualToString:@"default"])
		return NO;
	if ([new isEqualToString:@"default"])
		return NO;
	if ([old isEqualToString:new])
		return YES;
	BOOL isSelected = NO;
	if ([old isEqualToString:selectedPreambleName]) {
		[self setSelectedPreambleName:nil];
		isSelected = YES;
	}
	NSString *preamble = [preambleDict objectForKey:old];
#if ! __has_feature(objc_arc)
	[preamble retain];
#endif
	[preambleDict removeObjectForKey:old];
	[preambleDict setObject:preamble forKey:new];
#if ! __has_feature(objc_arc)
	[preamble release];
#endif
	if (isSelected) {
		[self setSelectedPreambleName:new];
	}
	return YES;
}

- (BOOL)removePreamble:(NSString*)name {
	if ([name isEqualToString:@"default"])
		return NO;
	// "name" may be held only by being the selected preamble...
#if ! __has_feature(objc_arc)
	[name retain];
#endif
	if ([name isEqualToString:selectedPreambleName])
		[self setSelectedPreambleName:nil];
	[preambleDict removeObjectForKey:name];
#if ! __has_feature(objc_arc)
	[name release];
#endif
	return YES;
}

- (NSString*)buildDocumentForTikz:(NSString*)tikz
{
	NSString *preamble = [self currentPreamble];
	NSString *doc_head = @"";
	if (![preamble hasPrefix:@"\\documentclass"]) {
		doc_head = @"\\documentclass{article}\n";
	}
	NSString *preamble_suffix = @"";
	if ([preamble rangeOfString:@"\\begin{document}"
						options:NSBackwardsSearch].length == 0) {
		preamble_suffix = PREAMBLE_TAIL;
	}
    return [NSString stringWithFormat:@"%@%@%@%@%@",
			 doc_head,
             [self currentPreamble],
			 preamble_suffix,
             tikz,
             POSTAMBLE];
}

- (NSString*)buildDocumentForGraph:(Graph*)g
{
	return [self buildDocumentForTikz:[g tikz]];
}

@end

// vi:ft=objc:ts=4:noet:sts=4:sw=4