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
|
#include "gmdocument.h"
#include "ui/color.h"
#include "ui/text.h"
#include <the_Foundation/array.h>
struct Impl_GmDocument {
iObject object;
iString source;
iInt2 size;
iArray layout; /* contents of source, laid out in document space */
};
iDefineObjectConstruction(GmDocument)
static void doLayout_GmDocument_(iGmDocument *d) {
if (d->size.x <= 0 || isEmpty_String(&d->source)) {
return;
}
clear_Array(&d->layout);
iBool isPreformat = iFalse;
iInt2 pos = zero_I2();
const iRangecc content = range_String(&d->source);
iRangecc line = iNullRange;
while (nextSplit_Rangecc(&content, "\n", &line)) {
iGmRun run;
run.text = line;
run.font = paragraph_FontId;
run.color = white_ColorId;
run.bounds.pos = pos;
run.bounds.size = advanceN_Text(run.font, line.start, size_Range(&line));
run.linkId = 0;
pushBack_Array(&d->layout, &run);
pos.y += run.bounds.size.y;
}
d->size.y = pos.y;
}
void init_GmDocument(iGmDocument *d) {
init_String(&d->source);
d->size = zero_I2();
init_Array(&d->layout, sizeof(iGmRun));
}
void deinit_GmDocument(iGmDocument *d) {
deinit_Array(&d->layout);
deinit_String(&d->source);
}
void setWidth_GmDocument(iGmDocument *d, int width) {
d->size.x = width;
doLayout_GmDocument_(d); /* TODO: just flag need-layout and do it later */
}
void setSource_GmDocument(iGmDocument *d, const iString *source, int width) {
set_String(&d->source, source);
setWidth_GmDocument(d, width);
/* TODO: just flag need-layout and do it later */
}
void render_GmDocument(const iGmDocument *d, iRangei visRangeY, iGmDocumentRenderFunc render,
void *context) {
iBool isInside = iFalse;
iConstForEach(Array, i, &d->layout) {
const iGmRun *run = i.value;
if (isInside) {
if (top_Rect(run->bounds) > visRangeY.end) {
break;
}
render(context, run);
}
else if (bottom_Rect(run->bounds) >= visRangeY.start) {
isInside = iTrue;
render(context, run);
}
}
}
iDefineClass(GmDocument)
|