OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::IconManager Class Reference

Keep texture with icons for UI Manage texture live -> create and destruct texture by live of icon shared pointers. More...

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/IconManager.hpp>

Collaboration diagram for Slic3r::GUI::IconManager:

Classes

struct  InitType
struct  Icon
 Data for render texture with icon. More...

Public Types

enum class  RasterType : int { color = 1 << 1 , white_only_data = 1 << 2 , gray_only_data = 1 << 3 , color_wite_gray = color | white_only_data | gray_only_data }
 Define way to convert svg data to raster. More...
using InitTypes = std::vector<InitType>
using Icons = std::vector<std::shared_ptr<Icon> >
using VIcons = std::vector<Icons>

Public Member Functions

 ~IconManager ()
 Release texture Set shared pointers to invalid texture.
Icons init (const InitTypes &input)
 Initialize raster texture on GPU with given images NOTE: Have to be called after OpenGL initialization.
VIcons init (const std::vector< std::string > &file_paths, const ImVec2 &size, RasterType type=RasterType::color)
 Initialize multiple icons with same settings for size and type NOTE: Have to be called after OpenGL initialization.
void release ()
 Release icons which are hold only by this manager May change texture and position of icons.

Private Attributes

GLTexture m_icons_texture
unsigned int m_id { 0 }
Icons m_icons

Detailed Description

Keep texture with icons for UI Manage texture live -> create and destruct texture by live of icon shared pointers.


Class Documentation

◆ Slic3r::GUI::IconManager::InitType

struct Slic3r::GUI::IconManager::InitType
Collaboration diagram for Slic3r::GUI::IconManager::InitType:
Class Members
string filepath
ImVec2 size
RasterType type = RasterType::color

Member Typedef Documentation

◆ Icons

using Slic3r::GUI::IconManager::Icons = std::vector<std::shared_ptr<Icon> >

◆ InitTypes

◆ VIcons

Member Enumeration Documentation

◆ RasterType

enum class Slic3r::GUI::IconManager::RasterType : int
strong

Define way to convert svg data to raster.

Enumerator
color 
white_only_data 
gray_only_data 
color_wite_gray 
29 : int{
30 color = 1 << 1,
31 white_only_data = 1 << 2,
32 gray_only_data = 1 << 3,
33 color_wite_gray = color | white_only_data | gray_only_data
34 // TODO: add type with backgrounds
35 };

Constructor & Destructor Documentation

◆ ~IconManager()

IconManager::~IconManager ( )

Release texture Set shared pointers to invalid texture.

39 {
41 // release opengl texture is made in ~GLTexture()
42
43 if (m_id != 0)
45}
#define glsafe(cmd)
Definition OpenGLUtils.hpp:25
unsigned int m_id
Definition IconManager.hpp:101
Icons m_icons
Definition IconManager.hpp:102
#define glDeleteTextures
Definition gl.h:3587
static void clear(IconManager::Icons &icons)
Definition IconManager.cpp:305

References priv::clear(), glDeleteTextures, glsafe, m_icons, and m_id.

Here is the call graph for this function:

Member Function Documentation

◆ init() [1/2]

IconManager::Icons IconManager::init ( const InitTypes & input)

Initialize raster texture on GPU with given images NOTE: Have to be called after OpenGL initialization.

