Skip to content
Myr

✨ 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

8 files+226 added−10 removed

modified:application.h+6 added
10 unmodified lines
11#if defined(FUZZEL_ENABLE_SVG_NANOSVG)12 #include <nanosvg/nanosvg.h>13#endif1415#if defined(FUZZEL_ENABLE_SVG_RESVG)16 #include <resvg.h>17#endif1819#include <fcft/fcft.h>20#include <tllist.h>2122#include "prompt.h"
19 unmodified lines
42#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 lines
modified:CHANGELOG.md+9 added-2 removed
31 unmodified lines
323334## Unreleased35### Added3637 * 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]).4142[650]: https://codeberg.org/dnkl/fuzzel/issues/6504344### Changed45### Deprecated46### Removed47### Fixed
6 unmodified lines
5455### Security56### Contributors5758 * Mark Stosberg5960## 1.13.16162### Fixed
6 unmodified lines
69[647]: https://codeberg.org/dnkl/fuzzel/issues/64770[653]: https://codeberg.org/dnkl/fuzzel/issues/65371[652]: https://codeberg.org/dnkl/fuzzel/issues/652726473### Contributors7475 * Mark Stosberg76 * rehanzo777078## 1.13.07980### Added81
1023 unmodified lines
modified:icon.c+47 added-2 removed
21 unmodified lines
22#if defined(FUZZEL_ENABLE_SVG_NANOSVG)23 #include <nanosvg/nanosvg.h>24#endif2526#if defined(FUZZEL_ENABLE_SVG_RESVG)27 #include <resvg.h>28#endif2930#include <tllist.h>3132#define LOG_MODULE "icon"33#define LOG_ENABLE_DBG 0
421 unmodified lines
455 return true;456}457#endif458459#if defined(FUZZEL_ENABLE_SVG_RESVG)460static bool461icon_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 }467468 /* Set DPI to 96, same as nanosvg */469 resvg_options_set_dpi(opt, 96);470471 resvg_render_tree *tree = NULL;472 int32_t result = resvg_parse_tree_from_file(file_name, opt, &tree);473 resvg_options_destroy(opt);474475 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 }484485 if (resvg_is_image_empty(tree)) {486 resvg_tree_destroy(tree);487 return false;488 }489490 icon->type = ICON_SVG;491 icon->svg = tree;492 return true;493}494#endif495496bool497icon_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 lines
550#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 lines
592 path[path_len - 2] = 'n';593 path[path_len - 1] = 'g';594595 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 lines
604 }605606 return true;607563#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 lines
modified:main.c+2 added
320 unmodified lines
321#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 lines
modified:meson_options.txt+2 added-2 removed
1option('enable-cairo', type: 'feature', description: 'use cairo; required by the librsvg SVG icon backend')1option('enable-cairo', type: 'feature', value: 'auto', description: 'use cairo; required by the librsvg SVG icon backend, disabled by default with resvg')2option('png-backend', type: 'combo', choices: ['none', 'libpng'], value: 'libpng', description: 'enable support for PNG icons')3option('svg-backend', type: 'combo', choices: ['none', 'librsvg', 'nanosvg'], value: 'nanosvg', description: 'enables support for SVG icons (librsvg: also requires cairo, nanosvg: bundled)')3option('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)')4option('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 lines
56math = cc.find_library('m')57threads = [dependency('threads'), cc.find_library('stdthreads', required: false)]58fontconfig = dependency('fontconfig')59pixman = dependency('pixman-1', version: '>=0.46.0')60# Cairo is required for librsvg but not for resvg/nanosvg61if 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 else60cairo = dependency('cairo', required: get_option('enable-cairo'))66 cairo = dependency('cairo', required: get_option('enable-cairo'))67 endif68else69 # For librsvg or no SVG backend, respect the enable-cairo option70 cairo = dependency('cairo', required: get_option('enable-cairo'))71endif72libepoll = dependency('epoll-shim', required: false)73wayland_protocols = dependency('wayland-protocols', version: '>=1.41')74wayland_client = dependency('wayland-client')75wayland_cursor = dependency('wayland-cursor')
17 unmodified lines
9394if 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')100elif get_option('svg-backend') == 'nanosvg'101 if system_nanosvg.found() and system_nanosvgrast.found()
10 unmodified lines
112 svg_backend = 'nanosvg (bundled)'113 endif114 add_project_arguments('-DFUZZEL_ENABLE_SVG_NANOSVG=1', language: 'c')115 librsvg = declare_dependency()116 resvg = declare_dependency()117elif 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()128else129 svg_backend = 'none'130 librsvg = declare_dependency()131 nanosvg = declare_dependency()132 resvg = declare_dependency()133endif134135wayland_protocols_datadir = wayland_protocols.get_variable('pkgdatadir')136
84 unmodified lines
221 libepoll,222 libpng,223 librsvg,224 nanosvg,225 resvg,226 wayland_client,227 wayland_cursor,228 xkb,229 tllist,
45 unmodified lines
modified:render.c+130 added
30 unmodified lines
31#if defined(FUZZEL_ENABLE_SVG_NANOSVG)32 #include <nanosvg/nanosvgrast.h>33#endif3435#if defined(FUZZEL_ENABLE_SVG_RESVG)36 #include <resvg.h>37#endif3839#define LOG_MODULE "render"40#define LOG_ENABLE_DBG 041#include "log.h"42#include "char32.h"
1017 unmodified lines
1060#endif1061}1062#endif /* FUZZEL_ENABLE_SVG_NANOSVG */10631064#if defined(FUZZEL_ENABLE_SVG_RESVG)1065static void1066render_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#endif10721073 pixman_image_t *img = NULL;10741075 /* 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 }10821083 if (img == NULL) {1084 resvg_render_tree *tree = icon->svg;1085 resvg_size svg_size = resvg_get_image_size(tree);10861087 if (svg_size.width == 0 || svg_size.height == 0)1088 return;10891090 float scale = svg_size.width > svg_size.height ? size / svg_size.width : size / svg_size.height;10911092 const int width = roundf(svg_size.width * scale);1093 const int height = roundf(svg_size.height * scale);10941095 uint8_t *data_8bit = xmalloc(width * height * 4);1096 memset(data_8bit, 0, width * height * 4);10971098 /* 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);11031104 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;11081109 /* 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;11181119 /* 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);11271128 /* 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);11331134 /* Re-premultiply in 16-bit linear space */1135 blue = blue * alpha / 0xffff;1136 green = green * alpha / 0xffff;1137 red = red * alpha / 0xffff;1138 }11391140 *abgr16 = (uint64_t)alpha << 48 | (uint64_t)blue << 32 | (uint64_t)green << 16 | (uint64_t)red;1141 abgr16++;1142 }11431144 /* Free the 8-bit buffer as we've converted everything to 16-bit */1145 free(data_8bit);11461147 /* 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;11611162 *rgba = (uint32_t)alpha << 24 | blue << 16 | green << 8 | red;1163 }11641165 /* 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 }11691170 tll_push_back(icon->rasterized, ((struct rasterized){img, size}));1171 }11721173 const int w = pixman_image_get_width(img);1174 const int h = pixman_image_get_height(img);11751176 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);11811182#if defined(FUZZEL_ENABLE_CAIRO)1183 cairo_surface_mark_dirty(cairo_get_target(cairo));1184#endif1185}1186#endif /* FUZZEL_ENABLE_SVG_RESVG */11871188static void1189render_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 lines
1218#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#endif12251226 if (print_timing_info) {1227 gettimeofday(&stop, NULL);
798 unmodified lines
modified:wayland.c+3 added-3 removed
2841 unmodified lines
2842 wayl->fractional_scale, &fractional_scale_listener, wayl);2843 }28442845 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);28512852 wayl->color_management_surface = wp_color_manager_v1_get_surface(
19 unmodified lines
2872 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