summaryrefslogtreecommitdiff
path: root/src/gmdocument.c
blob: 42bc7515eac8ae083a7de3fe85c8d4e256b2f2cf (plain)
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
#include "gmdocument.h"
#include "ui/color.h"
#include "ui/text.h"
#include "ui/metrics.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)

enum iGmLineType {
    text_GmLineType,
    bullet_GmLineType,
    preformatted_GmLineType,
    quote_GmLineType,
    header1_GmLineType,
    header2_GmLineType,
    header3_GmLineType,
    max_GmLineType,
};

static enum iGmLineType lineType_Rangecc_(const iRangecc *line) {
    if (isEmpty_Range(line)) {
        return text_GmLineType;
    }
    if (startsWithSc_Rangecc(line, "###", &iCaseSensitive)) {
        return header3_GmLineType;
    }
    if (startsWithSc_Rangecc(line, "##", &iCaseSensitive)) {
        return header2_GmLineType;
    }
    if (startsWithSc_Rangecc(line, "#", &iCaseSensitive)) {
        return header1_GmLineType;
    }
    if (startsWithSc_Rangecc(line, "```", &iCaseSensitive)) {
        return preformatted_GmLineType;
    }
    if (*line->start == '>') {
        return quote_GmLineType;
    }
    if (size_Range(line) >= 2 && line->start[0] == '*' && isspace(line->start[1])) {
        return bullet_GmLineType;
    }
    return text_GmLineType;
}

static void trimLine_Rangecc_(iRangecc *line, enum iGmLineType type) {
    static const unsigned int skip[max_GmLineType] = { 0, 2, 3, 1, 1, 2, 3 };
    line->start += skip[type];
    trim_Rangecc(line);
}

static int lastVisibleRunBottom_GmDocument_(const iGmDocument *d) {
    iReverseConstForEach(Array, i, &d->layout) {
        const iGmRun *run = i.value;
        if (isEmpty_Range(&run->text)) {
            continue;
        }
        return bottom_Rect(run->bounds);
    }
    return 0;
}

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;
    static const int fonts[max_GmLineType] = {
        paragraph_FontId,
        paragraph_FontId, /* bullet */
        preformatted_FontId,
        quote_FontId,
        header1_FontId,
        header2_FontId,
        header3_FontId
    };
    static const int indents[max_GmLineType] = {
        4, 10, 4, 10, 0, 0, 0
    };
    static const float topMargin[max_GmLineType] = {
        0.0f, 0.5f, 1.0f, 0.5f, 2.0f, 1.5f, 1.0f
    };
    static const float bottomMargin[max_GmLineType] = {
        0.0f, 0.5f, 1.0f, 0.5f, 1.0f, 1.0f, 1.0f
    };
    static const char *bullet = "\u2022";
    iRangecc preAltText = iNullRange;
    enum iGmLineType prevType = text_GmLineType;
    iBool isFirstText = iTrue;
    while (nextSplit_Rangecc(&content, "\n", &line)) {
        iGmRun run;
        run.color = white_ColorId;
        run.linkId = 0;
        enum iGmLineType type;
        int indent = 0;
        if (!isPreformat) {
            type = lineType_Rangecc_(&line);
            indent = indents[type];
            if (type == preformatted_GmLineType) {
                isPreformat = iTrue;
                trimLine_Rangecc_(&line, type);
                preAltText = line;
                /* TODO: store and link the alt text to this run */
                continue;
            }
            trimLine_Rangecc_(&line, type);
            run.font = fonts[type];
        }
        else {
            type = preformatted_GmLineType;
            if (startsWithSc_Rangecc(&line, "```", &iCaseSensitive)) {
                isPreformat = iFalse;
                preAltText = iNullRange;
                continue;
            }
            run.font = preformatted_FontId;
            indent = indents[type];
        }
        /* Check the margin. */
        if (isEmpty_Range(&line)) {
            pos.y += lineHeight_Text(run.font);
            prevType = text_GmLineType;
            continue;
        }
        if (!isPreformat || (prevType != preformatted_GmLineType)) {
            int required =
                iMax(topMargin[type], bottomMargin[prevType]) * lineHeight_Text(paragraph_FontId);
            if (isEmpty_Array(&d->layout)) {
                required = 0; /* top of document */
            }
            int delta = pos.y - lastVisibleRunBottom_GmDocument_(d);
            if (delta < required) {
                pos.y += required - delta;
            }
        }
        if (type == bullet_GmLineType) {
            run.bounds.pos = addX_I2(pos, indent * gap_UI);
            run.bounds.size = advance_Text(run.font, bullet);
            run.bounds.pos.x -= 4 * gap_UI - run.bounds.size.x / 2;
            run.text = (iRangecc){ bullet, bullet + strlen(bullet) };
            pushBack_Array(&d->layout, &run);
        }
        if (type == text_GmLineType && isFirstText) {
            run.font = firstParagraph_FontId;
            isFirstText = iFalse;
        }
        else if (type != header1_GmLineType) {
            isFirstText = iFalse;
        }
        iRangecc runLine = line;
        while (!isEmpty_Range(&runLine)) {
            run.bounds.pos = addX_I2(pos, indent * gap_UI);
            const char *contPos;
            run.bounds.size = tryAdvanceRange_Text(
                run.font, runLine, isPreformat ? 0 : (d->size.x - run.bounds.pos.x), &contPos);
            run.text = (iRangecc){ runLine.start, contPos };
            pushBack_Array(&d->layout, &run);
            runLine.start = contPos;
            trimStart_Rangecc(&runLine);
            pos.y += lineHeight_Text(run.font);
        }
        prevType = type;
    }
    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 */
}

