~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-gui/gui/core/Renderer.cpp -rw-r--r-- 11.3 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

extern "C"
{
#include "FreeRTOS.h"
#include "task.h"
}

// for memset
#include <string.h>
#include "Color.hpp"
#include "Renderer.hpp"
#include "Context.hpp"
#include "ImageManager.hpp"
#include "../Common.hpp"
// renderers
#include "renderers/LineRenderer.hpp"
#include "renderers/ArcRenderer.hpp"
#include "renderers/CircleRenderer.hpp"
#include "renderers/RectangleRenderer.hpp"
// utils
#include "log/log.hpp"

#include "PixMap.hpp"
#include "VecMap.hpp"
// module-utils
#include "utf8/UTF8.hpp"
#include <cmath>
#include <cassert>
#include <FontManager.hpp>
#include <FontGlyph.hpp>
#include <RawFont.hpp>
#include "Style.hpp"
#include "Common.hpp"

#if DEBUG_FONT == 1
#define log_warn_glyph(...) LOG_WARN(__VA_ARGS__)
#else
#define log_warn_glyph(...)
#endif

namespace gui
{
    void Renderer::drawLine(Context *ctx, CommandLine *cmd)
    {
        using renderer::LineRenderer;
        const Point p1{cmd->x1, cmd->y1};
        const Point p2{cmd->x2, cmd->y2};
        const auto color = cmd->color;
        LineRenderer::draw(ctx, p1, p2, color);
    }

    void Renderer::drawRectangle(Context *ctx, CommandRectangle *cmd)
    {
        using renderer::RectangleRenderer;

        // First, check if there is anything to draw
        if (cmd->w == 0 || cmd->h == 0) {
            return;
        }
        if (cmd->fillColor.alpha == Color::FullTransparent && cmd->borderColor.alpha == Color::FullTransparent) {
            return;
        }
        if (!cmd->filled && cmd->borderColor.alpha == Color::FullTransparent) {
            return;
        }

        Context *drawingContext = ctx;
        Point position;
        if (cmd->yaps & (RectangleYap::BottomLeft | RectangleYap::TopLeft)) {
            position.x += cmd->yapSize;
        }
        Length width  = cmd->areaW;
        Length height = cmd->areaH;
        if (cmd->yaps != RectangleYap::None) {
            width -= cmd->yapSize;
        }

        if (cmd->areaW == cmd->w && cmd->areaH == cmd->h) {
            position.x += cmd->x;
            position.y += cmd->y;
        }
        else {
            const auto x   = cmd->areaX < 0 ? cmd->x + cmd->areaX : cmd->x;
            const auto y   = cmd->areaY < 0 ? cmd->y + cmd->areaY : cmd->y;
            drawingContext = ctx->get(x, y, cmd->areaW, cmd->areaH);
        }

        Length radius = cmd->radius;
        if (radius == 0) {
            RectangleRenderer::drawFlat(
                drawingContext, position, width, height, RectangleRenderer::DrawableStyle::from(*cmd));
        }
        else {
            RectangleRenderer::draw(
                drawingContext, position, width, height, RectangleRenderer::DrawableStyle::from(*cmd));
        }

        if (drawingContext != ctx) {
            ctx->insertArea(cmd->x, cmd->y, cmd->areaX, cmd->areaY, cmd->w, cmd->h, drawingContext);
            delete drawingContext;
        }
    }

    void Renderer::drawArc(Context *ctx, CommandArc *cmd)
    {
        using renderer::ArcRenderer;
        const auto center{cmd->center};
        const auto radius{cmd->radius};
        const auto startAngle{cmd->start};
        const auto sweepAngle{cmd->sweep};
        const auto style = ArcRenderer::DrawableStyle::from(*cmd);
        ArcRenderer::draw(ctx, center, radius, startAngle, sweepAngle, style);
    }

    void Renderer::drawCircle(Context *ctx, CommandCircle *cmd)
    {
        using renderer::CircleRenderer;
        const auto center{cmd->center};
        const auto radius{cmd->radius};
        const auto style = CircleRenderer::DrawableStyle::from(*cmd);
        CircleRenderer::draw(ctx, center, radius, style);
    }

