nappgui-sys 0.2.0

Rust raw bindings to NAppGUI
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: draw2d.c
 *
 */

/* Operating system 2D drawing support */

#include "draw2d.h"
#include "draw2d.inl"
#include "draw.inl"
#include "font.inl"
#include "image.inl"
#include "image.h"
#include "color.h"
#include "font.h"
#include <core/arrpt.h>
#include <core/arrst.h>
#include <core/core.h>
#include <core/dbind.h>
#include <core/dbindh.h>
#include <core/heap.h>
#include <core/strings.h>
#include <osbs/log.h>
#include <sewer/blib.h>
#include <sewer/bmath.h>
#include <sewer/bmem.h>
#include <sewer/cassert.h>

typedef struct _icolor_t IColor;

struct _icolor_t
{
    uint16_t index;
    color_t color;
};

DeclSt(IColor);

#define i_WORD_TYPE_END 0

#define i_WORD_TYPE_NEW_LINE 1

#define i_WORD_TYPE_BLANCKS 2

#define i_WORD_TYPE_TEXT 3


static uint32_t i_NUM_USERS = 0;
static ArrPt(String) *i_FONT_FAMILIES;
static ArrSt(IColor) *i_INDEXED_COLORS;
static String *i_USER_MONOSPACE_FONT_FAMILY = NULL;
static String *i_MONOSPACE_FONT_FAMILY = NULL;
static const char_t *i_AVG_CHAR_WIDTH = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
static uint32_t i_AVG_CHAR_WIDTH_LEN = 0;

/*---------------------------------------------------------------------------*/

static void i_draw2d_atexit(void)
{
    if (i_NUM_USERS != 0)
        log_printf("Error! draw2d is not properly closed (%d)\n", i_NUM_USERS);
}

/*---------------------------------------------------------------------------*/

void draw2d_start(void)
{
    if (i_NUM_USERS == 0)
    {
        core_start();
        osimage_alloc_globals();
        osfont_alloc_globals();
        _draw_alloc_globals();
        blib_atexit(i_draw2d_atexit);

        i_FONT_FAMILIES = arrpt_create(String);

        {
            String *str = str_c("__SYSTEM__");
            arrpt_append(i_FONT_FAMILIES, str, String);
        }

        {
            String *str = str_c("__MONOSPACE__");
            arrpt_append(i_FONT_FAMILIES, str, String);
        }

        i_INDEXED_COLORS = arrst_create(IColor);
        i_AVG_CHAR_WIDTH_LEN = str_len_c(i_AVG_CHAR_WIDTH);
        dbind_binary(Image, image_copy, image_read, image_write, image_destroy);
    }

    i_NUM_USERS += 1;
}

/*---------------------------------------------------------------------------*/

void draw2d_finish(void)
{
    cassert(i_NUM_USERS > 0);
    if (i_NUM_USERS == 1)
    {
        /*
         * In Windows, we will get a crash if we destroy an image (Gdiplus::Bitmap) once
         * closed Gdiplus. dbind may have images in cache, as 'default' objects. This command
         * eliminates all the images in cache, before closing Gdiplus.
         */
        dbind_defaults_unreg(Image);
        arrpt_destroy(&i_FONT_FAMILIES, str_destroy, String);
        arrst_destroy(&i_INDEXED_COLORS, NULL, IColor);
        str_destopt(&i_USER_MONOSPACE_FONT_FAMILY);
        str_destopt(&i_MONOSPACE_FONT_FAMILY);
        osfont_dealloc_globals();
        osimage_dealloc_globals();
        _draw_dealloc_globals();
        core_finish();
    }

    i_NUM_USERS -= 1;
}

/*---------------------------------------------------------------------------*/

void draw2d_preferred_monospace(const char_t *family)
{
    str_upd(&i_USER_MONOSPACE_FONT_FAMILY, family);
    str_destopt(&i_MONOSPACE_FONT_FAMILY);
}

/*---------------------------------------------------------------------------*/

uint32_t _draw2d_register_font(const char_t *font_family)
{
    /* Check if font name is a system font */
    font_family_t fsystem = osfont_system(font_family);

    if (fsystem != ENUM_MAX(font_family_t))
    {
        return (uint32_t)fsystem;
    }
    else
    {
        arrpt_foreach(family, i_FONT_FAMILIES, String)
            if (str_cmp(family, font_family) == 0)
                return family_i;
        arrpt_end()

        if (font_exists_family(font_family) == TRUE)
        {
            uint32_t num_elems = arrpt_size(i_FONT_FAMILIES, String);
            String *family = str_c(font_family);
            arrpt_append(i_FONT_FAMILIES, family, String);
            return num_elems;
        }
    }

    cassert_msg(FALSE, "Font is not available on this computer.");
    return (uint32_t)ekFONT_FAMILY_SYSTEM;
}

/*---------------------------------------------------------------------------*/

const char_t *_draw2d_font_family(const uint32_t family)
{
    const String *font_family = arrpt_get(i_FONT_FAMILIES, family, String);
    return tc(font_family);
}

/*---------------------------------------------------------------------------*/

color_t color_indexed(const uint16_t index, const color_t color)
{
    if (index == 0)
        return kCOLOR_DEFAULT;

    cassert((uint8_t)(color >> 24) != 0);

    arrst_foreach(ic, i_INDEXED_COLORS, IColor)
        if (index == ic->index)
        {
            ic->color = color;
            return color;
        }
    arrst_end()

    {
        IColor *nc = arrst_new(i_INDEXED_COLORS, IColor);
        nc->index = index;
        nc->color = color;
        return color;
    }
}

/*---------------------------------------------------------------------------*/

color_t _draw2d_get_indexed_color(const uint16_t index)
{
    if (index == 0)
        return kCOLOR_DEFAULT;

    arrst_foreach(ic, i_INDEXED_COLORS, IColor)
        if (ic->index == index)
            return ic->color;
    arrst_end()

    cassert(FALSE);
    return kCOLOR_DEFAULT;
}

/*---------------------------------------------------------------------------*/

static const char_t *i_jump_blanks(const char_t *str)
{
    cassert_no_null(str);
    for (; *str != '\0';)
    {
        if (*str == ' ' || *str == '\t' || *str == '\r')
        {
            str += 1;
        }
        else
        {
            return str;
        }
    }

    return str;
}

/*---------------------------------------------------------------------------*/

static const char_t *i_jump_not_blanks(const char_t *str)
{
    cassert_no_null(str);
    for (; *str != '\0';)
    {
        if (*str != ' ' && *str != '\t' && *str != '\r' && *str != '\0' && *str != '\n')
        {
            str += 1;
        }
        else
        {
            return str;
        }
    }

    return str;
}

/*---------------------------------------------------------------------------*/

static const char_t *i_next_word(const char_t *str, int *word_type)
{
    cassert_no_null(str);
    cassert_no_null(word_type);
    if (*str == '\0')
    {
        *word_type = i_WORD_TYPE_END;
        return NULL;
    }
    else if (*str == '\n')
    {
        *word_type = i_WORD_TYPE_NEW_LINE;
        return str + 1;
    }

    {
        const char_t *end = i_jump_blanks(str);
        if (end != str)
        {
            *word_type = i_WORD_TYPE_BLANCKS;
            return end;
        }
    }

    {
        const char_t *end = i_jump_not_blanks(str);
        cassert(end != str);
        *word_type = i_WORD_TYPE_TEXT;
        return end;
    }
}

/*---------------------------------------------------------------------------*/

static void i_new_line(void *data, FPtr_word_extents func_word_extents, real32_t *current_width, real32_t *current_height, real32_t *current_width_without_spaces, uint32_t *num_lines, real32_t *width, real32_t *height)
{
    cassert_no_null(current_width);
    cassert_no_null(current_height);
    cassert_no_null(current_width_without_spaces);
    cassert_no_null(num_lines);
    cassert_no_null(width);
    cassert_no_null(height);

    if (*current_width_without_spaces == 0)
    {
        real32_t word_width = 0, word_height = 0;
        func_word_extents(data, "A", &word_width, &word_height);
        *current_width_without_spaces = word_width;
        *current_height = word_height;
    }
    else
    {
        cassert(*current_height > 0);
    }

    if (*current_width_without_spaces > *width)
        *width = *current_width_without_spaces;
    *height += *current_height;

    *current_width = 0;
    *current_width_without_spaces = 0;
    *current_height = 0;
    *num_lines += 1;
}

/*---------------------------------------------------------------------------*/

