Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Draw_text is ignoring position and font_size parameters
-
I am having problems with the canvas.draw_text() method. I have Pythonista 2.0 on an iPad and iPhone, and when I try to use the draw_text method it appears that the text is always drawn at (0, 0) in the current coordinate system. It also appears that the font_size parameter is ignored. This code on my implementation will draw the text with different fonts but with the same size and relative positions. What's up with that!
coding: utf-8
import canvas
canvas.set_size(1000, 1000)
canvas.translate(100, 100)
canvas.draw_line(0 , 0, 50, 0)
canvas.draw_line(0, 0, 0, 50)
canvas.draw_text('Hello, World', 200, 200, font_name = 'Chalkduster', font_size = 64.0)
canvas.translate(100, 100)
canvas.draw_line(0 , 0, 50, 0)
canvas.draw_line(0, 0, 0, 50)
canvas.draw_text('Hello, World', 500, -200, font_name = 'Helvetica', font_size = 128.0) -
Thanks! It looks as if this bug was introduced with the transition to 64 bit. It's easy for me to fix, but I can't think of a workaround, unfortunately.
-
@omz
I have a workaround for the position issue, but the font_size issue is less tractable.def my_draw_text(txt, x, y, **kwargs):
""" my_draw_text(txt, x, y, **kwargs):The canvas.draw_text() method in this incarnation completely ignores the x and y parameters and just uses (0, 0). This function will use canvas.translate() to temporarily shift the origin to the desired point, draw the text, and then restore the gstate(). The keyword arguments (kwargs) are font_name and font_size as in the canvas.draw_text() definition. """ canvas.save_gstate() canvas.translate(x, y) canvas.draw_text(txt, 0, 0, **kwargs) canvas.restore_gstate()
-
@captainunivac Good idea! To fix the font size, you could extend your function to use
canvas.scale
in addition to the translation:def my_draw_text(txt, x, y, font_name='Helvetica', font_size=16.0): canvas.save_gstate() canvas.translate(x, y) canvas.scale(font_size/16.0, font_size/16.0) canvas.draw_text(txt, 0, 0, font_name, 16.0) canvas.restore_gstate()
-
Thanks, @omz! Will do.