-
Notifications
You must be signed in to change notification settings - Fork 39
Description
TLDR:
After added the PlotLines function, there is another function I want to add which is the variant of AddText() function that support custom fonts and size. This function can make loading monochrome icons less tedious comparing with possibly the only current solution addImage() for drawlist.
Am i missing something existed in the zgui library? Or should I jump into adding this feature?
Story:
I was learning to build some custom components using zgui for building my mock mixer interface (The solo and mute buttons), particularly a toggle button with a custom monochrome icon on top:

Originally, after I did some researches on the imgui issues, seems like they have this function
IMGUI_API void AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL);
Which I can draw some texts with custom font with specific size using the drawlist, but after I have search through the zgui library, I don't seem to see an api corresponding to the function. Thus, I wish to add this feature.
The Reasons For The Suggestion:
As you can see, I did able to load my letters onto my custom button, but they are actually image files in disguises

Because of the missing feature, I have to use gimp to render a specific text in specific font, storing a collection of images in a folder loaded as a texture, but handling textures can be quite complicated at times especially having large quantity of them:
// Create a single texture:
const texture = gctx.createTexture(.{ .usage = .{ .texture_binding = true, .copy_dst = true }, .size = .{
.width = image.width,
.height = image.height,
.depth_or_array_layers = 1,
}, .format = zgpu.imageInfoToTextureFormat(
image.num_components,
image.bytes_per_component,
image.is_hdr,
), .mip_level_count = 1 });
gctx.queue.writeTexture(
.{ .texture = gctx.lookupResource(texture).? },
.{
.bytes_per_row = image.bytes_per_row,
.rows_per_image = image.height,
},
.{
.width = image.width,
.height = image.height,
},
u8,
image.data,
);
And load that texture into the addImage() function:
drawlist.addImage(
gctx.lookupResource(texture_list.items[button_style.tex_index]).?,
.{
.pmin = base_pos,
.pmax = base_size,
.uvmin = .{ 0, 0 },
.uvmax = .{ 1, 1 },
.col = zgui.colorConvertFloat4ToU32(button_style.tex_color),
},
);
It works, but it can be annoying at times if I have more icons in different size or font style because I have to render those fonts into individual images, and load them all as a texture. I can load them in a single spite sheet, but since the uv is accessed as float, it is quite tedious to calculate each partition of sprites and extra lookup table is required, not to mention doing a sprite sheet for all the required icons can be tedious in gimp if all I need is a simple monochrome icons.
By contrast, if we have the addText variant function, we can use the addFontFromMemory() function which is way simpler comparing to load a texture:
font_id = zgui.io.addFontFromMemory(
FONT_MAIN,
std.math.floor(16.0 * scale_factor),
);
The return type is Font, so we should able to override the font style using the addText variant on the fly:
// Theoretical Function for addText variant with minimizing impact to the current addText function.
// Although it is possible to upgrade the current addText function, the side effect could be disastrous
// which it requires an additional args type as a parameter, breaking the original addText function.
// This requires discussions if we should upgrade the existing function or add a new one.
drawList.addTextExtended(
"Our Text With {s}", // txt: []const u8
.{"FONTs"}, // args: anytype
.{ // addTextArgs
.font = font_id,
.size = 32,
}
);
drawList.addTextExtendedUnformatted(
"Our Text no format", // txt: []const u8
.{ // addTextArgs
.font = font_id,
.size = 32,
}
);
Enabling this feature brings another advantage that we can load icon fonts like those.