Page 1 of 1

automate text input from .txt and place in inkscape?

Posted: Sat Sep 30, 2017 4:17 am
by paulsomlo
I'm making buttons (name badges) for an event. In Inkscape, each button will consist of graphics inside of a circle. Each page will contain 12 buttons in three columns of four. I also have the names in a .txt file, one name per line. Is it possible to automate the process? I would like an extension/script that can fetch the names from the .txt file, change the font and size, then place them in the individual circles, having the "center" of the name coincide with the center of it's corresponding circle.

The alternative, is that I have to type 140 or so names into Inkscape and manually place them, which is painful.

Paul

Re: automate text input from .txt and place in inkscape?

Posted: Sat Sep 30, 2017 7:30 am
by Moini
If you use a CSV file instead, you can use the Generator extension:
http://wiki.inkscape.org/wiki/index.php ... #Generator
For usage instructions, see linked example files.

Re: automate text input from .txt and place in inkscape?

Posted: Sat Sep 30, 2017 7:30 am
by Moini
Oh, sorry, overlooked that it needs to change the font size - that's currently not possible.
But both centering and adapting the size should be possible via script...

Re: automate text input from .txt and place in inkscape?

Posted: Sat Sep 30, 2017 8:34 am
by tylerdurden
If there are only 140 names, copy /paste into a template is not so bad, and would probably be faster than making a script.

Re: automate text input from .txt and place in inkscape?

Posted: Sat Sep 30, 2017 12:04 pm
by Moini
It's not a totally unusual request, I've seen this asked a couple of times. It's probably not even too difficult to do...
I'd make the script create the text in the standard font size first, then I'd ask Inkscape how wide the text is, and then, if too wide, I'd scale it down. Then center on object with specified ID.
Another approach could make the texts all the same width, but that runs the risk of getting a too tall text, when it's short, so that's not really better.

Re: automate text input from .txt and place in inkscape?

Posted: Sun Oct 01, 2017 10:09 pm
by brynn
It would make for a nice extension, if an Inkscape extension could do that.

Re: automate text input from .txt and place in inkscape?

Posted: Mon Oct 02, 2017 4:18 am
by paulsomlo
Well, it sort of worked. While it inherited the font and size from %VAR_name%, which is very nice, the positioning was not consistent. I was able to get the positioning correct for one name, but not for other names. Vertically, all the names were in the correct place, but horizontally the names shifted, seeming to correspond to the size of the name, but not the number of letters. Also, the output consists of one file for each name. Ideally, I would like to start with 12 graphics on a page and be able to place a different name in each graphic.

Re: automate text input from .txt and place in inkscape?

Posted: Mon Oct 02, 2017 4:22 am
by paulsomlo
I compared some output files. It appears that the first letter in each name begins in the same place, consistently.
How does the .svg file know how to position the text?

Re: automate text input from .txt and place in inkscape?

Posted: Mon Oct 02, 2017 6:58 am
by tylerdurden
paulsomlo wrote:I compared some output files. It appears that the first letter in each name begins in the same place, consistently.
How does the .svg file know how to position the text?

Usually by the justification buttons in the text controls... for left/center/right/jusified. Positions a "text anchor" that is the snap-point for the text object.

Re: automate text input from .txt and place in inkscape?

Posted: Tue Oct 03, 2017 6:53 am
by paulsomlo
tylerdurden wrote:
paulsomlo wrote:I compared some output files. It appears that the first letter in each name begins in the same place, consistently.
How does the .svg file know how to position the text?

Usually by the justification buttons in the text controls... for left/center/right/jusified. Positions a "text anchor" that is the snap-point for the text object.

Doesn't work as expected.

Re: automate text input from .txt and place in inkscape?

Posted: Tue Oct 03, 2017 10:52 am
by paulsomlo
Moini wrote:It's not a totally unusual request, I've seen this asked a couple of times. It's probably not even too difficult to do...
I'd make the script create the text in the standard font size first, then I'd ask Inkscape how wide the text is, and then, if too wide, I'd scale it down. Then center on object with specified ID.
Another approach could make the texts all the same width, but that runs the risk of getting a too tall text, when it's short, so that's not really better.

I've looked at the XML file and the coordinates given for text remain a mystery to me. They apparently don't correspond to the center and I don't know if they correspond to an edge, a corner, one of the "handles", or what. I finally found a way to center the text - I drew two vertical lines, placed each one at the periphery of the circle, diametrically opposed. I then centered %VAR_name% between them using the "distribution" function, rather than "align". Then using "stroke", I made the two vertical lines invisible. Now, my text is centered in the circle, but I'd like to be able to fill the circle equally for different size names. If I set the font size of %VAR_name% large, "Bob" works, but not so good for "Caroline". So, getting back to your suggestion Moini, how do I extract the width of the text? I see it displayed in inkscape, but not in the corresponding XML file. Or maybe I don't need to extract the text width, I just need to know how wide a space I have to fill. Then, I need to figure out how to scale the text to fit.

Re: automate text input from .txt and place in inkscape?

Posted: Wed Oct 04, 2017 1:14 am
by Moini
The centering can be done via setting text justification in the template file, as TylerDurden wrote (and of course, one must take care that the text center for the %blah% thing is in the correct position, as you found out).

The automatical adjustment of the width of the text or the font size would need to be done by editing the extensions program code. Inkscape can return an object's width via the command line, and it can also be set there.

If you want to work on the extension, see https://inkscape.org/en/develop/extensions/ and https://inkscape.org/doc/inkscape-man.html.
You can remove the helper lines, they don't need to be made invisible - unless you want to reuse them for all 144 names, and do the centering manually.

Re: automate text input from .txt and place in inkscape?

Posted: Wed Oct 04, 2017 11:25 am
by paulsomlo
I got the centering to work, for the most part - longer names need a slight horizontal adjustment. I was able to figure out how to query the text width from the command line, but am not able to figure out how to change it from the command line. I can't find an inkscape verb that seems to do this directly. I also noticed that the XML file does not give font width, it gives font size in pixels, which corresponds to the height of the font as given in the svg file.

Re: automate text input from .txt and place in inkscape?

Posted: Thu Oct 05, 2017 2:04 am
by Moini
Yes, sorry for causing confusion. There's no direct verb for this. You'd need to use the python-based XML-editing extensions part. Take a look at other extensions that do scaling. They use functions from simpletransform.py. It will take a combination of both the bash and the python approach, due to the inconvenience of verbs not taking any parameters.

Re: automate text input from .txt and place in inkscape?

Posted: Thu Oct 05, 2017 2:15 am
by Moini
You'd have to set a scale transform for the text, I think.

Re: automate text input from .txt and place in inkscape?

Posted: Fri Oct 06, 2017 4:18 pm
by paulsomlo
Well, I finished up this evening. I never did figure out how to scale the text; I chose a font size that would make the shortest names large enough to read, then went through the files with long names and manually reduced their size. I put 12 graphics on each page and did so manually. I may find a way to automate that in the future, but it only took about an hour and a half to place 200 something graphics into 20 pages of 12 each.

I did learn quite a bit about Inkscape, and some of my shell programming is coming back to me. Thank you all for your help.

Re: automate text input from .txt and place in inkscape?

Posted: Sat Oct 07, 2017 6:37 am
by Moini
Will you share the modified code?