/
gltextcomposer.cpp
565 lines (476 loc) · 16.2 KB
/
gltextcomposer.cpp
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
/** @file gltextcomposer.cpp
*
* @authors Copyright (c) 2013-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/GLTextComposer"
namespace de {
using namespace ui;
DE_PIMPL(GLTextComposer)
{
static const Rangei MAX_VISIBLE_RANGE;
const Font * font = nullptr;
Atlas * atlas = nullptr;
String text;
FontLineWrapping const *wraps = nullptr;
Font::RichFormat format;
bool needRedo = false; ///< Release completely and allocate.
int maxGeneratedWidth = 0;
Rangei visibleLineRange{MAX_VISIBLE_RANGE}; ///< Only these lines will be updated/drawn.
bool visibleLineRangeChanged = false;
int maxGeneratedWidth = 0;
struct Line {
struct Segment {
Id id;
// CString range;
String text;
int x;
int width;
bool compressed;
Segment() : id(Id::None), x(0), width(0), compressed(false) {}
int right() const { return x + width; }
};
List<Segment> segs;
};
List<Line> lines;
Impl(Public *i) : Base(i) {}
~Impl()
{
releaseLines();
}
void releaseLines()
{
if (atlas)
{
for (int i = 0; i < lines.sizei(); ++i)
{
releaseLine(i);
}
}
lines.clear();
}
void releaseOutsideRange()
{
if (!atlas) return;
for (int i = 0; i < lines.sizei(); ++i)
{
if (!isLineVisible(i))
{
releaseLine(i, ReleaseButKeepSegs);
}
}
}
enum ReleaseBehavior { ReleaseFully, ReleaseButKeepSegs };
void releaseLine(int index, ReleaseBehavior behavior = ReleaseFully)
{
Line &ln = lines[index];
for (int i = 0; i < ln.segs.sizei(); ++i)
{
if (!ln.segs[i].id.isNone())
{
atlas->release(ln.segs[i].id);
ln.segs[i].id = Id::None;
}
}
if (behavior == ReleaseFully)
{
ln.segs.clear();
}
}
bool isLineVisible(int line) const
{
return visibleLineRange.contains(line);
}
CString segmentText(int seg, FontLineWrapping::LineInfo const &info) const
{
return info.segs[seg].range;
}
bool matchingSegments(int lineIndex, FontLineWrapping::LineInfo const &info) const
{
if (info.segs.size() != lines[lineIndex].segs.size())
{
//qDebug() << "line" << lineIndex << "number of segs changed";
return false;
}
for (int i = 0; i < info.segs.sizei(); ++i)
{
if (lines[lineIndex].segs[i].text != info.segs[i].range)
{
// Range has changed.
//qDebug() << "line" << lineIndex << "seg" << i << "range change";
return false;
}
if (lines[lineIndex].segs[i].text != segmentText(i, info))
{
// Text has changed.
//qDebug() << "line" << lineIndex << "seg" << i << "text change";
return false;
}
if (lines[lineIndex].segs[i].id.isNone() && info.segs[i].range.size() > 0)
{
// This segment has previously failed allocation.
//qDebug() << "line" << lineIndex << "seg" << i << "not alloced before, len" <<
// info.segs[i].range.size();
return false;
}
}
return true;
}
bool allocLines()
{
DENG2_GUARD(wraps);
bool changed = false;
const int wrapsHeight = wraps->height();
for (int i = 0; i < wrapsHeight; ++i)
{
const auto &info = wraps->lineInfo(i);
if (i < lines.sizei())
{
// Is the rasterized copy up to date?
if (matchingSegments(i, info))
{
// This line can be kept as is.
continue;
}
// Needs to be redone.
releaseLine(i);
}
changed = true;
if (i >= lines.sizei())
{
// Need another line.
lines << Line();
}
DE_ASSERT(i < lines.sizei());
DE_ASSERT(lines[i].segs.isEmpty());
Line &line = lines[i];
for (int k = 0; k < info.segs.sizei(); ++k)
{
Line::Segment seg;
seg.text = info.segs[k].range;
// seg.text = segmentText(k, info);
if (isLineVisible(i) && seg.text)
{
// The color is white unless a style is defined.
Vec4ub fgColor(255, 255, 255, 255);
if (format.hasStyle())
{
fgColor = format.style().richStyleColor(Font::RichFormat::NormalColor);
}
//qDebug() << "allocating" << seg.text << seg.range.asText();
Image const segmentImage = wraps->rasterizedSegment(i, k);
seg.id = atlas->alloc(segmentImage.multiplied(fgColor));
}
line.segs << seg;
}
DE_ASSERT(line.segs.size() == info.segs.size());
}
// Remove the excess lines.
while (lines.sizei() > wraps->height())
{
releaseLine(lines.sizei() - 1);
lines.removeLast();
changed = true;
}
wraps->clearRasterizedLines();
DE_ASSERT(wraps->height() == lines.sizei());
return changed;
}
void updateLineLayout(Rangei const &lineRange)
{
if (lineRange.isEmpty()) return;
Rangei current = lineRange;
for (;;)
{
int end = updateLineLayoutUntilUntabbed(current);
if (end == lineRange.end)
{
break; // Whole range done.
}
current = Rangei(end, lineRange.end);
}
}
/**
* Attempts to update lines in the specified range, but stops if an
* untabbed line is encountered. This ensures that each distinct tabbed
* content subrange uses its own alignment.
*
* @param lineRange Range of lines to update.
*
* @return The actual end of the updated range.
*/
inline int updateLineLayoutUntilUntabbed(Rangei const &lineRange)
{
bool includesTabbedLines = false;
int rangeEnd = lineRange.end;
// Find the highest tab in use and initialize seg widths.
int highestTab = 0;
for (int i = lineRange.start; i < lineRange.end; ++i)
{
int lineStop = wraps->lineInfo(i).highestTabStop();
if (lineStop >= 0)
{
// The range now includes at least one tabbed line.
includesTabbedLines = true;
}
if (lineStop < 0)
{
// This is an untabbed line.
if (!includesTabbedLines)
{
// We can do many untabbed lines in the range as long as
// there are no tabbed ones.
rangeEnd = i + 1;
}
else
{
// An untabbed line will halt the process for now.
rangeEnd = de::max(i, lineRange.start + 1);
break;
}
}
highestTab = de::max(highestTab, lineStop);
// Initialize the segments with indentation.
for (int k = 0; k < lines[i].segs.sizei(); ++k)
{
lines[i].segs[k].width = wraps->lineInfo(i).segs[k].width;
}
}
DE_ASSERT(rangeEnd > lineRange.start);
// Set segment X coordinates by stacking them left-to-right on each line.
for (int i = lineRange.start; i < rangeEnd; ++i)
{
if (lines[i].segs.isEmpty() || i >= visibleLineRange.end)
continue;
lines[i].segs[0].x = wraps->lineInfo(i).indent;
for (int k = 1; k < lines[i].segs.sizei(); ++k)
{
Impl::Line::Segment &seg = lines[i].segs[k];
seg.x = lines[i].segs[k - 1].right();
}
}
// Align each tab stop with other matching stops on the other lines.
for (int tab = 1; tab <= highestTab; ++tab)
{
int maxRight = 0;
// Find the maximum right edge for this spot.
for (int i = lineRange.start; i < rangeEnd; ++i)
{
if (i >= visibleLineRange.end) break;
FontLineWrapping::LineInfo const &info = wraps->lineInfo(i);
DE_ASSERT(info.segs.size() == lines[i].segs.size());
for (int k = 0; k < info.segs.sizei(); ++k)
{
Impl::Line::Segment &seg = lines[i].segs[k];
if (info.segs[k].tabStop >= 0 && info.segs[k].tabStop < tab)
{
maxRight = de::max(maxRight, seg.right());
}
}
}
// Move the segments to this position.
for (int i = lineRange.start; i < rangeEnd; ++i)
{
if (i >= visibleLineRange.end) break;
int localRight = maxRight;
FontLineWrapping::LineInfo const &info = wraps->lineInfo(i);
for (int k = 0; k < info.segs.sizei(); ++k)
{
if (info.segs[k].tabStop == tab)
{
lines[i].segs[k].x = localRight;
localRight += info.segs[k].width;
}
}
}
}
return rangeEnd;
}
};
const Rangei GLTextComposer::Impl::MAX_VISIBLE_RANGE{0, 0x7fffffff};
GLTextComposer::GLTextComposer() : d(new Impl(this))
{}
void GLTextComposer::release()
{
d->releaseLines();
setRange(Impl::MAX_VISIBLE_RANGE);
setState(false);
}
void GLTextComposer::releaseLinesOutsideRange()
{
d->releaseOutsideRange();
}
void GLTextComposer::setAtlas(Atlas &atlas)
{
d->atlas = &atlas;
}
void GLTextComposer::setWrapping(FontLineWrapping const &wrappedLines)
{
if (d->wraps != &wrappedLines)
{
d->wraps = &wrappedLines;
d->needRedo = true;
setState(false);
}
}
void GLTextComposer::setText(String const &text)
{
setText(text, Font::RichFormat::fromPlainText(text));
}
void GLTextComposer::setStyledText(String const &styledText)
{
d->format.clear();
d->text = d->format.initFromStyledText(styledText);
d->needRedo = true;
setState(false);
}
void GLTextComposer::setText(String const &text, Font::RichFormat const &format)
{
d->text = text;
d->format = format;
d->needRedo = true;
setState(false);
}
void GLTextComposer::setRange(Rangei const &visibleLineRange)
{
if (d->visibleLineRange != visibleLineRange)
{
d->visibleLineRange = visibleLineRange;
d->visibleLineRangeChanged = true;
}
}
Rangei GLTextComposer::range() const
{
return d->visibleLineRange;
}
bool GLTextComposer::update()
{
DE_ASSERT(d->wraps);
bool changed = false;
// If a font hasn't been defined, there isn't much to do.
if (!d->wraps->hasFont())
{
return false;
}
if (d->font != &d->wraps->font())
{
d->font = &d->wraps->font();
d->needRedo = true;
}
if (d->needRedo)
{
d->releaseLines();
d->needRedo = false;
changed = d->allocLines();
}
else if (d->visibleLineRangeChanged)
{
changed = d->allocLines();
d->visibleLineRangeChanged = false;
}
setState(true);
return changed;
}
void GLTextComposer::forceUpdate()
{
d->needRedo = true;
}
void GLTextComposer::makeVertices(GuiVertexBuilder &triStrip,
Vec2i const & topLeft,
Alignment const & lineAlign,
Vec4f const & color)
{
makeVertices(triStrip, Rectanglei(topLeft, topLeft), AlignTopLeft, lineAlign, color);
}
void GLTextComposer::makeVertices(GuiVertexBuilder &triStrip,
Rectanglei const &rect,
Alignment const & alignInRect,
Alignment const & lineAlign,
Vec4f const & color)
{
if (!isReady()) return;
DE_ASSERT(d->wraps != nullptr);
DE_ASSERT(d->font != nullptr);
Vec2i const contentSize(d->wraps->width(), d->wraps->totalHeightInPixels());
// Apply alignment within the provided rectangle.
Vec2f p = applyAlignment(alignInRect, contentSize, rect);
DE_ASSERT(d->wraps->height() == d->lines.sizei());
// Align segments based on tab stops.
d->updateLineLayout(Rangei(0, d->lines.sizei()));
// Compress lines to fit into the maximum allowed width.
for (int i = 0; i < d->lines.sizei(); ++i)
{
Impl::Line &line = d->lines[i];
if (!d->isLineVisible(i) || line.segs.isEmpty()) continue;
/*
if (!d->wraps->lineInfo(i).segs.last().tabStop)
continue;
*/
Impl::Line::Segment &seg = line.segs.last();
int const leeway = 3;
if (seg.right() > d->wraps->maximumWidth() + leeway)
{
// Needs compressing (up to 10%).
seg.compressed = true;
seg.width = de::max(int(seg.width * .9f), d->wraps->maximumWidth() + leeway - seg.x);
}
}
d->maxGeneratedWidth = 0;
// Generate vertices for each line.
for (int i = 0; i < d->wraps->height(); ++i)
{
Impl::Line const &line = d->lines[i];
if (d->isLineVisible(i))
{
FontLineWrapping::LineInfo const &info = d->wraps->lineInfo(i);
Vec2f linePos = p;
for (int k = 0; k < info.segs.sizei(); ++k)
{
Impl::Line::Segment const &seg = line.segs[k];
// Empty lines are skipped.
if (seg.id.isNone()) continue;
Vec2ui size = d->atlas->imageRect(seg.id).size();
if (seg.compressed)
{
size.x = seg.width;
}
// Line alignment.
/// @todo How to center/right-align text that uses tab stops?
if (line.segs.size() == 1 && d->wraps->lineInfo(0).segs[0].tabStop < 0)
{
if (lineAlign.testFlag(AlignRight))
{
linePos.x += int(contentSize.x) - int(size.x);
}
else if (!lineAlign.testFlag(AlignLeft))
{
linePos.x += (int(contentSize.x) - int(size.x)) / 2;
}
}
Rectanglef const uv = d->atlas->imageRectf(seg.id);
auto const segRect = Rectanglef::fromSize(linePos + Vec2f(seg.x, 0), size);
triStrip.makeQuad(segRect, color, uv);
// Keep track of how wide the geometry really is.
d->maxGeneratedWidth = de::max(d->maxGeneratedWidth, int(segRect.right() - p.x));
}
}
p.y += d->font->lineSpacing().value();
}
}
int GLTextComposer::verticesMaxWidth() const
{
return d->maxGeneratedWidth;
}
} // namespace de