Parameters
inputDefine files and its size with rasterization
Returns
Rasterized icons stored on GPU, Same size and order as input, each item of vector is set of texture in order by RasterType
82{
83 assert(!input.empty());
84 if (input.empty())
85 return {};
86
87 // TODO: remove in future
88 if (m_id != 0) {
90 m_id = 0;
91 }
92
93 int total_surface = 0;
94 for (const InitType &i : input)
95 total_surface += i.size.x * i.size.y;
96 const int surface_sqrt = (int)sqrt((float)total_surface) + 1;
97
98 // Start packing
99 // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
100 const int TEX_HEIGHT_MAX = 1024 * 32;
101 int width = (surface_sqrt >= 4096 * 0.7f) ? 4096 : (surface_sqrt >= 2048 * 0.7f) ? 2048 : (surface_sqrt >= 1024 * 0.7f) ? 1024 : 512;
102
103 int num_nodes = width;
104 std::vector<stbrp_node> nodes(num_nodes);
105 stbrp_context context;
106 stbrp_init_target(&context, width, TEX_HEIGHT_MAX, nodes.data(), num_nodes);
107
108 ImVector<stbrp_rect> pack_rects;
109 pack_rects.resize(input.size());
110 memset(pack_rects.Data, 0, (size_t) pack_rects.size_in_bytes());
111 for (size_t i = 0; i < input.size(); i++) {
112 const ImVec2 &size = input[i].size;
113 assert(size.x > 1);
114 assert(size.y > 1);
115 pack_rects[i].w = size.x;
116 pack_rects[i].h = size.y;
117 }
118 int pack_rects_res = stbrp_pack_rects(&context, &pack_rects[0], pack_rects.Size);
119 assert(pack_rects_res == 1);
120 if (pack_rects_res != 1)
121 return {};
122
123 ImVec2 tex_size(width, width);
124 for (const stbrp_rect &rect : pack_rects) {
125 float x = rect.x + rect.w;
126 float y = rect.y + rect.h;
127 if(x > tex_size.x) tex_size.x = x;
128 if(y > tex_size.y) tex_size.y = y;
129 }
130
131 Icons result(input.size());
132 for (int i = 0; i < pack_rects.Size; i++) {
133 const stbrp_rect &rect = pack_rects[i];
134 assert(rect.was_packed);
135 if (!rect.was_packed)
136 return {};
137
138 ImVec2 tl(rect.x / tex_size.x, rect.y / tex_size.y);
139 ImVec2 br((rect.x + rect.w) / tex_size.x, (rect.y + rect.h) / tex_size.y);
140
141 assert(input[i].size.x == rect.w);
142 assert(input[i].size.y == rect.h);
143 Icon icon = {input[i].size, tl, br};
144 result[i] = std::make_shared<Icon>(std::move(icon));
145 }
146
147 NSVGrasterizer *rast = nsvgCreateRasterizer();
148 assert(rast != nullptr);
149 if (rast == nullptr)
150 return {};
151 ScopeGuard sg_rast([rast]() { ::nsvgDeleteRasterizer(rast); });
152
153 int channels = 4;
154 int n_pixels = tex_size.x * tex_size.y;
155 // store data for whole texture
156 std::vector<unsigned char> data(n_pixels * channels, {0});
157
158 // initialize original index locations
159 std::vector<size_t> idx(input.size());
160 std::iota(idx.begin(), idx.end(), 0);
161
162 // Group same filename by sort inputs
163 // sort indexes based on comparing values in input
164 std::sort(idx.begin(), idx.end(), [&input](size_t i1, size_t i2) { return input[i1].filepath < input[i2].filepath; });
165 for (size_t j: idx) {
166 const InitType &i = input[j];
167 if (i.filepath.empty())
168 continue; // no file path only reservation of space for texture
169 assert(boost::filesystem::exists(i.filepath));
170 if (!boost::filesystem::exists(i.filepath))
171 continue;
172 assert(boost::algorithm::iends_with(i.filepath, ".svg"));
173 if (!boost::algorithm::iends_with(i.filepath, ".svg"))
174 continue;
175
176 NSVGimage *image = parse_file(i.filepath.c_str());
177 assert(image != nullptr);
178 if (image == nullptr)
179 return {};
180
181 ScopeGuard sg_image([image]() { ::nsvgDelete(image); });
182
183 float svg_scale = i.size.y / image->height;
184 // scale should be same in both directions
185 assert(is_approx(svg_scale, i.size.y / image->width));
186
187 const stbrp_rect &rect = pack_rects[j];
188 int n_pixels = rect.w * rect.h;
189 std::vector<unsigned char> icon_data(n_pixels * channels, {0});
190 ::nsvgRasterize(rast, image, 0, 0, svg_scale, icon_data.data(), i.size.x, i.size.y, i.size.x * channels);
191
192 // makes white or gray only data in icon
193 if (i.type == RasterType::white_only_data ||
194 i.type == RasterType::gray_only_data) {
195 unsigned char value = (i.type == RasterType::white_only_data) ? 255 : 127;
196 for (size_t k = 0; k < icon_data.size(); k += channels)
197 if (icon_data[k] != 0 || icon_data[k + 1] != 0 || icon_data[k + 2] != 0) {
198 icon_data[k] = value;
199 icon_data[k + 1] = value;
200 icon_data[k + 2] = value;
201 }
202 }
203
204 int start_offset = (rect.y*tex_size.x + rect.x) * channels;
205 int data_stride = tex_size.x * channels;
206 subdata(data.data() + start_offset, data_stride, icon_data, rect.w * channels);
207 }
208
209 if (m_id != 0)
211
218 glsafe(::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) tex_size.x, (GLsizei) tex_size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*) data.data()));
219
220 // bind no texture
222
223 for (const auto &i : result)
224 i->tex_id = m_id;
225 return result;
226}
@ gray_only_data
Definition IconManager.hpp:32
@ white_only_data
Definition IconManager.hpp:31
std::vector< std::shared_ptr< Icon > > Icons
Definition IconManager.hpp:66
Definition IconManager.hpp:37
#define GL_TEXTURE_MIN_FILTER
Definition gl.h:1828
#define glPixelStorei
Definition gl.h:4451
#define GL_TEXTURE_MAX_LEVEL
Definition gl.h:1826
#define GL_LINEAR
Definition gl.h:930
#define glTexParameteri
Definition gl.h:4911
#define glTexImage2D
Definition gl.h:4895
#define GL_UNSIGNED_BYTE
Definition gl.h:1937
#define glGenTextures
Definition gl.h:3787
#define GL_UNPACK_ALIGNMENT
Definition gl.h:1924
int GLsizei
Definition gl.h:2065
#define glBindTexture
Definition gl.h:3299
#define GL_RGBA
Definition gl.h:1493
unsigned int GLuint
Definition gl.h:2063
#define GL_TEXTURE_MAG_FILTER
Definition gl.h:1822
#define GL_TEXTURE_2D
Definition gl.h:1739
coord_t width(const BoundingBox &box)
Definition Arrange.cpp:1184
constexpr bool is_approx(Number value, Number test_value, Number precision=EPSILON)
Definition libslic3r.h:320
void subdata(unsigned char *data, size_t data_stride, const std::vector< unsigned char > &data2, size_t data2_row)
Definition IconManager.cpp:72
NSVGimage * parse_file(const char *filepath)
Definition IconManager.cpp:48
Data for render texture with icon.
Definition IconManager.hpp:53

