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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include "RichText.h"

NAMESPACE_UPP

#define IMAGECLASS RichTextImg
#define IMAGEFILE <RichText/RichText.iml>
#include <Draw/iml.h>

RichPara::Lines RichPara::Begin(const Rect& page, PageY& py, int nbefore, int nline) const
{
Lines pl = FormatLines(page.Width());
int cy = format.ruler + format.before;
if(format.keep || format.keepnext)
cy += pl.BodyHeight();
else
cy += pl[0].Sum();
int after = format.after;
if(!format.keepnext)
nbefore = nline = after = 0;
if(page.Height() < 32000 &&
(format.newpage || py.y + cy + after + nbefore + nline > page.bottom &&
cy < page.Height())) {
py.page++;
py.y = page.top;
}
py.y += format.before + format.ruler;
pl.Justify(format);
return pl;
}

void RichPara::Flush(Draw& draw, const PaintInfo& pi, wchar *text,
const CharFormat **i0,
int *wd, int pos, int len, int x0, int x, int y0,
int y, int linecy, int lineascent, Zoom z,
bool highlight)
{
if(*i0) {
const CharFormat& f = **i0;
int width = z * x - z * x0;
int zy0 = z * y0;
int zx0 = z * x0;
if(!IsNull(f.paper) && !highlight)
draw.DrawRect(zx0, z * y, width, z * (y + linecy) - z * y,
pi.coloroverride ? SColorPaper() : f.paper);
if(!IsNull(f.indexentry) && !IsNull(pi.indexentry))
draw.DrawRect(zx0, zy0, width, 2, pi.indexentry);
Font fnt = f;
int zht = z * tabs(f.GetHeight());
int ssa = 0;
int ssd = 0;
if(f.sscript) {
FontInfo fi = fnt(zht).Info();
ssa = fi.GetAscent();
ssd = fi.GetDescent();
zht = 3 * zht / 5;
}
fnt.Height(zht ? zht : 1);
FontInfo fi = fnt.Info();
if(f.dashed) {
int dx = max(fi.GetAscent() / 5, 2);
for(int i = 0; dx * i < width; i++)
draw.DrawRect(zx0 + i * dx, zy0 + fi.GetDescent() / 2,
dx / 2, max(fi.GetDescent() / 3, 1),
pi.coloroverride ? SColorText() : f.ink);
}
Color ink = pi.coloroverride ? SColorText() : f.ink;
if(!IsNull(f.link) && !IsNull(pi.hyperlink) && !(fnt.IsUnderline() || f.dashed)) {
fnt.Underline();
if(!pi.coloroverride)
ink = pi.hyperlink;
}
x = zx0;
for(int i = 0; i < len; i++) {
int w = wd[pos + i];
Image img;
wchar& c = text[pos + i];
if(c == ' ')
img = RichTextImg::SpaceChar();
if(c == 160)
img = RichTextImg::HardSpaceChar();
if(c >= 9 && c < 9 + 4) {
if(c > 9) {
static char fct[] = { ' ', '.', '-', '_' };
int fc = fct[c - 9];
int pw = fi[fc];
int px = (x + pw - 1) / pw * pw;
while(px + pw < x + w) {
draw.DrawText(px, zy0 - fi.GetAscent(), String(fc, 1), fnt, Black);
px += pw;
}
}
img = RichTextImg::TabChar();
c = ' ';
}
if(img && !IsNull(pi.showcodes)) {
Size sz = img.GetSize();
if(sz.cy < z * lineascent)
draw.DrawImage(x + (w - sz.cx) / 2, zy0 - sz.cy, img, pi.showcodes);
}
x += w;
}
draw.DrawText(zx0,
f.sscript == 1 ? zy0 - ssa :
f.sscript == 2 ? zy0 + ssd - fi.GetHeight()
: zy0 - fi.GetAscent(),
text + pos, fnt, ink, len, wd + pos);
}
}

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

struct RichObjectImageMaker : ImageMaker {
RichObject object;
Size sz;
void *context;

virtual String Key() const;
virtual Image Make() const;
};

