Hey all, I’ve been trying to figure out how to add a layer with a string of text to the canvas in my plugin. Basically I want to use the built-in text tool, but with my script.
I’ve searched all relevant terms I could think of on the Krita API page, but didn’t find anything that looked like it could add text. LazyTextTool was the only other plugin I found that does something similar to what I’m trying to do, though much more complicated, and I couldn’t make heads or tails of the implementation there.
I’d really appreciate it if someone could point me in the right direction!
@AhabGreybeard
Thanks for linking the thread. I heard about 5.3 getting a new text tool, but this really does look amazing! Can’t wait to use it!
And as for biding my time until the new tool is out, I believe figuring this out now would still be beneficial, even if I have to fix or rewrite the code later.
@AkiR
You’re a godsend, thank you so much. I’ve been trying to figure this out for the better part of a week now.
A small nitpick; font sizes don’t match up between text created with this function and text created with the text tool, and the function text also doesn’t scale with the canvas’ DPI. Weird thing is, when you copy the SVG Source, make a new text element with the text tool, and paste the source in there, it scales correctly.
My best guess here is that there are some properties in the SVG tag of the text inserted by the text tool that the function is missing. Unfortunately, I have no idea where in the source code to look to investigate this.
After some digging, I found this in the KoSvgTextShapeMarkupConverter.cpp file:
/**
* DIRTY-DIRTY-DIRTY HACK ALERT!!!
*
* All the internals of QTextDocument work with the primary screen's DPI.
* That is, when we ask Qt about font metrics, it returns values scaled
* to the pixels in the current screen, that is,
* ptValue * qt_defaultDpi() / 72.0. We need to convert this value back
* into points before writing as SVG. Therefore, all the metrics returned
* by the document are scaled with `fixQtDpi()`.
*
* Official Qt's way to workaround this problem is to set logicalDpiX/Y()
* values on the paint device's associated with the document. But it seems
* like in our version of Qt (5.12.12, which is rather old) it doesn't work
* properly.
*/
This is probably it? Plugging font-size * canvas-DPI / 72 into the font-size yields text that is consistent with the scale of the normal text tool. But it’s still weird to me that the text created by the text tool works with the “normal” values, while the text created by the function doesn’t.
I remember seeing something about font-size adjustment in the LazyTextTool code, so maybe that’ll give me a hint on what’s wrong.
But of course, if you have an idea, please tell me.
It was just a matter of setting the width, height and viewport of the <svg> tag correctly!
Thanks again for your help! I probably would have been stuck on this for ages if you didn’t push me in the right direction.
Oh and just a note if anyone wants to do something similar in the future, the text does remain editable with the standard text-tool (as of Krita version 5.2.2 at least).