Disable automatic stretching upon deleting characters in text object
-
- Posts: 4
- Joined: Mon Jun 11, 2018 11:59 pm
Disable automatic stretching upon deleting characters in text object
Assume a text object in Inkscape that contains the text "foo bar baz". This text object was generated via R package svglite. When I delete characters in this object, the width of the text object does not change automatically but is maintained. At the same time, the text is stretched. How can I disable this behaviour and instead have the object width decrease in accordance with the number of characters?
Re: Disable automatic stretching upon deleting characters in text object
As this isn't the normal behaviour for an Inkscape text object, I think you need to supply an example file so that we can see what sort of structure is being created under the hood.
-
- Posts: 4
- Joined: Mon Jun 11, 2018 11:59 pm
Re: Disable automatic stretching upon deleting characters in text object
Here's an example file: https://file.io/pohWIL It was generated via R package svglite.
You can ignore the overlaying text in the lower half of the svg-file. If you try to delete a letter or two in any of the text in the upper half of the svg-file, you will notice that the entire text is stretched to the original size.
Here are the details of my operation system and my Inkscape version:
$ uname -o
GNU/Linux
$ inkscape --version
Inkscape 0.92.2 2405546, 2018-03-11
You can ignore the overlaying text in the lower half of the svg-file. If you try to delete a letter or two in any of the text in the upper half of the svg-file, you will notice that the entire text is stretched to the original size.
Here are the details of my operation system and my Inkscape version:
$ uname -o
GNU/Linux
$ inkscape --version
Inkscape 0.92.2 2405546, 2018-03-11
-
- Posts: 4
- Joined: Mon Jun 11, 2018 11:59 pm
Re: Disable automatic stretching upon deleting characters in text object
Here is the xml-code of the file I am refering to (and which I just shared):
Code: Select all
<?xml version='1.0' encoding='UTF-8' ?>
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 720.00 576.00'>
<defs>
<style type='text/css'><![CDATA[
line, polyline, path, rect, circle {
fill: none;
stroke: #000000;
stroke-linecap: round;
stroke-linejoin: round;
stroke-miterlimit: 10.00;
}
]]></style>
</defs>
<rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/>
<defs>
<clipPath id='cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA=='>
<rect x='59.04' y='59.04' width='630.72' height='443.52' />
</clipPath>
</defs>
<line x1='350.76' y1='293.63' x2='437.87' y2='206.51' style='stroke-width: 0.75;' clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)' />
<line x1='350.76' y1='293.63' x2='310.93' y2='253.80' style='stroke-width: 0.75;' clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)' />
<line x1='350.76' y1='353.48' x2='350.44' y2='353.80' style='stroke-width: 0.75;' clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)' />
<line x1='350.76' y1='353.48' x2='352.36' y2='355.09' style='stroke-width: 0.75;' clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)' />
<line x1='350.76' y1='353.48' x2='350.76' y2='293.63' style='stroke-width: 0.75;' clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)' />
<g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)'><text x='437.87' y='206.51' style='font-size: 12.00px; font-style: italic; font-family: Arial;' textLength='196.12px' lengthAdjust='spacingAndGlyphs'>combined Cabomba aquatica NY684</text></g>
<g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)'><text x='352.36' y='355.09' style='font-size: 12.00px; font-style: italic; font-family: Arial;' textLength='208.79px' lengthAdjust='spacingAndGlyphs'>combined Cabomba caroliniana NY112</text></g>
<g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)'><text x='350.44' y='353.80' style='font-size: 12.00px; font-style: italic; font-family: Arial;' textLength='288.16px' lengthAdjust='spacingAndGlyphs'>combined Cabomba caroliniana cv Silvergreen NY690</text></g>
<g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)'><text x='310.93' y='253.80' style='font-size: 12.00px; font-style: italic; font-family: Arial;' textLength='187.44px' lengthAdjust='spacingAndGlyphs'>combined Cabomba furcata NY691</text></g>
<g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDUwMi41Nnw1OS4wNA==)'><text x='340.74' y='327.85' style='font-size: 12.00px; font-family: Arial;' textLength='20.02px' lengthAdjust='spacingAndGlyphs'>100</text></g>
</svg>
-
- Posts: 4
- Joined: Mon Jun 11, 2018 11:59 pm
Re: Disable automatic stretching upon deleting characters in text object
Here seems to be the answer: Text elements in the svg-file in question contain attributes called "textLength" (e.g., textLength='187.44px'). Upon removal of these attributes, the default behaviour of the text elements resumed.
Re: Disable automatic stretching upon deleting characters in text object
It's always good when someone manages to answer their own question 
Unfortunately I can't think of a way to remove just these attributes in Inkscape alone, other than manually editing them in the XML editor, which is awkward if there are more than a handful of entries like this. The simplest solution is probably to open the file in a text editor and use search and replace to replace "textLength=" with "XtextLength=" (or some other attribute name that isn't a real SVG attribute). It's not as neat as removing the attribute entirely, but should do the job.

Unfortunately I can't think of a way to remove just these attributes in Inkscape alone, other than manually editing them in the XML editor, which is awkward if there are more than a handful of entries like this. The simplest solution is probably to open the file in a text editor and use search and replace to replace "textLength=" with "XtextLength=" (or some other attribute name that isn't a real SVG attribute). It's not as neat as removing the attribute entirely, but should do the job.
Re: Disable automatic stretching upon deleting characters in text object
(or use a regular expression to really remove the attributes, if you search+replace supports those)
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!
Inkscape FAQ - Learning Resources - Website with tutorials (German and English)
Inkscape FAQ - Learning Resources - Website with tutorials (German and English)
Re: Disable automatic stretching upon deleting characters in text object
Yeah, I just went with the "safe" option, as a mistyped regex could break the whole file very easily. Still, if anyone wants to give it a try, the following regex should work:
(note that there's a space at the start of the line)
As most people probably aren't familiar with regex syntax, this finds any string that starts with a space, followed by "textLength=" then an opening quote, then any number of non-quote characters, then another quote. This approach means that whatever the value of textLength, whether or not it's got decimal a decimal point or different units, it will still get found.
If you've got access to a Linux or Unix-like command line which has the "sed" program, the following one-liner will remove all the textLength attributes from the entire file (where "file.svg" is the filename - replace with "*.svg" to hit all the SVG files in the directory). Make sure you've got backups first!
Code: Select all
textLength='[^']*'
(note that there's a space at the start of the line)
As most people probably aren't familiar with regex syntax, this finds any string that starts with a space, followed by "textLength=" then an opening quote, then any number of non-quote characters, then another quote. This approach means that whatever the value of textLength, whether or not it's got decimal a decimal point or different units, it will still get found.
If you've got access to a Linux or Unix-like command line which has the "sed" program, the following one-liner will remove all the textLength attributes from the entire file (where "file.svg" is the filename - replace with "*.svg" to hit all the SVG files in the directory). Make sure you've got backups first!
Code: Select all
sed -i "s/ textLength='[^']*'//" file.svg