String RichObjectImageMaker::Key() const
{
StringBuffer b;
int64 id = object.GetSerialId();
b.Cat((const char *)&id, sizeof(id));
b.Cat((const char *)&sz.cx, sizeof(sz.cx));
b.Cat((const char *)&sz.cy, sizeof(sz.cy));
b.Cat((const char *)&context, sizeof(context));
return b;
}

Image RichObjectImageMaker::Make() const
{
return object.ToImage(sz, context);
/* ImageDraw iw(sz);
iw.DrawRect(sz, SColorPaper());
object.Paint(iw, sz, context);
return iw;
*/
}

void RichPara::Paint(PageDraw& pw, const Rect& page, PageY py, const PaintInfo& pi,
const Number& n, const Bits& spellerror,
int nbefore, int nline) const
{
Zoom z = pi.zoom;
PageY opy = py;
if(pw.tracer)
pw.tracer->Paragraph(page, py, *this);
Lines pl = Begin(page, py, nbefore, nline);
bool highlight = pi.highlightpara >= 0 && pi.highlightpara < pl.len;
int hy = py.y - format.before - format.ruler;
int phy = py.page;
if(format.ruler && hy >= 0 && hy + format.ruler < page.bottom)
pw.Page(phy).DrawRect(z * page.left + z * format.lm, z * hy,
z * page.right - z * page.left - z * format.rm - z * format.lm,
max(1, z * format.ruler), format.rulerink);
if(pi.sell < 0 && pi.selh > 0)
for(int p = opy.page; p <= py.page; p++) {
int top = z * (p == opy.page ? opy.y : page.top);
int bottom = z * (p == py.page ? py.y : page.bottom);
pw.Page(p).DrawRect(z * page.left, top, z * page.right - z * page.left,
bottom - top, InvertColor);
}
opy = py;
int oi = 0;
int x = 0;
int y0 = 0;
int lineascent = 0;
for(int lni = 0; lni < pl.GetCount(); lni++) {
const Line& li = pl[lni];
int linecy = li.Sum();
lineascent = li.ascent;
if(BreaksPage(py, pl, lni, page)) {
if(li.ppos > pi.sell && li.ppos < pi.selh) {
int y = z * py.y;
pw.Page(py.page).DrawRect(z * page.left, y, z * page.right - z * page.left,
z * page.bottom - y, InvertColor);
}
py.y = page.top;
py.page++;
}
if(py > pi.bottom)
break;

if(lni == 0 && format.label.GetCount() && !IsNull(pi.showcodes) && pi.showlabels)
pw.Page(py.page).DrawImage(z * page.left - 12, z * (py.y + (lineascent - 7) / 2),
RichTextImg::Label(), pi.showcodes);
const CharFormat **cf = pl.format + li.pos;
const CharFormat **i = cf;
const CharFormat **ilim = i + li.len;
const HeightInfo *hg = pl.height + li.pos;
if(py + linecy >= pi.top) {
Draw& draw = pw.Page(py.page);
#ifdef _DEBUG
int cloff = draw.GetCloffLevel();
#endif
Buffer<int> wd(li.len);
int x0 = li.xpos + page.left;
x = x0;
int *w = pl.width + li.pos;
int *wl = w + li.len;
int *t = wd;
while(w < wl) {
int x1 = x + *w++;
*t++ = z * x1 - z * x;
x = x1;
}

if(highlight)
draw.DrawRect(z * page.left, z * py.y, z * page.Width(),
z * (py.y + linecy) - z * py.y,
pi.highlight);

const CharFormat **i0 = i;
wchar *text = pl.text + li.pos;
x = x0;
w = pl.width + li.pos;
y0 = py.y + li.ascent;
int pp = li.pos;
int l = page.right;
int h = -1;

while(i < ilim) {
if(pl.pos[pp] == pi.sell && x < l)
l = x;
if(pl.pos[pp] == pi.selh && h < 0)
h = x;
pp++;
if(*i0 != *i || hg->object) {
Flush(draw, pi, text, i0, wd, (int)(i0 - cf),(int)( i - i0), x0, x, y0, py.y, linecy,
lineascent, z, highlight);
i0 = i;
x0 = x;
}
if(hg->object) {
const RichObject& o = *hg->object;
if(o) {
Size sz = z * o.GetSize();
int ix = z * x;
if(pi.shrink_oversized_objects && sz.cx + ix > page.right)
sz.cx = page.right - ix;
draw.DrawRect(ix, z * py.y, sz.cx, z * linecy, (*i)->paper);
draw.Clipoff(ix, z * (y0 - hg->ascent), sz.cx, sz.cy);
if(pi.sizetracking)
draw.DrawRect(sz, SColorFace);
else
if(pi.usecache) {
RichObjectImageMaker im;
im.object = o;
im.sz = sz;
im.context = pi.context;
draw.DrawImage(0, 0, MakeImagePaintOnly(im));
}
else
o.Paint(draw, sz, pi.context);
draw.End();
}
i++;
hg++;
x += *w++;
i0 = i;
x0 = x;
}
else {
i++;
hg++;
x += *w++;
}
}
if(i > i0)
Flush(draw, pi, text, i0, wd, (int)(i0 - cf), (int)(i - i0), x0, x, y0, py.y, linecy,
lineascent, z, highlight);
if(lni == 0) {
Rect r;
r.left = page.left + format.lm;
int q = li.ascent / 2;
r.top = py.y + 4 * (li.ascent - q) / 5;
r.right = r.left + q;
r.bottom = r.top + q;
q = z * (r.Width() / 4);
r.left = z * r.left;
r.top = z * r.top;
r.right = z * r.right;
r.bottom = z * r.bottom;
Rect r1 = r;
r1.Deflate(max(1, q));
switch(format.bullet) {
case BULLET_BOX:
draw.DrawRect(r, pi.coloroverride ? SColorText() : format.ink);
break;
case BULLET_BOXWHITE:
draw.DrawRect(r, pi.coloroverride ? SColorText() : format.ink);
draw.DrawRect(r1, White);
break;
case BULLET_ROUNDWHITE:
draw.DrawEllipse(r, pi.coloroverride ? SColorText() : format.ink);
draw.DrawEllipse(r1, pi.coloroverride ? SColorPaper() : White);
break;
case BULLET_ROUND:
draw.DrawEllipse(r, pi.coloroverride ? SColorText() : format.ink);
break;
default:
String s = n.AsText(format);
if(!IsNull(s)) {
CharFormat cf = li.len && *pl.format ? **pl.format : format;
cf.Height(z * cf.GetHeight());
draw.DrawText(r.left,
z * y0 - cf.Info().GetAscent(),
s, cf, pi.coloroverride ? SColorText() : cf.ink);
}
}
}
int zlcy = z * linecy;
if(pi.spellingchecker && zlcy > 3) {
int x = z * (li.xpos + page.left);
w = wd;
wl = w + li.len;
int i = li.pos;
int q = z * (py.y + linecy);
while(w < wl) {
if(spellerror[pl.pos[i++]]) {
if(zlcy > 16)
draw.DrawRect(x, q - 3, *w, 1, Red);
draw.DrawRect(x, q - 2, *w, 1, LtRed);
}
x += *w++;
}
}

if(pi.selh == li.ppos + li.plen)
h = x;
if(pi.sell == li.ppos + li.plen)
l = x;
if(pi.sell < li.ppos)
l = page.left;
if(pi.selh > li.ppos + li.plen)
h = page.right;
if(pi.sell < pi.selh && pi.selh > li.ppos)
draw.DrawRect(z * l, z * py.y, z * h - z * l,
z * (py.y + linecy) - z * py.y, InvertColor);
ASSERT(draw.GetCloffLevel() == cloff);
}
else
while(i < ilim) {
if(hg->object)
oi++;
i++;
hg++;
}
py.y += linecy;
}
Size sz = RichTextImg::EndParaChar().GetSize();
if(sz.cy < z * lineascent && !IsNull(pi.showcodes))
pw.Page(py.page).DrawImage(z * x, z * y0 - sz.cy,
RichTextImg::EndParaChar(),
format.indexentry.GetCount() ? pi.indexentry : pi.showcodes);
if(format.newpage && !IsNull(pi.showcodes)) {
Draw& w = pw.Page(opy.page);
int wd = z * page.right - z * page.left;
int step = w.Pixels() ? 8 : 50;
int y = z * opy.y;
for(int x = 0; x < wd; x += step)
w.DrawRect(z * page.left + x, y, step >> 1, step >> 3, pi.showcodes);
}
if(pl.len >= pi.sell && pl.len < pi.selh && py < pi.bottom) {
int top = z * py.y;
pw.Page(py.page).DrawRect(z * page.left, top, z * page.right - z * page.left,
z * min(py.y + format.after, page.bottom) - top, InvertColor);
}
if(pw.tracer)
pw.tracer->EndParagraph(py);
}

void RichPara::GetRichPos(RichPos& rp, int pos) const
{
rp.format = format;
int i = FindPart(pos);
if(i < GetCount()) {
const Part& p = part[i];
(CharFormat&)rp.format = p.format;
if(p.object) {
rp.chr = -1;
rp.object = p.object;
}
else
if(p.IsText())
rp.chr = p.text[pos];
else {
rp.chr = -2;
rp.field = p.field;
rp.fieldparam = p.fieldparam;
if(p.fieldpart.GetCount() > 0)
rp.fieldformat = p.fieldpart[0].format;
}
}
else
rp.chr = '\n';
}

RichCaret RichPara::GetCaret(int pos, const Rect& page, PageY py, int nbefore, int nline) const
{
Lines pl = Begin(page, py, nbefore, nline);
RichCaret pr;
FontInfo fi = format.Info();
pr.caretascent = fi.GetAscent();
pr.caretdescent = fi.GetDescent();
for(int lni = 0; lni < pl.GetCount(); lni++) {
Line& li = pl[lni];
int linecy = li.Sum();
if(BreaksPage(py, pl, lni, page)) {
py.y = page.top;
py.page++;
}
pr.page = py.page;
pr.top = py.y;
pr.bottom = py.y + linecy;
pr.lineascent = li.ascent;
pr.line = lni;
if(pos < li.ppos + li.plen) {
int *w = pl.width + li.pos;
int *p = pl.pos + li.pos;
const CharFormat **i = pl.format + li.pos;
const HeightInfo *h = pl.height + li.pos;
int x = li.xpos + page.left;
if(li.len && *i) {
pr.caretascent = h->ascent;
pr.caretdescent = h->descent;
}
while(pos > *p) {
x += *w++;
if(*i) {
pr.caretascent = h->ascent;
pr.caretdescent = h->descent;
}
h++;
i++;
p++;
}
pr.objectcy = h->ascent + h->ydelta;
pr.objectyd = h->ydelta;
pr.left = x;
pr.right = x + *w;
pr.caretdescent = min(pr.caretdescent, pr.Height() - pr.lineascent);
return pr;
}
py.y += linecy;
}
const Line& li = pl.line.Top();
pr.left = li.cx + li.xpos + page.left;
pr.right = page.right;
pr.caretdescent = min(pr.caretdescent, pr.Height() - pr.lineascent);
return pr;
}

int RichPara::PosInLine(int x, const Rect& page, const Lines& pl, int lni) const
{
const Line& li = pl[lni];
const int *w = pl.width + li.pos;
const int *wl = w + li.len;
if(lni < pl.GetCount() - 1 && li.len > 0 && pl.text[li.pos + li.len - 1] == ' ')
wl--;
int xp = li.xpos + page.left;
while(w < wl && xp + *w <= x)
xp += *w++;
int pos = (int)(w - pl.width);
return pos < pl.clen ? pl.pos[pos] : pl.len;
}

int RichPara::GetPos(int x, PageY y, const Rect& page, PageY py, int nbefore, int nline) const
{
Lines pl = Begin(page, py, nbefore, nline);
if(pl.len)
for(int lni = 0; lni < pl.GetCount(); lni++) {
const Line& li = pl[lni];
int linecy = li.Sum();
if(BreaksPage(py, pl, lni, page)) {
py.y = page.top;
py.page++;
}
py.y += linecy;
if(y < py || lni == pl.GetCount() - 1)
return PosInLine(x, page, pl, lni);
}
return pl.len;
}

int RichPara::GetVertMove(int pos, int gx, const Rect& page, int dir) const
{
Lines pl = FormatLines(page.Width());
int lni;
if(pos >= 0) {
for(lni = 0; lni < pl.GetCount() - 1; lni++) {
const Line& li = pl[lni];
if(pos < li.ppos + li.plen)
break;
}
lni += sgn(dir);
if(lni < 0 || lni >= pl.GetCount())
return -1;
}
else
lni = dir < 0 ? pl.GetCount() - 1 : 0;
return PosInLine(gx, page, pl, lni);
}

