✨ Add resvg as SVG render option.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Faster, gamma support, excellent SVG feature support
authored by Mark Stosberg · committed by Daniel Eklöf · parent 8d3fcd1 · Browse files · 8 files·+226 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+226 added−10 removed
- other:+226 added−10 removed
Provenance
- Commit
faf6fea- Tree
39871ba- Author
Mark Stosberg <mark@stosberg.com>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
8d3fcd1
8 files changed+226 added−10 removed
modified:application.h+6 added
10 unmodified lines11 #if defined(FUZZEL_ENABLE_SVG_NANOSVG)12 #include <nanosvg/nanosvg.h>13 #endif14 15+#if defined(FUZZEL_ENABLE_SVG_RESVG)16+ #include <resvg.h>17+#endif18+19 #include <fcft/fcft.h>20 #include <tllist.h>21 22 #include "prompt.h"19 unmodified lines42 #if defined(FUZZEL_ENABLE_SVG_LIBRSVG)43 RsvgHandle *svg;44 #elif defined(FUZZEL_ENABLE_SVG_NANOSVG)45 NSVGimage *svg;46+#elif defined(FUZZEL_ENABLE_SVG_RESVG)47+ resvg_render_tree *svg;48 #else49 void *svg;50 #endif51 };66 unmodified linesmodified:CHANGELOG.md+9 added-2 removed
31 unmodified lines32 33 34 ## Unreleased35 ### Added36+37+ * Faster, more accurate SVG rendering with `resvg` backend. It does not38+ require cairo and supports gamma correction. Packagers are advised to39+ prefer this backend. The librsvg and nanosvg backends may be removed40+ in a future release ([#650][650]).41+42+[650]: https://codeberg.org/dnkl/fuzzel/issues/65043+44 ### Changed45 ### Deprecated46 ### Removed47 ### Fixed6 unmodified lines54 55 ### Security56 ### Contributors57 58+ * Mark Stosberg59 60 ## 1.13.161 62 ### Fixed6 unmodified lines69 [647]: https://codeberg.org/dnkl/fuzzel/issues/64770 [653]: https://codeberg.org/dnkl/fuzzel/issues/65371 [652]: https://codeberg.org/dnkl/fuzzel/issues/65272 64-73 ### Contributors74 75 * Mark Stosberg76 * rehanzo77 70-78 ## 1.13.079 80 ### Added81 1023 unmodified linesmodified:icon.c+47 added-2 removed
21 unmodified lines22 #if defined(FUZZEL_ENABLE_SVG_NANOSVG)23 #include <nanosvg/nanosvg.h>24 #endif25 26+#if defined(FUZZEL_ENABLE_SVG_RESVG)27+ #include <resvg.h>28+#endif29+30 #include <tllist.h>31 32 #define LOG_MODULE "icon"33 #define LOG_ENABLE_DBG 0421 unmodified lines455 return true;456 }457 #endif458 459+#if defined(FUZZEL_ENABLE_SVG_RESVG)460+static bool461+icon_from_svg_resvg(struct icon *icon, const char *file_name)462+{463+ resvg_options *opt = resvg_options_create();464+ if (opt == NULL) {465+ return false;466+ }467+468+ /* Set DPI to 96, same as nanosvg */469+ resvg_options_set_dpi(opt, 96);470+471+ resvg_render_tree *tree = NULL;472+ int32_t result = resvg_parse_tree_from_file(file_name, opt, &tree);473+ resvg_options_destroy(opt);474+475+ if (result != RESVG_OK || tree == NULL) {476+ /* Only log actual parsing errors, not just files that aren't valid SVGs */477+ if (result == RESVG_ERROR_PARSING_FAILED || 478+ result == RESVG_ERROR_MALFORMED_GZIP ||479+ result == RESVG_ERROR_INVALID_SIZE) {480+ LOG_DBG("%s: resvg failed to parse SVG (code %d)", file_name, result);481+ }482+ return false;483+ }484+485+ if (resvg_is_image_empty(tree)) {486+ resvg_tree_destroy(tree);487+ return false;488+ }489+490+ icon->type = ICON_SVG;491+ icon->svg = tree;492+ return true;493+}494+#endif495+496 bool497 icon_from_svg(struct icon *icon, const char *name)498 {499 #if defined(FUZZEL_ENABLE_SVG_LIBRSVG)500 return icon_from_svg_librsvg(icon, name);501 #elif defined(FUZZEL_ENABLE_SVG_NANOSVG)502 return icon_from_svg_nanosvg(icon, name);503+#elif defined(FUZZEL_ENABLE_SVG_RESVG)504+ return icon_from_svg_resvg(icon, name);505 #else506 return false;507 #endif508 }41 unmodified lines550 #if defined(FUZZEL_ENABLE_SVG_LIBRSVG)551 g_object_unref(icon->svg);552 #elif defined(FUZZEL_ENABLE_SVG_NANOSVG)553 nsvgDelete(icon->svg);554+#elif defined(FUZZEL_ENABLE_SVG_RESVG)555+ resvg_tree_destroy(icon->svg);556 #endif557 icon->svg = NULL;558 }559 break;32 unmodified lines592 path[path_len - 2] = 'n';593 path[path_len - 1] = 'g';594 595 if (faccessat(dir_fd, path, R_OK, 0) < 0) {551-#if defined(FUZZEL_ENABLE_SVG_NANOSVG) || defined(FUZZEL_ENABLE_SVG_LIBRSVG)596+#if defined(FUZZEL_ENABLE_SVG_NANOSVG) || defined(FUZZEL_ENABLE_SVG_LIBRSVG) || defined(FUZZEL_ENABLE_SVG_RESVG)597 path[path_len - 3] = 's';598 path[path_len - 2] = 'v';599 path[path_len - 1] = 'g';600 return faccessat(dir_fd, path, R_OK, 0) == 0;3 unmodified lines604 }605 606 return true;607 563-#elif defined(FUZZEL_ENABLE_SVG_NANOSVG) || defined(FUZZEL_ENABLE_SVG_LIBRSVG)608+#elif defined(FUZZEL_ENABLE_SVG_NANOSVG) || defined(FUZZEL_ENABLE_SVG_LIBRSVG) || defined(FUZZEL_ENABLE_SVG_RESVG)609 path[path_len - 3] = 's';610 path[path_len - 2] = 'v';611 path[path_len - 1] = 'g';612 287 unmodified linesmodified:main.c+2 added
320 unmodified lines321 #if defined(FUZZEL_ENABLE_SVG_NANOSVG)322 '+', "(nanosvg)",323 #elif defined(FUZZEL_ENABLE_SVG_LIBRSVG)324 '+', "(librsvg)",325+#elif defined(FUZZEL_ENABLE_SVG_RESVG)326+ '+', "(resvg)",327 #else328 '-', "",329 #endif330 #if !defined(NDEBUG)1992 unmodified linesmodified:meson_options.txt+2 added-2 removed
1-option('enable-cairo', type: 'feature', description: 'use cairo; required by the librsvg SVG icon backend')1+option('enable-cairo', type: 'feature', value: 'auto', description: 'use cairo; required by the librsvg SVG icon backend, disabled by default with resvg')2 option('png-backend', type: 'combo', choices: ['none', 'libpng'], value: 'libpng', description: 'enable support for PNG icons')3-option('svg-backend', type: 'combo', choices: ['none', 'librsvg', 'nanosvg'], value: 'nanosvg', description: 'enables support for SVG icons (librsvg: also requires cairo, nanosvg: bundled)')3+option('svg-backend', type: 'combo', choices: ['none', 'librsvg', 'nanosvg', 'resvg'], value: 'nanosvg', description: 'enables support for SVG icons (librsvg: also requires cairo, nanosvg: bundled, resvg: C API)')4 option('system-nanosvg', type: 'feature', value: 'disabled',5 description: 'use system\'s nanosvg instead of the bundled version')modified:meson.build+27 added-1 removed
55 unmodified lines56 math = cc.find_library('m')57 threads = [dependency('threads'), cc.find_library('stdthreads', required: false)]58 fontconfig = dependency('fontconfig')59 pixman = dependency('pixman-1', version: '>=0.46.0')60+# Cairo is required for librsvg but not for resvg/nanosvg61+if get_option('svg-backend') == 'resvg' or get_option('svg-backend') == 'nanosvg'62+ # For resvg/nanosvg, cairo is optional and disabled by default63+ if get_option('enable-cairo').auto()64+ cairo = dependency('cairo', required: false)65+ else60-cairo = dependency('cairo', required: get_option('enable-cairo'))66+ cairo = dependency('cairo', required: get_option('enable-cairo'))67+ endif68+else69+ # For librsvg or no SVG backend, respect the enable-cairo option70+ cairo = dependency('cairo', required: get_option('enable-cairo'))71+endif72 libepoll = dependency('epoll-shim', required: false)73 wayland_protocols = dependency('wayland-protocols', version: '>=1.41')74 wayland_client = dependency('wayland-client')75 wayland_cursor = dependency('wayland-cursor')17 unmodified lines93 94 if cairo.found() and get_option('svg-backend') == 'librsvg'95 librsvg = dependency('librsvg-2.0')96 nanosvg = declare_dependency()97+ resvg = declare_dependency()98 svg_backend = 'librsvg'99 add_project_arguments('-DFUZZEL_ENABLE_SVG_LIBRSVG=1', language: 'c')100 elif get_option('svg-backend') == 'nanosvg'101 if system_nanosvg.found() and system_nanosvgrast.found()10 unmodified lines112 svg_backend = 'nanosvg (bundled)'113 endif114 add_project_arguments('-DFUZZEL_ENABLE_SVG_NANOSVG=1', language: 'c')115 librsvg = declare_dependency()116+ resvg = declare_dependency()117+elif get_option('svg-backend') == 'resvg'118+ # resvg doesn't provide pkg-config, so we need to find it manually119+ resvg = cc.find_library('resvg', required: true)120+ if cc.has_header('resvg.h')121+ svg_backend = 'resvg'122+ add_project_arguments('-DFUZZEL_ENABLE_SVG_RESVG=1', language: 'c')123+ else124+ error('resvg.h not found. Please install resvg development files.')125+ endif126+ librsvg = declare_dependency()127+ nanosvg = declare_dependency()128 else129 svg_backend = 'none'130 librsvg = declare_dependency()131 nanosvg = declare_dependency()132+ resvg = declare_dependency()133 endif134 135 wayland_protocols_datadir = wayland_protocols.get_variable('pkgdatadir')136 84 unmodified lines221 libepoll,222 libpng,223 librsvg,224 nanosvg,225+ resvg,226 wayland_client,227 wayland_cursor,228 xkb,229 tllist,45 unmodified linesmodified:render.c+130 added
30 unmodified lines31 #if defined(FUZZEL_ENABLE_SVG_NANOSVG)32 #include <nanosvg/nanosvgrast.h>33 #endif34 35+#if defined(FUZZEL_ENABLE_SVG_RESVG)36+ #include <resvg.h>37+#endif38+39 #define LOG_MODULE "render"40 #define LOG_ENABLE_DBG 041 #include "log.h"42 #include "char32.h"1017 unmodified lines1060 #endif1061 }1062 #endif /* FUZZEL_ENABLE_SVG_NANOSVG */1063 1064+#if defined(FUZZEL_ENABLE_SVG_RESVG)1065 static void1066+render_svg_resvg(struct icon *icon, int x, int y, int size,1067+ pixman_image_t *pix, cairo_t *cairo, bool gamma_correct)1068+{1069+#if defined(FUZZEL_ENABLE_CAIRO)1070+ cairo_surface_flush(cairo_get_target(cairo));1071+#endif1072+1073+ pixman_image_t *img = NULL;1074+1075+ /* Look for a cached image, at the correct size */1076+ tll_foreach(icon->rasterized, it) {1077+ if (it->item.size == size) {1078+ img = it->item.pix;1079+ break;1080+ }1081+ }1082+1083+ if (img == NULL) {1084+ resvg_render_tree *tree = icon->svg;1085+ resvg_size svg_size = resvg_get_image_size(tree);1086+1087+ if (svg_size.width == 0 || svg_size.height == 0)1088+ return;1089+1090+ float scale = svg_size.width > svg_size.height ? size / svg_size.width : size / svg_size.height;1091+1092+ const int width = roundf(svg_size.width * scale);1093+ const int height = roundf(svg_size.height * scale);1094+1095+ uint8_t *data_8bit = xmalloc(width * height * 4);1096+ memset(data_8bit, 0, width * height * 4);1097+1098+ /* resvg renders to RGBA8888 premultiplied */1099+ resvg_transform transform = resvg_transform_identity();1100+ transform.a = scale;1101+ transform.d = scale;1102+ resvg_render(tree, transform, width, height, (char *)data_8bit);1103+1104+ if (gamma_correct) {1105+ /* For gamma-correct blending, create 16-bit buffer and image */1106+ uint8_t *data_16bit = xmalloc(width * height * 8);1107+ uint64_t *abgr16 = (uint64_t *)data_16bit;1108+1109+ /* resvg produces premultiplied RGBA, need to convert to ABGR16 for pixman */1110+ for (uint32_t *rgba = (uint32_t *)data_8bit;1111+ rgba < (uint32_t *)(data_8bit + width * height * 4);1112+ rgba++)1113+ {1114+ uint16_t red = (*rgba >> 0) & 0xff;1115+ uint16_t green = (*rgba >> 8) & 0xff;1116+ uint16_t blue = (*rgba >> 16) & 0xff;1117+ uint16_t alpha = (*rgba >> 24) & 0xff;1118+1119+ /* Decode sRGB to linear for gamma-correct blending */1120+ if (alpha == 0x00) {1121+ blue = green = red = 0x00;1122+ } else {1123+ /* First, un-premultiply */1124+ blue = (uint16_t)(blue * 0xff / alpha);1125+ green = (uint16_t)(green * 0xff / alpha);1126+ red = (uint16_t)(red * 0xff / alpha);1127+1128+ /* Convert to linear 16-bit */1129+ alpha |= alpha << 8; /* Alpha already linear, expand to 16-bit */1130+ blue = srgb_decode_8_to_16(blue);1131+ green = srgb_decode_8_to_16(green);1132+ red = srgb_decode_8_to_16(red);1133+1134+ /* Re-premultiply in 16-bit linear space */1135+ blue = blue * alpha / 0xffff;1136+ green = green * alpha / 0xffff;1137+ red = red * alpha / 0xffff;1138+ }1139+1140+ *abgr16 = (uint64_t)alpha << 48 | (uint64_t)blue << 32 | (uint64_t)green << 16 | (uint64_t)red;1141+ abgr16++;1142+ }1143+1144+ /* Free the 8-bit buffer as we've converted everything to 16-bit */1145+ free(data_8bit);1146+1147+ /* Create 16-bit pixman image */1148+ img = pixman_image_create_bits_no_clear(1149+ PIXMAN_a16b16g16r16, width, height, (uint32_t *)data_16bit,1150+ width * 8);1151+ } else {1152+ /* Convert RGBA to ABGR (already premultiplied) */1153+ for (uint32_t *rgba = (uint32_t *)data_8bit;1154+ rgba < (uint32_t *)(data_8bit + width * height * 4);1155+ rgba++)1156+ {1157+ uint16_t red = (*rgba >> 0) & 0xff;1158+ uint16_t green = (*rgba >> 8) & 0xff;1159+ uint16_t blue = (*rgba >> 16) & 0xff;1160+ uint16_t alpha = (*rgba >> 24) & 0xff;1161+1162+ *rgba = (uint32_t)alpha << 24 | blue << 16 | green << 8 | red;1163+ }1164+1165+ /* Create 8-bit pixman image */1166+ img = pixman_image_create_bits_no_clear(1167+ PIXMAN_a8b8g8r8, width, height, (uint32_t *)data_8bit, width * 4);1168+ }1169+1170+ tll_push_back(icon->rasterized, ((struct rasterized){img, size}));1171+ }1172+1173+ const int w = pixman_image_get_width(img);1174+ const int h = pixman_image_get_height(img);1175+1176+ pixman_image_composite32(1177+ PIXMAN_OP_OVER, img, NULL, pix, 0, 0, 0, 0,1178+ x + (size - w) / 2,1179+ y + (size - h) / 2,1180+ w, h);1181+1182+#if defined(FUZZEL_ENABLE_CAIRO)1183+ cairo_surface_mark_dirty(cairo_get_target(cairo));1184+#endif1185+}1186+#endif /* FUZZEL_ENABLE_SVG_RESVG */1187+1188+static void1189 render_svg(struct icon *icon, int x, int y, int size,1190 pixman_image_t *pix, cairo_t *cairo, bool gamma_correct,1191 bool print_timing_info)1192 {25 unmodified lines1218 #if defined(FUZZEL_ENABLE_SVG_LIBRSVG)1219 render_svg_librsvg(icon, x, y, size, cairo);1220 #elif defined(FUZZEL_ENABLE_SVG_NANOSVG)1221 render_svg_nanosvg(icon, x, y, size, pix, cairo, gamma_correct);1222+#elif defined(FUZZEL_ENABLE_SVG_RESVG)1223+ render_svg_resvg(icon, x, y, size, pix, cairo, gamma_correct);1224 #endif1225 1226 if (print_timing_info) {1227 gettimeofday(&stop, NULL);798 unmodified linesmodified:wayland.c+3 added-3 removed
2841 unmodified lines2842 wayl->fractional_scale, &fractional_scale_listener, wayl);2843 }2844 2845 if (conf->gamma_correct) {2846-#if !defined(FUZZEL_ENABLE_CAIRO)2846+#if defined(FUZZEL_ENABLE_CAIRO)2847+ LOG_WARN("gamma-correct-blending: disabling; not supported in cairo-enabled builds");2848+#else2849 if (wayl->color_management.img_description != NULL) {2850 assert(wayl->color_management.manager != NULL);2851 2852 wayl->color_management_surface = wp_color_manager_v1_get_surface(19 unmodified lines2872 LOG_WARN(" - TF: ext_linear");2873 LOG_WARN(" - primaries: sRGB");2874 }2875 }2874-#else2875- LOG_WARN("gamma-correct-blending: disabling; not supported in cairo-enabled builds");2876 #endif2877 } else2878 LOG_INFO("gamma-correct blending: disabled");2879 190 unmodified lines