summaryrefslogtreecommitdiffstats
path: root/src/modules/font/font.c
blob: a32a7e80c2b35d9ccacdad0ddb4843f1c84b5154 (plain) (blame)
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
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/strutil.h"
#include "detection/font/font.h"
#include "modules/font/font.h"

bool ffPrintFont(FFFontOptions* options) {
    bool success = false;
    FFFontResult font;
    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
        ffStrbufInit(&font.fonts[i]);
    }
    ffStrbufInit(&font.display);

    const char* error = ffDetectFont(&font);

    if (error) {
        ffPrintError(FF_FONT_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
    } else {
        if (options->moduleArgs.outputFormat.length == 0) {
            ffPrintLogoAndKey(FF_FONT_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
            ffStrbufPutTo(&font.display, stdout);
        } else {
            FF_PRINT_FORMAT_CHECKED(FF_FONT_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
                                                                                                             FF_ARG(font.fonts[0], "font1"),
                                                                                                             FF_ARG(font.fonts[1], "font2"),
                                                                                                             FF_ARG(font.fonts[2], "font3"),
                                                                                                             FF_ARG(font.fonts[3], "font4"),
                                                                                                             FF_ARG(font.display, "combined"),
                                                                                                         }));
        }

        success = true;
    }

    ffStrbufDestroy(&font.display);
    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
        ffStrbufDestroy(&font.fonts[i]);
    }

    return success;
}

void ffParseFontJsonObject(FFFontOptions* options, yyjson_val* module) {
    yyjson_val *key, *val;
    size_t idx, max;
    yyjson_obj_foreach (module, idx, max, key, val) {
        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
            continue;
        }

        ffPrintError(FF_FONT_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
    }
}

void ffGenerateFontJsonConfig(FFFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
}

bool ffGenerateFontJsonResult(FF_A_UNUSED FFFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    bool success = false;
    FFFontResult font;
    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
        ffStrbufInit(&font.fonts[i]);
    }
    ffStrbufInit(&font.display);

    const char* error = ffDetectFont(&font);
    if (error) {
        yyjson_mut_obj_add_str(doc, module, "error", error);
    } else {
        yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
        yyjson_mut_obj_add_strbuf(doc, obj, "display", &font.display);
        yyjson_mut_val* fontsArr = yyjson_mut_obj_add_arr(doc, obj, "fonts");
        for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
            yyjson_mut_arr_add_strbuf(doc, fontsArr, &font.fonts[i]);
        }
        success = true;
    }

    ffStrbufDestroy(&font.display);
    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
        ffStrbufDestroy(&font.fonts[i]);
    }

    return success;
}

void ffInitFontOptions(FFFontOptions* options) {
    ffOptionInitModuleArg(&options->moduleArgs, "");
}

void ffDestroyFontOptions(FFFontOptions* options) {
    ffOptionDestroyModuleArg(&options->moduleArgs);
}

FFModuleBaseInfo ffFontModuleInfo = {
    .name = FF_FONT_MODULE_NAME,
    .description = "Print system font names",
    .initOptions = (void*) ffInitFontOptions,
    .destroyOptions = (void*) ffDestroyFontOptions,
    .parseJsonObject = (void*) ffParseFontJsonObject,
    .printModule = (void*) ffPrintFont,
    .generateJsonResult = (void*) ffGenerateFontJsonResult,
    .generateJsonConfig = (void*) ffGenerateFontJsonConfig,
    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
        { "Font 1", "font1" },
        { "Font 2", "font2" },
        { "Font 3", "font3" },
        { "Font 4", "font4" },
        { "Combined fonts for display", "combined" },
    }))
};