iLocalDef iBool isNormalizableSpace_(char ch) {
    return ch == ' ' || ch == '\t';
}

static void normalize_GmDocument(iGmDocument *d) {
    iString *normalized = new_String();
    iRangecc src = range_String(&d->source);
    iRangecc line = iNullRange;
    iBool isPreformat = iFalse;
    const int preTabWidth = 4; /* TODO: user-configurable parameter */
    while (nextSplit_Rangecc(&src, "\n", &line)) {
        if (isPreformat) {
            /* Replace any tab characters with spaces for visualization. */
            for (const char *ch = line.start; ch != line.end; ch++) {
                if (*ch == '\t') {
                    int column = ch - line.start;
                    int numSpaces = (column / preTabWidth + 1) * preTabWidth - column;
                    while (numSpaces-- > 0) {
                        appendCStrN_String(normalized, " ", 1);
                    }
                }
                else {
                    appendCStrN_String(normalized, ch, 1);
                }
            }
            appendCStr_String(normalized, "\n");
            if (lineType_Rangecc_(&line) == preformatted_GmLineType) {
                isPreformat = iFalse;
            }
            continue;
        }
        if (lineType_Rangecc_(&line) == preformatted_GmLineType) {
            isPreformat = iTrue;
            appendRange_String(normalized, line);
            appendCStr_String(normalized, "\n");
            continue;
        }
        iBool isPrevSpace = iFalse;
        for (const char *ch = line.start; ch != line.end; ch++) {
            char c = *ch;
            if (isNormalizableSpace_(c)) {
                if (isPrevSpace) {
                    continue; /* skip repeated spaces */
                }
                c = ' ';
                isPrevSpace = iTrue;
            }
            else {
                isPrevSpace = iFalse;
            }
            appendCStrN_String(normalized, &c, 1);
        }
        appendCStr_String(normalized, "\n");
    }
    set_String(&d->source, collect_String(normalized));
//    printf("normalized:\n%s\n", cstr_String(&d->source));
}

void setSource_GmDocument(iGmDocument *d, const iString *source, int width) {
    set_String(&d->source, source);
    normalize_GmDocument(d);
    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);
        }
    }
}

iInt2 size_GmDocument(const iGmDocument *d) {
    return d->size;
}

iDefineClass(GmDocument)