Skip to content

Instantly share code, notes, and snippets.

@reklis
Created November 4, 2011 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reklis/1339437 to your computer and use it in GitHub Desktop.
Save reklis/1339437 to your computer and use it in GitHub Desktop.
SVG Text Element parsing
diff --git a/Core/SVGParser.m b/Core/SVGParser.m
index a2a3106..7726901 100644
--- a/Core/SVGParser.m
+++ b/Core/SVGParser.m
@@ -23,6 +23,7 @@
#import "SVGPolylineElement.h"
#import "SVGRectElement.h"
#import "SVGTitleElement.h"
+#import "SVGTextElement.h"
@implementation SVGParser
@@ -63,6 +64,7 @@ static NSDictionary *elementMap;
[SVGPolygonElement class], @"polygon",
[SVGPolylineElement class], @"polyline",
[SVGRectElement class], @"rect",
+ [SVGTextElement class], @"text",
[SVGTitleElement class], @"title", nil] retain];
}
}
diff --git a/Core/SVGTextElement.h b/Core/SVGTextElement.h
new file mode 100644
index 0000000..82a5c8a
--- /dev/null
+++ b/Core/SVGTextElement.h
@@ -0,0 +1,34 @@
+//
+// SVGTextElement.h
+// SVGPad
+//
+// Created by Steven Fusco on 11/4/11.
+// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "SVGElement.h"
+
+/**
+ http://www.w3.org/TR/2011/REC-SVG11-20110816/text.html#TextElement
+ */
+@interface SVGTextElement : SVGElement <SVGLayeredElement>
+
+@property (readwrite,nonatomic,assign) CGFloat x;
+@property (readwrite,nonatomic,assign) CGFloat y;
+@property (readwrite,nonatomic,retain) NSString* fontFamily;
+@property (readwrite,nonatomic,assign) CGFloat fontSize;
+
+// TODO: class
+// TODO: style
+// TODO: externalResourcesRequired
+// TODO: transform
+// TODO: lengthAdjust
+// TODO: rotate
+// TODO: textLength
+// TODO: dx
+// TODO: dy
+// TODO: fill
+
+@end
diff --git a/Core/SVGTextElement.m b/Core/SVGTextElement.m
new file mode 100644
index 0000000..7fd4fac
--- /dev/null
+++ b/Core/SVGTextElement.m
@@ -0,0 +1,69 @@
+//
+// SVGTextElement.m
+// SVGPad
+//
+// Created by Steven Fusco on 11/4/11.
+// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "SVGTextElement.h"
+
+@implementation SVGTextElement
+
+@synthesize x = _x;
+@synthesize y = _y;
+@synthesize fontFamily = _fontFamily;
+@synthesize fontSize = _fontSize;
+
+- (void)dealloc {
+ [_fontFamily release];
+ [super dealloc];
+}
+
+- (void)parseAttributes:(NSDictionary *)attributes {
+ id value = nil;
+
+ if ((value = [attributes objectForKey:@"x"])) {
+ _x = [value floatValue];
+ }
+
+ if ((value = [attributes objectForKey:@"y"])) {
+ _y = [value floatValue];
+ }
+
+ // TODO: class
+ // TODO: style
+ // TODO: externalResourcesRequired
+ // TODO: transform
+ // TODO: lengthAdjust
+ // TODO: rotate
+ // TODO: textLength
+ // TODO: dx
+ // TODO: dy
+ // TODO: fill
+}
+
+- (CALayer *)layer {
+ NSString* textToDraw = self.stringValue;
+ UIFont* fontToDraw = [UIFont fontWithName:_fontFamily
+ size:_fontSize];
+ CGSize sizeOfTextRect = [textToDraw sizeWithFont:fontToDraw];
+
+ CATextLayer *label = [[[CATextLayer alloc] init] autorelease];
+ [label setName:self.identifier];
+ [label setFont:_fontFamily];
+ [label setFontSize:_fontSize];
+ [label setFrame:CGRectMake(_x, _y, sizeOfTextRect.width, sizeOfTextRect.height)];
+ [label setString:textToDraw];
+ [label setAlignmentMode:kCAAlignmentLeft];
+ [label setForegroundColor:[[UIColor blackColor] CGColor]];
+
+ return label;
+}
+
+- (void)layoutLayer:(CALayer *)layer
+{
+
+}
+
+@end
diff --git a/SVGPad/SVGPad.xcodeproj/project.pbxproj b/SVGPad/SVGPad.xcodeproj/project.pbxproj
index a83cdaf..5687973 100755
--- a/SVGPad/SVGPad.xcodeproj/project.pbxproj
+++ b/SVGPad/SVGPad.xcodeproj/project.pbxproj
@@ -17,6 +17,7 @@
2892E4100DC94CBA00A64D0F /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2892E40F0DC94CBA00A64D0F /* CoreGraphics.framework */; };
28AD73600D9D9599002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD735F0D9D9599002E5188 /* MainWindow.xib */; };
3B5C26B81448839E006FD609 /* SVGImageElement.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B5C26B71448839E006FD609 /* SVGImageElement.m */; };
+ 3B82A5F0146427AC007E40A9 /* SVGTextElement.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B82A5EF146427AC007E40A9 /* SVGTextElement.m */; };
3BA98E15142B9CC3003EA1FA /* SVGPattern.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA98E14142B9CC3003EA1FA /* SVGPattern.m */; };
3BA98E19142B9CFB003EA1FA /* SVGPathView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA98E18142B9CFB003EA1FA /* SVGPathView.m */; };
3BF31C2C14229647002A4B06 /* Map.svg in Resources */ = {isa = PBXBuildFile; fileRef = 3BF31C2B14229647002A4B06 /* Map.svg */; };
@@ -63,6 +64,8 @@
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
3B5C26B61448839E006FD609 /* SVGImageElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGImageElement.h; sourceTree = "<group>"; };
3B5C26B71448839E006FD609 /* SVGImageElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGImageElement.m; sourceTree = "<group>"; };
+ 3B82A5EE146427AC007E40A9 /* SVGTextElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGTextElement.h; sourceTree = "<group>"; };
+ 3B82A5EF146427AC007E40A9 /* SVGTextElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGTextElement.m; sourceTree = "<group>"; };
3BA98E13142B9CC3003EA1FA /* SVGPattern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGPattern.h; sourceTree = "<group>"; };
3BA98E14142B9CC3003EA1FA /* SVGPattern.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGPattern.m; sourceTree = "<group>"; };
3BA98E17142B9CFB003EA1FA /* SVGPathView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGPathView.h; sourceTree = "<group>"; };
@@ -203,10 +206,7 @@
C972073D1347794300DE3BE3 /* SVGKit Core */ = {
isa = PBXGroup;
children = (
- 3B5C26B61448839E006FD609 /* SVGImageElement.h */,
- 3B5C26B71448839E006FD609 /* SVGImageElement.m */,
- 3BA98E13142B9CC3003EA1FA /* SVGPattern.h */,
- 3BA98E14142B9CC3003EA1FA /* SVGPattern.m */,
+ C97207511347794300DE3BE3 /* SVGKit.h */,
C972073E1347794300DE3BE3 /* CGPathAdditions.h */,
C972073F1347794300DE3BE3 /* CGPathAdditions.m */,
C97207401347794300DE3BE3 /* SVGCircleElement.h */,
@@ -226,13 +226,16 @@
C972074E1347794300DE3BE3 /* SVGEllipseElement.m */,
C972074F1347794300DE3BE3 /* SVGGroupElement.h */,
C97207501347794300DE3BE3 /* SVGGroupElement.m */,
- C97207511347794300DE3BE3 /* SVGKit.h */,
+ 3B5C26B61448839E006FD609 /* SVGImageElement.h */,
+ 3B5C26B71448839E006FD609 /* SVGImageElement.m */,
C97207521347794300DE3BE3 /* SVGLineElement.h */,
C97207531347794300DE3BE3 /* SVGLineElement.m */,
C97207541347794300DE3BE3 /* SVGParser.h */,
C97207551347794300DE3BE3 /* SVGParser.m */,
C97207561347794300DE3BE3 /* SVGPathElement.h */,
C97207571347794300DE3BE3 /* SVGPathElement.m */,
+ 3BA98E13142B9CC3003EA1FA /* SVGPattern.h */,
+ 3BA98E14142B9CC3003EA1FA /* SVGPattern.m */,
C97207581347794300DE3BE3 /* SVGPolygonElement.h */,
C97207591347794300DE3BE3 /* SVGPolygonElement.m */,
C972075A1347794300DE3BE3 /* SVGPolylineElement.h */,
@@ -242,6 +245,8 @@
C972075E1347794300DE3BE3 /* SVGShapeElement+Private.h */,
C972075F1347794300DE3BE3 /* SVGShapeElement.h */,
C97207601347794300DE3BE3 /* SVGShapeElement.m */,
+ 3B82A5EE146427AC007E40A9 /* SVGTextElement.h */,
+ 3B82A5EF146427AC007E40A9 /* SVGTextElement.m */,
C97207611347794300DE3BE3 /* SVGTitleElement.h */,
C97207621347794300DE3BE3 /* SVGTitleElement.m */,
C97207631347794300DE3BE3 /* SVGUtils.h */,
@@ -301,7 +306,7 @@
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
attributes = {
- LastUpgradeCheck = 0410;
+ LastUpgradeCheck = 0420;
};
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "SVGPad" */;
compatibilityVersion = "Xcode 3.2";
@@ -370,6 +375,7 @@
3BA98E15142B9CC3003EA1FA /* SVGPattern.m in Sources */,
3BA98E19142B9CFB003EA1FA /* SVGPathView.m in Sources */,
3B5C26B81448839E006FD609 /* SVGImageElement.m in Sources */,
+ 3B82A5F0146427AC007E40A9 /* SVGTextElement.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment