Files
IJSVG/source/IJSVGStyle.m
T
Curtis Hard 70f9c29fe5 This fixes hidden layers from showing
- Basically not obbeying the hidden node properly
- Also fixes CSS parser from beach balling when parsing @ selectors
- Also dramatically improves parsing of key value pairs in CSS, by dramatically, i mean took a file down from 300ms parse to 5ms… woah.
2017-01-27 19:10:01 +00:00

140 lines
3.1 KiB
Objective-C

//
// IJSVGStyle.m
// IJSVGExample
//
// Created by Curtis Hard on 03/09/2014.
// Copyright (c) 2014 Curtis Hard. All rights reserved.
//
#import "IJSVGStyle.h"
#import "IJSVGUtils.h"
@implementation IJSVGStyle
- (void)dealloc
{
[_dict release], _dict = nil;
[super dealloc];
}
- (id)init
{
if( ( self = [super init] ) != nil ) {
_dict = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setPropertyValue:(id)value
forProperty:(NSString *)key
{
[_dict setObject:value
forKey:key];
}
- (NSDictionary *)properties
{
return _dict;
}
- (id)property:(NSString *)key
{
return [_dict objectForKey:key];
}
+ (IJSVGStyle *)parseStyleString:(NSString *)string
{
IJSVGStyle * style = [[[[self class] alloc] init] autorelease];
NSInteger length = string.length;
NSInteger index = 0;
NSString * key = nil;
NSString * value = nil;
// iterate over the string - its actually really simple what we need
// to do
for(NSInteger i = 0; i < length; i++) {
unichar c = [string characterAtIndex:i];
// find the key
if(c == ':') {
key = [string substringWithRange:NSMakeRange(index, (i-index))];
index = i+1;
}
// find the value
else if(c == ';' || i == (length-1)) {
value = [string substringWithRange:NSMakeRange(index, (i-index))];
index = i+1;
}
// set the propery if it actually exists
if(key != nil && value != nil) {
[self computeStyleProperty:key
value:value
style:style];
key = nil;
value = nil;
}
}
return style;
}
+ (NSString *)trimString:(NSString *)string
{
return [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
+ (NSArray *)allowedColourKeys
{
return @[@"fill",@"stroke-colour",@"stop-color",@"stroke"];
}
+ (void)computeStyleProperty:(NSString *)key
value:(NSString *)value
style:(IJSVGStyle *)style
{
key = [[self class] trimString:key];
value = [[self class] trimString:value];
[style setPropertyValue:value
forProperty:key];
}
+ (BOOL)isNumeric:(NSString *)string
{
return [[NSScanner scannerWithString:string] scanFloat:NULL];
}
- (void)setProperties:(NSDictionary *)properties
replaceAll:(BOOL)flag
{
if(flag) {
[_dict removeAllObjects];
}
[_dict addEntriesFromDictionary:properties];
}
- (NSString *)description
{
return [_dict description];
}
- (IJSVGStyle *)mergedStyle:(IJSVGStyle *)style
{
// create the new style
IJSVGStyle * newStyle = [[[IJSVGStyle alloc] init] autorelease];
// grab the current style
NSMutableDictionary * dict = [[[self properties] mutableCopy] autorelease];
// overwride the style with the new styles
[dict addEntriesFromDictionary:[style properties]];
// add the styles to the style
[newStyle setProperties:dict
replaceAll:YES];
return newStyle;
}
@end