| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-ws-lib |
| 6 |
* Created on: 5 мая 2023 г. |
| 7 |
* |
| 8 |
* lsp-ws-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-ws-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-ws-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/ws/factory.h> |
| 23 |
#include <lsp-plug.in/ws/IEventHandler.h> |
| 24 |
#include <lsp-plug.in/stdlib/math.h> |
| 25 |
#include <lsp-plug.in/test-fw/mtest.h> |
| 26 |
|
| 27 |
MTEST_BEGIN("ws.display", font2) |
| 28 |
|
| 29 |
class Handler: public ws::IEventHandler |
| 30 |
{ |
| 31 |
private: |
| 32 |
test_type_t *pTest; |
| 33 |
ws::IWindow *pWnd; |
| 34 |
|
| 35 |
public: |
| 36 |
inline Handler(test_type_t *test, ws::IWindow *wnd) |
| 37 |
{ |
| 38 |
pTest = test; |
| 39 |
pWnd = wnd; |
| 40 |
} |
| 41 |
|
| 42 |
virtual status_t handle_event(const ws::event_t *ev) |
| 43 |
{ |
| 44 |
switch (ev->nType) |
| 45 |
{ |
| 46 |
case ws::UIE_REDRAW: |
| 47 |
{ |
| 48 |
Color c(1.0f, 1.0f, 1.0f); |
| 49 |
ws::ISurface *s = pWnd->get_surface(); |
| 50 |
if (s == NULL) |
| 51 |
return STATUS_OK; |
| 52 |
|
| 53 |
// Perform drawing |
| 54 |
s->begin(); |
| 55 |
s->clear(c); |
| 56 |
|
| 57 |
// Draw the text |
| 58 |
ws::Font f; |
| 59 |
f.set_name("noto-sans"); |
| 60 |
f.set_size(32); |
| 61 |
|
| 62 |
ws::font_parameters_t fp; |
| 63 |
ws::text_parameters_t tp1, tp2; |
| 64 |
|
| 65 |
for (size_t i=0; i<=0x0f; ++i) |
| 66 |
{ |
| 67 |
f.set_bold(i & 0x1); |
| 68 |
f.set_italic(i & 0x2); |
| 69 |
f.set_underline(i & 0x4); |
| 70 |
f.set_antialias((i & 0x8) ? ws::FA_ENABLED : ws::FA_DISABLED); |
| 71 |
|
| 72 |
float x = s->width() * ((i >> 2) * 0.25f + 0.0625f); |
| 73 |
float y = s->height() * ((i & 0x03) * 0.25f + 0.125f); |
| 74 |
|
| 75 |
s->get_font_parameters(f, &fp); |
| 76 |
s->get_text_parameters(f, &tp1, "Text"); |
| 77 |
s->get_text_parameters(f, &tp2, " Текст"); |
| 78 |
|
| 79 |
// Text bar 1 |
| 80 |
c.alpha(0.0f); |
| 81 |
c.set_rgb24(0xffff00); |
| 82 |
s->fill_rect(c, SURFMASK_NONE, 0, x, y - fp.Ascent, tp1.Width, fp.Height); |
| 83 |
|
| 84 |
c.set_rgb24(0xff0000); |
| 85 |
s->line(c, x + tp1.XBearing, y - fp.Ascent, x + tp1.XBearing, y + fp.Descent, 1.0f); |
| 86 |
c.set_rgb24(0x00cc00); |
| 87 |
s->line(c, x + tp1.XAdvance, y - fp.Ascent, x + tp1.XAdvance, y + fp.Descent, 1.0f); |
| 88 |
c.set_rgb24(0x0000ff); |
| 89 |
s->line(c, x + tp1.XBearing, y + tp1.YBearing, x + tp1.XAdvance, y + tp1.YBearing, 1.0f); |
| 90 |
c.set_rgb24(0x00ccff); |
| 91 |
s->line(c, x + tp1.XBearing, y, x + tp1.XAdvance, y, 1.0f); |
| 92 |
c.set_rgb24(0xffcc00); |
| 93 |
s->line(c, x - 8, y - 8, x + 8, y + 8, 1.0f); |
| 94 |
s->line(c, x - 8, y + 8, x + 8, y - 8, 1.0f); |
| 95 |
|
| 96 |
// Output text |
| 97 |
c.set_rgb24(0x000000); |
| 98 |
c.alpha(0.25f); |
| 99 |
s->out_text(f, c, x, y, "Text"); |
| 100 |
|
| 101 |
// Update position |
| 102 |
y += 40; |
| 103 |
|
| 104 |
// Text bar 2 |
| 105 |
c.alpha(0.0f); |
| 106 |
c.set_rgb24(0x00ffff); |
| 107 |
s->fill_rect(c, SURFMASK_NONE, 0, x, y - fp.Ascent, tp2.Width, fp.Height); |
| 108 |
|
| 109 |
c.set_rgb24(0xff0000); |
| 110 |
s->line(c, x + tp2.XBearing, y - fp.Ascent, x + tp2.XBearing, y + fp.Descent, 1.0f); |
| 111 |
c.set_rgb24(0x00cc00); |
| 112 |
s->line(c, x + tp2.XAdvance, y - fp.Ascent, x + tp2.XAdvance, y + fp.Descent, 1.0f); |
| 113 |
c.set_rgb24(0x0000ff); |
| 114 |
s->line(c, x + tp2.XBearing, y + tp2.YBearing, x + tp2.XAdvance, y + tp2.YBearing, 1.0f); |
| 115 |
c.set_rgb24(0x00ccff); |
| 116 |
s->line(c, x + tp2.XBearing, y, x + tp2.XAdvance, y, 1.0f); |
| 117 |
c.set_rgb24(0xffcc00); |
| 118 |
s->line(c, x - 8, y - 8, x + 8, y + 8, 1.0f); |
| 119 |
s->line(c, x - 8, y + 8, x + 8, y - 8, 1.0f); |
| 120 |
|
| 121 |
// Output text |
| 122 |
c.set_rgb24(0x000000); |
| 123 |
c.alpha(0.25f); |
| 124 |
s->out_text(f, c, x, y, " Текст"); |
| 125 |
} |
| 126 |
|
| 127 |
s->end(); |
| 128 |
|
| 129 |
return STATUS_OK; |
| 130 |
} |
| 131 |
|
| 132 |
case ws::UIE_CLOSE: |
| 133 |
{ |
| 134 |
pWnd->hide(); |
| 135 |
pWnd->display()->quit_main(); |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
default: |
| 140 |
return IEventHandler::handle_event(ev); |
| 141 |
} |
| 142 |
|
| 143 |
return STATUS_OK; |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
MTEST_MAIN |
| 148 |
{ |
| 149 |
ws::IDisplay *dpy = ws::create_display(0, NULL); |
| 150 |
MTEST_ASSERT(dpy != NULL); |
| 151 |
lsp_finally { ws::free_display(dpy); }; |
| 152 |
|
| 153 |
ws::IWindow *wnd = dpy->create_window(); |
| 154 |
MTEST_ASSERT(wnd != NULL); |
| 155 |
lsp_finally { |
| 156 |
wnd->destroy(); |
| 157 |
delete wnd; |
| 158 |
}; |
| 159 |
|
| 160 |
io::Path font; |
| 161 |
MTEST_ASSERT(font.fmt("%s/font/NotoSansDisplay-Regular.ttf", resources())); |
| 162 |
MTEST_ASSERT(dpy->add_font("noto-sans", &font) == STATUS_OK); |
| 163 |
|
| 164 |
MTEST_ASSERT(wnd->init() == STATUS_OK); |
| 165 |
MTEST_ASSERT(wnd->set_caption("Test custom font output") == STATUS_OK); |
| 166 |
MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK); |
| 167 |
MTEST_ASSERT(wnd->resize(640, 640) == STATUS_OK); |
| 168 |
MTEST_ASSERT(wnd->set_size_constraints(640, 640, 640, 640) == STATUS_OK); |
| 169 |
|
| 170 |
Handler h(this, wnd); |
| 171 |
wnd->set_handler(&h); |
| 172 |
|
| 173 |
MTEST_ASSERT(wnd->show() == STATUS_OK); |
| 174 |
|
| 175 |
MTEST_ASSERT(dpy->main() == STATUS_OK); |
| 176 |
} |
| 177 |
|
| 178 |
MTEST_END |
| 179 |
|
| 180 |
|
| 181 |
|