I'm modifying a script, and I think I may have run afoul of item number 6 from the Notes & Hints file, which states the following.
It's NOT recommended to create objects in callback function on_paint(), since it will be called frequently.
But it's just a tiny little object that doesn't seem all that, um, objectionable. I'd like to know how bad this is and what to look for that might indicate that I need to find another way.
Basically, I was getting spacing issues with the original script and thought that the problem might be the use of gr.MeasureString in conjunction with gr.GdiDrawText inside the on_paint callback. So I replaced those calls with calls to this function instead. (Maybe using gr this way violates Notes & Hints #4, but I think I'm in the clear there.)
function calc_text(gr, str, font, use_exact=false) {
// Get text width and height with output like gr.MeasureString
let w = gr.CalcTextWidth(str, font, use_exact);
let h = gr.CalcTextHeight(str, font);
return {Width: w, Height: h};
};
I wanted to mimic the output of gr.MeasureString to avoid changing the way it is subsequently used. I figure I'm just switching one object for one that was already there, but I'm still learning this stuff.
So can I get away with that? At the moment, there aren't too many other SMPs present, but this will change. The script I'm modifying autoscrolls long artist-title strings, and that still seems to be happening smoothly. OK, maybe there are some slight jolts if I concentrate on watching it.