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
#include "RichText.h"

NAMESPACE_UPP

void RichPara::Smh(Lines& lines, HeightInfo *th, int cx) const
{
Line& l = lines.line.Top();
l.ascent = l.descent = l.external = 0;
const HeightInfo *he = th + l.pos + l.len;
for(const HeightInfo *h = th + l.pos; h < he; h++) {
if(h->ascent > l.ascent) l.ascent = h->ascent;
if(h->descent > l.descent) l.descent = h->descent;
if(h->external > l.external) l.external = h->external;
}
if(format.linespacing == LSP15) {
l.ascent = (3 * l.ascent) >> 1;
l.descent = (3 * l.descent) >> 1;
}
if(format.linespacing == LSP20) {
l.ascent = 2 * l.ascent;
l.descent = 2 * l.descent;
}
l.xpos = format.lm;
cx -= format.lm + format.rm;
l.xpos += lines.GetCount() == 1 ? lines.first_indent : lines.next_indent;
if(!l.withtabs)
switch(format.align) {
case ALIGN_RIGHT:
l.xpos += cx - l.cx;
break;
case ALIGN_CENTER:
l.xpos += (cx - l.cx) / 2;
break;
}
l.cx -= lines.GetCount() == 1 ? lines.first_indent : lines.next_indent;
}

RichPara::Tab RichPara::GetNextTab(int pos, int cx) const
{
int tabi = -1;
int dist = INT_MAX;
for(int i = 0; i < format.tab.GetCount(); i++) {
const Tab& tab = format.tab[i];
int tabpos = tab.pos;
if(tabpos & TAB_RIGHTPOS)
tabpos = cx - (tabpos & ~TAB_RIGHTPOS);
if(tabpos > pos && tabpos - pos < dist) {
tabi = i;
dist = tabpos - pos;
}
}
if(format.bullet == BULLET_TEXT) {
int q = format.indent + format.lm;
if(q > pos && q - pos < dist) {
Tab tab;
tab.align = ALIGN_LEFT;
tab.pos = q;
return tab;
}
}
if(tabi < 0) {
Tab tab;
tab.pos = format.tabsize ? (pos + format.tabsize) / format.tabsize * format.tabsize : 0;
tab.align = ALIGN_LEFT;
return tab;
}
Tab tab = format.tab[tabi];
if(tab.pos & TAB_RIGHTPOS)
tab.pos = cx - (tab.pos & ~TAB_RIGHTPOS);
return tab;
}

struct RichPara::StorePart {
wchar *t;
int *w;
int *p;
const CharFormat **f;
HeightInfo *h;
int pos;
FontInfo pfi;

void Store(Lines& lines, const Part& p, int pinc);
};

void RichPara::StorePart::Store(Lines& lines, const Part& part, int pinc)
{
CharFormat& pfmt = lines.hformat.Add();
pfmt = part.format;
if(part.field && pinc) {
for(int i = 0; i < part.fieldpart.GetCount(); i++)
Store(lines, part.fieldpart[i], 0);
pos++;
}
else
if(part.object) {
*f++ = &pfmt;
Size sz = part.object.GetSize();
*w++ = sz.cx;
h->ydelta = part.object.GetYDelta();
h->ascent = sz.cy - h->ydelta;
h->descent = max(h->ydelta, 0);
h->external = 0;
lines.object.Add(part.object);
h->object = &lines.object.Top();
h++;
*t++ = 'x';
*p++ = pos;
pos += pinc;
}
else {
const wchar *s = part.text;
const wchar *lim = part.text.End();
Font fnt = part.format;
FontInfo fi = fnt.Info();
FontInfo wfi = fi;
if(part.format.sscript) {
fnt.Height(fnt.GetHeight() * 3 / 5);
wfi = fnt.Info();
}
if(part.format.capitals) {
CharFormat& cfmt = lines.hformat.Add();
cfmt = part.format;
cfmt.Height(cfmt.GetHeight() * 4 / 5);
FontInfo cfi = cfmt.Info();
FontInfo cwfi = cfi;
if(part.format.sscript) {
Font fnt = cfmt;
fnt.Height(fnt.GetHeight() * 3 / 5);
cwfi = fnt.Info();
}

while(s < lim) {
wchar c = *s++;
if(c == 9) {
*f++ = &pfmt;
h->ascent = pfi.GetAscent();
h->descent = pfi.GetDescent();
h->external = pfi.GetExternal();
*w++ = 0;
}
else
if(IsLower(c)) {
*f++ = &cfmt;
c = ToUpper(c);
h->ascent = cfi.GetAscent();
h->descent = cfi.GetDescent();
h->external = cfi.GetExternal();
*w++ = c >= 32 ? cwfi[c] : 0;
}
else {
*f++ = &pfmt;
h->ascent = fi.GetAscent();
h->descent = fi.GetDescent();
h->external = fi.GetExternal();
*w++ = c >= 32 ? wfi[c] : 0;
}
h->object = NULL;
*t++ = c;
*p++ = pos;
pos += pinc;
h++;
}
}
else {
while(s < lim) {
wchar c = *s++;
*f++ = &pfmt;
if(c == 9) {
h->ascent = pfi.GetAscent();
h->descent = pfi.GetDescent();
h->external = pfi.GetExternal();
}
else {
h->ascent = fi.GetAscent();
h->descent = fi.GetDescent();
h->external = fi.GetExternal();
}
h->object = NULL;
*p++ = pos;
pos += pinc;
h++;
*w++ = c >= 32 ? wfi[c] : 0;
*t++ = c;
}
}
}
}

static int CountChars(const Array<RichPara::Part>& part)
{
int n = 0;
for(int i = 0; i < part.GetCount(); i++) {
const RichPara::Part& p = part[i];
if(p.field)
n += CountChars(p.fieldpart);
else
n += p.GetLength();
}
return n;
}

RichPara::Lines::Lines()
{
justified = false;
incache = false;
cacheid = 0;
}

Array<RichPara::Lines>& RichPara::Lines::Cache()
{
static Array<Lines> x;
return x;
}

static StaticMutex sLineCacheMutex;

RichPara::Lines::~Lines()
{
if(cacheid && !line.IsPicked() && !incache) {
Mutex::Lock __(sLineCacheMutex);
Array<Lines>& cache = Cache();
incache = true;
cache.Insert(0) = *this;
cache.SetCount(1);
int total = 0;
for(int i = 1; i < cache.GetCount(); i++) {
total += cache[i].clen;
if(total > 10000 || i > 64) {
cache.SetCount(i);
break;
}
i++;
}
}
}

RichPara::Lines RichPara::FormatLines(int acx) const
{
Lines lines;
if(cacheid) {
Mutex::Lock __(sLineCacheMutex);
Array<Lines>& cache = Lines::Cache();
for(int i = 0; i < cache.GetCount(); i++)
if(cache[i].cacheid == cacheid && cache[i].cx == acx) {
lines = cache[i];
lines.incache = false;
cache.Remove(i);
return lines;
}
}

int i;
lines.cacheid = cacheid;
lines.cx = acx;
lines.len = GetLength();
lines.clen = CountChars(part);
lines.first_indent = lines.next_indent = format.indent;
if(format.bullet == BULLET_TEXT)
lines.first_indent = 0;
else
if(!format.bullet && !format.IsNumbered())
lines.next_indent = 0;

FontInfo pfi = format.Info();
if(lines.len == 0) {
Line& l = lines.line.Add();
l.pos = 0;
l.ppos = 0;
l.plen = 0;
l.len = 0;
l.cx = lines.first_indent;
l.withtabs = false;
HeightInfo dummy;
Smh(lines, &dummy, lines.cx);
l.ascent = pfi.GetAscent();
l.descent = pfi.GetDescent();
l.external = pfi.GetExternal();
return lines;
}

lines.text.Alloc(lines.clen);
lines.width.Alloc(lines.clen);
lines.pos.Alloc(lines.clen);
lines.format.Alloc(lines.clen);
lines.height.Alloc(lines.clen);

StorePart sp;
sp.t = lines.text;
sp.w = lines.width;
sp.p = lines.pos;
sp.f = lines.format;
sp.h = lines.height;
sp.pfi = pfi;
sp.pos = 0;

for(i = 0; i < part.GetCount(); i++)
sp.Store(lines, part[i], 1);

wchar *s = lines.text;
wchar *text = s;
wchar *end = lines.text + lines.clen;
wchar *space = NULL;
int *w = lines.width;
int cx = lines.first_indent;
int rcx = lines.cx - format.lm - format.rm;
bool withtabs = false;
int scx = cx;
while(s < end) {
Tab t;
if(*s == ' ') {
space = s;
scx = cx;
}
else {
if(*s == '\t') {
t = GetNextTab(cx + format.lm, rcx);
space = NULL;
}
if(cx + *w > rcx && s > text ||
*s == '\t' && (t.align == ALIGN_RIGHT ? t.pos - format.lm > rcx
: t.pos - format.lm >= rcx)) {
Line& l = lines.line.Add();
l.withtabs = withtabs;
l.pos = (int)(text - lines.text);
if(space) {
l.len = (int)(space - text) + 1;
l.cx = scx;
text = s = space + 1;
}
else {
l.len = (int)(s - text);
l.cx = cx;
text = s;
}
Smh(lines, lines.height, lines.cx);
cx = lines.next_indent;
w = text - ~lines.text + lines.width;
space = NULL;
rcx = lines.cx - format.lm - format.rm;
withtabs = false;
t = GetNextTab(cx + format.lm, acx);
}
}
if(*s == '\t') {
*s += t.fillchar;
if(t.align == ALIGN_LEFT) {
*w++ = t.pos - format.lm - cx;
cx = t.pos - format.lm;
}
else {
int tcx = 0;
int *tw = w + 1;
for(wchar *ts = s + 1; ts < end && *ts != '\t'; ts++)
tcx += *tw++;
int ww = t.pos - format.lm - cx - (t.align == ALIGN_RIGHT ? tcx : tcx / 2);
if(ww > 0) {
*w++ = ww;
cx += ww;
}
else
*w++ = 0;
}
withtabs = true;
}
else
cx += *w++;
s++;
}
Line& l = lines.line.Add();
l.withtabs = withtabs;
l.pos = (int)(text - lines.text);
l.len = (int)(s - text);
l.cx = cx;
Smh(lines, lines.height, lines.cx);
for(i = 0; i < lines.line.GetCount(); i++) {
Line& l = lines.line[i];
l.ppos = lines.pos[l.pos];
l.plen = (l.pos + l.len < lines.clen ? lines.pos[l.pos + l.len] : lines.len) - l.ppos;
}

return lines;
}

