forked from scribendi/cindex-mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutDescriptor.m
163 lines (155 loc) · 5.23 KB
/
LayoutDescriptor.m
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// LayoutDescriptor.m
// Cindex
//
// Created by PL on 4/9/05.
// Copyright 2005 Indexing Research. All rights reserved.
//
#import "LayoutDescriptor.h"
#import "strings_c.h"
@implementation LayoutDescriptor
- (id)init {
if (self = [super init]) {
_lineRanges = [[NSMutableArray alloc] initWithCapacity:5];
_lineHeights = [[NSMutableArray alloc] initWithCapacity:5];
}
return self;
}
- (id)initWithStorage:(NSTextStorage *)storage range:(NSRange)range {
if (self = [self init]) {
NSLayoutManager *lm = [[storage layoutManagers] objectAtIndex:0];
NSUInteger glyphIndex;
NSRange /* allCharactersRange, */ allGlyphsRange;
NSRange lineFragmentGlyphRange, lineFragmentCharacterRange;
_length = range.length;
// Find our range of characters
// allCharactersRange = NSMakeRange (0, _length);
// Find how many glyphs there are
allGlyphsRange = [lm glyphRangeForCharacterRange:range actualCharacterRange: NULL];
glyphIndex = allGlyphsRange.location;
while (glyphIndex < NSMaxRange (allGlyphsRange)) {
NSRect lrect = [lm lineFragmentRectForGlyphAtIndex: glyphIndex effectiveRange: &lineFragmentGlyphRange];
lineFragmentCharacterRange = [lm characterRangeForGlyphRange:lineFragmentGlyphRange actualGlyphRange:NULL];
[_lineRanges addObject:[NSValue valueWithRange:lineFragmentCharacterRange]];
[_lineHeights addObject:[NSNumber numberWithFloat:lrect.size.height]];
_height += lrect.size.height;
_lineCount++;
glyphIndex = NSMaxRange(lineFragmentGlyphRange);
}
}
return self;
}
- (id)initWithStorage:(NSTextStorage *)storage entry:(IRAttributedDisplayString *)string {
if (self = [self init]) {
NSLayoutManager *lm = [[storage layoutManagers] objectAtIndex:0];
NSUInteger glyphIndex;
NSRange allCharactersRange, allGlyphsRange;
NSRange lineFragmentGlyphRange, lineFragmentCharacterRange;
[storage setAttributedString:string];
_length = [[storage string] length];
// Find our range of characters
allCharactersRange = NSMakeRange (0, _length);
// Find how many glyphs there are
allGlyphsRange = [lm glyphRangeForCharacterRange: allCharactersRange actualCharacterRange: NULL];
glyphIndex = 0;
while (glyphIndex < NSMaxRange (allGlyphsRange)) {
NSRect lrect = [lm lineFragmentRectForGlyphAtIndex: glyphIndex effectiveRange: &lineFragmentGlyphRange];
lineFragmentCharacterRange = [lm characterRangeForGlyphRange:lineFragmentGlyphRange actualGlyphRange:NULL];
[_lineRanges addObject:[NSValue valueWithRange:lineFragmentCharacterRange]];
if (!glyphIndex) { // if first character
NSParagraphStyle * pp = [storage attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:NULL];
lrect.size.height += [pp paragraphSpacingBefore]; // add any otherwise uncounted lead
}
[_lineHeights addObject:[NSNumber numberWithFloat:lrect.size.height]];
_height += lrect.size.height;
_lineCount++;
glyphIndex = NSMaxRange(lineFragmentGlyphRange);
}
_entry = string;
_record = string.record;
_recordsConsumed = [string entryInformation]->consumed;
}
return self;
}
- (NSRange)displayRangeForSourceRange:(NSRange)range{ // for draft formatted text only
INDEX * FF = ((IRDisplayString *)_entry.string).index;
RECORD * recptr = rec_getrec(FF,_record);
int baseLevel = ((IRDisplayString *)_entry.string).entryInformation->ulevel;
ATTRIBUTEDSTRING * att = ((IRDisplayString *)_entry.string).attributedText;
CSTR levels[FIELDLIM];
str_xparse(recptr->rtext,levels);
char * rptr = levels[baseLevel].str;
unichar * aptr = att->string;
int offset, location, mcount, tabcount;
for (offset = 0; * rptr != EOCS && rptr < recptr->rtext+range.location; ) { // find offset of ptr in original record (starting at right level)
if (iscodechar(*rptr) && *(rptr+1))
rptr += 2;
else {
offset++;
rptr = u8_forward1(rptr);
}
}
location = mcount = tabcount = 0;
do {
if (!location || *(aptr-1) == FO_LEVELBREAK) { // if at start of any line, count and skip lead chars
while (*aptr != '\t') { // skip any lead markers up to first tab
location++;
aptr++;
}
location++; // must be at first tab; skip it and all through next one
while (*++aptr != '\t')
location++;
location++;
aptr++;
if (*aptr == FO_ELIPSIS) // skip any elipsis
location++;
}
} while (mcount++ < offset && location++ && aptr++);
if (!range.length) // if want all record text
range.length = att->length-location-1;
return NSMakeRange(location, range.length);
}
- (void)dealloc {
[self setEntry:nil];
}
- (RECN)record {
return _record;
}
- (int)recordsConsumed {
return _recordsConsumed;
}
- (void)setEntry:(NSAttributedString *)text {
_entry = text;
}
- (NSAttributedString *)entry {
return _entry;
}
- (void)setEntryLength:(NSUInteger)length {
_length = length;
}
- (NSUInteger)entryLength {
return _length;
}
- (void)setLineCount:(NSUInteger)count {
_lineCount = count;
}
- (NSUInteger)lineCount {
return _lineCount;
}
- (void)setHeight:(float)height {
_height = height;
}
- (float)height {
return _height;
}
- (NSMutableArray *)lineRanges {
return _lineRanges;
}
- (NSMutableArray *)lineHeights {
return _lineHeights;
}
- (NSString *)description {
return [NSString stringWithFormat:@"Record: %u, Lines: %ld, Height: %g, Ranges: %@",
_record, (long)_lineCount, _height, [_lineRanges description]];
}
@end