My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "RichText.h"

NAMESPACE_UPP

int RichTxt::GetWidth(const RichStyles& st) const
{
int cx = 0;
for(int i = 0; i < part.GetCount(); i++) {
if(IsPara(i)) {
RichPara p = Get(i, st, true);
RichPara::Lines pl = p.FormatLines(INT_MAX);
int ccx = 0;
Sum(ccx, ~pl.width, ~pl.width + pl.clen);
cx = max(cx, ccx);
}
else
return 10000;
}
return cx;
}


void RichTxt::Sync0(const Para& pp, int parti, const RichContext& rc) const
{
int cx = rc.page.Width();
pp.ccx = cx;
RichPara p = Get(parti, rc.styles, false);
RichPara::Lines pl = p.FormatLines(cx);
pp.ruler = p.format.ruler;
pp.before = p.format.before;
pp.linecy.Clear();
pp.linecy.SetCount(pl.GetCount());
for(int i = 0; i < pl.GetCount(); i++)
pp.linecy[i] = pl[i].Sum();
pp.cy = Sum0(pp.linecy);
pp.after = p.format.after;
pp.newpage = p.format.newpage;
pp.keep = p.format.keep;
pp.keepnext = p.format.keepnext;
pp.orphan = p.format.orphan;
}

void RichTxt::Sync(int parti, const RichContext& rc) const {
ASSERT(part[parti].Is<Para>());
const Para& pp = part[parti].Get<Para>();
if(rc.page.Width() != pp.ccx)
pp.dirty.Invalidate();
if(pp.dirty.BeginUpdate()) {
Sync0(pp, parti, rc);
pp.dirty.EndUpdate();
}
}

bool RichTxt::BreaksPage(PageY py, const Para& pp, int i, const Rect& page) const
{
int linecy = pp.linecy[i];
if(linecy >= page.Height()) return false;
if(linecy + py.y > page.bottom)
return true;
if(pp.orphan || pp.linecy.GetCount() < 2) return false;
if((i == 0 || i == pp.linecy.GetCount() - 2) &&
py.y + linecy + pp.linecy[i + 1] > page.bottom)
return true;
return false;
}

PageY RichTxt::GetNextPageY(int parti, const RichContext& rc) const
{
if(part[parti].Is<RichTable>())
return GetTable(parti).GetHeight(rc);
else {
Sync(parti, rc);
const Para& pp = part[parti].Get<Para>();
int cy = pp.before + pp.ruler;
if(pp.keep || pp.keepnext)
cy += pp.cy;
else
cy += pp.linecy[0];
PageY py = rc.py;
if(rc.page.Height() < 30000) {
int nbefore = 0;
int nline = 0;
if(pp.keepnext && parti + 1 < part.GetCount() && part[parti + 1].Is<Para>()) {
Sync(parti + 1, rc);
const Para& p = part[parti + 1].Get<Para>();
nbefore = p.before + p.ruler;
nline = p.linecy[0];
}
if(pp.newpage || py.y + cy + nbefore + nline > rc.page.bottom && cy < rc.page.Height()) {
py.page++;
py.y = rc.page.top;
}
py.y += pp.before + pp.ruler;
if(py.y + pp.cy < rc.page.bottom)
py.y += pp.cy;
else
for(int lni = 0; lni < pp.linecy.GetCount(); lni++) {
if(BreaksPage(py, pp, lni, rc.page)) {
py.y = rc.page.top;
py.page++;
}
py.y += pp.linecy[lni];
}
py.y += pp.after;
if(py.y > rc.page.bottom) {
py.y = rc.page.top;
py.page++;
}
}
else
py.y += pp.before + pp.cy + pp.after + pp.ruler;
return py;
}
}

PageY RichTxt::GetPartPageY(int parti, RichContext rc) const
{
for(int i = 0; i < parti; i++)
rc.py = GetNextPageY(i, rc);
return rc.py;
}

bool IsPainting(PageDraw& pw, Zoom z, const Rect& page, PageY top, PageY bottom)
{
for(int pi = top.page; pi <= bottom.page; pi++)
if(pw.Page(pi).IsPainting(Rect(z * page.left, z * (pi == top.page ? top.y : page.top),
z * page.right, z * (pi == bottom.page ? bottom.y : page.bottom))))
return true;
return false;
}


