Skip to content
Myr

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

9 files+317 added−61 removed

modified:application.c+14 added-4 removed
227 unmodified lines
228 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;235236 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;242243 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 line
modified:application.h+9 added-3 removed
1#pragma once23#include <stdbool.h>4#include <pixman.h>56#include <cairo.h>7#ifdef FUZZEL_ENABLE_SVG8#include <librsvg/rsvg.h>9#endif1011#include "prompt.h"1212enum icon_type { ICON_NONE, ICON_SURFACE, ICON_SVG };13enum icon_type { ICON_NONE, ICON_PNG, ICON_SVG };1415struct 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 lines
modified:CHANGELOG.md+8 added
modified:icon.c+58 added-26 removed
8 unmodified lines
910#include <sys/stat.h>11#include <fcntl.h>1213#ifdef FUZZEL_ENABLE_SVG14#include <librsvg/rsvg.h>13#if defined(FUZZEL_ENABLE_PNG)14 #include "png-fuzzel.h"15#endif1617#if defined(FUZZEL_ENABLE_SVG)18 #include <librsvg/rsvg.h>19#endif2021#include <tllist.h>2223#define LOG_MODULE "icon"24#define LOG_ENABLE_DBG 025#include "log.h"26#include "xdg.h"2728typedef tll(char *) theme_names_t;
247 unmodified lines
276 icon->type = ICON_NONE;277 return true;278}279280#if defined(FUZZEL_ENABLE_PNG)281static bool276icon_from_surface(struct icon *icon, cairo_surface_t *surf)282icon_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#endif290283#ifdef FUZZEL_ENABLE_SVG291#if defined(FUZZEL_ENABLE_SVG)292static bool293icon_from_svg(struct icon *icon, RsvgHandle *svg)294{295 icon->type = ICON_SVG;
4 unmodified lines
300301static void302icon_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;307308 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;316317 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 lines
336 if (name == NULL)337 return icon_null(icon);338339 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);324340#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 }356357 LOG_DBG("looking for %s (wanted size: %d)", name, icon_size);
48 unmodified lines
406 } else {407 /* Use anyone available */408 }409381#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) {418419#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 lines
431 else432 LOG_DBG("%s: %s: nothing else matched", name, full_path);433434 free(full_path);404 return icon_from_surface(icon, surf);435 return icon_from_png(icon, png);436 }406437#endif438 free(full_path);408 cairo_surface_destroy(surf);439 }440 }441 }442
2 unmodified lines
445 char path[strlen(it->item) + 1 +446 strlen("pixmaps") + 1 +447 strlen(name) + strlen(".svg") + 1];448419#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 line
455 return icon_from_svg(icon, svg);456 }457#endif458459460#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);471472 return icon_null(icon);
12 unmodified lines
modified:meson_options.txt+1 added
1option('enable-png', type: 'feature')2option('enable-svg', type: 'feature')
modified:meson.build+15 added
48 unmodified lines
49math = cc.find_library('m')50pixman = dependency('pixman-1')51cairo = dependency('cairo')52cairo_ft = dependency('cairo-ft')53libpng = dependency('libpng', required: get_option('enable-png'))54librsvg = dependency('librsvg-2.0', required: get_option('enable-svg'))55wayland_protocols = dependency('wayland-protocols')56wayland_client = dependency('wayland-client')57wayland_cursor = dependency('wayland-cursor')58xkb = dependency('xkbcommon')5960if (libpng.found())61 add_project_arguments('-DFUZZEL_ENABLE_PNG=1', language: 'c')62endif6364if librsvg.found()65 add_project_arguments('-DFUZZEL_ENABLE_SVG=1', language: 'c')66endif67
41 unmodified lines
109 '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 lines
121 dependencies: [math,122 pixman,123 cairo,124 cairo_ft,125 libpng,126 librsvg,127 wayland_client,128 wayland_cursor,129 xkb,
6 unmodified lines
136 install_dir: join_paths(get_option('datadir'), 'doc', 'fuzzel'))137138subdir('completions')139subdir('doc')140141summary(142 {143 'PNG icons': libpng.found(),144 'SVG icons': librsvg.found(),145 },146 bool_yn: true147)
added:png-fuzzel.h+9 added
1#pragma once23#if defined(FUZZEL_ENABLE_PNG)45#include <pixman.h>67pixman_image_t *png_load(const char *path);89#endif /* FUZZEL_ENABLE_PNG */
added:png.c+147 added
1#if defined(FUZZEL_ENABLE_PNG)23#include "png-fuzzel.h"45#include <stdlib.h>6#include <stdio.h>7#include <stdbool.h>8#include <string.h>9#include <assert.h>1011#include <png.h>12#include <pixman.h>1314#define LOG_MODULE "png"15#define LOG_ENABLE_DBG 016#include "log.h"17#include "stride.h"1819pixman_image_t *20png_load(const char *path)21{22 pixman_image_t *pix = NULL;2324 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;2930 /* 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 }3536 /* 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 }4243 /* 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 }5152 if (setjmp(png_jmpbuf(png_ptr))) {53 LOG_ERR("%s: libpng error", path);54 goto err;55 }5657 png_init_io(png_ptr, fp);58 png_set_sig_bytes(png_ptr, 8);5960 /* 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);6768 LOG_DBG("%s: %dx%d@%hhubpp, %d channels", path, width, height, bit_depth, channels);6970 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);7475 /* pixman expects pre-multiplied alpha */76 png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, 1.0);7778 /* 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" : "");8687 if (bit_depth < 8)88 png_set_expand_gray_1_2_4_to_8(png_ptr);8990 png_set_gray_to_rgb(png_ptr);91 format = color_type == PNG_COLOR_TYPE_GRAY ? PIXMAN_r8g8b8 : PIXMAN_a8r8g8b8;92 break;9394 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" : "");9798 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;105106 case PNG_COLOR_TYPE_RGB:107 LOG_DBG("RGB");108 format = PIXMAN_r8g8b8;109 break;110111 case PNG_COLOR_TYPE_RGBA:112 LOG_DBG("RGBA");113 format = PIXMAN_a8r8g8b8;114 break;115 }116117 png_read_update_info(png_ptr, info_ptr);118119 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);122123 LOG_DBG("stride=%d, row-bytes=%zu", stride, row_bytes);124 assert(stride >= row_bytes);125126 row_pointers = malloc(height * sizeof(png_bytep));127 for (int i = 0; i < height; i++)128 row_pointers[i] = &image_data[i * stride];129130 png_read_image(png_ptr, row_pointers);131132 pix = pixman_image_create_bits_no_clear(133 format, width, height, (uint32_t *)image_data, stride);134135err: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);143144 return pix;145}146147#endif /* FUZZEL_ENABLE_PNG */
modified:render.c+56 added-28 removed
233 unmodified lines
234 * there's "enough" free space, render a large235 * representation of the icon */236237 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 lines
267 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 }273274 /* 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 lines
283 cairo_fill(buf->cairo);284 }285286 double cur_x = border_size + x_margin;287 struct icon *icon = &match->application->icon;288287 switch (match->application->icon.type) {289 switch (icon->type) {290 case ICON_NONE:291 break;292291 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);296297 pixman_image_t *png = icon->png.pix;298 int height = pixman_image_get_height(png);299 int width = pixman_image_get_width(png);300301 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;303304 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);308309 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);313314 int param_count = 0;315 pixman_kernel_t kernel = PIXMAN_KERNEL_LANCZOS3;316 pixman_fixed_t *params = pixman_filter_create_separable_convolution(317 &param_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));324325 if (params != NULL || param_count == 0) {326 pixman_image_set_filter(327 png, PIXMAN_FILTER_SEPARABLE_CONVOLUTION,328 params, param_count);329 }330331 free(params);332 icon->png.has_scale_transform = true;333 }334335 assert(icon->png.has_scale_transform);336 width *= scale;337 height *= scale;338 }339306 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);345309 /* 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);314315 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 }350351 case ICON_SVG: {322#ifdef FUZZEL_ENABLE_SVG323 RsvgHandle *svg = match->application->icon.svg;324352#if defined(FUZZEL_ENABLE_SVG)353 RsvgDimensionData dim;326 rsvg_handle_get_dimensions(svg, &dim);354 rsvg_handle_get_dimensions(icon->svg, &dim);355356 double height = font->height;357 double scale = height / dim.height;358
6 unmodified lines
365 first_row + i * row_height + (row_height - height) / 2);366 cairo_scale(buf->cairo, scale, scale);367368 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 }375376 cur_x += row_height;
97 unmodified lines