forked from splashdust/SVGQuartzRenderer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVGQuartzRenderer.m
1286 lines (986 loc) · 39.3 KB
/
SVGQuartzRenderer.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
/*--------------------------------------------------
* Copyright (c) 2010 Joacim Magnusson
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*--------------------------------------------------*/
#import "SVGQuartzRenderer.h"
#import "NSData+Base64.h"
#import "SVGStyle.h"
#import "Sprite.h"
#import "math.h"
#import "float.h"
#import "QuadTreeNode.h"
#import "PathFrag.h"
#import "CGPathReader.h"
#import "TextFrag.h"
// Function prototypes for SAX callbacks. This sample implements a minimal subset of SAX callbacks.
// Depending on your application's needs, you might want to implement more callbacks.
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes);
static void endElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI);
static void charactersFoundSAX(void * ctx, const xmlChar * ch, int len);
static void errorEncounteredSAX(void * ctx, const char * msg, ...);
// Forward reference. The structure is defined in full at the end of the file.
static xmlSAXHandler simpleSAXHandlerStruct;
@interface SVGQuartzRenderer (hidden)
- (void) prepareToDraw;
-(void) createPathFrag;
-(void) createTextFrag:(const xmlChar *)ch :(int) len;
-(void) drawPath;
- (SVG_TRANS)applyTransformations:(const char *)transformations;
- (void)applyTransformation:(SVG_TRANS)trans;
- (void) cleanupAfterFinishedParsing;
void CGPathAddRoundRect(CGMutablePathRef currPath, CGRect rect, float radius);
-(BOOL) doCenter:(CGPoint)location withBoundingBox:(CGSize)box;
-(Sprite*) currentSprite;
-(void) copyAttributes:(const xmlChar **) attributes size:(int)nb_attributes toDest:(NSMutableDictionary*) dest;
-(void) startElementSAX:(const xmlChar *)localname :(const xmlChar *)prefix :(const xmlChar *)URI :(int) nb_namespaces :(const xmlChar **)namespaces :(int) nb_attributes :(int) nb_defaulted :(const xmlChar **)attributes;
-(void) endElementSAX:(const xmlChar *)localname :(const xmlChar *)prefix :(const xmlChar *)URI;
-(void) charactersFoundSAX:(const xmlChar *)ch :(int) len;
@end
@implementation SVGQuartzRenderer
@synthesize viewFrame;
@synthesize documentSize;
@synthesize delegate;
@synthesize globalScaleX, globalScaleY, offsetX, offsetY;
@synthesize curLayerName;
@synthesize svgFile;
typedef void (*CGPatternDrawPatternCallback) (void * info, CGContextRef context);
- (id)init {
self = [super init];
if (self) {
offsetX = 0;
offsetY = 0;
sprites = [NSMutableDictionary new];
fragments = [NSMutableArray new];
firstRender = YES;
inDefSection = NO;
rootNode = [[QuadTreeNode alloc] initWithRect:CGRectMake(0,0,1,1)];
isParsed = NO;
}
return self;
}
-(CGPoint) getTranslation
{
return CGPointMake(-offsetX, -offsetY);
}
-(CGPoint) getScale
{
return CGPointMake(globalScaleX, globalScaleY);
}
- (void)setDelegate:(id<SVGQuartzRenderDelegate>)rendererDelegate
{
delegate = rendererDelegate;
}
- (void)parse
{
if (isParsed)
return;
svgXml = [[NSData alloc ] initWithContentsOfFile:svgFile];
[svgFile release];
NSDate* start;
NSTimeInterval timeInterval;
start = [NSDate date];
// This creates a context for "push" parsing in which chunks of data that are not "well balanced" can be passed
// to the context for streaming parsing. The handler structure defined above will be used for all the parsing.
// The second argument, self, will be passed as user data to each of the SAX handlers. The last three arguments
// are left blank to avoid creating a tree in memory.
context = xmlCreatePushParserCtxt(&simpleSAXHandlerStruct, self, NULL, 0, NULL);
xmlParseChunk(context, (const char *)[svgXml bytes], [svgXml length], 0);
// Signal the context that parsing is complete by passing "1" as the last parameter.
xmlParseChunk(context, NULL, 0, 1);
// Release resources used only in this thread.
xmlFreeParserCtxt(context);
context = NULL;
timeInterval = [start timeIntervalSinceNow];
NSLog(@"lib2xml parse: %f seconds",-timeInterval);
[svgXml release];
svgXml = nil;
isParsed = YES;
}
-(void) redraw
{
NSDate* start;
NSTimeInterval timeInterval;
start = [NSDate date];
if(delegate) {
CGContextRelease(cgContext);
cgContext = [delegate svgRenderer:self requestedCGContextWithSize:documentSize];
}
for (int i = 0; i < [fragments count]; ++i)
{
GraphicFrag* frag = (GraphicFrag*)[fragments objectAtIndex:i];
[frag draw:cgContext];
}
if (delegate)
[delegate svgRenderer:self finishedRenderingInCGContext:cgContext];
[self cleanupAfterFinishedParsing];
timeInterval = [start timeIntervalSinceNow];
NSLog(@"Redraw: %f seconds",-timeInterval);
}
- (void) resetScale
{
globalScaleX = initialScaleX;
globalScaleY = initialScaleY;
[self doCenter:CGPointMake(0.5,0.5) withBoundingBox:CGSizeMake(1,1)];
}
-(CGPoint) scaledImagePointFromViewPoint:(CGPoint)viewPoint
{
float x = (offsetX + viewPoint.x)/(globalScaleX*width);
float y = (offsetY + viewPoint.y)/(globalScaleY*height);
return CGPointMake(x,y);
}
-(BOOL) doCenter:(CGPoint)location withBoundingBox:(CGSize)box
{
//reject locations outside of the image
if (location.x <0 || location.y < 0 || location.x > 1 || location.y > 1)
return NO;
//reject bounding box that is not wholly contained in image
if (box.width <0 || box.height < 0 || box.width > 1 || box.height > 1)
return NO;
globalScaleX = initialScaleX/box.width;
globalScaleY = initialScaleY/box.height;
//reverse calculation from relativeImagePointFrom above, with viewPoint set to middle of screen
offsetX = -viewFrame.size.width/2 + location.x* globalScaleX* width;
offsetY = -viewFrame.size.height/2 + location.y * globalScaleY* height;
return YES;
}
-(void) center:(CGPoint)location withBoundingBox:(CGSize)box
{
if ([self doCenter:location withBoundingBox:box])
{
[self redraw];
}
}
-(NSString*) find:(CGPoint)viewPoint
{
// un-highlight all sprites
NSEnumerator *enumerator = [sprites keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
Sprite* sprite = [sprites objectForKey:key];
sprite.isHighlighted = NO;
}
NSArray* group = [rootNode groupContainingPoint:[self scaledImagePointFromViewPoint:viewPoint]];
if (group != nil && [group count] > 0)
{
Sprite* sprite = (Sprite*)[group objectAtIndex:0];
sprite.isHighlighted = YES;
return sprite.name;
}
return nil;
}
- (void) prepareToDraw
{
if(delegate) {
CGContextRelease(cgContext);
cgContext = [delegate svgRenderer:self requestedCGContextWithSize:documentSize];
}
//default transformation
transform = CGAffineTransformScale(CGAffineTransformIdentity, globalScaleX, globalScaleY);
transform = CGAffineTransformTranslate(transform, -offsetX/globalScaleX, -offsetY/globalScaleY);
CGContextConcatCTM(cgContext,transform);
}
-(void) copyAttributes:(const xmlChar **) attributes size:(int)nb_attributes toDest:(NSMutableDictionary*) dest
{
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *localname = attributes[index];
// const xmlChar *prefix = attributes[index+1];
// const xmlChar *nsURI = attributes[index+2];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
xmlChar val[vlen + 1];
strncpy((char*)val, (const char*)valueBegin, vlen);
val[vlen] = '\0';
NSString* key = [NSString stringWithUTF8String:(char*)localname];
NSString* nsval = [NSString stringWithUTF8String:(char*)val];
[dest setObject:nsval forKey:key];
/*
printf( " attribute: localname='%s', prefix='%s', uri=(%p)'%s', value='%s'\n",
localname,
prefix,
nsURI,
nsURI,
val);
*/
}
}
-(Sprite*) currentSprite
{
if (!currId || ![curLayerName isEqualToString:@"location_overlay"] )
return nil;
NSObject* obj = [sprites objectForKey:currId];
if (!obj)
{
Sprite* sprite = [Sprite new];
sprite.name = currId;
[sprites setObject:sprite forKey:currId];
[sprite release];
}
return (Sprite*)[sprites objectForKey:currId];
}
-(void) createTextFrag:(const xmlChar *)ch :(int) len;
{
TextFrag* frag = [[TextFrag alloc] init:self];
CGPoint location = CGPointMake([[curText valueForKey:@"x"] floatValue],
[[curText valueForKey:@"y"] floatValue]);
// location = CGPointMake(50,50);
char* val = malloc(len+1);
strncpy(val, (const char*)ch, len);
val[len] = '\0';
[frag wrap:val location:location style:currentStyle transform:localTransform.transform type:localTransform.type];
[fragments addObject:frag];
[frag release];
}
-(void) createPathFrag
{
PathFrag* frag = [[PathFrag alloc] init:self];
[frag wrap:currPath style:currentStyle transform:localTransform.transform type:localTransform.type];
[fragments addObject:frag];
currPath = nil;
Sprite* currentSprite = [self currentSprite];
if (currentSprite)
currentSprite.frag = frag;
[frag release];
}
-(void) drawPath
{
CGContextSaveGState(cgContext);
Sprite* info = (Sprite*)[sprites objectForKey:currId];
currentStyle.isHighlighted = info.isHighlighted;
[currentStyle drawPath:currPath withContext:cgContext];
CGContextRestoreGState(cgContext);
}
- (SVG_TRANS)applyTransformations:(const char *)transformations
{
float a=1;
float b=0;
float c=0;
float d=1;
float tx=0;
float ty=0;
enum TRANSFORMATION_TYPE type;
if (strncmp(transformations,"matrix",strlen("matrix")-1) == 0)
{
int scanned = sscanf(transformations+7,"%f,%f,%f,%f,%f,%f)",&a,&b,&c,&d,&tx,&ty);
if (scanned == 6)
{
type = AFFINE;
}
else
{
NSLog(@"Error scanning matrix tranform");
}
}
else if (strncmp(transformations,"scale",strlen("scale")-1) == 0)
{
int scanned = sscanf(transformations+6,"%f,%f)",&a,&d);
if (scanned == 2)
{
type = SCALE;
}
else
{
NSLog(@"Error scanning scale tranform");
}
}
else if (strncmp(transformations,"translate",strlen("translate")-1) == 0)
{
int scanned = sscanf(transformations+10,"%f,%f)",&tx,&ty);
if (scanned == 2)
{
type = TRANS;
}
else
{
NSLog(@"Error scanning matrix tranform");
}
}
else if (strncmp(transformations,"rotate",strlen("rotate")-1) == 0)
{
int scanned = sscanf(transformations+7,"%f)",&a);
if (scanned == 1)
{
type = ROT;
}
else
{
NSLog(@"Error scanning rotate tranform");
}
}
SVG_TRANS localTransformation;
localTransformation.transform = CGAffineTransformMake(a,b,c,d,tx,ty);
localTransformation.type = type;
[self applyTransformation:localTransformation];
return localTransformation;
}
- (void)applyTransformation:(SVG_TRANS)trans
{
currentScaleX = globalScaleX;
currentScaleY = globalScaleY;
CGContextConcatCTM(cgContext,CGAffineTransformInvert(transform));
transform = CGAffineTransformIdentity;
float a = trans.transform.a;
float b = trans.transform.b;
float c = trans.transform.c;
float d = trans.transform.d;
float tx = trans.transform.tx;
float ty = trans.transform.ty;
// Matrix
if (trans.type == AFFINE)
{
// local translation, with correction for global scale, and global offset
tx = tx*globalScaleX - offsetX;
ty = ty*globalScaleY - offsetY;
// transfer all scaling to single transformation
currentScaleX *= a;
currentScaleY *= d;
a = 1;
b /= d;
c /= a;
d = 1;
//move all scaling into separate transformation
if (currentScaleX != 1.0 || currentScaleY != 1.0)
transform = CGAffineTransformMakeScale(currentScaleX, currentScaleY);
CGAffineTransform matrixTransform = CGAffineTransformMake (a,b,c,d, tx, ty);
transform = CGAffineTransformConcat(transform, matrixTransform);
// Apply to graphics context
CGContextConcatCTM(cgContext,transform);
return;
}
// Scale
if (trans.type == SCALE)
{
currentScaleX *= a;
currentScaleY *= d;
}
if (currentScaleX != 1.0 || currentScaleY != 1.0)
transform = CGAffineTransformScale(transform, currentScaleX, currentScaleY);
// Rotate
if ( (trans.type == ROT) && a != 0)
{
transform = CGAffineTransformRotate(transform, a);
}
// Translate
float transX = -offsetX/currentScaleX;
float transY = -offsetY/currentScaleY;
if (trans.type == TRANS)
{
transX += tx;
transY += ty;
}
if (transX != 0 || transY != 0)
transform = CGAffineTransformTranslate(transform, transX, transY);
// Apply to graphics context
CGContextConcatCTM(cgContext,transform);
}
- (CGContextRef)createBitmapContext
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, (int)documentSize.width, (int)documentSize.height, 8, (int)documentSize.width*4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
return ctx;
}
void CGPathAddRoundRect(CGMutablePathRef currPath, CGRect rect, float radius)
{
CGPathMoveToPoint(currPath, NULL, rect.origin.x, rect.origin.y + radius);
CGPathAddLineToPoint(currPath, NULL, rect.origin.x, rect.origin.y + rect.size.height - radius);
CGPathAddArc(currPath, NULL, rect.origin.x + radius, rect.origin.y + rect.size.height - radius,
radius, M_PI / 1, M_PI / 2, 1);
CGPathAddLineToPoint(currPath, NULL, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height);
CGPathAddArc(currPath, NULL, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
CGPathAddLineToPoint(currPath, NULL, rect.origin.x + rect.size.width, rect.origin.y + radius);
CGPathAddArc(currPath, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + radius,
radius, 0.0f, -M_PI / 2, 1);
CGPathAddLineToPoint(currPath, NULL, rect.origin.x + radius, rect.origin.y);
CGPathAddArc(currPath, NULL, rect.origin.x + radius, rect.origin.y + radius, radius,
-M_PI / 2, M_PI, 1);
}
- (void)dealloc
{
[self cleanupAfterFinishedParsing];
[sprites release];
[fragments release];
[rootNode release];
[super dealloc];
}
-(void) cleanupAfterFinishedParsing
{
[defDict release];
defDict = nil;
[curPat release];
curPat = nil;
[curGradient release];
curGradient = nil;
[curFilter release];
curFilter = nil;
[curText release];
curText = nil;
CGContextRelease(cgContext);
cgContext = NULL;
[curFlowRegion release];
curFlowRegion = nil;
}
-(void) startElementSAX:(const xmlChar *)elt :(const xmlChar *)prefix :(const xmlChar *)URI :(int) nb_namespaces :(const xmlChar **)namespaces :(int) nb_attributes :(int) nb_defaulted :(const xmlChar **)attributes
{
/*
printf( "startElementNs: name = '%s' prefix = '%s' uri = (%p)'%s'\n", localname, prefix, URI, URI );
for ( int indexNamespace = 0; indexNamespace < nb_namespaces; ++indexNamespace )
{
const xmlChar *prefix = namespaces[indexNamespace*2];
const xmlChar *nsURI = namespaces[indexNamespace*2+1];
printf( " namespace: name='%s' uri=(%p)'%s'\n", prefix, nsURI, nsURI );
}
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *element = attributes[index];
const xmlChar *prefix = attributes[index+1];
const xmlChar *nsURI = attributes[index+2];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
unsigned char val[vlen + 1];
strncpy(val, valueBegin, vlen);
val[vlen] = '\0';
printf( " %sattribute: localname='%s', prefix='%s', uri=(%p)'%s', value='%s'\n",
indexAttribute >= (nb_attributes - nb_defaulted) ? "defaulted " : "",
element,
prefix,
nsURI,
nsURI,
val);
}
*/
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
const char* element = (const char*)elt;
// Path node
// -------------------------------------------------------------------------
if(strcmp(element,"path")==0) {
// For now, we'll ignore paths in definitions
if(inDefSection)
return;
// set the current scale, in case there is no transform
currentScaleX = globalScaleX;
currentScaleY = globalScaleY;
Sprite* currentSprite = nil;
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *attr = attributes[index];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
char val[vlen + 1];
strncpy(val, (const char*)valueBegin, vlen);
val[vlen] = '\0';
if (!currId && strcmp((const char*)attr,"id")==0)
{
currId = [NSString stringWithUTF8String:val] ;
currentSprite = [self currentSprite];
if ([currentSprite isInitialized])
currentSprite = nil;
}
else if (!currentStyle && strcmp((const char*)attr,"style")==0)
{
[currentStyle release];
currentStyle = [SVGStyle new];
[currentStyle setStyleContext:val withDefDict:defDict];
}
else if (strcmp((const char*)attr,"transform")==0)
{
localTransform = [self applyTransformations:val];
}
else if (strcmp((const char*)attr,"d")==0)
{
CFErrorRef error;
currPath = CGPathCreateFromSVG(val, &error);
}
else if (strcmp((const char*)attr,"fill")==0)
{
localTransform = [self applyTransformations:val];
currentStyle.doFill = YES;
currentStyle.fillType = @"solid";
[currentStyle setFillColorFromAttribute:[NSString stringWithUTF8String:val]];
}
}
if (currentSprite)
{
//transform to non-offset image coordinates
CGAffineTransform temp = CGAffineTransformTranslate(transform, offsetX/currentScaleX, offsetY/currentScaleY);
//scale down to relative image coordinates
temp = CGAffineTransformConcat(temp, CGAffineTransformMakeScale(1.0/(globalScaleX*width),1.0/(globalScaleY*height) ));
[currentSprite calcBoundingBox:CGPathGetBoundingBox(currPath) withTransform:temp];
[rootNode addSprite:currentSprite];
}
[self createPathFrag];
}
// -------------------------------------------------------------------------
else if(strcmp(element,"svg")==0) {
if (firstRender)
{
width = -1;
height = -1;
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *attr = attributes[index];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
char val[vlen + 1];
strncpy(val, (char *)valueBegin, vlen);
val[vlen] = '\0';
if (width == -1 && strcmp((const char*)attr,"width")==0)
{
width = atof(val);
}
else if (height == -1 && strcmp((const char*)attr,"height")==0)
{
height = atof(val);
}
}
documentSize = viewFrame.size;
float sx = viewFrame.size.width/width;
float sy = viewFrame.size.height/height;
float scale = fmax(sx,sy);
initialScaleX =scale;
initialScaleY = scale;
globalScaleX = initialScaleX;
globalScaleY = initialScaleY;
[self doCenter:CGPointMake(0.5,0.5) withBoundingBox:CGSizeMake(1,1)];
firstRender = NO;
}
[self prepareToDraw];
}
// Group node
// -------------------------------------------------------------------------
else if(strcmp(element,"g")==0) {
[curLayer release];
curLayer = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:curLayer];
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *attr = attributes[index];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
char val[vlen + 1];
strncpy(val, (const char*)valueBegin, vlen);
val[vlen] = '\0';
if (!currId && strcmp((const char*)attr,"id")==0)
{
currId = [NSString stringWithUTF8String:val] ;
self.curLayerName = currId;
}
else if (!currentStyle && strcmp((const char*)attr,"style")==0)
{
currentStyle = [SVGStyle new];
[currentStyle setStyleContext:val withDefDict:defDict];
}
else if (strcmp((const char*)attr,"transform")==0)
{
[self applyTransformations:val];
}
}
}
else if(strcmp(element,"defs")==0) {
defDict = [[NSMutableDictionary alloc] init];
inDefSection = YES;
}
else if(strcmp(element,"pattern")==0) {
[curPat release];
curPat = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:curPat];
NSMutableArray* imagesArray = [NSMutableArray new];
[curPat setObject:imagesArray forKey:@"images"];
[imagesArray release];
[curPat setObject:@"pattern" forKey:@"type"];
}
else if(strcmp(element,"image")==0) {
NSMutableDictionary *imageDict = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:imageDict];
[[curPat objectForKey:@"images"] addObject:imageDict];
[imageDict release];
}
else if(strcmp(element,"linearGradient")==0) {
[curGradient release];
curGradient = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:curGradient];
[curGradient setObject:@"linearGradient" forKey:@"type"];
NSMutableArray* stopsArray = [NSMutableArray new];
[curGradient setObject:stopsArray forKey:@"stops"];
[stopsArray release];
}
else if(strcmp(element,"stop")==0) {
NSMutableDictionary *stopDict = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:stopDict];
[[curGradient objectForKey:@"stops"] addObject:stopDict];
[stopDict release];
}
else if(strcmp(element,"radialGradient")==0) {
[curGradient release];
curGradient = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:curGradient];
[curGradient setObject:@"radialGradient" forKey:@"type"];
}
else if(strcmp(element,"filter")==0) {
[curFilter release];
curFilter = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:curFilter];
NSMutableArray* gaussianBlursArray = [NSMutableArray new];
[curFilter setObject:gaussianBlursArray forKey:@"feGaussianBlurs"];
[gaussianBlursArray release];
}
else if(strcmp(element,"feGaussianBlur")==0) {
NSMutableDictionary *blurDict = [[NSMutableDictionary alloc] init];
[self copyAttributes:attributes size:nb_attributes toDest:blurDict];
[[curFilter objectForKey:@"feGaussianBlurs"] addObject:blurDict];
[blurDict release];
}
// Text node
// -------------------------------------------------------------------------
else if(strcmp(element,"text")==0) {
if(inDefSection)
return;
if(curText)
[curText release];
NSMutableDictionary* temp = [NSMutableDictionary new];
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *attr = attributes[index];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
char val[vlen + 1];
strncpy(val, (const char*)valueBegin, vlen);
val[vlen] = '\0';
if (strcmp((const char*)attr,"style")==0)
{
if (!currentStyle)
{
currentStyle = [SVGStyle new];
[currentStyle setStyleContext:val withDefDict:defDict];
}
}
else if (strcmp((const char*)attr,"transform")==0)
{
localTransform = [self applyTransformations:val];
}
else if (strcmp((const char*)attr,"id")==0)
{
[temp setObject:[NSString stringWithUTF8String:val] forKey:@"id"];
}
else if (strcmp((const char*)attr,"x")==0)
{
[temp setObject:[NSString stringWithUTF8String:val] forKey:@"x"];
}
else if (strcmp((const char*)attr,"y")==0)
{
[temp setObject:[NSString stringWithUTF8String:val] forKey:@"y"];
}
else if (strcmp((const char*)attr,"width")==0)
{
[temp setObject:[NSString stringWithUTF8String:val] forKey:@"width"];
}
else if (strcmp((const char*)attr,"height")==0)
{
[temp setObject:[NSString stringWithUTF8String:val] forKey:@"height"];
}
}
curText = temp;
}
// TSpan node
// Assumed to always be a child of a Text node
// ---------------------------------------------------------------------
else if(strcmp(element,"tspan")==0) {
if(inDefSection)
return;
unsigned int index = 0;
for ( int indexAttribute = 0;
indexAttribute < nb_attributes;
++indexAttribute, index += 5 )
{
const xmlChar *attr = attributes[index];
const xmlChar *valueBegin = attributes[index+3];
const xmlChar *valueEnd = attributes[index+4];
int vlen = valueEnd - valueBegin;
char val[vlen + 1];
strncpy(val, (const char*)valueBegin, vlen);
val[vlen] = '\0';
if (strcmp((const char*)attr,"style")==0)
{
[currentStyle setStyleContext:val withDefDict:defDict];
}
}
}
// FlowRegion node
// -------------------------------------------------------------------------
else if(strcmp(element,"flowRegion")==0) {
[curFlowRegion release];
curFlowRegion = [NSDictionary new];
}
//ToDo
// else if(strcmp(element,"feColorMatrix"]) {
//
// }
// else if(strcmp(element,"feFlood"]) {
//
// }
// else if(strcmp(element,"feBlend"]) {
//