Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for viewBox attribute #185

Merged
merged 8 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file. Items under

## [Unreleased]

## [2.7.0]
### New Features
- Support for `viewBox` tag. [Alessio Prosperi](https://github.com/NeedNap)
and [Ariel Elkin](https://github.com/arielelkin) [#181](https://github.com/pocketsvg/PocketSVG/pull/181) and [#185](https://github.com/pocketsvg/PocketSVG/pull/185).

## [2.6.0]
### New Features
- Xcode 12 and SPM Support. [Ariel Elkin](https://github.com/arielelkin), [#178](https://github.com/pocketsvg/PocketSVG/pull/178/)
Expand Down
5 changes: 4 additions & 1 deletion Demos/Demo-macOS/Demo-macOS/MacDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {}
class DemoController: NSViewController {
override func loadView() {
let url = Bundle.main.url(forResource: "iceland", withExtension: "svg")!
view = SVGImageView.init(contentsOf: url)
let j = SVGImageView.init(contentsOf: url)
print(j.viewBox == .null)
view = j

view.frame = NSRect(x: 0, y: 0, width: 300, height: 200)
}
}
2 changes: 1 addition & 1 deletion PocketSVG.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pod::Spec.new do |s|
s.summary = "Easily convert your SVG files into CGPaths, CAShapeLayers, and UIBezierPaths"
s.homepage = "https://github.com/pocketsvg/PocketSVG"
s.authors = { "Ponderwell, Fjölnir Ásgeirsson, Ariel Elkin, and Contributors" => "https://github.com/pocketsvg/PocketSVG" }
s.ios.deployment_target = '8.1'
s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.10'
s.license = {
:type => 'MIT',
Expand Down
10 changes: 10 additions & 0 deletions Sources/SVGBezierPath.mm
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ - (void)applyTransform:(CGAffineTransform)cgTransform
[self transformUsingAffineTransform:transform];
}
#endif

- (CGRect) viewBox {
NSString *viewBoxString = _svgAttributes[@"viewBox"];
NSArray *stringArray = [viewBoxString componentsSeparatedByString:@" "];
if (stringArray && stringArray.count == 4) {
return CGRectMake(CGFloat([stringArray[0] doubleValue]), CGFloat([stringArray[1] doubleValue]), CGFloat([stringArray[2] doubleValue]), CGFloat([stringArray[3] doubleValue]));
} else {
return CGRectNull;
}
}
@end


Expand Down
6 changes: 5 additions & 1 deletion Sources/SVGEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
@interface SVGAttributeSet () {
@public
NSMapTable *_attributes;
CGRect _viewBox;
}
@end

Expand Down Expand Up @@ -118,7 +119,7 @@ @interface SVGAttributeSet () {
else if(type == XML_READER_TYPE_END_ELEMENT)
--depthWithinUnknownElement;
} else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "svg") == 0) {
// recognize the root svg element but we don't need to do anything with it
pushGroup(readAttributes());
} else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "path") == 0)
path = readPathTag();
else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "polyline") == 0)
Expand Down Expand Up @@ -996,6 +997,9 @@ - (id)mutableCopyWithZone:(NSZone *)zone
}
return copy;
}
- (CGRect) viewBox {
return _viewBox;
}
@end

@implementation SVGMutableAttributeSet
Expand Down
4 changes: 4 additions & 0 deletions Sources/SVGImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,8 @@ - (CGSize)intrinsicContentSize
{
return [self sizeThatFits:CGSizeZero];
}

- (CGRect) viewBox {
return _svgLayer.viewBox;
}
@end
4 changes: 4 additions & 0 deletions Sources/SVGLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ - (void)dealloc
CGColorRelease(_strokeColor);
}

- (CGRect) viewBox {
return [[_paths firstObject] viewBox];
}

