Skip to content

Commit 143fde2

Browse files
author
Steven Fusco
committed
merging in changes from @adamgit
2 parents fea5874 + 0d3dba3 commit 143fde2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1769
-1229
lines changed

Core/CGPathAdditions.h

+6
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
// Copyright Matt Rajca 2011. All rights reserved.
66
//
77

8+
#if TARGET_OS_IPHONE
9+
10+
#import <UIKit/UIKit.h>
11+
12+
#endif
13+
814
CGPathRef CGPathCreateByOffsettingPath (CGPathRef aPath, CGFloat x, CGFloat y);

Core/SVGDocument+CA.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ - (CALayer *)layerTree {
4444
}
4545

4646
- (CALayer *)layerWithElement:(SVGElement <SVGLayeredElement> *)element {
47-
CALayer *layer = [element layer];
47+
CALayer *layer = [element newLayer];
4848

4949
if (![element.children count]) {
5050
return layer;

Core/SVGDocument.h

100755100644
+5-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
#import "SVGGroupElement.h"
1111

12+
#import "SVGParser.h"
13+
1214
#if NS_BLOCKS_AVAILABLE
1315
typedef void (^SVGElementAggregationBlock)(SVGElement < SVGLayeredElement > * layeredElement);
1416
#endif
1517

1618
@class SVGDefsElement;
1719

18-
@interface SVGDocument : SVGElement < SVGLayeredElement > { }
20+
@interface SVGDocument : SVGElement < SVGLayeredElement > {
21+
}
1922

2023
// only absolute widths and heights are supported (no percentages)
2124
@property (nonatomic, readonly) CGFloat width;
@@ -30,6 +33,7 @@ typedef void (^SVGElementAggregationBlock)(SVGElement < SVGLayeredElement > * la
3033
/*! from the SVG spec, each "g" tag in the XML is a separate "group of graphics things" */
3134
@property (nonatomic, retain) NSDictionary *graphicsGroups;
3235

36+
+ (void) addSVGParserExtension:(NSObject<SVGParserExtension>*) extension;
3337
+ (id)documentNamed:(NSString *)name; // 'name' in mainBundle
3438
+ (id)documentWithContentsOfFile:(NSString *)aPath;
3539

Core/SVGDocument.m

100755100644
+24-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#import "SVGTitleElement.h"
1515
#import "SVGPathElement.h"
1616

17+
#import "SVGParserSVG.h"
18+
1719
@interface SVGDocument ()
1820

1921
@property (nonatomic, copy) NSString *version;
@@ -35,6 +37,17 @@ @implementation SVGDocument
3537

3638
@dynamic title, desc, defs;
3739

40+
static NSMutableArray* _parserExtensions;
41+
+ (void) addSVGParserExtension:(NSObject<SVGParserExtension>*) extension
42+
{
43+
if( _parserExtensions == nil )
44+
{
45+
_parserExtensions = [[NSMutableArray array] retain];
46+
}
47+
48+
[_parserExtensions addObject:extension];
49+
}
50+
3851
/* TODO: parse 'viewBox' */
3952

4053
+ (id)documentNamed:(NSString *)name {
@@ -102,6 +115,12 @@ - (BOOL)parseFileAtPath:(NSString *)aPath {
102115
NSError *error = nil;
103116

104117
SVGParser *parser = [[SVGParser alloc] initWithPath:aPath document:self];
118+
SVGParserSVG *subParserSVG = [[[SVGParserSVG alloc] init] autorelease];
119+
[parser.parserExtensions addObject:subParserSVG];
120+
for( NSObject<SVGParserExtension>* extension in _parserExtensions )
121+
{
122+
[parser.parserExtensions addObject:extension];
123+
}
105124

106125
if (![parser parse:&error]) {
107126
NSLog(@"Parser error: %@", error);
@@ -115,11 +134,12 @@ - (BOOL)parseFileAtPath:(NSString *)aPath {
115134
return YES;
116135
}
117136

118-
- (CALayer *)layer {
119-
CALayer *layer = [CALayer layer];
120-
layer.frame = CGRectMake(0.0f, 0.0f, _width, _height);
137+
- (CALayer *)newLayer {
138+
139+
CALayer* _layer = [[CALayer layer] retain];
140+
_layer.frame = CGRectMake(0.0f, 0.0f, _width, _height);
121141

122-
return layer;
142+
return _layer;
123143
}
124144

125145
- (void)layoutLayer:(CALayer *)layer { }

Core/SVGElement.h

+20-3
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,43 @@
1414
NSMutableArray *_children;
1515
}
1616

17-
@property (nonatomic, readonly) __weak SVGDocument *document;
17+
/*! This is used when generating CALayer objects, to store the id of the SVGElement that created the CALayer */
18+
#define kSVGElementIdentifier @"SVGElementIdentifier"
19+
20+
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
21+
@property (nonatomic, readonly) SVGDocument *document;
22+
#else
23+
@property (nonatomic, readonly) __weak SVGDocument *document;
24+
#endif
1825

1926
@property (nonatomic, readonly) NSArray *children;
2027
@property (nonatomic, readonly, copy) NSString *stringValue;
2128
@property (nonatomic, readonly) NSString *localName;
2229

2330
@property (nonatomic, readwrite, retain) NSString *identifier; // 'id' is reserved
2431

32+
@property (nonatomic, retain) NSMutableArray* metadataChildren;
33+
2534
+ (BOOL)shouldStoreContent; // to optimize parser, default is NO
2635

2736
- (id)initWithDocument:(SVGDocument *)aDocument name:(NSString *)name;
2837

2938
- (void)loadDefaults; // should be overriden to set element defaults
3039

31-
@end
40+
/*! Parser uses this to add non-rendering-SVG XML tags to the element they were embedded in */
41+
- (void) addMetadataChild:(NSObject*) child;
3242

43+
@end
3344

3445
@protocol SVGLayeredElement < NSObject >
3546

36-
- (CALayer *)layer;
47+
/*!
48+
NB: the returned layer has - as its "name" property - the "identifier" property of the SVGElement that created it;
49+
but that can be overwritten by applications (for valid reasons), so we ADDITIONALLY store the identifier into a
50+
custom key - kSVGElementIdentifier - on the CALayer. Because it's a custom key, it's (almost) guaranteed not to be
51+
overwritten / altered by other application code
52+
*/
53+
- (CALayer *)newLayer;
3754
- (void)layoutLayer:(CALayer *)layer;
3855

3956
@end

Core/SVGElement.m

100755100644
+19-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ @interface SVGElement ()
1313

1414
@end
1515

16-
16+
/*! main class implementation for the base SVGElement: NOTE: in practice, most of the interesting
17+
stuff happens in subclasses, e.g.:
18+
19+
SVGShapeElement
20+
SVGGroupElement
21+
SVGImageElement
22+
SVGLineElement
23+
SVGPathElement
24+
...etc
25+
*/
1726
@implementation SVGElement
1827

1928
@synthesize document = _document;
@@ -24,6 +33,8 @@ @implementation SVGElement
2433

2534
@synthesize identifier = _identifier;
2635

36+
@synthesize metadataChildren;
37+
2738
+ (BOOL)shouldStoreContent {
2839
return NO;
2940
}
@@ -33,6 +44,7 @@ - (id)init {
3344
if (self) {
3445
[self loadDefaults];
3546
_children = [[NSMutableArray alloc] init];
47+
self.metadataChildren = [NSMutableArray array];
3648
}
3749
return self;
3850
}
@@ -47,6 +59,7 @@ - (id)initWithDocument:(SVGDocument *)aDocument name:(NSString *)name {
4759
}
4860

4961
- (void)dealloc {
62+
self.metadataChildren = nil;
5063
[_children release];
5164
[_stringValue release];
5265
[_localName release];
@@ -63,6 +76,11 @@ - (void)addChild:(SVGElement *)element {
6376
[_children addObject:element];
6477
}
6578

79+
-(void) addMetadataChild:(NSObject*) child
80+
{
81+
[self.metadataChildren addObject:child];
82+
}
83+
6684
- (void)parseAttributes:(NSDictionary *)attributes {
6785
// to be overriden by subclasses
6886
// make sure super implementation is called

Core/SVGGroupElement.h

100755100644
File mode changed.

Core/SVGGroupElement.m

+19-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88
#import "SVGGroupElement.h"
99

1010
#import "SVGDocument.h"
11+
1112
#import "SVGElement+Private.h"
13+
#import "CALayerWithChildHitTest.h"
1214

1315
@implementation SVGGroupElement
1416

1517
@synthesize opacity = _opacity;
1618

19+
- (void)dealloc {
20+
21+
[super dealloc];
22+
}
23+
1724
- (void)loadDefaults {
1825
_opacity = 1.0f;
1926
}
@@ -28,17 +35,20 @@ - (void)parseAttributes:(NSDictionary *)attributes {
2835
}
2936
}
3037

31-
- (CALayer *)layer {
32-
CALayer *layer = [CALayer layer];
33-
layer.name = self.identifier;
34-
layer.opacity = _opacity;
38+
- (CALayer *)newLayer {
3539

36-
if ([layer respondsToSelector:@selector(setShouldRasterize:)]) {
37-
[layer performSelector:@selector(setShouldRasterize:)
38-
withObject:[NSNumber numberWithBool:YES]];
39-
}
40+
CALayer* _layer = [[CALayerWithChildHitTest layer] retain];// [[CALayer layer] retain];
41+
42+
_layer.name = self.identifier;
43+
[_layer setValue:self.identifier forKey:kSVGElementIdentifier];
44+
_layer.opacity = _opacity;
45+
46+
if ([_layer respondsToSelector:@selector(setShouldRasterize:)]) {
47+
[_layer performSelector:@selector(setShouldRasterize:)
48+
withObject:[NSNumber numberWithBool:YES]];
49+
}
4050

41-
return layer;
51+
return _layer;
4252
}
4353

4454
- (void)layoutLayer:(CALayer *)layer {

Core/SVGImageElement.m

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#if TARGET_OS_IPHONE
1212

13+
#import <UIKit/UIKit.h>
14+
1315
#else
1416
#endif
1517

@@ -80,10 +82,11 @@ - (void)parseAttributes:(NSDictionary *)attributes {
8082
}
8183
}
8284

83-
- (CALayer *)layer {
84-
__block CALayer *layer = [CALayer layer];
85+
- (CALayer *)newLayer {
86+
__block CALayer *layer = [[CALayer layer] retain];
8587

8688
layer.name = self.identifier;
89+
[layer setValue:self.identifier forKey:kSVGElementIdentifier];
8790
layer.frame = CGRectMake(_x, _y, _width, _height);
8891

8992
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

Core/SVGParser.h

100755100644
+18-1
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,36 @@
77

88
@class SVGDocument;
99

10+
@protocol SVGParserExtension <NSObject>
11+
-(BOOL) createdItemShouldStoreContent:(NSObject*) item;
12+
- (NSObject*)handleStartElement:(NSString *)name document:(SVGDocument*) document xmlns:(NSString*) prefix attributes:(NSMutableDictionary *)attributes;
13+
-(void) addChildObject:(NSObject*)child toObject:(NSObject*)parent;
14+
-(void) parseContent:(NSMutableString*) content forItem:(NSObject*) item;
15+
16+
-(NSArray*) supportedNamespaces;
17+
-(NSArray*) supportedTags;
18+
@end
19+
1020
@interface SVGParser : NSObject {
1121
@private
1222
NSString *_path;
1323
BOOL _failed;
1424
BOOL _storingChars;
1525
NSMutableString *_storedChars;
1626
NSMutableArray *_elementStack;
27+
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
28+
SVGDocument *_document;
29+
#else
1730
__weak SVGDocument *_document;
18-
NSMutableDictionary *_graphicsGroups;
31+
#endif
1932
}
2033

34+
@property(nonatomic,retain) NSMutableArray* parserExtensions;
35+
2136
- (id)initWithPath:(NSString *)aPath document:(SVGDocument *)document;
2237

2338
- (BOOL)parse:(NSError **)outError;
2439

40+
+(NSDictionary *) NSDictionaryFromCSSAttributes: (NSString *)css;
41+
2542
@end

0 commit comments

Comments
 (0)