void RichTxt::Paint(PageDraw& pw, RichContext rc, const PaintInfo& _pi) const
{
PaintInfo pi = _pi;
int parti = 0;
int pos = 0;
RichPara::Number n;
while(rc.py < pi.bottom && parti < part.GetCount()) {
if(part[parti].Is<RichTable>()) {
pi.tablesel--;
const RichTable& tab = GetTable(parti);
tab.Paint(pw, rc, pi);
rc.py = tab.GetHeight(rc);
pi.tablesel -= tab.GetTableCount();
}
else {
const Para& pp = part[parti].Get<Para>();
if(pp.number) {
n.TestReset(*pp.number);
n.Next(*pp.number);
}
PageY next = GetNextPageY(parti, rc);
if(next >= pi.top) {
int nbefore = 0;
int nline = 0;
if(pp.keepnext && parti + 1 < part.GetCount() && part[parti + 1].Is<Para>()) {
Sync(parti + 1, rc);
const Para& pp = part[parti + 1].Get<Para>();
nbefore = pp.before;
nline = pp.linecy[0];
}
RichPara p = Get(parti, rc.styles, true);
if(pi.spellingchecker) {
if(!pp.checked) {
pp.spellerrors = (*pi.spellingchecker)(p);
pp.checked = true;
}
}
else {
pp.checked = false;
pp.spellerrors.Clear();
}
if(IsPainting(pw, pi.zoom, rc.page, rc.py, next))
p.Paint(pw, rc.page, rc.py, pi, n, pp.spellerrors, nbefore, nline);
}
rc.py = next;
}
int l = GetPartLength(parti) + 1;
pi.highlightpara -= l;
pi.sell -= l;
pi.selh -= l;
pos += l;
++parti;
}
}

RichCaret RichTxt::GetCaret(int pos, RichContext rc) const
{
int parti = 0;
if(pos > GetLength())
pos = GetLength();
while(parti < part.GetCount()) {
int l = GetPartLength(parti) + 1;
if(pos < l)
if(IsTable(parti))
return GetTable(parti).GetCaret(pos, rc);
else {
const Para& p = part[parti].Get<Para>();
int nbefore = 0;
int nline = 0;
if(p.keepnext && parti + 1 < part.GetCount() && part[parti + 1].Is<Para>()) {
Sync(parti + 1, rc);
const Para& pp = part[parti + 1].Get<Para>();
nbefore = pp.before + pp.ruler;
nline = pp.linecy[0];
}
RichCaret tp = Get(parti, rc.styles, true)
.GetCaret(pos, rc.page, rc.py, nbefore, nline);
tp.textpage = rc.page;
return tp;
}
pos -= l;
rc.py = GetNextPageY(parti++, rc);
}
return RichCaret();
}

int RichTxt::GetPos(int x, PageY y, RichContext rc) const
{
int parti = 0;
int pos = 0;

if(part.GetCount()) {
PageY nnext = GetNextPageY(parti, rc);
while(parti < part.GetCount()) {
PageY next = nnext;
if(parti + 1 < part.GetCount()) {
RichContext nrc = rc;
nrc.py = next;
nnext = GetNextPageY(parti + 1, nrc);
}
if(y < next || y.page < next.page) {
if(IsTable(parti))
return GetTable(parti).GetPos(x, y, rc) + pos;
else {
int nbefore = 0;
int nline = 0;
if(part[parti].Get<Para>().keepnext && parti + 1 < part.GetCount() && IsPara(parti + 1)) {
Sync(parti + 1, rc);
const Para& pp = part[parti + 1].Get<Para>();
nbefore = pp.before + pp.ruler;
nline = pp.linecy[0];
}
return Get(parti, rc.styles, true)
.GetPos(x, y, rc.page, rc.py, nbefore, nline) + pos;
}
}
pos += GetPartLength(parti) + 1;
parti++;
rc.py = next;
}
}

return pos - 1;
}