void RichPara::Lines::Justify(const RichPara::Format& format)
{
if(justified)
return;
justified = true;
if(format.align != ALIGN_JUSTIFY) return;
for(int i = 0; i < line.GetCount() - 1; i++) {
const Line& li = line[i];
if(!li.withtabs && li.len) {
const wchar *s = ~text + li.pos;
const wchar *lim = s + li.len;
while(lim - 1 > s) {
if(*(lim - 1) != ' ') break;
lim--;
}
while(s < lim) {
if(*s != ' ' && *s != 160) break;
s++;
}

const wchar *beg = s;
int nspc = 0;
while(s < lim) {
if(*s == ' ' || *s == 160) nspc++;
s++;
}
s = beg;
if(nspc) {
int q = ((cx - format.lm - format.rm -
(i == 0 ? first_indent : next_indent) - li.cx) << 16)
/ nspc;
int *w = beg - ~text + width;
int prec = 0;
while(s < lim) {
if(*s == ' ' || *s == 160) {
*w += (prec + q) >> 16;
prec = (prec + q) & 0xffff;
}
w++;
s++;
}
}
}
}
}

int RichPara::Lines::BodyHeight() const
{
int sum = 0;
for(int i = 0; i < line.GetCount(); i++)
sum += line[i].Sum();
return sum;
}

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

r2167 by cxl on Mar 3, 2010   Diff
*RichText: Fixed Image
typesetting/caching issue
r2095 by cxl on Feb 18, 2010   Diff
RichText: RightPos tabulators
(relative to right side)
r1524 by cxl on Aug 23, 2009   Diff
RichText: Optimized by caching
paragraph data and layout
All revisions of this file

File info

Size: 9700 bytes, 438 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting