Set transparent area of text using gradient fill

Post questions on how to use or achieve an effect in Inkscape.
thangalin
Posts: 6
Joined: Mon Jul 09, 2012 9:47 am

Set transparent area of text using gradient fill

Postby thangalin » Mon Jul 09, 2012 10:17 am

Using the GIMP, I can create text with a fill pattern (http://i.imgur.com/qMZzy.png) shown as the bottom text in the following image:

Image

How can you accomplish the same task on any arbitrary text using Inkscape?

Effectively, I'd like to create an XML file where I can perform the following steps:

1. Replace the string "Arbitrary Text" with, for example, "Ultimate Future Global Constitution".
2. Load Inkscape with the new file.
3. Export the cropped result to a PNG image at a specific resolution (e.g., 1024 x 400 @ 300 dpi).

The font is SF Fedora Shadow. http://www.dafont.com/sf-fedora.font

There are about 1,000 short phrases that I'd like to convert, and creating the images manually would take countless hours.

Thank you!

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Set transparent area of text using gradient fill

Postby brynn » Mon Jul 09, 2012 12:36 pm

Image
Welcome to InkscapeForum!

Text can certainly be given a gradient fill, with Inkscape, although I think it's done differently than in GIMP. And Inkscape can certainly export PNGs. What you want to do, which is to create an automated process to apply a particular gradient to many different text strings, and then export PNGs, will require writing some sort of script. And that's the point where I can no longer be helpful. I could tell you how to fill text with a gradient, on the Inkscape canvas, and export a PNG, but I don't know how to write scripts. If you know any languages, you can probably do it yourself. Or maybe you know someone who could do it for you?

Actually I'm not sure if you're asking how to write the script, or how to apply a gradient fill to text?

thangalin
Posts: 6
Joined: Mon Jul 09, 2012 9:47 am

Re: Set transparent area of text using gradient fill

Postby thangalin » Wed Jul 11, 2012 8:40 am

Hi, brynn.

Thanks for the reply.

The problem is filling this specific font. When I set the gradient fill for the font, it fills the black parts of the font. I want it to fill only the inside of the font, but not fill the inside of the loops for letters like A, B, R, O, P, and Q (as shown above).

I can write the script to automate this... But what I cannot figure out is how to fill the transparent areas of the SF Fedora Shadow font (as opposed to the black areas that actually define the outline of the font).

Usually a font is a solid black, which makes it easy to fill. But in this case, the black is not to be filled.

Any ideas?

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Set transparent area of text using gradient fill

Postby brynn » Wed Jul 11, 2012 2:20 pm

Oh ok, I understand. Let me dl the font, and do some tests. I'll reply again shortly :D

thangalin
Posts: 6
Joined: Mon Jul 09, 2012 9:47 am

Re: Set transparent area of text using gradient fill

Postby thangalin » Wed Jul 11, 2012 2:27 pm

Thank you, Brynn!

User avatar
druban
Posts: 1917
Joined: Fri Nov 20, 2009 10:48 pm

Re: Set transparent area of text using gradient fill

Postby druban » Wed Jul 11, 2012 3:13 pm

Hi , while Brynn is coming up with an ingenious solution, I will stick my rather uninspired comment in here!
You can't do it. The only way to do this is to use the font Fedora, filled with the gradient you choose, and position it precisely under the same text in Fedora shadow. The same procedure applies to any 'shadow' font, except you might want to place the filled font on top sometimes. The edging on Fedora shadow probably would look better with the filled font in the back.
Your mind is what you think it is.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Set transparent area of text using gradient fill

Postby brynn » Wed Jul 11, 2012 3:24 pm

Ok, I think I've got a reasonable solution. Although I have zero experience with scripting, and there may be some reason it would be easier to do certain things than others with a script. Instead of using the SF Fedora Shadow font, use SF Fedora. Type the text, then Duplicate. Fill the top one with your gradient. Select the bottom one (which is still black) and shift it down and to the left, until it looks like the Shadow version of the font. You may need to add a black stroke to the one with the gradient fill. Actually, since you're doing this via script, ......uh-oh.....

Ok...rrrrrch (apply breaks!) No, that won't work after all. I thought the fonts would be interchangeable, but they're not. The Shadow one has more space between the letters, to allow for the shadow to be there. Hhmmm......

Yeah, you know, I think someone else should probably answer this. Since I'm not familiar with scripting, I don't know if some things are easier to script than others. I was thinking that you could do some kerning, to make more space. But by the time you do that (different amounts of kerning for each letter combination) (which if I recall college beginning statistics (which is not likely) would be 26^26?? or 26 x 26??) you may as well find a way to use the Shadow font from the start.

I was trying to come up with some way to use a path operation to do this. But you have to go back and delete little "leftover" parts, which would be different for each word.....which I'm thinking would make scripting it very hard. Gosh, the more I think about this, the harder it seems to be, via script.

If I were doing it on the canvas, and only doing it once, there are a few ways to approach it. But I have no idea which way would be easier to script. I'll be glad to go over some of those ways, if you like. But it would probably be better for someone familiar with scripting Inkscape to reply. For all I know, this task could be easier via script, than it is on the canvas Image

And whadda you know, there's druban, posting once again, while I type, lol. But I see that I was not too far off base!

thangalin
Posts: 6
Joined: Mon Jul 09, 2012 9:47 am

Re: Set transparent area of text using gradient fill

Postby thangalin » Wed Jul 11, 2012 3:32 pm

The two Fedora Fonts don't align. They have different spacing that would be difficult to adjust for arbitrary words.

thangalin
Posts: 6
Joined: Mon Jul 09, 2012 9:47 am

Re: Set transparent area of text using gradient fill

Postby thangalin » Wed Jul 11, 2012 3:55 pm

Hi, brynn.

Sure, if you would show me a few steps to get something to work, that would be incredibly fantastic.

Thank you!

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Set transparent area of text using gradient fill

Postby brynn » Wed Jul 11, 2012 5:19 pm

Ok, this is for on the canvas. I had a couple of different approaches in mind.

Use path Difference.
-- Type the text, then Duplicate
-- Draw a rectangle, fill with your gradient, move to bottom
-- Select top text, Path menu > Object to Path, Ungroup, Path menu > Combine
-- Select new path and bottom rectangle, then Path menu > Difference, then Path menu > Break Apart
-- Deselect everything, then you can select the rectangle and delete
-- Now you'll see you're almost there. First, select the tiny pieces where the rectangle color fills the open part of letters, and delete
That should work. Note though that the colored part of each letter are separate. If you try to move, they'll come apart. So be sure to either re-combine, or group, if you need them to stay together when you move them. Also, when I did this, and I don't know why, the gradient itself moved when I did Path Difference. But if you select any colored part of a letter, and switch to Gradient tool, you'll see the handles somewhere, and you can grab them and move back into place.

The other way I was thinking may be simpler, depending on your perspective, uses the Paint Bucket tool.
-- Engage the Paint Bucket tool. Zoom in until a letter fills the screen, then click. That letter will be filled with either that tool's color, or the last color you used for the Paint Bucket (depending on your settings). But it won't be entirely filled. There will probably be some gaps between the color and the black. You can help alleviate the gap issue, by adjusting the Grow/Shrink setting on the control bar. I'd use something like 0.5 to 0.7. With this type of font, that will probably fill most of the gaps. But you still may need to tweak a node handle or 2, in certain places.
-- After they are all filled and tweaked, you can select them all (being careful not to select the text itself) and either use Path menu > Combine, or Object menu > Group (or command bar button).
-- Now you can apply the gradient to them all at once.
Be sure and Group the paths with the original text, if you want to move it around.

Or maybe someone else will have another approach?

User avatar
druban
Posts: 1917
Joined: Fri Nov 20, 2009 10:48 pm

Re: Set transparent area of text using gradient fill

Postby druban » Wed Jul 11, 2012 5:36 pm

Sorry, I should have pointed out that you will have to kern Fedora to make it match Fedora Shadow. I gather this is not acceptable for your workflow?
I also am assuming that you do not want to convert your text to objects, making it uneditable as text?
How about opening Fedora in a font editor, and pasting in the letterspacing from Fedora Shadow? If you have a font editor handy... if you're on a Linux system Fontforge is available. :roll:
These really are the only options that come to my mind.
Last edited by druban on Wed Jul 11, 2012 7:45 pm, edited 1 time in total.
Your mind is what you think it is.

User avatar
druban
Posts: 1917
Joined: Fri Nov 20, 2009 10:48 pm

Re: Set transparent area of text using gradient fill

Postby druban » Wed Jul 11, 2012 6:56 pm

Well, I think I have found the most likely solution for your problem.
As I mentioned above, you will enter the text twice, once in Fedora and once in Fedora Shadow, exactly the same font size. Fill them as you want them i.e. black on bottom, gradient on top. Also important! text must be aligned right or left or else things get very difficult.
Then you have to align them at one end the best you can. Center them up and down as well.
Now here's the trick: in the :tool_text: there's a letter spacing spinbox. Slowly increase this for the fedora (top) text until all the letters are aligned! This works because whoever made the font added exactly the same amount of spacing to every fedora glyph to make fedora shadow.
I used 48 size type and ended up spacing the letters 3.85 px more. i also added a very tiny stroke (0.1px) to the black fedora shadow text to cover any little misalignments.
rect1572.png
rect1572.png (238.29 KiB) Viewed 2974 times

Using this method, you can set up once and then keep swapping the text in both strings and generate a bitmap each time using a script. You will not be able to change the type size without setting up again though. But you could make up a file for each size.
Your mind is what you think it is.

User avatar
druban
Posts: 1917
Joined: Fri Nov 20, 2009 10:48 pm

Re: Set transparent area of text using gradient fill

Postby druban » Thu Jul 12, 2012 9:01 am

...aaaand you can see how spelling might be important! :lol:
g1560.png
g1560.png (28.49 KiB) Viewed 2940 times
This is the same file as before except I opened the svg in notepad and typed in Arbitrary where it said Fedora in each text string. Well, i thought I did.
Your mind is what you think it is.

thangalin
Posts: 6
Joined: Mon Jul 09, 2012 9:47 am

Re: Set transparent area of text using gradient fill

Postby thangalin » Sun Aug 12, 2012 1:57 pm

Brilliant. Thank you!

Here are the instructions for the script (in Linux or MacOS).

1. Edit the SVG file "template.svg".
2. Position the text and set the colours, as outlined above. Make sure the text area is as wide as the page (to account for long words or sentences).
3. Set the text (in both places) to a unique value, such as (in all caps): ARBITRARY
4. sed 's/ARBITRARY/Super Amazing Replacement/g' template.svg > replacement.svg
5. inkscape -z -D -d 300 -f replacement.svg -e replacement.png

Line #4. This line runs the "stream editor" (known as sed). It searches for all occurrences of the word ARBITRARY and replaces it with "Super Amazing Replacement" within the file template.svg. It then writes a new file called replacement.svg.
Line #5. This line runs Inkscape without a GUI. It reads "replacement.svg" and writes the final image "replacement.png".

So, if you had thousands of words to convert, you put them in a file (called "words.txt") and do something like:

for word in $(cat words.txt); do sed "s/ARBITRARY/$word/g" template.svg > temp.svg; inkscape -z -D -d 300 -f temp.svg -e $word.png; done; rm temp.svg

That'll do the trick. Here is a comprehensive script that can handle spaces in words:

--

Code: Select all

#!/bin/bash

language=$1
resolution=1200

mkdir -p $language

while read word; do
  sed "s/ARBITRARY/$word/g" template.svg > temp.svg

  file=${word// /-}
  file=${file//’/}
  file=${file//\&/}
  file=$language/title-$(echo $file | tr '[:upper:]' '[:lower:]').png

  inkscape -z -D -d $resolution -f temp.svg -e $file
done < $language-words.txt

rm temp.svg

--

Thanks again!


Return to “Help with using Inkscape”