References Slic3r::GUI::IconManager::InitType::filepath, GL_LINEAR, GL_RGBA, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_TEXTURE_MAX_LEVEL, GL_TEXTURE_MIN_FILTER, GL_UNPACK_ALIGNMENT, GL_UNSIGNED_BYTE, glBindTexture, glDeleteTextures, glGenTextures, glPixelStorei, glsafe, glTexImage2D, glTexParameteri, gray_only_data, Slic3r::is_approx(), m_id, Slic3r::GUI::IconManager::InitType::size, Slic3r::GUI::IconManager::InitType::type, and white_only_data.

Referenced by anonymous_namespace{GLGizmoSVG.cpp}::init_icons().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init() [2/2]

std::vector< IconManager::Icons > IconManager::init ( const std::vector< std::string > & file_paths,
const ImVec2 & size,
RasterType type = RasterType::color )

Initialize multiple icons with same settings for size and type NOTE: Have to be called after OpenGL initialization.

Parameters
file_pathsDefine files with icon
sizeSize of stored texture[in px], float will be rounded
typeDefine way to rasterize icon, together color, white and gray = RasterType::color | RasterType::white_only_data | RasterType::gray_only_data
Returns
Rasterized icons stored on GPU, Same size and order as file_paths, each item of vector is set of texture in order by RasterType
229{
230 assert(!file_paths.empty());
231 assert(size.x >= 1);
232 assert(size.x < 256*16);
233
234 // TODO: remove in future
235 if (!m_icons.empty()) {
236 // not first initialization
238 m_icons.clear();
239 m_icons_texture.reset();
240 }
241
242 // only rectangle are supported
243 assert(size.x == size.y);
244 // no subpixel supported
245 unsigned int width = static_cast<unsigned int>(std::abs(std::round(size.x)));
246 assert(size.x == static_cast<float>(width));
247
248 // state order has to match the enum IconState
249 const auto& states = priv::get_states(type);
250
251 bool compress = false;
252 bool is_loaded = m_icons_texture.load_from_svg_files_as_sprites_array(file_paths, states, width, compress);
253 if (!is_loaded || (size_t) m_icons_texture.get_width() < (states.size() * width) ||
254 (size_t) m_icons_texture.get_height() < (file_paths.size() * width)) {
255 // bad load of icons, but all usage of m_icons_texture check that texture is initialized
256 assert(false);
257 m_icons_texture.reset();
258 return {};
259 }
260
261 unsigned count_files = file_paths.size();
262 // count icons per file
263 unsigned count = states.size();
264 // create result
265 std::vector<Icons> result;
266 result.reserve(count_files);
267
268 Icon def_icon;
269 def_icon.tex_id = m_icons_texture.get_id();
270 def_icon.size = size;
271
272 // float beacouse of dividing
273 float tex_height = static_cast<float>(m_icons_texture.get_height());
274 float tex_width = static_cast<float>(m_icons_texture.get_width());
275
276 //for (const auto &f: file_paths) {
277 for (unsigned f = 0; f < count_files; ++f) {
278 // NOTE: there are space between icons
279 unsigned start_y = static_cast<unsigned>(f) * (width + 1) + 1;
280 float y1 = start_y / tex_height;
281 float y2 = (start_y + width) / tex_height;
282 Icons file_icons;
283 file_icons.reserve(count);
284 //for (const auto &s : states) {
285 for (unsigned j = 0; j < count; ++j) {
286 auto icon = std::make_shared<Icon>(def_icon);
287 // NOTE: there are space between icons
288 unsigned start_x = static_cast<unsigned>(j) * (width + 1) + 1;
289 float x1 = start_x / tex_width;
290 float x2 = (start_x + width) / tex_width;
291 icon->tl = ImVec2(x1, y1);
292 icon->br = ImVec2(x2, y2);
293 file_icons.push_back(icon);
294 m_icons.push_back(std::move(icon));
295 }
296 result.emplace_back(std::move(file_icons));
297 }
298 return result;
299}
static unsigned int count
Definition AuxiliaryDataViewModel.cpp:246
GLTexture m_icons_texture
Definition IconManager.hpp:99
static double f(double x, double z_sin, double z_cos, bool vertical, bool flip)
Definition FillGyroid.cpp:124
static const std::vector< std::pair< int, bool > > & get_states(IconManager::RasterType type)
Definition IconManager.cpp:328
unsigned int tex_id
Definition IconManager.hpp:62

References priv::clear(), count, Slic3r::f(), priv::get_states(), m_icons, m_icons_texture, Slic3r::GUI::IconManager::Icon::size, and Slic3r::GUI::IconManager::Icon::tex_id.

Here is the call graph for this function:

◆ release()

void IconManager::release ( )

Release icons which are hold only by this manager May change texture and position of icons.

301 {
302 BOOST_LOG_TRIVIAL(error) << "Not implemented yet";
303}

Referenced by anonymous_namespace{GLGizmoSVG.cpp}::init_icons().

Here is the caller graph for this function:

Member Data Documentation

◆ m_icons

Icons Slic3r::GUI::IconManager::m_icons
private

Referenced by ~IconManager(), and init().

◆ m_icons_texture

GLTexture Slic3r::GUI::IconManager::m_icons_texture
private

Referenced by init().

◆ m_id

unsigned int Slic3r::GUI::IconManager::m_id { 0 }
private
101{ 0 };

Referenced by ~IconManager(), and init().


The documentation for this class was generated from the following files: