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

/* Font native implementation */

#include <sewer/nowarn.hxx>
#include <Cocoa/Cocoa.h>
#include <sewer/warn.hxx>
#include "draw2d_osx.ixx"
#include "../font.h"
#include "../font.inl"
#include "../draw2d.inl"
#include "../draw.inl"
#include <core/arrpt.h>
#include <core/strings.h>
#include <sewer/bmath.h>
#include <sewer/cassert.h>

#if !defined(__MACOS__)
#error This file is only for OSX
#endif

static String *i_SYSTEM_FONT_FAMILY = NULL;
static String *i_MONOSPACE_FONT_FAMILY = NULL;
static bool_t i_SYSTEM_FONT_REDUCED = FALSE;

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

void osfont_alloc_globals(void)
{
    /*
     * I don't know why, in some newer macOS the system default font
     * has a great font scaling by default. This affect when apply font
     * transform, necessary for some fonts italic shear and x-scaling.
     * This code detects if the system font has this big scaling
     */
    real32_t xscale = 1.1f;
    OSFont *font = osfont_create("__SYSTEM__", font_regular_size(), -1, xscale, 0);
    real32_t w, h;
    osfont_extents(font, "OO", -1, xscale, &w, &h);
    osfont_destroy(&font);
    unref(w);
    if (h > 40)
        i_SYSTEM_FONT_REDUCED = TRUE;
}

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

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

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

real32_t font_regular_size(void)
{
    return (real32_t)[NSFont systemFontSize];
}

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

real32_t font_small_size(void)
{
    return (real32_t)[NSFont smallSystemFontSize];
}

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

real32_t font_mini_size(void)
{
    return (real32_t)[NSFont smallSystemFontSize] - 2.f;
}

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

static const char_t *i_monospace_font_family(void)
{
    if (i_MONOSPACE_FONT_FAMILY == NULL)
    {
        const char_t *desired_fonts[] = {"SF Mono", "Menlo", "Monaco", "Andale Mono", "Courier New"};
        const char_t *monofont = _draw2d_monospace_family(desired_fonts, sizeof(desired_fonts) / sizeof(const char_t *));
        i_MONOSPACE_FONT_FAMILY = str_c(monofont);
    }

    return tc(i_MONOSPACE_FONT_FAMILY);
}

/*---------------------------------------------------------------------------*/
/*
 * This funtion apply two transforms:
 *  - A shear if itatic is required.
 *  - A x-scale if we need a char width different that font defaults.
 */
static NSFont *i_font_transform(NSFont *font, const CGFloat xscale, const CGFloat height, const BOOL italic, NSFontManager *font_manager)
{
    NSFont *tfont = nil;
    BOOL with_italic = NO;
    cassert_no_null(font);

    if (italic == YES)
    {
        /* The NSFontManager can apply the italic without affine */
        NSFontTraitMask traits = 0;
        tfont = [font_manager convertFont:font toHaveTrait:NSItalicFontMask];
        traits = [font_manager traitsOfFont:tfont];
        if ((traits & NSItalicFontMask) == NSItalicFontMask)
            with_italic = YES;
    }
    else
    {
        tfont = font;
    }

    /* We have to apply an affine transform to text */
    if ((italic == YES && with_italic == NO) || (xscale > 0 && fabs((double)xscale - 1) > 0.01))
    {
        NSAffineTransform *font_transform = [NSAffineTransform transform];

        /* Apply the x-scale */
        [font_transform scaleXBy:xscale * height yBy:height];

        /* Italic is required, but don't apply by FontManager */
        if (italic == YES && with_italic == NO)
        {
            NSAffineTransformStruct data;
            NSAffineTransform *italic_transform = nil;
            data.m11 = 1.f;
            data.m12 = 0.f;
            data.m21 = -tanf(/*italic_angle*/ -10.f * 0.017453292519943f);
            data.m22 = 1.f;
            data.tX = 0.f;
            data.tY = 0.f;
            italic_transform = [NSAffineTransform transform];
            [italic_transform setTransformStruct:data];
            [font_transform appendTransform:italic_transform];
        }

        tfont = [NSFont fontWithDescriptor:[tfont fontDescriptor] textTransform:font_transform];
    }

    return tfont;
}

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

static NSFont *i_nsfont(const char_t *family, const real32_t size, const uint32_t style)
{
    const char_t *name = NULL;
    NSFont *nsfont = nil;
    cassert(size > 0.f);

    if (str_equ_c(family, "__SYSTEM__") == TRUE)
    {
        if (style & ekFBOLD)
            nsfont = [NSFont boldSystemFontOfSize:(CGFloat)size];
        else
            nsfont = [NSFont systemFontOfSize:(CGFloat)size];
    }
    else if (str_equ_c(family, "__MONOSPACE__") == TRUE)
    {
        /* From Catalina, we have a system predefined monospace font */
#if defined(MAC_OS_X_VERSION_10_15) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
        if (_draw2d_get_preferred_monospace() == NULL)
        {
            if (style & ekFBOLD)
                nsfont = [NSFont monospacedSystemFontOfSize:(CGFloat)size weight:NSFontWeightBold];
            else
                nsfont = [NSFont monospacedSystemFontOfSize:(CGFloat)size weight:NSFontWeightLight];
        }
#endif

        if (nsfont == nil)
            name = i_monospace_font_family();
    }
    else
    {
        name = family;
    }

    if (nsfont == nil && name != NULL)
    {
        NSFontManager *fontManager = [NSFontManager sharedFontManager];
        NSString *ffamily = [NSString stringWithUTF8String:name];
        NSUInteger mask = (style & ekFBOLD) ? NSBoldFontMask : 0;
        nsfont = [fontManager fontWithFamily:ffamily traits:(NSFontTraitMask)mask weight:5 size:(CGFloat)size];
    }

    return nsfont;
}

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

static real32_t i_cell_size(NSFont *font, const real32_t size)
{
    const char_t *reftext = "ABCDEabcde";
    real32_t twidth = 0, theight = 0;
    real32_t scale = 0;
    osfont_extents(cast(font, OSFont), reftext, 1, -1, &twidth, &theight);
    scale = size / theight;
    return bmath_floorf(size * scale);
}

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

OSFont *osfont_create(const char_t *family, const real32_t size, const real32_t width, const real32_t xscale, const uint32_t style)
{
    real32_t esize = size;
    NSFont *nsfont = i_nsfont(family, esize, style);
    cassert_fatal_msg(nsfont != nil, "Font is not available on this computer.");
    unref(width);

    if ((style & ekFCELL) == ekFCELL)
    {
        esize = i_cell_size(nsfont, esize);
        nsfont = i_nsfont(family, esize, style);
    }

    if (nsfont != nil)
    {
        BOOL with_italic = (style & ekFITALIC) == ekFITALIC;
        if (with_italic || (xscale > 0 && fabsf(xscale - 1) > 0.01))
        {
            NSFontManager *manager = [NSFontManager sharedFontManager];
            if (str_equ_c(family, "__SYSTEM__") == TRUE && i_SYSTEM_FONT_REDUCED == TRUE)
                esize /= size;
            nsfont = i_font_transform(nsfont, (CGFloat)xscale, (CGFloat)esize, with_italic, manager);
        }

        [nsfont retain];
    }

    return cast(nsfont, OSFont);
}

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

void osfont_destroy(OSFont **font)
{
    cassert_no_null(font);
    cassert_no_null(*font);
    [cast(*font, NSFont) release];
    *font = NULL;
}

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

String *osfont_family_name(const OSFont *font)
{
    NSFont *nsfont = cast(font, NSFont);
    NSString *fname = nil;
    const char_t *utf8name = NULL;
    cassert_no_null(nsfont);
    fname = [nsfont familyName];
    utf8name = cast_const([fname UTF8String], char_t);
    return str_c(utf8name);
}

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

font_family_t osfont_system(const char_t *family)
{
    if (i_SYSTEM_FONT_FAMILY == NULL)
    {
        Font *font = font_system(15, 0);
        const char_t *family = font_family(font);
        i_SYSTEM_FONT_FAMILY = str_c(family);
        font_destroy(&font);
    }

    if (str_equ(i_SYSTEM_FONT_FAMILY, family) == TRUE)
        return ekFONT_FAMILY_SYSTEM;

    if (i_MONOSPACE_FONT_FAMILY == NULL)
    {
        Font *font = font_monospace(15, 0);
        const char_t *family = font_family(font);
        i_MONOSPACE_FONT_FAMILY = str_c(family);
        font_destroy(&font);
    }

    if (str_equ(i_MONOSPACE_FONT_FAMILY, family) == TRUE)
        return ekFONT_FAMILY_MONOSPACE;

    return ENUM_MAX(font_family_t);
}

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

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)
{
    NSFont *nsfont = cast(font, NSFont);

    if (ascent != NULL)
        *ascent = (real32_t)[nsfont ascender];

    if (descent != NULL)
        *descent = (real32_t) - [nsfont descender];

    /* 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 = (bool_t)[nsfont isFixedPitch];
}

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

void osfont_extents(const OSFont *font, const char_t *text, const real32_t xscale, const real32_t refwidth, real32_t *width, real32_t *height)
{
    id objects[1];
    id keys[1];
    MeasureStr data;
    NSUInteger count = sizeof(objects) / sizeof(id);
    cassert_no_null(font);
    cassert(count == 1);
    unref(xscale);
    objects[0] = cast(font, NSFont);
    keys[0] = NSFontAttributeName;
    data.dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count];
    _draw2d_extents(&data, _draw_word_extents, TRUE, text, refwidth, width, height, MeasureStr);
}

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

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

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

bool_t font_exists_family(const char_t *ffamily)
{
    NSFontManager *fontManager = [NSFontManager sharedFontManager];
    NSArray *families = nil;
    NSUInteger i, count;
    cassert_no_null(fontManager);
    families = [fontManager availableFontFamilies];
    count = [families count];
    for (i = 0; i < count; ++i)
    {
        NSString *family = cast([families objectAtIndex:i], NSString);
        const char_t *family_str = [family UTF8String];
        if (str_equ_c(ffamily, family_str) == TRUE)
            return TRUE;
    }

    return FALSE;
}

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

static ArrPt(String) *i_installed_families(const bool_t only_mono)
{
    NSFontManager *fontManager = [NSFontManager sharedFontManager];
    NSArray *families = nil;
    NSUInteger i, count;
    ArrPt(String) *font_families = NULL;
    cassert_no_null(fontManager);
    families = [fontManager availableFontFamilies];
    count = [families count];
    font_families = arrpt_create(String);
    for (i = 0; i < count; ++i)
    {
        NSString *family = cast([families objectAtIndex:i], NSString);
        BOOL add = YES;

        if (only_mono == TRUE)
        {
            NSFont *nsfont = [fontManager fontWithFamily:family traits:0 weight:5 size:20];
            add = [nsfont isFixedPitch];
        }

        if (add == YES)
        {
            const char_t *family_str = [family UTF8String];
            String *ffamily = str_c(family_str);
            arrpt_append(font_families, ffamily, String);
        }
    }

    arrpt_sort(font_families, str_scmp, String);
    return font_families;
}

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

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

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

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