summaryrefslogtreecommitdiff
path: root/platform/ios/Classes/MuWord.m
blob: 64fb561d0f1a52945b221c1c059cfa42cdcebdc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#import "MuWord.h"

@implementation MuWord
{
	NSMutableString *string;
	CGRect rect;
}

@synthesize string, rect;

- (id) init
{
	self = [super init];
	if (self)
	{
		self.string = [NSMutableString string];
		self.rect = CGRectNull;
	}
	return self;
}

- (void) dealloc
{
	self.string = nil;
	[super dealloc];
}

+ (MuWord *) word
{
	return [[[MuWord alloc] init] autorelease];
}

- (void) appendChar:(unichar)c withRect:(CGRect)_rect
{
	[string appendFormat:@"%C", c];
	rect = CGRectUnion(rect, _rect);
}

+ (void) selectFrom:(CGPoint)pt1 to:(CGPoint)pt2 fromWords:(NSArray *)words onStartLine:(void (^)(void))startBlock onWord:(void (^)(MuWord *))wordBlock onEndLine:(void (^)(void))endBLock
{
	CGPoint toppt, botpt;

	if (pt1.y < pt2.y)
	{
		toppt = pt1;
		botpt = pt2;
	}
	else
	{
		toppt = pt2;
		botpt = pt1;
	}

	for (NSArray *line in words)
	{
		MuWord *fst = [line objectAtIndex:0];
		float ltop = fst.rect.origin.y;
		float lbot = ltop + fst.rect.size.height;

		if (toppt.y < lbot && ltop < botpt.y)
		{
			BOOL topline = toppt.y > ltop;
			BOOL botline = botpt.y < lbot;
			float left = -INFINITY;
			float right = INFINITY;

			if (topline && botline)
			{
				left = MIN(toppt.x, botpt.x);
				right = MAX(toppt.x, botpt.x);
			}
			else if (topline)
			{
				left = toppt.x;
			}
			else if (botline)
			{
				right = botpt.x;
			}

			startBlock();

			for (MuWord *word in line)
			{
				float wleft = word.rect.origin.x;
				float wright = wleft + word.rect.size.width;

				if (wright > left && wleft < right)
					wordBlock(word);
			}

			endBLock();
		}
	}
}

@end