summaryrefslogtreecommitdiff
path: root/src/nw4r/ut/ut_TagProcessorBase.cpp
blob: 11669ae4e5bb2709e838d526af22642284590aad (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
// Ported from https://github.com/kiwi515/ogws/blob/master/src/nw4r/ut/ut_TagProcessorBase.cpp

#include "nw4r/ut.h"

namespace nw4r {
namespace ut {

template <typename T>
TagProcessorBase<T>::TagProcessorBase() {}

template <typename T>
TagProcessorBase<T>::~TagProcessorBase() {}

template <typename T>
Operation TagProcessorBase<T>::Process(u16 ch, PrintContext<T> *ctx) {
    switch (ch) {
        case '\n': ProcessLinefeed(ctx); return OPERATION_NEXT_LINE;
        case '\t': ProcessTab(ctx); return OPERATION_NO_CHAR_SPACE;
    }

    return OPERATION_DEFAULT;
}

template <typename T>
Operation TagProcessorBase<T>::CalcRect(Rect *rect, u16 ch, PrintContext<T> *ctx) {
    switch (ch) {
        case '\n': {
            const TextWriterBase<T> &writer = *ctx->writer;
            rect->right = writer.GetCursorX();
            rect->top = writer.GetCursorY();
            ProcessLinefeed(ctx);
            rect->left = writer.GetCursorX();
            rect->bottom = writer.GetCursorY() + ctx->writer->GetFontHeight();
            rect->Normalize();
            return OPERATION_NEXT_LINE;
        }
        case '\t': {
            const TextWriterBase<T> &writer = *ctx->writer;
            rect->left = writer.GetCursorX();
            ProcessTab(ctx);
            rect->right = writer.GetCursorX();
            rect->top = writer.GetCursorY();
            rect->bottom = rect->top + writer.GetFontHeight();
            rect->Normalize();
            return OPERATION_NO_CHAR_SPACE;
        }
    }

    return OPERATION_DEFAULT;
}

template <typename T>
void TagProcessorBase<T>::ProcessTab(PrintContext<T> *ctx) {
    TextWriterBase<T> &writer = *ctx->writer;

    int tabWidth = writer.GetTabWidth();
    if (tabWidth <= 0) {
        return;
    }

    f32 charWidth = writer.IsWidthFixed() ? writer.GetFixedWidth() : writer.GetFontWidth();

    f32 dx = writer.GetCursorX() - ctx->x;
    f32 tabPixel = tabWidth * charWidth;
    int numTab = static_cast<int>(dx / tabPixel) + 1;
    f32 x = ctx->x + (tabPixel * numTab);

    writer.SetCursorX(x);
}

template <typename T>
void TagProcessorBase<T>::ProcessLinefeed(PrintContext<T> *ctx) {
    TextWriterBase<T> &writer = *ctx->writer;

    f32 x = ctx->x;
    f32 y = writer.GetCursorY() + writer.GetLineHeight();

    writer.SetCursorX(x);
    writer.SetCursorY(y);
}

template class TagProcessorBase<char>;
template class TagProcessorBase<wchar_t>;

} // namespace ut
} // namespace nw4r