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
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: osfont.c
 *
 */

/* Fonts */

#include "../font.h"
#include "../font.inl"
#include "../dctxh.h"
#include "../draw2d.inl"
#include <core/arrpt.h>
#include <core/heap.h>
#include <core/strings.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>

#if !defined(__GTK3__)
#error This file is only for GTK Toolkit
#endif

#include <sewer/nowarn.hxx>
#include <pango/pangocairo.h>
#include <sewer/warn.hxx>

/* System font should be set from GTK or other toolkit manager */
static String *i_SYSTEM_FONT_FAMILY = NULL;
static real32_t kFONT_REGULAR_SIZE = 0.f;
static real32_t kFONT_SMALL_SIZE = 0.f;
static real32_t kFONT_MINI_SIZE = 0.f;
static real32_t i_PANGO_TO_PIXELS = -1;
static cairo_t *i_CAIRO = NULL;
static PangoLayout *i_LAYOUT = NULL;

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

void osfont_alloc_globals(void)
{
}

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

void osfont_dealloc_globals(void)
{
    str_destopt(&i_SYSTEM_FONT_FAMILY);

    if (i_CAIRO != NULL)
    {
        g_object_unref(i_LAYOUT);
        cairo_destroy(i_CAIRO);
        i_LAYOUT = NULL;
        i_CAIRO = NULL;
    }
}

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

static const char_t *i_monospace_font_family(void)
{
    const char_t *desired_fonts[] = {"Ubuntu Mono", "DejaVu Sans Mono", "Courier New"};
    return _draw2d_monospace_family(desired_fonts, sizeof(desired_fonts) / sizeof(const char_t *));
}

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

static real32_t i_device_to_pixels(void)
{
    if (i_PANGO_TO_PIXELS < 0)
    {
        /* This object is owned by Pango and must not be freed */
        PangoFontMap *fontmap = pango_cairo_font_map_get_default();
        real32_t dpi = (real32_t)pango_cairo_font_map_get_resolution(cast(fontmap, PangoCairoFontMap));
        i_PANGO_TO_PIXELS = (dpi / 72.f) / PANGO_SCALE;
    }

    return i_PANGO_TO_PIXELS;
}

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

static gint i_font_size(const real32_t size, const uint32_t style)
{
    if ((style & ekFPOINTS) == ekFPOINTS)
    {
        return (gint)(size * (real32_t)PANGO_SCALE);
    }
    else
    {
        cassert((style & ekFPIXELS) == ekFPIXELS);
        return (gint)(size / i_device_to_pixels());
    }
}

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

static gint i_scale_size_to_cell(const real32_t size, const int pango_size, PangoFontDescription *font)
{
    real32_t cell_size = 1e8f;
    gint new_pango_size = 0;
    osfont_extents(cast(font, OSFont), "REFTEXT", 1, -1, NULL, &cell_size);
    new_pango_size = (gint)((size * (real32_t)pango_size) / cell_size);
    return new_pango_size;
}

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

