// // MPFunctionLayout.m // MathPad // // Created by Kim Wittenburg on 07.08.14. // Copyright (c) 2014 Kim Wittenburg. All rights reserved. // #import "MPFunctionLayout.h" #import "MPFunction.h" #import "MPExpressionLayout.h" #import "MPSumFunction.h" #import "MPSumFunctionLayout.h" @implementation MPFunctionLayout #pragma mark Creation Methods + (MPFunctionLayout *)functionLayoutForFunction:(MPFunction *)function parent:(MPExpressionLayout *)parent { Class class = [function class]; if (class == [MPSumFunction class]) { return [[MPSumFunctionLayout alloc] initWithFunction:function parent:parent]; } return [[self alloc] initWithFunction:function parent:parent]; } - (instancetype)initWithFunction:(MPFunction *)function parent:(MPExpressionLayout *)parent { self = [super initWithParent:parent]; if (self) { _function = function; } return self; } #pragma mark Cache Methods - (CTLineRef)lineForPrivateCacheIndex:(NSUInteger)index generator:(CTLineRef (^)())generator { NSUInteger actualIndex = self.function.numberOfChildren + index; id (^actualGenerator)() = ^{ CTLineRef line = generator(); return CFBridgingRelease(line); }; id lineObject = [self cachableObjectForIndex:actualIndex generator:actualGenerator]; return (__bridge CTLineRef)(lineObject); } - (MPLayout *)childLayoutAtIndex:(NSUInteger)index { return [self cachableObjectForIndex:index generator:^id{ MPExpression *child = [self.function childAtIndex:index]; MPExpressionLayout *layout = [[MPExpressionLayout alloc] initWithExpression:child parent:self]; layout.flipped = self.flipped; layout.usesSmallSize = (index == 0 || index == 1) ? YES : self.usesSmallSize; return layout; }]; } - (NSUInteger)indexOfChildBeforeChildAtIndex:(NSUInteger)index { return NSNotFound; } - (NSUInteger)indexOfChildAfterChildAtIndex:(NSUInteger)index { return NSNotFound; } @end