Skip to content

Commit

Permalink
Optimised out styling of empty nodes, significantly speeding overall …
Browse files Browse the repository at this point in the history
…styling of api objects
  • Loading branch information
beelsebob committed Apr 9, 2012
1 parent 1e2d0df commit 812d4ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
41 changes: 35 additions & 6 deletions OpenStreetPad/Model/MapCSS/OSPMapCSSStyleSheet.m
Expand Up @@ -10,14 +10,23 @@

#import "OSPMapCSSStyledObject.h"

#import "OSPNode.h"

#import <objc/runtime.h>

static char styleRef;
static char oldZoomRef;

@interface OSPMapCSSStyleSheet ()

@property (readwrite, strong) NSMutableDictionary *emptyNodeStyles;

@end

@implementation OSPMapCSSStyleSheet

@synthesize ruleset;
@synthesize emptyNodeStyles;

- (id)initWithRules:(OSPMapCSSRuleset *)initRuleset
{
Expand All @@ -26,6 +35,7 @@ - (id)initWithRules:(OSPMapCSSRuleset *)initRuleset
if (nil != self)
{
[self setRuleset:initRuleset];
[self setEmptyNodeStyles:[NSMutableDictionary dictionary]];
}

return self;
Expand All @@ -49,15 +59,34 @@ - (NSArray *)styledObjects:(NSSet *)objects atZoom:(float)zoom
}
if (nil == newStyledObjects)
{
NSDictionary *layerStyles = [[self ruleset] applyToObject:object atZoom:zoom];
NSMutableArray *sos = [NSMutableArray arrayWithCapacity:[layerStyles count]];
for (NSString *layerStyle in layerStyles)
NSNumber *zNum = [NSNumber numberWithFloat:zoom];
if ([object memberType] == OSPMemberTypeNode && [[object tags] count] == 0)
{
newStyledObjects = [[self emptyNodeStyles] objectForKey:zNum];
if (nil == newStyledObjects)
{
NSDictionary *layerStyles = [[self ruleset] applyToObject:object atZoom:zoom];
NSMutableArray *sos = [NSMutableArray arrayWithCapacity:[layerStyles count]];
for (NSString *layerStyle in layerStyles)
{
[sos addObject:[OSPMapCSSStyledObject object:object withStyle:[layerStyles objectForKey:layerStyle]]];
}
newStyledObjects = [sos copy];
[[self emptyNodeStyles] setObject:newStyledObjects forKey:zNum];
}
}
else
{
[sos addObject:[OSPMapCSSStyledObject object:object withStyle:[layerStyles objectForKey:layerStyle]]];
NSDictionary *layerStyles = [[self ruleset] applyToObject:object atZoom:zoom];
NSMutableArray *sos = [NSMutableArray arrayWithCapacity:[layerStyles count]];
for (NSString *layerStyle in layerStyles)
{
[sos addObject:[OSPMapCSSStyledObject object:object withStyle:[layerStyles objectForKey:layerStyle]]];
}
newStyledObjects = [sos copy];
}
newStyledObjects = [sos copy];
objc_setAssociatedObject(object, &styleRef, newStyledObjects, OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(object, &oldZoomRef, [NSNumber numberWithFloat:zoom], OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(object, &oldZoomRef, zNum, OBJC_ASSOCIATION_RETAIN);
}
[styledObjects addObjectsFromArray:newStyledObjects];
}
Expand Down
1 change: 0 additions & 1 deletion OpenStreetPad/OSPMetaTileView.m
Expand Up @@ -159,7 +159,6 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1.0f, -1.0f));

NSSet *objects = [[self dataSource] objectsInBounds:dataRect];

NSArray *styledObjects = [[self stylesheet] styledObjects:objects atZoom:[self mapArea].zoomLevel];
[self renderLayers:[self sortedObjects:styledObjects] inContext:ctx withScaleMultiplier:oneOverScale];
}
Expand Down

0 comments on commit 812d4ba

Please sign in to comment.