Blender V5.3
wm_window_icon.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <algorithm>
13#include <cstring>
14#include <string>
15
16#include "GHOST_Types.hh"
17
18#include "nanosvgrast.h"
19
20#include "wm_window_icon.hh"
21
22extern "C" const char datatoc_blender_app_icon_svg[];
23
24static void wm_ghost_icon_generate(const GHOST_IconGenerator * /*icon_generator*/,
25 GHOST_IWindow * /*window*/,
26 uint8_t *pixels,
27 int icon_size)
28{
29 const size_t buffer_size = size_t(icon_size) * icon_size * 4;
30 const int stride = icon_size * 4;
31
32 /* Rasterize the Blender logo SVG into the icon buffer.
33 * `nsvgParse` modifies the source string, so make a copy. */
34 std::string svg_source = datatoc_blender_app_icon_svg;
35 NSVGimage *image = nsvgParse(svg_source.data(), "px", 96.0f);
36 /* The bundled SVG is known to be valid. */
37 if (image == nullptr || image->width == 0 || image->height == 0) [[unlikely]] {
38 if (image) {
39 nsvgDelete(image);
40 }
41 memset(pixels, 0, buffer_size);
42 return;
43 }
44
45 NSVGrasterizer *rast = nsvgCreateRasterizer();
46 /* Rasterizer allocation should not fail for a small buffer. */
47 if (rast == nullptr) [[unlikely]] {
48 nsvgDelete(image);
49 memset(pixels, 0, buffer_size);
50 return;
51 }
52
53 /* Scale to fit the icon, centering the shorter axis. */
54 const float scale = float(icon_size) / std::max(image->width, image->height);
55 const float offset_x = (icon_size - image->width * scale) * 0.5f;
56 const float offset_y = (icon_size - image->height * scale) * 0.5f;
57
58 /* Clear the buffer first since the SVG may not fill the entire square. */
59 memset(pixels, 0, buffer_size);
60
61 nsvgRasterize(rast, image, offset_x, offset_y, scale, pixels, icon_size, icon_size, stride);
62
63 nsvgDeleteRasterizer(rast);
64 nsvgDelete(image);
65}
66
68 /*generate_fn*/ wm_ghost_icon_generate,
69 /*user_data*/ nullptr,
70};
#define image(slot, qualifiers, format)
float3x3 scale(float3x3 mat, float2 scale)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const GHOST_IconGenerator wm_ghost_icon_generator
const char datatoc_blender_app_icon_svg[]
static void wm_ghost_icon_generate(const GHOST_IconGenerator *, GHOST_IWindow *, uint8_t *pixels, int icon_size)