summaryrefslogtreecommitdiff
path: root/include/nw4r/ut/ut_TextWriterBase.h
blob: f5f9f35e14ab11a78fb555c413ff4cece475b6ae (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
#ifndef NW4R_UT_TEXT_WRITER_BASE_H
#define NW4R_UT_TEXT_WRITER_BASE_H

#include "nw4r/types_nw4r.h"
#include "nw4r/ut/ut_CharWriter.h"
#include "nw4r/ut/ut_TagProcessorBase.h"
#include "stdio.h"
#include "wprintf.h"

namespace nw4r {
namespace ut {

template <typename T>
class TextWriterBase : public CharWriter {
public:
    static T *GetBuffer() {
        return mFormatBuffer;
    }
    static T *SetBuffer(T *buffer, u32 size) {
        T *old = mFormatBuffer;
        mFormatBuffer = buffer;
        mFormatBufferSize = size;
        return old;
    }

    static u32 GetBufferSize() {
        return mFormatBufferSize;
    }

    TextWriterBase();
    ~TextWriterBase();

    f32 GetWidthLimit() const {
        return mWidthLimit;
    }
    void SetWidthLimit(f32 limit) {
        mWidthLimit = limit;
    }
    void ResetWidthLimit() {
        mWidthLimit = NW4R_MATH_FLT_MAX;
    }

    f32 GetCharSpace() const {
        return mCharSpace;
    }
    void SetCharSpace(f32 space) {
        mCharSpace = space;
    }

    f32 GetLineSpace() const {
        return mLineSpace;
    }
    void SetLineSpace(f32 space) {
        mLineSpace = space;
    }

    int GetTabWidth() const {
        return mTabWidth;
    }
    void SetTabWidth(int width) {
        mTabWidth = width;
    }

    void SetDrawFlag(u32 flag) {
        mDrawFlag = flag;
    }

    TagProcessorBase<T> *GetTagProcessor() const {
        return mTagProcessor;
    }
    void SetTagProcessor(TagProcessorBase<T> *processor) {
        mTagProcessor = processor;
    }
    void ResetTagProcessor() {
        mTagProcessor = &mDefaultTagProcessor;
    }

    void SetLineHeight(f32 height);
    f32 GetLineHeight() const;

    f32 CalcFormatStringWidth(const T *str, ...) const;
    f32 CalcFormatStringHeight(const T *str, ...) const;
    void CalcFormatStringRect(Rect *rect, const T *str, ...) const;
    void CalcVStringRect(Rect *rect, const T *str, va_list args) const;

    f32 CalcStringWidth(const T *format, int len) const;
    f32 CalcStringHeight(const T *format, int len) const;
    void CalcStringRect(Rect *rect, const T *format, int len) const;

    f32 Printf(const T *format, ...);
    f32 VPrintf(const T *str, va_list args);
    f32 Print(const T *str, int len);
    f32 PrintfMutable(const T *format, ...);
    f32 VPrintfMutable(const T *format, va_list args);
    f32 PrintMutable(const T *str, int n);

    // static int VSNPrintf(T *buffer, u32 count, const T *fmt, va_list args);

    static int VSNPrintf(char *buffer, u32 count, const char *fmt, va_list args) {
        return vsnprintf(buffer, count, fmt, args);
    }

    static int VSNPrintf(wchar_t *buffer, u32 count, const wchar_t *fmt, va_list args) {
        return vswprintf(buffer, count, fmt, args);
    }

    f32 CalcLineWidth(const T *format, int len);
    int CalcLineRectImpl(Rect *rect, const T **str, int len);
    void CalcStringRectImpl(Rect *rect, const T *str, int len);
    f32 PrintImpl(const T *str, int len, bool m);
    f32 AdjustCursor(f32 *x1, f32 *y1, const T *str, int len);

    bool IsDrawFlagSet(u32 mask, u32 flag) const {
        return (mDrawFlag & mask) == flag;
    }

private:
    f32 mWidthLimit;                    // at 0x4C
    f32 mCharSpace;                     // at 0x50
    f32 mLineSpace;                     // at 0x54
    int mTabWidth;                      // at 0x58
    u32 mDrawFlag;                      // at 0x5C
    TagProcessorBase<T> *mTagProcessor; // at 0x60

    static T *mFormatBuffer;
    static int mFormatBufferSize;
    static TagProcessorBase<T> mDefaultTagProcessor;
};

} // namespace ut
} // namespace nw4r

#endif