void RichPara::GatherLabels(Vector<RichValPos>& info, const Rect& page, PageY py,
int pos, int nbefore, int nline) const
{
Lines pl = Begin(page, py, nbefore, nline);
WString ie;
if(!pl.GetCount())
return;
if(BreaksPage(py, pl, 0, page)) {
py.y = page.top;
py.page++;
}
if(format.label.IsEmpty())
return;
RichValPos& f = info.Add();
f.py = py;
f.pos = pos;
f.data = format.label.ToWString();
}

void RichPara::GatherIndexes(Vector<RichValPos>& info, const Rect& page, PageY py,
int pos, int nbefore, int nline) const
{
Lines pl = Begin(page, py, nbefore, nline);
WString ie;
for(int lni = 0; lni < pl.GetCount(); lni++) {
Line& li = pl[lni];
int linecy = li.Sum();
if(BreaksPage(py, pl, lni, page)) {
py.y = page.top;
py.page++;
}
const CharFormat **i0 = pl.format + li.pos;
const CharFormat **i = i0;
const CharFormat **ilim = i + li.len;
while(i < ilim) {
if(*i && (*i)->indexentry != ie) {
ie = (*i)->indexentry;
if(!ie.IsEmpty()) {
RichValPos& f = info.Add();
f.py = py;
f.pos = (int)(i - i0) + pos;
f.data = ie;
}
}
i++;
}
py.y += linecy;
}
}

void FontHeightRound(Font& fnt, Zoom z)
{
fnt.Height((fnt.GetHeight() * z.m + (z.d >> 1)) / z.d);
}

void operator*=(RichPara::Format& format, Zoom z)
{
FontHeightRound(format, z);
format.before *= z;
if(format.ruler) {
format.ruler *= z;
if(format.ruler == 0)
format.ruler = 1;
}
int ll = format.lm + format.indent;
format.lm *= z;
format.indent = z * ll - format.lm;
format.rm *= z;
format.after *= z;
format.tabsize *= z;
for(int i = 0; i < format.tab.GetCount(); i++) {
int& pos = format.tab[i].pos;
pos = ((pos & ~RichPara::TAB_RIGHTPOS) * z) | (pos & RichPara::TAB_RIGHTPOS);
}
}

void RichPara::ApplyZoom(Zoom z)
{
format *= z;
for(int i = 0; i < part.GetCount(); i++)
if(part[i].IsText())
FontHeightRound(part[i].format, z);
else
if(part[i].object)
part[i].object.SetSize(z * part[i].object.GetSize());
}

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

r2172 by cxl on Mar 4, 2010   Diff
RichText, CtrlLib: PaintInfo -
shrink_oversized_objects, used in
RichTextView
r2095 by cxl on Feb 18, 2010   Diff
RichText: RightPos tabulators
(relative to right side)
r1720 by cxl on Nov 25, 2009   Diff
RichEdit minor fixes, ViewBorder
method
All revisions of this file

File info

Size: 16474 bytes, 604 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting