SVGPolygonElement.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // SVGPolygonElement.m
  3. // SVGKit
  4. //
  5. // Copyright Matt Rajca 2011. All rights reserved.
  6. //
  7. #import "SVGPolygonElement.h"
  8. #import "SVGKPointsAndPathsParser.h"
  9. #import "SVGElement_ForParser.h" // to resolve Xcode circular dependencies; in long term, parsing SHOULD NOT HAPPEN inside any class whose name starts "SVG" (because those are reserved classes for the SVG Spec)
  10. @interface SVGPolygonElement()
  11. - (void) parseData:(NSString *)data;
  12. @end
  13. @implementation SVGPolygonElement
  14. - (void)postProcessAttributesAddingErrorsTo:(SVGKParseResult *)parseResult {
  15. [super postProcessAttributesAddingErrorsTo:parseResult];
  16. [self parseData:[self getAttribute:@"points"]];
  17. }
  18. /*! According to SVG spec, a 'polygon' is EXACTYLY IDENTICAL to a 'path', if you prepend the letter "M", and
  19. postfix the letter 'z'.
  20. So, we take the complicated parser from SVGPathElement, remove all the multi-command stuff, and just use the
  21. "M" command
  22. */
  23. - (void)parseData:(NSString *)data
  24. {
  25. CGMutablePathRef path = CGPathCreateMutable();
  26. NSScanner* dataScanner = [NSScanner scannerWithString:data];
  27. NSCharacterSet* knownCommands = [NSCharacterSet characterSetWithCharactersInString:@""];
  28. NSString* cmdArgs = nil;
  29. [dataScanner scanUpToCharactersFromSet:knownCommands
  30. intoString:&cmdArgs];
  31. NSString* commandWithParameters = [@"M" stringByAppendingString:cmdArgs];
  32. NSScanner* commandScanner = [NSScanner scannerWithString:commandWithParameters];
  33. SVGCurve lastCurve = [SVGKPointsAndPathsParser readMovetoDrawtoCommandGroups:commandScanner
  34. path:path
  35. relativeTo:CGPointZero
  36. isRelative:FALSE];
  37. [SVGKPointsAndPathsParser readCloseCommand:[NSScanner scannerWithString:@"z"]
  38. path:path
  39. relativeTo:lastCurve.p];
  40. self.pathForShapeInRelativeCoords = path;
  41. CGPathRelease(path);
  42. }
  43. /* reference
  44. http://www.w3.org/TR/2011/REC-SVG11-20110816/shapes.html#PointsBNF
  45. */
  46. /*
  47. list-of-points:
  48. wsp* coordinate-pairs? wsp*
  49. coordinate-pairs:
  50. coordinate-pair
  51. | coordinate-pair comma-wsp coordinate-pairs
  52. coordinate-pair:
  53. coordinate comma-wsp coordinate
  54. | coordinate negative-coordinate
  55. coordinate:
  56. number
  57. number:
  58. sign? integer-constant
  59. | sign? floating-point-constant
  60. negative-coordinate:
  61. "-" integer-constant
  62. | "-" floating-point-constant
  63. comma-wsp:
  64. (wsp+ comma? wsp*) | (comma wsp*)
  65. comma:
  66. ","
  67. integer-constant:
  68. digit-sequence
  69. floating-point-constant:
  70. fractional-constant exponent?
  71. | digit-sequence exponent
  72. fractional-constant:
  73. digit-sequence? "." digit-sequence
  74. | digit-sequence "."
  75. exponent:
  76. ( "e" | "E" ) sign? digit-sequence
  77. sign:
  78. "+" | "-"
  79. digit-sequence:
  80. digit
  81. | digit digit-sequence
  82. digit:
  83. "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
  84. */
  85. @end