Skip to content

Conversation

Logickin-Lambda
Copy link
Contributor

As proposed in my issues of missing add text functions variant, I have enabled the function with the name addTextExtendedUnformatted and addTextExtended to handle text drawing with custom font and sizes:

image

As you can see, this update could load and render different fonts, along with changing the clipped region and wrap width defined in an additional struct AddTextArgs.

// Loading the embed font files, and put them into a map for ease of access
try font_map.put("default", zgui.io.addFontFromMemory(FONT_MAIN, std.math.floor(16.0 * scale_factor)));
try font_map.put("wolf", zgui.io.addFontFromMemory(FONT_WOLF, std.math.floor(64.0 * scale_factor)));
try font_map.put("square", zgui.io.addFontFromMemory(FONT_FRSQ, std.math.floor(64.0 * scale_factor)));
try font_map.put("tfat", zgui.io.addFontFromMemory(FONT_TFAT, std.math.floor(64.0 * scale_factor)));

// in the main event loop...
draw_list.addText(pos, 0xffffffff, "This is written by a {s}", .{"human"});
draw_list.addTextExtended(.{ pos[0], pos[1] + 40 }, 0xffffffff, "This is written by a {s}, but Larger", .{"HUMAN"}, .{ .font = null, .font_size = 72 });
draw_list.addTextExtended(.{ pos[0], pos[1] + 120 }, 0x880000ff, "This is written by a {s}", .{"wolf"}, .{ .font = font_map.get("wolf").?, .font_size = 24 });
draw_list.addTextExtended(.{ pos[0], pos[1] + 180 }, 0xffffffff, "This is written by a {s}", .{"...square?"}, .{ .font = font_map.get("square").?, .font_size = 48, .cpu_fine_clip_rect = &.{.{ 50, 0, 1000, 250 }} });
draw_list.addTextExtendedUnformatted(.{ pos[0], pos[1] + 240 }, 0xffffffff, "Analog, Transform and Transmit", .{ .font = font_map.get("tfat").?, .font_size = 48, .wrap_width = window_width });

Let's see what would you think about this change.

Logickin-Lambda and others added 7 commits July 28, 2025 15:06
I am about to test these functions, so I am going
to commit those for a backup.
The PlotLines() and PlotHistogram() are now tested
and they can produce the exact same result from the
web imgui demo.
The code is done, and testing is in sight.
@Logickin-Lambda
Copy link
Contributor Author

Wait, is it normal that my commits in the previous pull request also appeared in this pull request?

@hazeycode
Copy link
Member

Wait, is it normal that my commits in the previous pull request also appeared in this pull request?

It's because I squash-merged your previous PR, so your branch has diverged from upstream main. You can simply rebase your branch onto the upstream main branch to get it synced up properly (and then force push), but it's not a big deal in this case.
Perhaps I should stop using squash-merge if it's causing people friction.

@hazeycode hazeycode requested a review from Copilot July 29, 2025 10:23
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds extended text drawing functionality to the zgui DrawList API, enabling custom font and sizing options. The changes introduce two new functions: addTextExtended (with formatting) and addTextExtendedUnformatted (without formatting) that support additional text rendering parameters.

  • Added AddTextArgs struct to configure font, font size, wrap width, and clipping rectangle
  • Implemented addTextExtended and addTextExtendedUnformatted functions in Zig
  • Added corresponding C++ binding function zguiDrawList_AddTextExtended

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/zgui.cpp Added C++ binding function for extended text drawing with additional parameters
src/gui.zig Added Zig wrapper functions and struct for extended text drawing functionality
Comments suppressed due to low confidence (1)

src/gui.zig:4554

  • [nitpick] The struct name 'AddTextArgs' should follow consistent naming conventions. Consider renaming to 'TextDrawArgs' or 'DrawTextArgs' to better align with the context of drawing operations.
    const AddTextArgs = struct {

src/zgui.cpp Outdated
float wrap_width,
const float cpu_fine_clip_rect[][4])
{
draw_list->AddText(font, font_size, {pos[0], pos[1]}, col, text_begin, text_end, wrap_width, (const ImVec4 *)&cpu_fine_clip_rect[0][0]);
Copy link
Preview

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential null pointer dereference. When 'cpu_fine_clip_rect' is null, accessing '&cpu_fine_clip_rect[0][0]' will cause undefined behavior. The cast should handle the null case properly.

Suggested change
draw_list->AddText(font, font_size, {pos[0], pos[1]}, col, text_begin, text_end, wrap_width, (const ImVec4 *)&cpu_fine_clip_rect[0][0]);
const ImVec4* clip_rect = (cpu_fine_clip_rect != nullptr) ? (const ImVec4 *)&cpu_fine_clip_rect[0][0] : nullptr;
draw_list->AddText(font, font_size, {pos[0], pos[1]}, col, text_begin, text_end, wrap_width, clip_rect);

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch bot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, will change the name, and try to apply that suggestion for clip_rect to see if that works.

Copy link
Member

@hazeycode hazeycode Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the naming is good as is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I thought the copilot suggests to rename the struct. If the name change isn't needed, then I am going to commit the suggested fix to the null pointer dereference only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go; the suggestion is applied.

As suggested by the code review, I have replaced
my direct array access with a null pointer check.
@Logickin-Lambda
Copy link
Contributor Author

All Good? After fixed the potential null pointer reference, anything else I need to do?

@hazeycode hazeycode merged commit ca4f1b8 into zig-gamedev:main Jul 31, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants