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
Provenance
- Commit
331b12f- Tree
8fd852e- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
e8562b4
4 files changed+69 added−38 removed
modified:application.h+7 added-3 removed
1 unmodified line2 3 #include <cairo.h>4 #include <librsvg/rsvg.h>5 6+enum icon_type { ICON_NONE, ICON_SURFACE, ICON_SVG };7+8 struct 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 };15 16 struct 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 lines1381 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 linesmodified:render.c+35 added-26 removed
247 unmodified lines248 }249 250 double cur_x = border_size + x_margin;251 252- 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;255+256+ 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;261 258- /* Get SVG size */259- RsvgDimensionData dim = {0};260- if (svg != NULL)261- rsvg_handle_get_dimensions(svg, &dim);262-263- /* 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;271-272- /* 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 lines276 buf->cairo, cur_x,277 first_row + i * row_height + (row_height - height) / 2);278 cairo_scale(buf->cairo, scale, scale);279 291- 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+ }285 286+ case ICON_SVG: {287+ RsvgHandle *svg = match->application->icon.svg;288+289+ RsvgDimensionData dim;290+ rsvg_handle_get_dimensions(svg, &dim);291+292+ double height = row_height - 4;293+ double scale = height / dim.height;294+295+ cairo_save(buf->cairo);296+ cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);297+298+ /* 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);303+304+ rsvg_handle_render_cairo(svg, buf->cairo);305 cairo_restore(buf->cairo);306+ break;307 }308+ }309 310 cur_x += row_height;311 312 /* Application title */28 unmodified linesmodified:xdg.c+25 added-7 removed
18 unmodified lines19 #include "log.h"20 #include "icon.h"21 22 static struct icon23+icon_null(void)24+{25+ return (struct icon){.type = ICON_NONE};26+}27+28+static struct icon29+icon_from_surface(cairo_surface_t *surf)30+{31+ return (struct icon){.type = ICON_SURFACE, .surface = surf};32+}33+34+static struct icon35+icon_from_svg(RsvgHandle *svg)36+{37+ return (struct icon){.type = ICON_SVG, .svg = svg};38+}39+40+static struct icon41 load_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();45 46 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);50 33- return (struct icon){NULL, NULL};51+ return icon_null();52 }53 54 tll_foreach(themes, theme_it) {55 const struct icon_theme *theme = &theme_it->item;46 unmodified lines102 else103 LOG_DBG("%s: %s: nothing else matched", name, full_path);104 105 free(full_path);88- return (struct icon){surf, NULL};106+ return icon_from_surface(surf);107 }108 109 free(full_path);110 cairo_surface_destroy(surf);11 unmodified lines122 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 }128 129 /* 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);139 122- return (struct icon){NULL, NULL};140+ return icon_null();141 }142 143 static void144 parse_desktop_file(int fd, icon_theme_list_t themes, int icon_size,237 unmodified lines