Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
#import "MuWord.h"
2
3
@implementation MuWord
4
{
5
NSMutableString *string;
6
CGRect rect;
7
}
8
9
@synthesize string, rect;
10
11
- (id) init
12
{
13
self = [super init];
14
if (self)
15
{
16
self.string = [NSMutableString string];
17
self.rect = CGRectNull;
18
}
19
return self;
20
}
21
22
- (void) dealloc
23
{
24
self.string = nil;
25
[super dealloc];
26
}
27
28
+ (MuWord *) word
29
{
30
return [[[MuWord alloc] init] autorelease];
31
}
32
33
- (void) appendChar:(unichar)c withRect:(CGRect)_rect
34
{
35
[string appendFormat:@"%C", c];
36
rect = CGRectUnion(rect, _rect);
37
}
38
39
+ (void) selectFrom:(CGPoint)pt1 to:(CGPoint)pt2 fromWords:(NSArray *)words onStartLine:(void (^)(void))startBlock onWord:(void (^)(MuWord *))wordBlock onEndLine:(void (^)(void))endBLock
40
{
41
CGPoint toppt, botpt;
42
43
if (pt1.y < pt2.y)
44
{
45
toppt = pt1;
46
botpt = pt2;
47
}
48
else
49
{
50
toppt = pt2;
51
botpt = pt1;
52
}
53
54
for (NSArray *line in words)
55
{
56
MuWord *fst = [line objectAtIndex:0];
57
float ltop = fst.rect.origin.y;
58
float lbot = ltop + fst.rect.size.height;
59
60
if (toppt.y < lbot && ltop < botpt.y)
61
{
62
BOOL topline = toppt.y > ltop;
63
BOOL botline = botpt.y < lbot;
64
float left = -INFINITY;
65
float right = INFINITY;
66
67
if (topline && botline)
68
{
69
left = MIN(toppt.x, botpt.x);
70
right = MAX(toppt.x, botpt.x);
71
}
72
else if (topline)
73
{
74
left = toppt.x;
75
}
76
else if (botline)
77
{
78
right = botpt.x;
79
}
80
81
startBlock();
82
83
for (MuWord *word in line)
84
{
85
float wleft = word.rect.origin.x;
86
float wright = wleft + word.rect.size.width;
87
88
if (wright > left && wleft < right)
89
wordBlock(word);
90
}
91
92
endBLock();
93
}
94
}
95
}
96
97
@end
98
99