RichHotPos RichTxt::GetHotPos(int x, PageY y, int tolerance, RichContext rc) const
{
int parti = 0;
int pos = 0;
int ti = 0;
if(part.GetCount()) {
PageY nnext = GetNextPageY(parti, rc);
while(parti < part.GetCount()) {
PageY next = nnext;
if(parti + 1 < part.GetCount()) {
RichContext nrc = rc;
nrc.py = next;
nnext = GetNextPageY(parti + 1, nrc);
}
if(y < next || y.page < next.page) {
if(IsTable(parti)) {
RichHotPos pos = GetTable(parti).GetHotPos(x, y, tolerance, rc);
pos.table += ti + 1;
return pos;
}
else
break;
}
if(IsTable(parti))
ti += 1 + GetTable(parti).GetTableCount();
pos += GetPartLength(parti) + 1;
parti++;
rc.py = next;
}
}

return RichHotPos();
}

int RichTxt::GetVertMove(int pos, int gx, RichContext rc, int dir) const
{
ASSERT(dir == -1 || dir == 1);
if(GetPartCount() == 0)
return -1;
int pi;
int p = pos;
if(pos >= 0) {
pi = FindPart(p);
pos -= p;
}
else {
pi = dir > 0 ? 0 : GetPartCount() - 1;
p = -1;
pos = 0;
}
while(pi < GetPartCount()) {
int q = IsTable(pi) ? GetTable(pi).GetVertMove(p, gx, rc, dir)
: Get(pi, rc.styles, true).GetVertMove(p, gx, rc.page, dir);
if(q >= 0)
return q + pos;
if(dir > 0)
pos += GetPartLength(pi) + 1;
p = -1;
pi += dir;
if(pi < 0)
break;
if(dir < 0)
pos -= GetPartLength(pi) + 1;
}
return -1;
}

void RichTxt::GatherValPos(Vector<RichValPos>& f, RichContext rc, int pos, int type) const
{
int parti = 0;
while(parti < part.GetCount()) {
if(part[parti].Is<RichTable>())
GetTable(parti).GatherValPos(f, rc, pos, type);
else {
int nbefore = 0;
int nline = 0;
const Para& p = part[parti].Get<Para>();
if(p.keepnext && parti + 1 < part.GetCount() && IsPara(parti + 1)) {
Sync(parti + 1, rc);
const Para& pp = part[parti + 1].Get<Para>();
nbefore = pp.before + pp.ruler;
nline = pp.linecy[0];
}
if(p.haspos)
if(type == LABELS)
Get(parti, rc.styles, true).GatherLabels(f, rc.page, rc.py, pos, nbefore, nline);
else
Get(parti, rc.styles, true).GatherIndexes(f, rc.page, rc.py, pos, nbefore, nline);
}
pos += GetPartLength(parti) + 1;
rc.py = GetNextPageY(parti++, rc);
}
}

PageY RichTxt::GetHeight(RichContext rc) const
{
for(int i = 0; i < GetPartCount(); i++)
rc.py = GetNextPageY(i, rc);
return rc.py;
}

PageY RichTxt::GetTop(RichContext rc) const
{
if(part.GetCount() == 0)
return rc.py;
if(part[0].Is<RichTable>())
return GetTable(0).GetTop(rc);
else {
Sync(0, rc);
const Para& pp = part[0].Get<Para>();
rc.py.y += pp.before + pp.ruler;
if(BreaksPage(rc.py, pp, 0, rc.page))
rc.Page();
return rc.py;
}
}

void RichTxt::ApplyZoom(Zoom z, const RichStyles& ostyle, const RichStyles& zstyle)
{
for(int i = 0; i < GetPartCount(); i++)
if(IsTable(i))
part[i].Get<RichTable>().ApplyZoom(z, ostyle, zstyle);
else {
RichPara p = Get(i, ostyle);
p.ApplyZoom(z);
Set(i, p, zstyle);
}
}

END_UPP_NAMESPACE

Change log

r4457 by cxl on Jan 21, 2012   Diff
*uppsrc: Fixed many GCC warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r3862 by cxl on Sep 14, 2011   Diff
*RichText: IsPainting fix, Web:
HttpClient put fix
r3858 by cxl on Sep 12, 2011   Diff
*RichText: Fixed IsPainting for
multipaged paragraphs (was causing
rendering quirks)
r2972 by cxl on Jan 8, 2011   Diff
*uppsrc: mr_ped's GCC warnings fix,
Core: now methods in Thread based on
tojocky's ideas
All revisions of this file

File info

Size: 9569 bytes, 387 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting