Image: enable Chafa on Windows

It's not built by default, as imagemagick requires too many dlls.
This commit is contained in:
李通洲 2022-12-22 02:07:35 +08:00
parent 5283f10189
commit 0cacedfc5d
7 changed files with 47 additions and 5 deletions

View File

@ -59,7 +59,7 @@ cmake_dependent_option(ENABLE_DBUS "Enable dbus-1" ON "LINUX OR BSD" OFF)
cmake_dependent_option(ENABLE_XFCONF "Enable libxfconf-0" ON "LINUX OR BSD" OFF)
cmake_dependent_option(ENABLE_SQLITE3 "Enable sqlite3" ON "LINUX OR BSD" OFF)
cmake_dependent_option(ENABLE_RPM "Enable rpm" ON "LINUX" OFF)
cmake_dependent_option(ENABLE_IMAGEMAGICK7 "Enable imagemagick 7" ON "LINUX OR BSD OR APPLE" OFF)
cmake_dependent_option(ENABLE_IMAGEMAGICK7 "Enable imagemagick 7" ON "LINUX OR BSD OR APPLE OR WIN32" OFF)
cmake_dependent_option(ENABLE_IMAGEMAGICK6 "Enable imagemagick 6" ON "LINUX OR BSD OR APPLE" OFF)
cmake_dependent_option(ENABLE_CHAFA "Enable chafa" ON "ENABLE_IMAGEMAGICK6 OR ENABLE_IMAGEMAGICK7" OFF)
cmake_dependent_option(ENABLE_ZLIB "Enable zlib" ON "ENABLE_IMAGEMAGICK6 OR ENABLE_IMAGEMAGICK7" OFF)

View File

@ -138,6 +138,9 @@ static void defaultConfig(FFinstance* instance)
instance->config.logo.paddingRight = 4;
instance->config.logo.printRemaining = true;
instance->config.logo.chafaFgOnly = true;
ffStrbufInitS(&instance->config.logo.chafaSymbols, "block+border+space-wide-inverted"); // Chafa default
ffStrbufInit(&instance->config.colorKeys);
ffStrbufInit(&instance->config.colorTitle);
@ -381,6 +384,7 @@ static void destroyModuleArg(FFModuleArgs* args)
static void destroyConfig(FFinstance* instance)
{
ffStrbufDestroy(&instance->config.logo.source);
ffStrbufDestroy(&instance->config.logo.chafaSymbols);
for(uint8_t i = 0; i < (uint8_t) FASTFETCH_LOGO_MAX_COLORS; ++i)
ffStrbufDestroy(&instance->config.logo.colors[i]);
ffStrbufDestroy(&instance->config.colorKeys);

View File

@ -38,6 +38,8 @@ Logo options:
--sixel <file>: short for --logo-type sixel --logo <file>
--kitty <file>: short for --logo-type kitty --logo <file>
--chafa <file>: short for --logo-type chafa --logo <file>
--chafa-symbols <str>: specify character symbols to employ in final output. See chafa document for detail
--chafa-fg-only <?value>: produces character-cell output using foreground colors only. See chafa document for detail
Display options:
-s,--structure <structure>: sets the structure of the fetch. Must be a colon separated list of keys. Use "fastfetch --list-modules" to see the ones available.

View File

@ -1017,6 +1017,14 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
optionParseString(key, value, &instance->config.logo.source);
instance->config.logo.type = FF_LOGO_TYPE_IMAGE_CHAFA;
}
else if(strcasecmp(key, "--chafa-fg-only") == 0)
{
instance->config.logo.chafaFgOnly = optionParseBoolean(value);
}
else if(strcasecmp(key, "--chafa-symbols") == 0)
{
optionParseString(key, value, &instance->config.logo.chafaSymbols);
}
///////////////////
//Display options//

View File

@ -75,6 +75,9 @@ typedef struct FFconfig
uint32_t paddingLeft;
uint32_t paddingRight;
bool printRemaining;
bool chafaFgOnly;
FFstrbuf chafaSymbols;
} logo;
//If one of those is empty, ffLogoPrint will set them

View File

@ -14,7 +14,11 @@ static void* logoResize(const void* image, size_t width, size_t height, void* ex
FFLogoImageResult ffLogoPrintImageIM7(FFinstance* instance, FFLogoRequestData* requestData)
{
FF_LIBRARY_LOAD(imageMagick, &instance->config.libImageMagick, FF_LOGO_IMAGE_RESULT_INIT_ERROR, "libMagickCore-7.Q16HDRI" FF_LIBRARY_EXTENSION, 11, "libMagickCore-7.Q16" FF_LIBRARY_EXTENSION, 11)
FF_LIBRARY_LOAD(imageMagick, &instance->config.libImageMagick, FF_LOGO_IMAGE_RESULT_INIT_ERROR,
"libMagickCore-7.Q16HDRI" FF_LIBRARY_EXTENSION, 11,
"libMagickCore-7.Q16" FF_LIBRARY_EXTENSION, 11,
"libMagickCore-7.Q16HDRI-10" FF_LIBRARY_EXTENSION, -1 // Required for Windows
)
FF_LIBRARY_LOAD_SYMBOL_ADDRESS(imageMagick, ffResizeImage, ResizeImage, FF_LOGO_IMAGE_RESULT_INIT_ERROR);
FFIMData imData;

View File

@ -226,19 +226,30 @@ static bool printImageKitty(FFinstance* instance, FFLogoRequestData* requestData
#include <chafa.h>
static bool printImageChafa(FFinstance* instance, FFLogoRequestData* requestData, const ImageData* imageData)
{
FF_LIBRARY_LOAD(chafa, &instance->config.libChafa, false, "libchafa" FF_LIBRARY_EXTENSION, 1)
FF_LIBRARY_LOAD(chafa, &instance->config.libChafa, false,
"libchafa" FF_LIBRARY_EXTENSION, 1,
"libchafa-0" FF_LIBRARY_EXTENSION, -1 // Required for Windows
)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_symbol_map_new, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_symbol_map_add_by_tags, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_symbol_map_apply_selectors, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_new, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_set_geometry, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_set_symbol_map, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_set_color_space, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_set_canvas_mode, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_set_fg_only_enabled, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_new, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_draw_all_pixels, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_print, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_unref, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_canvas_config_unref, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, chafa_symbol_map_unref, false)
#ifndef _WIN32
// FIXME: These functions must be imported from `libglib` dlls. Leak them for now
FF_LIBRARY_LOAD_SYMBOL(chafa, g_string_free, false)
FF_LIBRARY_LOAD_SYMBOL(chafa, g_error_free, false)
#endif
imageData->ffCopyMagickString(imageData->imageInfo->magick, "RGBA", 5);
size_t length;
@ -250,11 +261,17 @@ static bool printImageChafa(FFinstance* instance, FFLogoRequestData* requestData
}
ChafaSymbolMap* symbolMap = ffchafa_symbol_map_new();
ffchafa_symbol_map_add_by_tags(symbolMap, CHAFA_SYMBOL_TAG_ALL);
GError* error = NULL;
if(!ffchafa_symbol_map_apply_selectors(symbolMap, instance->config.logo.chafaSymbols.chars, &error))
fputs(error->message, stderr);
ChafaCanvasConfig* canvasConfig = ffchafa_canvas_config_new();
ffchafa_canvas_config_set_geometry(canvasConfig, (gint) requestData->logoCharacterWidth, (gint) requestData->logoCharacterHeight);
ffchafa_canvas_config_set_symbol_map(canvasConfig, symbolMap);
ffchafa_canvas_config_set_color_space(canvasConfig, CHAFA_COLOR_SPACE_DIN99D);
ffchafa_canvas_config_set_canvas_mode(canvasConfig, CHAFA_CANVAS_MODE_TRUECOLOR);
ffchafa_canvas_config_set_fg_only_enabled(canvasConfig, instance->config.logo.chafaFgOnly);
// TODO: expose more chafa configs to fastfetch flags
ChafaCanvas* canvas = ffchafa_canvas_new(canvasConfig);
ffchafa_canvas_draw_all_pixels(
@ -275,7 +292,11 @@ static bool printImageChafa(FFinstance* instance, FFLogoRequestData* requestData
ffLogoPrintChars(instance, result.chars, false);
writeCacheStrbuf(requestData, &result, FF_CACHE_FILE_CHAFA);
#ifndef _WIN32
ffg_string_free(str, TRUE);
ffg_error_free(error);
#endif
ffchafa_canvas_unref(canvas);
ffchafa_canvas_config_unref(canvasConfig);
ffchafa_symbol_map_unref(symbolMap);