World_to_camera_view only works for default resolution (1920, 1080)

Hello,
I want to draw a bounding box using my blender addon. For that bounding box I use the blf module to draw the dimensions of the box onto the axes.

Somehow, this only works for the default resolution (1920, 1080). As soon as I change this resolution (which I need to do) the text is at the wrong position and distorted. (the left picture is the correct projection for default resolution, the others are the wrong projection and distorted):

I first calculate the world position of the text, then try to project it into camera space using world_to_camera_view(). This works for the viewport and for the default resolution but not as soon as the resolution is changed. Note that I use GPU Module for rendering.

I looked up how to use world_to_camera_view in several examples (i.e. this one) and I try to do (almost) the same:

scene = bpy.context.scene
cam = scene.camera

# point in 3d scene
vec_point_3d = mathutils.Vector((point_3d[0], point_3d[1], point_3d[2], 1))

# mapping the 3d point into the camera space
obj = bpy.data.objects['Origin'] # empty representing the origin of my bounding box
matrix_world = obj.matrix_world
co_2d = object_utils.world_to_camera_view(scene, cam, matrix_world @ vec_point_3d)
render_scale = scene.render.resolution_percentage / 100
render_size = (int(scene.render.resolution_x * render_scale),
               int(scene.render.resolution_y * render_scale))
x_pos = round(co_2d.x * render_size[0]) #/ render_size[0]
y_pos = round(co_2d.y * render_size[1]) #/ render_size[1]

# draw text
point_2d = [x_pos, y_pos]
blf.position(font_id, point_2d[0], point_2d[1], 0)
blf.draw(font_id, text)

I don’t understand why this happens because as the source code shows, the camera matrix is multiplied onto the text coordinates, so the projection should work?

Does anyone have an idea why this might happen? I’d be thankful for any help!

I now finally found a solution to this problem:
In the documentation of the blf font module they say we need to use the bgl module as well. I don’t know exactly what this means as bgl is not used in the code example of the documentation…

As many code examples (e.g. here) load matrices using bgl, I thought about doing the same in GPUModule (as bgl is deprecated).

So these two lines of code fixed it:

gpu.matrix.load_matrix(view_matrix)
gpu.matrix.load_projection_matrix(mathutils.Matrix.Identity(4))

The projection of vec_point_3d is no longer needed now:

co_2d = object_utils.world_to_camera_view(scene, cam, vec_point_3d)