    void Renderer::drawChar(Context *context, const int16_t x, const int16_t y, FontGlyph *glyph, const Color color)
    {
        auto line_y_offset = (y - glyph->yoffset) * context->getW();
        auto *drawPtr      = context->getData() + x + line_y_offset;
        auto *glyphPtr     = glyph->data;
        assert(glyph->data);

        for (uint16_t yy = 0; yy < glyph->height; ++yy) {
            for (uint16_t xx = 0; xx < glyph->width; ++xx) {
                if (!context->addressInData(drawPtr + xx)) {
                    log_warn_glyph("drawing out of: %d vs %d", xx, context->getW() * context->getH());
                    return;
                }
                if (*(glyphPtr + xx) == 0) {
                    *(drawPtr + xx) = 0x0F & color.intensity;
                }
            }
            drawPtr += context->getW();
            glyphPtr += glyph->width;
        }
    }

    void Renderer::drawText(Context *ctx, CommandText *cmd)
    {

        // check if there are any characters to draw in the string provided with message.
        if (cmd->str.length() == 0) {
            return;
        }

        // get copy of original context using x,y of draw coordinates and original size of the widget
        Context *drawCtx;
        bool copyContext = false;
        int16_t wgtX = 0, wgtY = 0;
        // check if there is a need or making copy of context to use it as background
        if ((cmd->areaW == cmd->w) && (cmd->areaH == cmd->h)) {
            drawCtx = ctx;
            wgtX    = cmd->x;
            wgtY    = cmd->y;
        }
        else {
            copyContext = true;
            drawCtx     = ctx->get(cmd->x, cmd->y, cmd->areaW, cmd->areaH);
        }

        // retrieve font used to draw text
        FontManager &fontManager = FontManager::getInstance();
        RawFont *font            = fontManager.getFont(cmd->fontID);

        int16_t posX = cmd->tx;
        int16_t posY = cmd->ty;

        // draw every sign
        uint32_t idLast = 0, idCurrent = 0;
        for (uint32_t i = 0; i < cmd->str.length(); ++i) {
            idCurrent        = cmd->str[i]; // id stands for glued together utf-16 with no order bytes (0xFF 0xFE)
            FontGlyph *glyph = font->getGlyph(idCurrent);

            // do not start drawing outside of draw context.
            if ((wgtX + posX + glyph->xoffset >= drawCtx->getW()) || (wgtX + posX + glyph->xoffset < 0)) {
                LOG_FATAL("Drawing outside context's X boundary for glyph: %" PRIu32, glyph->id);
                return;
            }
            if ((wgtY + posY >= drawCtx->getH()) || (wgtY + posY < 0)) {
                LOG_FATAL("Drawing outside context's Y boundary for glyph: %" PRIu32, glyph->id);
                return;
            }

            int32_t kernValue = 0;
            if (i > 0) {
                kernValue = font->getKerning(idLast, idCurrent);
            }
            drawChar(drawCtx, wgtX + posX + glyph->xoffset + kernValue, wgtY + posY, glyph, cmd->color);
            posX += glyph->xadvance + kernValue;

            idLast = idCurrent;
        }

        // if drawing was performed in temporary context
        // reinsert drawCtx into bast context
        if (copyContext) {
            ctx->insert(cmd->x, cmd->y, drawCtx);
            delete drawCtx;
        }
    }

    void Renderer::drawImage(Context *ctx, CommandImage *cmd)
    {

        // retrive pixmap from the pixmap manager
        ImageMap *imageMap = ImageManager::getInstance().getImageMap(cmd->imageID);

        // if image is not found return;
        if (imageMap == nullptr)
            return;

        // get copy of original context using x,y of draw coordinates and original size of the widget
        Context *drawCtx = ctx->get(cmd->x, cmd->y, cmd->areaW, cmd->areaH);
        uint8_t *ctxData = drawCtx->getData();

        auto check_wh = [&](const std::string &name, auto w, auto h) {
            if (h > ctx->getH() || w > ctx->getW()) {
                LOG_WARN("image %s {w: %d,h %d} > context {w %d,h %d}",
                         name.c_str(),
                         w,
                         drawCtx->getW(),
                         h,
                         drawCtx->getH());
            }
        };

        if (imageMap->getType() == gui::ImageMap::Type::PIXMAP) {
            auto pixMap = dynamic_cast<PixMap *>(imageMap);
            assert(pixMap);
            uint32_t offsetImage   = 0;
            uint32_t offsetContext = 0;
            uint8_t *pixData       = pixMap->getData();
            check_wh(pixMap->getName(), pixMap->getWidth(), pixMap->getHeight());

            for (uint32_t row = 0; row < std::min(drawCtx->getH(), pixMap->getHeight()); row++) {
                memcpy(ctxData + offsetContext, pixData + offsetImage, std::min(drawCtx->getW(), pixMap->getWidth()));
                offsetImage += pixMap->getWidth();
                offsetContext += drawCtx->getW();
            }
        }
        else if (imageMap->getType() == gui::ImageMap::Type::VECMAP) {
            auto vecMap = dynamic_cast<VecMap *>(imageMap);
            assert(vecMap);
            uint32_t offsetContext    = 0;
            uint32_t offsetRowContext = 0;
            uint32_t imageOffset      = 0;
            uint8_t alphaColor        = vecMap->getAlphaColor();
            for (uint32_t row = 0; row < std::min(vecMap->getHeight(), drawCtx->getH()); row++) {
                check_wh(vecMap->getName(), imageMap->getWidth(), imageMap->getHeight());
                uint16_t vecCount = *(vecMap->getData() + imageOffset);
                imageOffset += sizeof(uint16_t);

                offsetRowContext = offsetContext;

                for (uint32_t vec = 0; vec < vecCount; ++vec) {

                    uint16_t vecOffset = *(vecMap->getData() + imageOffset);
                    imageOffset += sizeof(uint16_t);
                    uint16_t vecLength = *(vecMap->getData() + imageOffset);
                    imageOffset += sizeof(uint8_t);
                    uint8_t vecColor = *(vecMap->getData() + imageOffset);
                    imageOffset += sizeof(uint8_t);

                    offsetRowContext += vecOffset;
                    if (vecColor != alphaColor) {
                        memset(ctxData + offsetRowContext, vecColor, std::min(drawCtx->getW(), vecLength));
                    }
                    offsetRowContext += vecLength;
                }
                offsetContext += drawCtx->getW();
            }
        }

        // reinsert drawCtx into bast context
        ctx->insert(cmd->x, cmd->y, drawCtx);

        // remove draw context
        delete drawCtx;
    }

    void Renderer::render(Context *ctx, std::vector<DrawCommand *> &commands)
    {
        for (auto cmd : commands) {
            switch (cmd->id) {
            case DrawCommandID::GUI_DRAW_CLEAR:
                ctx->fill(15);
                break;
            case DrawCommandID::GUI_DRAW_LINE:
                drawLine(ctx, static_cast<CommandLine *>(cmd));
                break;
            case DrawCommandID::GUI_DRAW_RECT:
                drawRectangle(ctx, static_cast<CommandRectangle *>(cmd));
                break;
            case DrawCommandID::GUI_DRAW_ARC:
                drawArc(ctx, static_cast<CommandArc *>(cmd));
                break;
            case DrawCommandID::GUI_DRAW_CIRCLE:
                drawCircle(ctx, static_cast<CommandCircle *>(cmd));
                break;
            case DrawCommandID::GUI_DRAW_TEXT:
                drawText(ctx, static_cast<CommandText *>(cmd));
                break;
            case DrawCommandID::GUI_DRAW_IMAGE:
                drawImage(ctx, static_cast<CommandImage *>(cmd));
                break;
            default:
                break;
            };
        }
    }

} /* namespace gui */