It is possible to do this by Javascript for INLINE SVG!
I've made a pen for you, to show you the principle.
I was looking for something like this myself and didn't find it anywhere.
It doesn't seem part of the spec, so doing it in Javascript is justified :)
This is the basic structure for a line of text to be justified, each word should be a text tag:
<g class="line" transform="translate(0,40)">
<text>Another</text>
<text>line</text>
<text>of</text>
<text>text</text>
</g>
This is the javascript-code:
var lines=document.getElementsByClassName("line");
for(var l=0;l<lines.length;l++)
{
var line=lines[l];
var total_width=200;
var texts=line.getElementsByTagName("text");
var width_of_texts=0;
for(var i=0;i<texts.length;i++)
{
width_of_texts+=texts[i].getBBox().width;
}
if(texts.length>1)
{
var extra_space=(total_width-width_of_texts)/(texts.length-1);
var x=0;
for(var i=0;i<texts.length;i++)
{
texts[i].setAttribute("x",x);
x=x+texts[i].getBBox().width+extra_space;
}
}
}
Here is a working example.
https://codepen.io/SnoepGames/pen/zVaRoZ
Short explanation.
We can get classes and elements within an inline SVG in much the same way as with HTML elements.
With getBBox() we can get the width of the text in the same way for SVG text-elements as getBoundingClientRect for HTML elements.
The rest is just adding up and subtracting.
You could put a data attribute in a group with class "justified lines" as well to set the width dynamically and you'd have a pretty usefull script.
The script will now justify all grouped texts with class line to a fixed width
(var total_width = 200)
This script does justification by adding to spaces, but you could also use this principle to do justification between letters.