From 54db9e8550222d8c40115808443af60e8a3b1d90 Mon Sep 17 00:00:00 2001 From: Chetan Padia Date: Thu, 13 Nov 2014 14:38:29 +0000 Subject: [PATCH 1/3] ignore percentage 'width' and 'height' attributes on --- .../Unported or Partial DOM/SVGSVGElement.m | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m b/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m index 42fc27975..686e77ff8 100644 --- a/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m +++ b/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m @@ -130,7 +130,17 @@ We would need to put extra (NON STANDARD) properties on SVGDocument, for the "vi NSString* stringWidth = [self getAttribute:@"width"]; NSString* stringHeight = [self getAttribute:@"height"]; - if( stringWidth == nil || stringWidth.length < 1 ) + /** + Ignore percetage width and heights which are only used when rendering in HTML + */ + + if ([stringWidth containsString:@"%"]) + stringWidth = nil; + + if ([stringHeight containsString:@"%"]) + stringHeight = nil; + + if( stringWidth == nil || stringWidth.length < 1 ) self.width = nil; // i.e. undefined else self.width = [SVGLength svgLengthFromNSString:[self getAttribute:@"width"]]; From d5591f1daaf78ed7bf6f82ae8838c87696fc3732 Mon Sep 17 00:00:00 2001 From: Chetan Padia Date: Thu, 13 Nov 2014 14:49:20 +0000 Subject: [PATCH 2/3] infer 'width' and 'height' from viewBox if not specified --- Source/DOM classes/SVG-DOM/SVGLength.h | 1 + Source/DOM classes/SVG-DOM/SVGLength.m | 12 ++++ .../Unported or Partial DOM/SVGSVGElement.m | 57 ++++++++++++------- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/Source/DOM classes/SVG-DOM/SVGLength.h b/Source/DOM classes/SVG-DOM/SVGLength.h index 91fb65585..fce181af6 100644 --- a/Source/DOM classes/SVG-DOM/SVGLength.h +++ b/Source/DOM classes/SVG-DOM/SVGLength.h @@ -57,6 +57,7 @@ typedef enum SVG_LENGTH_TYPE +(SVGLength*) svgLengthZero; +(SVGLength*) svgLengthFromNSString:(NSString*) s; ++(SVGLength*) svgLengthFromNumber:(float) f; /** returns this SVGLength as if it had been converted to pixels, using [self convertToSpecifiedUnits:SVG_LENGTHTYPE_PX] */ diff --git a/Source/DOM classes/SVG-DOM/SVGLength.m b/Source/DOM classes/SVG-DOM/SVGLength.m index 2f61e1b98..b9ad87991 100644 --- a/Source/DOM classes/SVG-DOM/SVGLength.m +++ b/Source/DOM classes/SVG-DOM/SVGLength.m @@ -112,6 +112,18 @@ +(SVGLength*) svgLengthFromNSString:(NSString*) s return result; } ++(SVGLength*) svgLengthFromNumber:(float) f +{ + CSSPrimitiveValue* pv = [[[CSSPrimitiveValue alloc] init] autorelease]; + + pv.pixelsPerInch = cachedDevicePixelsPerInch; + [pv setFloatValue:CSS_NUMBER floatValue:f]; + + SVGLength* result = [[[SVGLength alloc] initWithCSSPrimitiveValue:pv] autorelease]; + + return result; +} + -(float) pixelsValue { return [self.internalCSSPrimitiveValue getFloatValue:CSS_PX]; diff --git a/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m b/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m index 42fc27975..eb1caa4de 100644 --- a/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m +++ b/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m @@ -139,37 +139,50 @@ We would need to put extra (NON STANDARD) properties on SVGDocument, for the "vi self.height = nil; // i.e. undefined else self.height = [SVGLength svgLengthFromNSString:[self getAttribute:@"height"]]; - - /* set the frameRequestedViewport appropriately (NB: spec doesn't allow for this but it REQUIRES it to be done and saved!) */ - if( self.width != nil && self.height != nil ) - self.requestedViewport = SVGRectMake( 0, 0, [self.width pixelsValue], [self.height pixelsValue] ); - else - self.requestedViewport = SVGRectUninitialized(); - - - /** - NB: this is VERY CONFUSING due to badly written SVG Spec, but: the viewport MUST NOT be set by the parser, - it MUST ONLY be set by the "renderer" -- and the renderer MAY have decided to use a different viewport from - the one that the SVG file *implies* (e.g. if the user scales the SVG, the viewport WILL BE DIFFERENT, - by definition! - - ...However: the renderer will ALWAYS start with the default viewport values (that are calcualted by the parsing process) - and it makes it much cleaner and safer to implement if we have the PARSER set the viewport initially - - (and the renderer will IMMEDIATELY overwrite them once the parsing is finished IFF IT NEEDS TO) - */ - self.viewport = self.requestedViewport; // renderer can/will change the .viewport, but .requestedViewport can only be set by the PARSER - + + if( [[self getAttribute:@"viewBox"] length] > 0 ) { NSArray* boxElements = [[self getAttribute:@"viewBox"] componentsSeparatedByString:@" "]; _viewBox = SVGRectMake([[boxElements objectAtIndex:0] floatValue], [[boxElements objectAtIndex:1] floatValue], [[boxElements objectAtIndex:2] floatValue], [[boxElements objectAtIndex:3] floatValue]); - } + + /** + Infer width and height from viewBox if not specified + */ + + if (self.width == nil) + self.width = [SVGLength svgLengthFromNumber:_viewBox.width]; + + if (self.height == nil) + self.height = [SVGLength svgLengthFromNumber:_viewBox.height]; + } else { self.viewBox = SVGRectUninitialized(); // VERY IMPORTANT: we MUST make it clear this was never initialized, instead of saying its 0,0,0,0 ! } + + + /* set the frameRequestedViewport appropriately (NB: spec doesn't allow for this but it REQUIRES it to be done and saved!) */ + if( self.width != nil && self.height != nil ) + self.requestedViewport = SVGRectMake( 0, 0, [self.width pixelsValue], [self.height pixelsValue] ); + else + self.requestedViewport = SVGRectUninitialized(); + + + /** + NB: this is VERY CONFUSING due to badly written SVG Spec, but: the viewport MUST NOT be set by the parser, + it MUST ONLY be set by the "renderer" -- and the renderer MAY have decided to use a different viewport from + the one that the SVG file *implies* (e.g. if the user scales the SVG, the viewport WILL BE DIFFERENT, + by definition! + + ...However: the renderer will ALWAYS start with the default viewport values (that are calcualted by the parsing process) + and it makes it much cleaner and safer to implement if we have the PARSER set the viewport initially + + (and the renderer will IMMEDIATELY overwrite them once the parsing is finished IFF IT NEEDS TO) + */ + self.viewport = self.requestedViewport; // renderer can/will change the .viewport, but .requestedViewport can only be set by the PARSER + self.preserveAspectRatio = [[SVGAnimatedPreserveAspectRatio new] autorelease]; // automatically sets defaults From c206456e9ae244d2f24d47ace43c2775aa26701e Mon Sep 17 00:00:00 2001 From: Chetan Padia Date: Thu, 13 Nov 2014 16:26:34 +0000 Subject: [PATCH 3/3] removed width parsing duplication --- .../Unported or Partial DOM/SVGSVGElement.m | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m b/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m index c72a43742..4c5ca1ab0 100644 --- a/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m +++ b/Source/DOM classes/Unported or Partial DOM/SVGSVGElement.m @@ -232,19 +232,6 @@ We would need to put extra (NON STANDARD) properties on SVGDocument, for the "vi DDLogWarn(@"Found unexpected preserve-aspect-ratio command inside element's 'preserveAspectRatio' attribute. Command = '%@'", aspectRatioCommand ); } } - - if( stringWidth == nil || stringWidth.length < 1 ) - self.width = nil; // i.e. undefined - else - self.width = [SVGLength svgLengthFromNSString:[self getAttribute:@"width"]]; - //osx logging -#if TARGET_OS_IPHONE - DDLogVerbose(@"[%@] DEBUG INFO: set document viewBox = %@", [self class], NSStringFromCGRect( CGRectFromSVGRect(self.viewBox))); -#else - //mac logging - DDLogVerbose(@"[%@] DEBUG INFO: set document viewBox = %@", [self class], NSStringFromRect(self.viewBox)); -#endif - } - (SVGElement *)findFirstElementOfClass:(Class)classParameter {