- (void)setPaths:(NSArray<SVGBezierPath*> *)paths
{
[self willChangeValueForKey:@"paths"];
Expand Down
7 changes: 7 additions & 0 deletions Sources/include/SVGBezierPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ FOUNDATION_EXTERN void SVGDrawPathsWithBlock(NSArray<SVGBezierPath*> * const pat
*/
- (SVGBezierPath *)pathBySettingSVGAttributes:(NSDictionary *)attributes;

/*!
* @brief The value of the SVG's viewBox attribute, expressed as a CGRect. If there is
* no viewBox attribute, this property will be CGRect.null
*
*/
@property(nonatomic, readonly) CGRect viewBox;

#if !TARGET_OS_IPHONE
@property(nonatomic, readonly) CGPathRef CGPath;
#endif
Expand Down
1 change: 1 addition & 0 deletions Sources/include/SVGEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ NSString *SVGStringFromCGPaths(NSArray *paths, SVGAttributeSet *attributes);

@interface SVGAttributeSet : NSObject <NSCopying, NSMutableCopying>
- (NSDictionary<NSString*,id> *)attributesForPath:(CGPathRef)path;
@property(nonatomic, readonly) CGRect viewBox;
@end
@interface SVGMutableAttributeSet : SVGAttributeSet
- (void)setAttributes:(NSDictionary<NSString*,id> *)attributes forPath:(CGPathRef)path;
Expand Down
7 changes: 7 additions & 0 deletions Sources/include/SVGImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ IB_DESIGNABLE
*/
@property(nonatomic) IBInspectable BOOL scaleLineWidth;

/*!
* @brief The value of the SVG's viewBox attribute, expressed as a CGRect. If there is
* no viewBox attribute, this property will be CGRect.null
*
*/
@property(nonatomic, readonly) CGRect viewBox;

@end
NS_ASSUME_NONNULL_END
8 changes: 8 additions & 0 deletions Sources/include/SVGLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property(nonatomic) BOOL scaleLineWidth;


/*!
* @brief The value of the SVG's viewBox attribute, expressed as a CGRect. If there is
* no viewBox attribute, this property will be CGRect.null
*
*/
@property(nonatomic, readonly) CGRect viewBox;

@end
NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion Tests/PocketSVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class PocketSVGTests: XCTestCase {
XCTAssert(paths.count == 1)
let rectanglePath = paths[0]

let representation = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100\" height=\"100\">\n <path stroke=\"#fff000\" id=\"Page-1\" stroke-width=\"2px\" x=\"0\" sketch:type=\"MSPage\" rx=\"10px\" y=\"0\" fill-rule=\"evenodd\" width=\"100px\" fill=\"#ffffff\" fill-opacity=\"0\" ry=\"10px\" height=\"100px\" d=\"M100,50L100,90C100,95.523,95.523,100,90,100L10,100C4.477,100,0,95.523,0,90L0,10C0,4.477,4.477,-0,10,0L90,0C95.523,0,100,4.477,100,10Z\"/>\n\n</svg>\n"
let representation = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100\" height=\"100\">\n <path stroke=\"#fff000\" id=\"Page-1\" stroke-width=\"2px\" version=\"1.1\" sketch:type=\"MSPage\" x=\"0\" width=\"100px\" fill=\"#ffffff\" fill-opacity=\"0\" fill-rule=\"evenodd\" y=\"0\" rx=\"10px\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:sketch=\"http://www.bohemiancoding.com/sketch/ns\" ry=\"10px\" height=\"100px\" viewBox=\"0 0 300 300\" xmlns=\"http://www.w3.org/2000/svg\" d=\"M100,50L100,90C100,95.523,95.523,100,90,100L10,100C4.477,100,0,95.523,0,90L0,10C0,4.477,4.477,-0,10,0L90,0C95.523,0,100,4.477,100,10Z\"/>\n\n</svg>\n"

XCTAssertEqual(rectanglePath.svgRepresentation, representation)
}
Expand Down