OSFont *osfont_create(const char_t *family, const real32_t size, const real32_t width, const real32_t xscale, const uint32_t style)
{
    const char_t *name = NULL;
    gint psize = 0;
    PangoFontDescription *font = NULL;

    /*
     * In Pango, font width or scale is not assigned as PangoFontDescription property
     * With be use as scale factor in Cairo contexts by font_xscale()
     */
    unref(width);
    unref(xscale);

    if (str_equ_c(family, "__SYSTEM__") == TRUE)
    {
        cassert(i_SYSTEM_FONT_FAMILY != NULL);
        name = tc(i_SYSTEM_FONT_FAMILY);
    }
    else if (str_equ_c(family, "__MONOSPACE__") == TRUE)
    {
        name = i_monospace_font_family();
    }
    else
    {
        name = family;
    }

    psize = i_font_size(size, style);
    font = pango_font_description_new();
    pango_font_description_set_family(font, name);
    pango_font_description_set_size(font, psize);
    pango_font_description_set_style(font, (style & ekFITALIC) == ekFITALIC ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
    pango_font_description_set_weight(font, (style & ekFBOLD) == ekFBOLD ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);

    if ((style & ekFCELL) == ekFCELL)
    {
        gint s = i_scale_size_to_cell(size, psize, font);
        pango_font_description_set_size(font, s);
    }

    heap_auditor_add("PangoFontDescription");
    return cast(font, OSFont);
}

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

void osfont_destroy(OSFont **font)
{
    cassert_no_null(font);
    cassert_no_null(*font);
    pango_font_description_free(*dcast(font, PangoFontDescription));
    heap_auditor_delete("PangoFontDescription");
    *font = NULL;
}

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

String *osfont_family_name(const OSFont *font)
{
    const PangoFontDescription *ffont = cast_const(font, PangoFontDescription);
    const char *desc = NULL;
    cassert_no_null(ffont);
    desc = pango_font_description_get_family(ffont);
    return str_c(desc);
}

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

font_family_t osfont_system(const char_t *family)
{
    if (str_equ(i_SYSTEM_FONT_FAMILY, family) == TRUE)
        return ekFONT_FAMILY_SYSTEM;

    {
        const char_t *mono = i_monospace_font_family();
        if (str_equ_c(mono, family) == TRUE)
            return ekFONT_FAMILY_MONOSPACE;
    }

    return ENUM_MAX(font_family_t);
}

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

static bool_t i_is_monospace(PangoFontMap *fontmap, const PangoFontDescription *ffont)
{
    PangoFontFamily **families = NULL;
    const char *desc = NULL;
    bool_t mono = FALSE;
    int i, n;
    cassert_no_null(ffont);
    desc = pango_font_description_get_family(ffont);

    /* This array should be freed with g_free(). */
    pango_font_map_list_families(fontmap, &families, &n);
    for (i = 0; i < n; i++)
    {
        const char_t *name = cast_const(pango_font_family_get_name(families[i]), char_t);
        if (str_equ_c(name, desc) == TRUE)
        {
            mono = pango_font_family_is_monospace(families[i]);
            break;
        }
    }
    g_free(families);
    return mono;
}

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

void osfont_metrics(const OSFont *font, const real32_t size, const real32_t xscale, real32_t *ascent, real32_t *descent, real32_t *leading, real32_t *cell_size, real32_t *avg_width, bool_t *monospace)
{
    /* This object is owned by Pango and must not be freed */
    PangoFontMap *fontmap = pango_cairo_font_map_get_default();
    PangoContext *context = pango_font_map_create_context(fontmap);
    PangoFont *ffont = NULL;
    PangoFontMetrics *metrics = NULL;
    cassert_no_null(font);

    ffont = pango_font_map_load_font(fontmap, context, cast(font, PangoFontDescription));
    metrics = pango_font_get_metrics(ffont, NULL);

    if (ascent != NULL)
        *ascent = (real32_t)(pango_font_metrics_get_ascent(metrics) / PANGO_SCALE);

    if (descent != NULL)
        *descent = (real32_t)(pango_font_metrics_get_descent(metrics) / PANGO_SCALE);

    /* We need to get a real text measure */
    if (leading != NULL || cell_size != NULL || avg_width != NULL)
    {
        real32_t width, height;
        uint32_t len;
        const char_t *str = _draw2d_str_avg_char_width(&len);
        osfont_extents(font, str, xscale, -1, &width, &height);

        if (leading != NULL)
            *leading = height - size;

        if (cell_size != NULL)
            *cell_size = height;

        if (avg_width != NULL)
            *avg_width = width / len;
    }

    if (monospace != NULL)
        *monospace = i_is_monospace(fontmap, cast_const(font, PangoFontDescription));

    g_object_unref(context);
    g_object_unref(ffont);
    pango_font_metrics_unref(metrics);
}

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

void osfont_extents(const OSFont *font, const char_t *text, const real32_t xscale, const real32_t refwidth, real32_t *width, real32_t *height)
{
    int w, h;
    cassert_no_null(font);
    if (i_CAIRO == NULL)
    {
        i_CAIRO = cairo_create(NULL);
        i_LAYOUT = pango_cairo_create_layout(i_CAIRO);
    }

    pango_layout_set_font_description(i_LAYOUT, cast(font, PangoFontDescription));
    pango_layout_set_text(i_LAYOUT, cast_const(text, char), -1);
    pango_layout_set_width(i_LAYOUT, refwidth < 0 ? -1 : (int)((refwidth / xscale) * PANGO_SCALE));
    pango_layout_get_pixel_size(i_LAYOUT, &w, &h);
    ptr_assign(width, (real32_t)w * xscale);
    ptr_assign(height, (real32_t)h);
}

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

const void *osfont_native(const OSFont *font)
{
    cassert_no_null(font);
    return cast(font, void);
}

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

real32_t font_regular_size(void)
{
    cassert(i_SYSTEM_FONT_FAMILY != NULL);
    return kFONT_REGULAR_SIZE;
}

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

real32_t font_small_size(void)
{
    cassert(i_SYSTEM_FONT_FAMILY != NULL);
    return kFONT_SMALL_SIZE;
}

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

real32_t font_mini_size(void)
{
    cassert(i_SYSTEM_FONT_FAMILY != NULL);
    return kFONT_MINI_SIZE;
}

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

const char_t *font_register(const byte_t *data, const uint32_t size)
{
    unref(data);
    unref(size);
    cassert(FALSE);
    return NULL;
}

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

bool_t font_exists_family(const char_t *font_family)
{
    /* This object is owned by Pango and must not be freed. */
    PangoFontMap *fontmap = pango_cairo_font_map_get_default();
    PangoFontFamily **families = NULL;
    int i, n;

    /* This array should be freed with g_free(). */
    pango_font_map_list_families(fontmap, &families, &n);
    for (i = 0; i < n; i++)
    {
        const char_t *name = cast_const(pango_font_family_get_name(families[i]), char_t);
        if (str_equ_nocase(font_family, name) == TRUE)
            break;
    }
    g_free(families);
    return (bool_t)(i < n);
}

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

static ArrPt(String) *i_installed_families(const bool_t only_mono)
{
    ArrPt(String) *fonts = arrpt_create(String);
    /* This object is owned by Pango and must not be freed. */
    PangoFontMap *fontmap = pango_cairo_font_map_get_default();
    PangoFontFamily **families = NULL;
    int i, n;

    /* This array should be freed with g_free(). */
    pango_font_map_list_families(fontmap, &families, &n);
    for (i = 0; i < n; i++)
    {
        const char_t *name = cast_const(pango_font_family_get_name(families[i]), char_t);
        if (only_mono == FALSE || pango_font_family_is_monospace(families[i]) == TRUE)
        {
            String *font = str_c(name);
            arrpt_append(fonts, font, String);
        }
    }
    g_free(families);
    arrpt_sort(fonts, str_scmp, String);
    return fonts;
}

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

ArrPt(String) *font_installed_families(void)
{
    return i_installed_families(FALSE);
}

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

ArrPt(String) *font_installed_monospace(void)
{
    return i_installed_families(TRUE);
}

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

void dctx_set_default_osfont(DCtx *ctx, const void *font)
{
    const PangoFontDescription *fdesc = cast_const(font, PangoFontDescription);
    real32_t scale = i_device_to_pixels();
    const char *family = NULL;
    real32_t size = 0;
    unref(ctx);
    cassert(i_SYSTEM_FONT_FAMILY == NULL);
    family = pango_font_description_get_family(fdesc);
    size = (real32_t)pango_font_description_get_size(fdesc);
    i_SYSTEM_FONT_FAMILY = str_c(cast_const(family, char_t));
    kFONT_REGULAR_SIZE = size * scale;
    kFONT_SMALL_SIZE = kFONT_REGULAR_SIZE - 2.f;
    kFONT_MINI_SIZE = kFONT_REGULAR_SIZE - 4.f;
}