Merge branch 'libpng' into master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #8
authored by Daniel Eklöf · committed by Daniel Eklöf · parents 52c5b21, f662ff1 · Browse files · 9 files·+317 added−61 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+317 added−61 removed
- other:+317 added−61 removed
Provenance
- Commit
1704828- Tree
b7686a3- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
52c5b21- Parent 2
f662ff1
9 files changed+317 added−61 removed
modified:application.c+14 added-4 removed
227 unmodified lines228 free(app->exec);229 free(app->title);230 free(app->comment);231 free(app->icon.name);232- if (app->icon.type == ICON_SURFACE)233- cairo_surface_destroy(app->icon.surface);234- else if (app->icon.type == ICON_SVG) {235-#ifdef FUZZEL_ENABLE_SVG232+ switch (app->icon.type) {233+ case ICON_NONE:234+ break;235+236+ case ICON_PNG:237+#if defined(FUZZEL_ENABLE_PNG)238+ free(pixman_image_get_data(app->icon.png.pix));239+ pixman_image_unref(app->icon.png.pix);240+#endif241+ break;242+243+ case ICON_SVG:244+#if defined(FUZZEL_ENABLE_SVG)245 g_object_unref(app->icon.svg);246 #endif247+ break;248 }249 }250 free(apps->v);251 free(apps);1 unmodified linemodified:application.h+9 added-3 removed
1 #pragma once2 3 #include <stdbool.h>4+#include <pixman.h>5 6 #include <cairo.h>7 #ifdef FUZZEL_ENABLE_SVG8 #include <librsvg/rsvg.h>9 #endif10 11 #include "prompt.h"12 12-enum icon_type { ICON_NONE, ICON_SURFACE, ICON_SVG };13+enum icon_type { ICON_NONE, ICON_PNG, ICON_SVG };14 15 struct icon {16 char *name;17 enum icon_type type;18 union {18- cairo_surface_t *surface;19-#ifdef FUZZEL_ENABLE_SVG19+#if defined(FUZZEL_ENABLE_PNG)20+ struct {21+ pixman_image_t *pix;22+ bool has_scale_transform;23+ } png;24+#endif25+#if defined(FUZZEL_ENABLE_SVG)26 RsvgHandle *svg;27 #endif28 };29 };20 unmodified linesmodified:CHANGELOG.md+8 added
9 unmodified lines101112131415161718192021222324252627282916 unmodified lines9 unmodified lines
* meson option `Denable-svg=[auto|enabled|disabled]`. When disabled, _librsvg_ is no longer a dependency and SVG icons are disabled. Default: `auto`.* meson option `-Denable-png=[auto|enabled|disabled]`. When disabled, _libpng_ is no longer a dependency and PNG icons are disabled. Default: `auto`.
### Deprecated### Removed### Changed
* PNGs are now loaded and rendered with _libpng_ instead of _cairo_.
### Fixed### Security### Contributors
16 unmodified linesmodified:icon.c+58 added-26 removed
8 unmodified lines9 10 #include <sys/stat.h>11 #include <fcntl.h>12 13-#ifdef FUZZEL_ENABLE_SVG14-#include <librsvg/rsvg.h>13+#if defined(FUZZEL_ENABLE_PNG)14+ #include "png-fuzzel.h"15 #endif16 17+#if defined(FUZZEL_ENABLE_SVG)18+ #include <librsvg/rsvg.h>19+#endif20+21 #include <tllist.h>22 23 #define LOG_MODULE "icon"24+#define LOG_ENABLE_DBG 025 #include "log.h"26 #include "xdg.h"27 28 typedef tll(char *) theme_names_t;247 unmodified lines276 icon->type = ICON_NONE;277 return true;278 }279 280+#if defined(FUZZEL_ENABLE_PNG)281 static bool276-icon_from_surface(struct icon *icon, cairo_surface_t *surf)282+icon_from_png(struct icon *icon, pixman_image_t *png)283 {278- icon->type = ICON_SURFACE;279- icon->surface = surf;284+ icon->type = ICON_PNG;285+ icon->png.pix = png;286+ icon->png.has_scale_transform = false;287 return true;288 }289+#endif290 283-#ifdef FUZZEL_ENABLE_SVG291+#if defined(FUZZEL_ENABLE_SVG)292 static bool293 icon_from_svg(struct icon *icon, RsvgHandle *svg)294 {295 icon->type = ICON_SVG;4 unmodified lines300 301 static void302 icon_reset(struct icon *icon)303 {296- if (icon->type == ICON_SURFACE) {297- cairo_surface_destroy(icon->surface);298- icon->surface = NULL;299- } else if (icon->type == ICON_SVG) {300-#ifdef FUZZEL_ENABLE_SVG304+ switch (icon->type) {305+ case ICON_NONE:306+ break;307+308+ case ICON_PNG:309+#if defined(FUZZEL_ENABLE_PNG)310+ free(pixman_image_get_data(icon->png.pix));311+ pixman_image_unref(icon->png.pix);312+ icon->png.pix = NULL;313+ icon->png.has_scale_transform = false;314+#endif315+ break;316+317+ case ICON_SVG:318+#if defined(FUZZEL_ENABLE_SVG)319 g_object_unref(icon->svg);320 icon->svg = NULL;321 #endif322+ break;323 }324 icon->type = ICON_NONE;325 }326 9 unmodified lines336 if (name == NULL)337 return icon_null(icon);338 339 if (name[0] == '/') {321- cairo_surface_t *surf = cairo_image_surface_create_from_png(name);322- if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS)323- return icon_from_surface(icon, surf);324-340+#if defined(FUZZEL_ENABLE_SVG)341+ RsvgHandle *svg = rsvg_handle_new_from_file(name, NULL);342+ if (svg != NULL) {343+ LOG_DBG("%s: absolute path SVG", name);344+ return icon_from_svg(icon, svg);345+ }346+#endif347+#if defined(FUZZEL_ENABLE_PNG)348+ pixman_image_t *png = png_load(name);349+ if (png != NULL) {350+ LOG_DBG("%s: absolute path PNG", name);351+ return icon_from_png(icon, png);352+ }353+#endif354 return icon_null(icon);355 }356 357 LOG_DBG("looking for %s (wanted size: %d)", name, icon_size);48 unmodified lines406 } else {407 /* Use anyone available */408 }409 381-#ifdef FUZZEL_ENABLE_SVG410+#if defined(FUZZEL_ENABLE_SVG)411 RsvgHandle *svg = rsvg_handle_new_from_file(full_path, NULL);412 if (svg != NULL) {413 LOG_DBG("%s: %s scalable", name, full_path);414 free(full_path);415 return icon_from_svg(icon, svg);416 }417 #endif389- cairo_surface_t *surf = cairo_image_surface_create_from_png(full_path);390- if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {418+419+#if defined(FUZZEL_ENABLE_PNG)420+ pixman_image_t *png = png_load(full_path);421+ if (png != NULL) {422 if (scalable)423 LOG_DBG("%s: %s: scalable", name, full_path);424 else if (i == 0)425 LOG_DBG("%s: %s: exact match", name, full_path);5 unmodified lines431 else432 LOG_DBG("%s: %s: nothing else matched", name, full_path);433 434 free(full_path);404- return icon_from_surface(icon, surf);435+ return icon_from_png(icon, png);436 }406-437+#endif438 free(full_path);408- cairo_surface_destroy(surf);439 }440 }441 }442 2 unmodified lines445 char path[strlen(it->item) + 1 +446 strlen("pixmaps") + 1 +447 strlen(name) + strlen(".svg") + 1];448 419-#ifdef FUZZEL_ENABLE_SVG449+#if defined(FUZZEL_ENABLE_SVG)450 /* Try SVG variant first */451 sprintf(path, "%s/pixmaps/%s.svg", it->item, name);452 RsvgHandle *svg = rsvg_handle_new_from_file(path, NULL);453 if (svg != NULL) {1 unmodified line455 return icon_from_svg(icon, svg);456 }457 #endif458 459+460+#if defined(FUZZEL_ENABLE_PNG)461 /* No SVG, look for PNG instead */462 sprintf(path, "%s/pixmaps/%s.png", it->item, name);431- cairo_surface_t *surf = cairo_image_surface_create_from_png(path);432- if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {463+ pixman_image_t *png = png_load(path);464+ if (png != NULL) {465 xdg_data_dirs_destroy(dirs);434- return icon_from_surface(icon, surf);466+ return icon_from_png(icon, png);467 }436- cairo_surface_destroy(surf);468+#endif469 }470 xdg_data_dirs_destroy(dirs);471 472 return icon_null(icon);12 unmodified linesmodified:meson_options.txt+1 added
1+option('enable-png', type: 'feature')2 option('enable-svg', type: 'feature')modified:meson.build+15 added
48 unmodified lines49 math = cc.find_library('m')50 pixman = dependency('pixman-1')51 cairo = dependency('cairo')52 cairo_ft = dependency('cairo-ft')53+libpng = dependency('libpng', required: get_option('enable-png'))54 librsvg = dependency('librsvg-2.0', required: get_option('enable-svg'))55 wayland_protocols = dependency('wayland-protocols')56 wayland_client = dependency('wayland-client')57 wayland_cursor = dependency('wayland-cursor')58 xkb = dependency('xkbcommon')59 60+if (libpng.found())61+ add_project_arguments('-DFUZZEL_ENABLE_PNG=1', language: 'c')62+endif63+64 if librsvg.found()65 add_project_arguments('-DFUZZEL_ENABLE_SVG=1', language: 'c')66 endif67 41 unmodified lines109 'icon.c', 'icon.h',110 'log.c', 'log.h',111 'main.c',112 'match.c', 'match.h',113+ 'png.c', 'png-fuzzel.h',114 'prompt.c', 'prompt.h',115 'render.c', 'render.h',116 'shm.c', 'shm.h',117 'stride.h',3 unmodified lines121 dependencies: [math,122 pixman,123 cairo,124 cairo_ft,125+ libpng,126 librsvg,127 wayland_client,128 wayland_cursor,129 xkb,6 unmodified lines136 install_dir: join_paths(get_option('datadir'), 'doc', 'fuzzel'))137 138 subdir('completions')139 subdir('doc')140+141+summary(142+ {143+ 'PNG icons': libpng.found(),144+ 'SVG icons': librsvg.found(),145+ },146+ bool_yn: true147+)added:png-fuzzel.h+9 added
1+#pragma once2+3+#if defined(FUZZEL_ENABLE_PNG)4+5+#include <pixman.h>6+7+pixman_image_t *png_load(const char *path);8+9+#endif /* FUZZEL_ENABLE_PNG */added:png.c+147 added
1+#if defined(FUZZEL_ENABLE_PNG)2+3+#include "png-fuzzel.h"4+5+#include <stdlib.h>6+#include <stdio.h>7+#include <stdbool.h>8+#include <string.h>9+#include <assert.h>10+11+#include <png.h>12+#include <pixman.h>13+14+#define LOG_MODULE "png"15+#define LOG_ENABLE_DBG 016+#include "log.h"17+#include "stride.h"18+19+pixman_image_t *20+png_load(const char *path)21+{22+ pixman_image_t *pix = NULL;23+24+ FILE *fp = NULL;25+ png_structp png_ptr = NULL;26+ png_infop info_ptr = NULL;27+ png_bytepp row_pointers = NULL;28+ uint8_t *image_data = NULL;29+30+ /* open file and test for it being a png */31+ if ((fp = fopen(path, "rb")) == NULL) {32+ //LOG_ERRNO("%s: failed to open", path);33+ goto err;34+ }35+36+ /* Verify PNG header */37+ uint8_t header[8] = {0};38+ if (fread(header, 1, 8, fp) != 8 || png_sig_cmp(header, 0, 8)) {39+ // LOG_ERR("%s: not a PNG", path);40+ goto err;41+ }42+43+ /* Prepare for reading the PNG */44+ if ((png_ptr = png_create_read_struct(45+ PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL ||46+ (info_ptr = png_create_info_struct(png_ptr)) == NULL)47+ {48+ LOG_ERR("%s: failed to initialize libpng", path);49+ goto err;50+ }51+52+ if (setjmp(png_jmpbuf(png_ptr))) {53+ LOG_ERR("%s: libpng error", path);54+ goto err;55+ }56+57+ png_init_io(png_ptr, fp);58+ png_set_sig_bytes(png_ptr, 8);59+60+ /* Get meta data */61+ png_read_info(png_ptr, info_ptr);62+ int width = png_get_image_width(png_ptr, info_ptr);63+ int height = png_get_image_height(png_ptr, info_ptr);64+ png_byte color_type = png_get_color_type(png_ptr, info_ptr);65+ png_byte bit_depth __attribute__((unused)) = png_get_bit_depth(png_ptr, info_ptr);66+ int channels __attribute__((unused)) = png_get_channels(png_ptr, info_ptr);67+68+ LOG_DBG("%s: %dx%d@%hhubpp, %d channels", path, width, height, bit_depth, channels);69+70+ png_set_packing(png_ptr);71+ png_set_interlace_handling(png_ptr);72+ png_set_strip_16(png_ptr); /* "pack" 16-bit colors to 8-bit */73+ png_set_bgr(png_ptr);74+75+ /* pixman expects pre-multiplied alpha */76+ png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, 1.0);77+78+ /* Tell libpng to expand to RGB(A) when necessary, and tell pixman79+ * whether we have alpha or not */80+ pixman_format_code_t format;81+ switch (color_type) {82+ case PNG_COLOR_TYPE_GRAY:83+ case PNG_COLOR_TYPE_GRAY_ALPHA:84+ LOG_DBG("%d-bit gray%s", bit_depth,85+ color_type == PNG_COLOR_TYPE_GRAY_ALPHA ? "+alpha" : "");86+87+ if (bit_depth < 8)88+ png_set_expand_gray_1_2_4_to_8(png_ptr);89+90+ png_set_gray_to_rgb(png_ptr);91+ format = color_type == PNG_COLOR_TYPE_GRAY ? PIXMAN_r8g8b8 : PIXMAN_a8r8g8b8;92+ break;93+94+ case PNG_COLOR_TYPE_PALETTE:95+ LOG_DBG("%d-bit colormap%s", bit_depth,96+ png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ? "+tRNS" : "");97+98+ png_set_palette_to_rgb(png_ptr);99+ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {100+ png_set_tRNS_to_alpha(png_ptr);101+ format = PIXMAN_a8r8g8b8;102+ } else103+ format = PIXMAN_r8g8b8;104+ break;105+106+ case PNG_COLOR_TYPE_RGB:107+ LOG_DBG("RGB");108+ format = PIXMAN_r8g8b8;109+ break;110+111+ case PNG_COLOR_TYPE_RGBA:112+ LOG_DBG("RGBA");113+ format = PIXMAN_a8r8g8b8;114+ break;115+ }116+117+ png_read_update_info(png_ptr, info_ptr);118+119+ size_t row_bytes __attribute__((unused)) = png_get_rowbytes(png_ptr, info_ptr);120+ int stride = stride_for_format_and_width(format, width);121+ image_data = malloc(height * stride);122+123+ LOG_DBG("stride=%d, row-bytes=%zu", stride, row_bytes);124+ assert(stride >= row_bytes);125+126+ row_pointers = malloc(height * sizeof(png_bytep));127+ for (int i = 0; i < height; i++)128+ row_pointers[i] = &image_data[i * stride];129+130+ png_read_image(png_ptr, row_pointers);131+132+ pix = pixman_image_create_bits_no_clear(133+ format, width, height, (uint32_t *)image_data, stride);134+135+err:136+ if (pix == NULL)137+ free(image_data);138+ free(row_pointers);139+ if (png_ptr != NULL)140+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);141+ if (fp != NULL)142+ fclose(fp);143+144+ return pix;145+}146+147+#endif /* FUZZEL_ENABLE_PNG */modified:render.c+56 added-28 removed
233 unmodified lines234 * there's "enough" free space, render a large235 * representation of the icon */236 237 if (match->application->icon.type == ICON_SVG) {238-#ifdef FUZZEL_ENABLE_SVG238+#if defined(FUZZEL_ENABLE_SVG)239 RsvgHandle *svg = match->application->icon.svg;240 RsvgDimensionData dim;241 rsvg_handle_get_dimensions(svg, &dim);242 24 unmodified lines267 if (cairo_status(buf->cairo) == CAIRO_STATUS_SUCCESS)268 rsvg_handle_render_cairo(svg, buf->cairo);269 cairo_restore(buf->cairo);270 }271-#endif271+#endif /* FUZZEL_ENABLE_SVG */272 }273+274 /* Hightlight selected entry */275 const struct rgba *sc = &render->options.selection_color;276 cairo_set_source_rgba(buf->cairo, sc->r, sc->g, sc->b, sc->a);277 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);5 unmodified lines283 cairo_fill(buf->cairo);284 }285 286 double cur_x = border_size + x_margin;287+ struct icon *icon = &match->application->icon;288 287- switch (match->application->icon.type) {289+ switch (icon->type) {290 case ICON_NONE:291 break;292 291- case ICON_SURFACE: {292- cairo_surface_t *surf = match->application->icon.surface;293- double width = cairo_image_surface_get_width(surf);294- double height = cairo_image_surface_get_height(surf);295- double scale = 1.0;293+ case ICON_PNG: {294+#if defined(FUZZEL_ENABLE_PNG)295+ cairo_surface_flush(buf->cairo_surface);296 297+ pixman_image_t *png = icon->png.pix;298+ int height = pixman_image_get_height(png);299+ int width = pixman_image_get_width(png);300+301 if (height > row_height) {298- scale = font->height / height;299- LOG_DBG("%s: scaling: %f (row-height: %f, size=%fx%f)",300- match->application->title, scale, row_height, width, height);302+ double scale = (double)font->height / height;303 304+ if (!icon->png.has_scale_transform) {305+ pixman_f_transform_t _scale_transform;306+ pixman_f_transform_init_scale(307+ &_scale_transform, 1. / scale, 1. / scale);308+309+ pixman_transform_t scale_transform;310+ pixman_transform_from_pixman_f_transform(311+ &scale_transform, &_scale_transform);312+ pixman_image_set_transform(png, &scale_transform);313+314+ int param_count = 0;315+ pixman_kernel_t kernel = PIXMAN_KERNEL_LANCZOS3;316+ pixman_fixed_t *params = pixman_filter_create_separable_convolution(317+ ¶m_count,318+ pixman_double_to_fixed(1. / scale),319+ pixman_double_to_fixed(1. / scale),320+ kernel, kernel,321+ kernel, kernel,322+ pixman_int_to_fixed(1),323+ pixman_int_to_fixed(1));324+325+ if (params != NULL || param_count == 0) {326+ pixman_image_set_filter(327+ png, PIXMAN_FILTER_SEPARABLE_CONVOLUTION,328+ params, param_count);329+ }330+331+ free(params);332+ icon->png.has_scale_transform = true;333+ }334+335+ assert(icon->png.has_scale_transform);336 width *= scale;337 height *= scale;338 }339 306- cairo_save(buf->cairo);307- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);340+ pixman_image_composite32(341+ PIXMAN_OP_OVER, png, NULL, buf->pix,342+ 0, 0, 0, 0,343+ cur_x, first_row + i * row_height + (row_height - height) / 2,344+ width, height);345 309- /* Translate + scale. Note: order matters! */310- cairo_translate(311- buf->cairo, cur_x,312- first_row + i * row_height + (row_height - height) / 2);313- cairo_scale(buf->cairo, scale, scale);314-315- cairo_set_source_surface(buf->cairo, surf, 0, 0);316- cairo_paint(buf->cairo);317- cairo_restore(buf->cairo);346+ cairo_surface_mark_dirty(buf->cairo_surface);347+#endif /* FUZZEL_ENABLE_PNG */348 break;349 }350 351 case ICON_SVG: {322-#ifdef FUZZEL_ENABLE_SVG323- RsvgHandle *svg = match->application->icon.svg;324-352+#if defined(FUZZEL_ENABLE_SVG)353 RsvgDimensionData dim;326- rsvg_handle_get_dimensions(svg, &dim);354+ rsvg_handle_get_dimensions(icon->svg, &dim);355 356 double height = font->height;357 double scale = height / dim.height;358 6 unmodified lines365 first_row + i * row_height + (row_height - height) / 2);366 cairo_scale(buf->cairo, scale, scale);367 368 if (cairo_status(buf->cairo) == CAIRO_STATUS_SUCCESS)341- rsvg_handle_render_cairo(svg, buf->cairo);369+ rsvg_handle_render_cairo(icon->svg, buf->cairo);370 cairo_restore(buf->cairo);371+#endif /* FUZZEL_ENABLE_SVG */372 break;344-#endif373 }374 }375 376 cur_x += row_height;97 unmodified lines