master ohmu/fastfetch-openbsd / src / modules / font / font.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/font/font.h"
  5#include "modules/font/font.h"
  6
  7bool ffPrintFont(FFFontOptions* options) {
  8    bool success = false;
  9    FFFontResult font;
 10    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
 11        ffStrbufInit(&font.fonts[i]);
 12    }
 13    ffStrbufInit(&font.display);
 14
 15    const char* error = ffDetectFont(&font);
 16
 17    if (error) {
 18        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Font), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 19    } else {
 20        if (options->moduleArgs.outputFormat.length == 0) {
 21            ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Font), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 22            ffStrbufPutTo(&font.display, stdout);
 23        } else {
 24            FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Font), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 25                                                                                                             FF_ARG(font.fonts[0], "font1"),
 26                                                                                                             FF_ARG(font.fonts[1], "font2"),
 27                                                                                                             FF_ARG(font.fonts[2], "font3"),
 28                                                                                                             FF_ARG(font.fonts[3], "font4"),
 29                                                                                                             FF_ARG(font.display, "combined"),
 30                                                                                                         }));
 31        }
 32
 33        success = true;
 34    }
 35
 36    ffStrbufDestroy(&font.display);
 37    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
 38        ffStrbufDestroy(&font.fonts[i]);
 39    }
 40
 41    return success;
 42}
 43
 44void ffParseFontJsonObject(FFFontOptions* options, yyjson_val* module) {
 45    yyjson_val *key, *val;
 46    size_t idx, max;
 47    yyjson_obj_foreach (module, idx, max, key, val) {
 48        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 49            continue;
 50        }
 51
 52        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Font), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 53    }
 54}
 55
 56[[gnu::cold]]
 57void ffGenerateFontJsonConfig(FFFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 58    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 59}
 60
 61bool ffGenerateFontJsonResult([[maybe_unused]] FFFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 62    bool success = false;
 63    FFFontResult font;
 64    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
 65        ffStrbufInit(&font.fonts[i]);
 66    }
 67    ffStrbufInit(&font.display);
 68
 69    const char* error = ffDetectFont(&font);
 70    if (error) {
 71        yyjson_mut_obj_add_str(doc, module, "error", error);
 72    } else {
 73        yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 74        yyjson_mut_obj_add_strbuf(doc, obj, "display", &font.display);
 75        yyjson_mut_val* fontsArr = yyjson_mut_obj_add_arr(doc, obj, "fonts");
 76        for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
 77            yyjson_mut_arr_add_strbuf(doc, fontsArr, &font.fonts[i]);
 78        }
 79        success = true;
 80    }
 81
 82    ffStrbufDestroy(&font.display);
 83    for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) {
 84        ffStrbufDestroy(&font.fonts[i]);
 85    }
 86
 87    return success;
 88}
 89
 90void ffInitFontOptions(FFFontOptions* options) {
 91    ffOptionInitModuleArg(&options->moduleArgs, "");
 92}
 93
 94void ffDestroyFontOptions(FFFontOptions* options) {
 95    ffOptionDestroyModuleArg(&options->moduleArgs);
 96}
 97
 98FFModuleBaseInfo ffFontModuleInfo = {
 99    .name = "Font",
100    .description = "Print system font names",
101    .displayName = {
102        .en = "Font",
103        .ar = "الخط",
104        .cs = "Písmo",
105        .de = "Schriftart",
106        .es = "Fuente",
107        .fi = "Fontti",
108        .fr = "Police",
109        .gl = "Fonte",
110        .he = "גופן",
111        .id = "Font",
112        .it = "Carattere",
113        .ja = "フォント",
114        .ko = "글꼴",
115        .pl = "Czcionka",
116        .pt = "Fonte",
117        .ru = "Шрифт",
118        .tr = "Yazı Tipi",
119        .uk = "Шрифт",
120        .vi = "Phông chữ",
121        .zh_CN = "字体",
122        .zh_TW = "字型",
123    },
124    .initOptions = (void*) ffInitFontOptions,
125    .destroyOptions = (void*) ffDestroyFontOptions,
126    .parseJsonObject = (void*) ffParseFontJsonObject,
127    .printModule = (void*) ffPrintFont,
128    .generateJsonResult = (void*) ffGenerateFontJsonResult,
129    .generateJsonConfig = (void*) ffGenerateFontJsonConfig,
130    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
131        { "Font 1", "font1" },
132        { "Font 2", "font2" },
133        { "Font 3", "font3" },
134        { "Font 4", "font4" },
135        { "Combined fonts for display", "combined" },
136    })),
137    .defaultOrder = 26,
138};