Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/text.cpp

raw
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: 11 июл. 2022 г.
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", text)
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("arial");
60 f.set_size(32);
61
62 ws::font_parameters_t fp;
63 ws::text_parameters_t tp1, tp2;
64
65 LSPString str;
66 str.set_utf8("Text");
67
68 for (size_t i=0; i<=0x07; ++i)
69 {
70 f.set_bold(i & 0x1);
71 f.set_italic(i & 0x2);
72 f.set_underline(i & 0x4);
73
74 float x = s->width() * ((i >> 2) * 0.5f + 0.25f);
75 float y = s->height() * ((i & 0x03) * 0.25f + 0.125f);
76
77 s->get_font_parameters(f, &fp);
78 s->get_text_parameters(f, &tp1, "Text");
79 s->get_text_parameters(f, &tp2, " Text");
80
81 // Text bar 1
82 c.alpha(0.0f);
83 c.set_rgb24(0xffff00);
84 s->fill_rect(c, SURFMASK_NONE, 0, x, y - fp.Ascent, tp1.Width, fp.Height);
85
86 c.set_rgb24(0xff0000);
87 s->line(c, x + tp1.XBearing, y - fp.Ascent, x + tp1.XBearing, y + fp.Descent, 1.0f);
88 c.set_rgb24(0x00cc00);
89 s->line(c, x + tp1.XAdvance, y - fp.Ascent, x + tp1.XAdvance, y + fp.Descent, 1.0f);
90 c.set_rgb24(0x0000ff);
91 s->line(c, x + tp1.XBearing, y + tp1.YBearing, x + tp1.XAdvance, y + tp1.YBearing, 1.0f);
92 c.set_rgb24(0x00ccff);
93 s->line(c, x + tp1.XBearing, y, x + tp1.XAdvance, y, 1.0f);
94 c.set_rgb24(0xffcc00);
95 s->line(c, x - 8, y - 8, x + 8, y + 8, 1.0f);
96 s->line(c, x - 8, y + 8, x + 8, y - 8, 1.0f);
97
98 // Output text
99 c.set_rgb24(0x000000);
100 c.alpha(0.25f);
101 s->out_text(f, c, x, y, &str);
102
103 // Update position
104 y += 40;
105
106 // Text bar 2
107 c.alpha(0.0f);
108 c.set_rgb24(0x00ffff);
109 s->fill_rect(c, SURFMASK_NONE, 0, x, y - fp.Ascent, tp2.Width, fp.Height);
110
111 c.set_rgb24(0xff0000);
112 s->line(c, x + tp2.XBearing, y - fp.Ascent, x + tp2.XBearing, y + fp.Descent, 1.0f);
113 c.set_rgb24(0x00cc00);
114 s->line(c, x + tp2.XAdvance, y - fp.Ascent, x + tp2.XAdvance, y + fp.Descent, 1.0f);
115 c.set_rgb24(0x0000ff);
116 s->line(c, x + tp2.XBearing, y + tp2.YBearing, x + tp2.XAdvance, y + tp2.YBearing, 1.0f);
117 c.set_rgb24(0x00ccff);
118 s->line(c, x + tp2.XBearing, y, x + tp2.XAdvance, y, 1.0f);
119 c.set_rgb24(0xffcc00);
120 s->line(c, x - 8, y - 8, x + 8, y + 8, 1.0f);
121 s->line(c, x - 8, y + 8, x + 8, y - 8, 1.0f);
122
123 // Output text
124 c.set_rgb24(0x000000);
125 c.alpha(0.25f);
126 s->out_text(f, c, x, y, " Text");
127 }
128
129 s->end();
130
131 return STATUS_OK;
132 }
133
134 case ws::UIE_CLOSE:
135 {
136 pWnd->hide();
137 pWnd->display()->quit_main();
138 break;
139 }
140
141 default:
142 return IEventHandler::handle_event(ev);
143 }
144
145 return STATUS_OK;
146 }
147 };
148
149 MTEST_MAIN
150 {
151 ws::IDisplay *dpy = ws::create_display(0, NULL);
152 MTEST_ASSERT(dpy != NULL);
153 lsp_finally { ws::free_display(dpy); };
154
155 ws::IWindow *wnd = dpy->create_window();
156 MTEST_ASSERT(wnd != NULL);
157 lsp_finally {
158 wnd->destroy();
159 delete wnd;
160 };
161
162 MTEST_ASSERT(wnd->init() == STATUS_OK);
163 MTEST_ASSERT(wnd->set_caption("Test text") == STATUS_OK);
164 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
165 MTEST_ASSERT(wnd->resize(400, 640) == STATUS_OK);
166 MTEST_ASSERT(wnd->set_size_constraints(400, 640, 400, 640) == STATUS_OK);
167
168 Handler h(this, wnd);
169 wnd->set_handler(&h);
170
171 MTEST_ASSERT(wnd->show() == STATUS_OK);
172 MTEST_ASSERT(dpy->main() == STATUS_OK);
173 }
174
175 MTEST_END
176
177
178
179