Skip to content
Myr

icon: use an enum to distinguish between PNGs and SVGs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Daniel Eklöf · committed by Daniel Eklöf · parent e8562b4 · Browse files · 4 files+69 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+69 added−38 removed

  • other:+69 added−38 removed

4 files+69 added−38 removed

modified:application.h+7 added-3 removed
1 unmodified line
23#include <cairo.h>4#include <librsvg/rsvg.h>56enum icon_type { ICON_NONE, ICON_SURFACE, ICON_SVG };78struct icon {9 enum icon_type type;10 union {7 cairo_surface_t *surface;8 RsvgHandle *svg;11 cairo_surface_t *surface;12 RsvgHandle *svg;13 };14};1516struct application {17 char *path;18 char *exec;19 char *title;20 char *comment;16 //cairo_surface_t *icon;21 struct icon icon;22};
modified:main.c+2 added-2 removed
1380 unmodified lines
1381 free(it->item.path);1382 free(it->item.exec);1383 free(it->item.title);1384 free(it->item.comment);1385 if (it->item.icon.surface != NULL)1385 if (it->item.icon.type == ICON_SURFACE)1386 cairo_surface_destroy(it->item.icon.surface);1387 if (it->item.icon.svg != NULL)1387 else if (it->item.icon.type == ICON_SVG)1388 g_object_unref(it->item.icon.svg);1389 tll_remove(c.applications, it);1390 }1391
48 unmodified lines
modified:render.c+35 added-26 removed
247 unmodified lines
248 }249250 double cur_x = border_size + x_margin;251252 if (match->application->icon.surface != NULL ||253 match->application->icon.svg != NULL)254 {252 switch (match->application->icon.type) {253 case ICON_NONE:254 break;255256 case ICON_SURFACE: {257 cairo_surface_t *surf = match->application->icon.surface;256 RsvgHandle *svg = match->application->icon.svg;258 double width = cairo_image_surface_get_width(surf);259 double height = cairo_image_surface_get_height(surf);260 double scale = 1.0;261258 /* Get SVG size */259 RsvgDimensionData dim = {0};260 if (svg != NULL)261 rsvg_handle_get_dimensions(svg, &dim);262263 /* For SVGs, *we* set the final size and then scale264 * it. For PNGs, we use the icon's size, and only scale it265 * if necessary */266 double width = svg != NULL267 ? row_height - 4 : cairo_image_surface_get_width(surf);268 double height = svg != NULL269 ? row_height - 4 : cairo_image_surface_get_height(surf);270 double scale = svg != NULL ? height / dim.height : 1.0;271272 /* Do we need to scale the PNG? */273 if (svg == NULL && height > row_height) {262 if (height > row_height) {263 scale = (row_height - 2 * y_margin) / height;264 LOG_DBG("%s: scaling: %f (row-height: %f, size=%fx%f)",265 match->application->title, scale, row_height, width, height);266
9 unmodified lines
276 buf->cairo, cur_x,277 first_row + i * row_height + (row_height - height) / 2);278 cairo_scale(buf->cairo, scale, scale);279291 if (svg != NULL)292 rsvg_handle_render_cairo(svg, buf->cairo);293 else {294 cairo_set_source_surface(buf->cairo, surf, 0, 0);295 cairo_paint(buf->cairo);296 }280 cairo_set_source_surface(buf->cairo, surf, 0, 0);281 cairo_paint(buf->cairo);282 cairo_restore(buf->cairo);283 break;284 }285286 case ICON_SVG: {287 RsvgHandle *svg = match->application->icon.svg;288289 RsvgDimensionData dim;290 rsvg_handle_get_dimensions(svg, &dim);291292 double height = row_height - 4;293 double scale = height / dim.height;294295 cairo_save(buf->cairo);296 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);297298 /* Translate + scale. Note: order matters! */299 cairo_translate(300 buf->cairo, cur_x,301 first_row + i * row_height + (row_height - height) / 2);302 cairo_scale(buf->cairo, scale, scale);303304 rsvg_handle_render_cairo(svg, buf->cairo);305 cairo_restore(buf->cairo);306 break;307 }308 }309310 cur_x += row_height;311312 /* Application title */
28 unmodified lines
modified:xdg.c+25 added-7 removed
18 unmodified lines
19#include "log.h"20#include "icon.h"2122static struct icon23icon_null(void)24{25 return (struct icon){.type = ICON_NONE};26}2728static struct icon29icon_from_surface(cairo_surface_t *surf)30{31 return (struct icon){.type = ICON_SURFACE, .surface = surf};32}3334static struct icon35icon_from_svg(RsvgHandle *svg)36{37 return (struct icon){.type = ICON_SVG, .svg = svg};38}3940static struct icon41load_icon(const char *name, int icon_size, icon_theme_list_t themes)42{43 if (name == NULL)26 return (struct icon){NULL, NULL};44 return icon_null();4546 if (name[0] == '/') {47 cairo_surface_t *surf = cairo_image_surface_create_from_png(name);48 if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS)31 return (struct icon){surf, NULL};49 return icon_from_surface(surf);5033 return (struct icon){NULL, NULL};51 return icon_null();52 }5354 tll_foreach(themes, theme_it) {55 const struct icon_theme *theme = &theme_it->item;
46 unmodified lines
102 else103 LOG_DBG("%s: %s: nothing else matched", name, full_path);104105 free(full_path);88 return (struct icon){surf, NULL};106 return icon_from_surface(surf);107 }108109 free(full_path);110 cairo_surface_destroy(surf);
11 unmodified lines
122 sprintf(path, "%s/pixmaps/%s.svg", it->item, name);123 RsvgHandle *svg = rsvg_handle_new_from_file(path, NULL);124 if (svg != NULL) {125 xdg_data_dirs_destroy(dirs);108 return (struct icon){NULL, svg};126 return icon_from_svg(svg);127 }128129 /* No SVG, look for PNG instead */130 sprintf(path, "%s/pixmaps/%s.png", it->item, name);131 cairo_surface_t *surf = cairo_image_surface_create_from_png(path);132 if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {133 xdg_data_dirs_destroy(dirs);116 return (struct icon){surf, NULL};134 return icon_from_surface(surf);135 }136 cairo_surface_destroy(surf);137 }138 xdg_data_dirs_destroy(dirs);139122 return (struct icon){NULL, NULL};140 return icon_null();141}142143static void144parse_desktop_file(int fd, icon_theme_list_t themes, int icon_size,
237 unmodified lines