void _draw2d_extents_imp(void *data, FPtr_word_extents func_word_extents, const bool_t newlines, const char_t *str, const real32_t refwidth, real32_t *width, real32_t *height)
{
    uint32_t num_lines = 0;
    real32_t ref_width = refwidth > 0 ? refwidth : 1e8f;
    real32_t current_width = 0, current_height = 0, current_width_without_spaces = 0;
    const char_t *ctext = str;

    cassert_no_nullf(func_word_extents);
    cassert_no_null(width);
    cassert_no_null(height);
    *width = 0;
    *height = 0;

    while (ctext != NULL)
    {
        const char_t *next_text = NULL;
        int word_type = 0;

        next_text = i_next_word(ctext, &word_type);

        switch (word_type)
        {

        case i_WORD_TYPE_END:
            if (current_width > .01f || num_lines == 0)
                i_new_line(data, func_word_extents, &current_width, &current_height, &current_width_without_spaces, &num_lines, width, height);
            break;

        case i_WORD_TYPE_NEW_LINE:
            if (newlines == TRUE)
                i_new_line(data, func_word_extents, &current_width, &current_height, &current_width_without_spaces, &num_lines, width, height);
            break;

        case i_WORD_TYPE_BLANCKS:
            if (current_width_without_spaces > 0)
            {
                char_t word[256];
                real32_t word_width = 0, word_height = 0;
                uint32_t size = (uint32_t)(next_text - ctext);
                cassert(next_text > ctext);
                str_copy_cn(word, sizeof(word), ctext, size);
                word[size] = '\0';
                func_word_extents(data, word, &word_width, &word_height);
                if (current_width + word_width <= ref_width)
                {
                    current_width += word_width;
                    if (word_height > current_height)
                        current_height = word_height;
                }
                else
                {
                    i_new_line(data, func_word_extents, &current_width, &current_height, &current_width_without_spaces, &num_lines, width, height);
                }
            }
            break;

        case i_WORD_TYPE_TEXT:
        {
            char_t word[256];
            real32_t word_width = 0.f, word_height = 0.f;
            uint32_t size = (uint32_t)(next_text - ctext);
            cassert(next_text > ctext);
            cassert(word_type == i_WORD_TYPE_TEXT);
            str_copy_cn(word, sizeof(word), ctext, size);
            word[size] = '\0';
            func_word_extents(data, word, &word_width, &word_height);

            if (current_width + word_width <= ref_width)
            {
                current_width += word_width;
                if (word_height > current_height)
                    current_height = word_height;
            }
            else
            {
                i_new_line(data, func_word_extents, &current_width, &current_height, &current_width_without_spaces, &num_lines, width, height);
                current_width = word_width;
                current_height = word_height;
            }

            current_width_without_spaces = current_width;
            break;
        }

            cassert_default();
        }

        ctext = next_text;
    }

    *width = bmath_ceilf(*width);
    *height = bmath_ceilf(*height);
}

/*---------------------------------------------------------------------------*/

const char_t *_draw2d_monospace_family(const char_t **desired_fonts, const uint32_t n)
{
    if (i_MONOSPACE_FONT_FAMILY == NULL)
    {
        uint32_t i = 0;
        ArrPt(String) *installed_fonts = font_installed_families();

        if (str_empty(i_USER_MONOSPACE_FONT_FAMILY) == FALSE)
        {
            if (arrpt_bsearch_const(installed_fonts, str_cmp, tc(i_USER_MONOSPACE_FONT_FAMILY), NULL, String, char_t) != NULL)
                i_MONOSPACE_FONT_FAMILY = str_copy(i_USER_MONOSPACE_FONT_FAMILY);
        }

        for (i = 0; i < n && i_MONOSPACE_FONT_FAMILY == NULL; ++i)
        {
            if (arrpt_bsearch_const(installed_fonts, str_cmp, desired_fonts[i], NULL, String, char_t) != NULL)
            {
                i_MONOSPACE_FONT_FAMILY = str_c(desired_fonts[i]);
                break;
            }
        }

        arrpt_destroy(&installed_fonts, str_destroy, String);
        cassert(i_MONOSPACE_FONT_FAMILY != NULL);
    }

    return tc(i_MONOSPACE_FONT_FAMILY);
}

/*---------------------------------------------------------------------------*/

const char_t *_draw2d_get_preferred_monospace(void)
{
    if (i_USER_MONOSPACE_FONT_FAMILY != NULL)
        return tc(i_USER_MONOSPACE_FONT_FAMILY);
    else
        return NULL;
}

/*---------------------------------------------------------------------------*/

const char_t *_draw2d_str_avg_char_width(uint32_t *len)
{
    cassert_no_null(len);
    *len = i_AVG_CHAR_WIDTH_LEN;
    return i_AVG_CHAR_WIDTH;
}