Important: Calling this may recreate a new texture and therefore change atlas->TexData FIXME-NEWFONTS: Expose other glyph padding settings for custom alteration (e.g. drop shadows). See #7962
| 4307 | // Important: Calling this may recreate a new texture and therefore change atlas->TexData |
| 4308 | // FIXME-NEWFONTS: Expose other glyph padding settings for custom alteration (e.g. drop shadows). See #7962 |
| 4309 | ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry) |
| 4310 | { |
| 4311 | IM_ASSERT(w > 0 && w <= 0xFFFF); |
| 4312 | IM_ASSERT(h > 0 && h <= 0xFFFF); |
| 4313 | |
| 4314 | ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder; |
| 4315 | const int pack_padding = atlas->TexGlyphPadding; |
| 4316 | builder->MaxRectSize.x = ImMax(builder->MaxRectSize.x, w); |
| 4317 | builder->MaxRectSize.y = ImMax(builder->MaxRectSize.y, h); |
| 4318 | |
| 4319 | // Pack |
| 4320 | ImTextureRect r = { 0, 0, (unsigned short)w, (unsigned short)h }; |
| 4321 | for (int attempts_remaining = 3; attempts_remaining >= 0; attempts_remaining--) |
| 4322 | { |
| 4323 | // Try packing |
| 4324 | stbrp_rect pack_r = {}; |
| 4325 | pack_r.w = w + pack_padding; |
| 4326 | pack_r.h = h + pack_padding; |
| 4327 | stbrp_pack_rects((stbrp_context*)(void*)&builder->PackContext, &pack_r, 1); |
| 4328 | r.x = (unsigned short)pack_r.x; |
| 4329 | r.y = (unsigned short)pack_r.y; |
| 4330 | if (pack_r.was_packed) |
| 4331 | break; |
| 4332 | |
| 4333 | // If we ran out of attempts, return fallback |
| 4334 | if (attempts_remaining == 0 || builder->LockDisableResize) |
| 4335 | { |
| 4336 | IMGUI_DEBUG_LOG_FONT("[font] Failed packing %dx%d rectangle. Returning fallback.\n", w, h); |
| 4337 | return ImFontAtlasRectId_Invalid; |
| 4338 | } |
| 4339 | |
| 4340 | // Resize or repack atlas! (this should be a rare event) |
| 4341 | ImFontAtlasTextureMakeSpace(atlas); |
| 4342 | } |
| 4343 | |
| 4344 | builder->MaxRectBounds.x = ImMax(builder->MaxRectBounds.x, r.x + r.w + pack_padding); |
| 4345 | builder->MaxRectBounds.y = ImMax(builder->MaxRectBounds.y, r.y + r.h + pack_padding); |
| 4346 | builder->RectsPackedCount++; |
| 4347 | builder->RectsPackedSurface += (w + pack_padding) * (h + pack_padding); |
| 4348 | |
| 4349 | builder->Rects.push_back(r); |
| 4350 | if (overwrite_entry != NULL) |
| 4351 | return ImFontAtlasPackReuseRectEntry(atlas, overwrite_entry); // Write into an existing entry instead of adding one (used during repack) |
| 4352 | else |
| 4353 | return ImFontAtlasPackAllocRectEntry(atlas, builder->Rects.Size - 1); |
| 4354 | } |
| 4355 | |
| 4356 | // Generally for non-user facing functions: assert on invalid ID. |
| 4357 | ImTextureRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id) |
no test coverage detected