SVGGroupElement.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. SVGGroupElement.m
  3. In SVG, every single element can contain children.
  4. However, the SVG spec defines a special (optional) "group" element, that is never rendered,
  5. but allows additional nesting (e.g. for programmatic / organizational purposes).
  6. This is the "G" tag.
  7. */
  8. #import "SVGGroupElement.h"
  9. #import "CALayerWithChildHitTest.h"
  10. #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)
  11. #import "Node.h"
  12. @implementation SVGGroupElement
  13. @synthesize opacity = _opacity;
  14. - (void)loadDefaults {
  15. _opacity = 1.0f;
  16. }
  17. - (void)postProcessAttributesAddingErrorsTo:(SVGKParseResult *)parseResult {
  18. [super postProcessAttributesAddingErrorsTo:parseResult];
  19. if( [[self getAttribute:@"opacity"] length] > 0 )
  20. _opacity = [[self getAttribute:@"opacity"] floatValue];
  21. }
  22. - (CALayer *) newLayer
  23. {
  24. CALayer* _layer = [CALayerWithChildHitTest layer];
  25. _layer.name = self.identifier;
  26. [_layer setValue:self.identifier forKey:kSVGElementIdentifier];
  27. _layer.opacity = _opacity;
  28. return _layer;
  29. }
  30. - (void)layoutLayer:(CALayer *)layer {
  31. CGRect mainRect = CGRectZero;
  32. /** Adam: make a frame thats the UNION of all sublayers frames */
  33. for ( CALayer *currentLayer in [layer sublayers] )
  34. {
  35. CGRect subLayerFrame = currentLayer.frame;
  36. mainRect = CGRectUnion(mainRect, subLayerFrame);
  37. }
  38. layer.frame = mainRect;
  39. /** Adam:(dont know why this is here): set each sublayer to have a frame the same size as the parent frame, but with 0 offset.
  40. Adam: if I understand this correctly, the person who wrote it should have just written:
  41. "currentLayer.bounds = layer.frame"
  42. i.e. make every layer have the same size as the parent layer.
  43. But whoever wrote this didn't document their bad code, so I have no idea if thats correct or not
  44. */
  45. for (CALayer *currentLayer in [layer sublayers]) {
  46. CGRect frame = currentLayer.frame;
  47. frame.origin.x -= mainRect.origin.x;
  48. frame.origin.y -= mainRect.origin.y;
  49. currentLayer.frame = frame;
  50. }
  51. }
  52. /*
  53. FIXME: this cannot work; this is incompatible with the way that SVG spec was designed; this code comes from old SVGKit
  54. //we can't propagate opacity down unfortunately, so we need to build a set of all the properties except a few (opacity is applied differently to groups than simply inheriting it to it's children, <g opacity occurs AFTER blending all of its children
  55. BOOL attributesFound = NO;
  56. NSMutableDictionary *buildDictionary = [NSMutableDictionary new];
  57. for( Node* node in self.attributes )
  58. {
  59. if( ![node.localName isEqualToString:@"opacity"] )
  60. {
  61. attributesFound = YES;
  62. [buildDictionary setObject:[attributes objectForKey:key] forKey:node.localName];
  63. }
  64. }
  65. if( attributesFound )
  66. {
  67. _attributes = [[NSDictionary alloc] initWithDictionary:buildDictionary];
  68. //these properties are inherited by children of this group
  69. }
  70. [buildDictionary release];
  71. */
  72. @end