Welcome to yEd Q&A!
Here you can ask questions and receive answers from other members of the community and yEd developers. And you can tell us your most wanted feature requests.

Categories

Center text the elegant way in SVG export

0 votes

I have rectangles with centered text in them. When I export this to SVG, it is quite verbose and does not use the proper SVG centering mechanism, see excerpt at the bottom.

When I now use some XSLT to change some of the labels and the new labels are different in length, they are not centered anymore because the position is hardcoded, thus I request to change the export to use the SVG attributes dominant-baseline="middle" and text-anchor="middle" (and the label being positioned in the center of the rectangle).

This is very well explained in this StackOverflow answer, however there may be an even better way.

      <g class="node" id="EnterpriseFunctionClassified">
         <g fill="rgb(255,204,153)" text-rendering="geometricPrecision" shape-rendering="geometricPrecision" transform="matrix(1,0,0,1,-80,-95)" stroke="rgb(255,204,153)">
            <rect x="741" y="212" width="201.25" rx="4" ry="4" height="30" stroke="none"/>
         </g>
         <g text-rendering="geometricPrecision" stroke-miterlimit="1.45" shape-rendering="geometricPrecision" transform="matrix(1,0,0,1,-80,-95)" stroke-linecap="butt">
            <rect x="741" y="212" fill="none" width="201.25" rx="4" ry="4" height="30"/>
         </g>
         <g>
            <g text-rendering="geometricPrecision" stroke-miterlimit="1.45" shape-rendering="geometricPrecision" font-family="sans-serif" transform="matrix(1,0,0,1,-80,-95)" stroke-linecap="butt">
               <text x="765.27" xml:space="preserve" y="229.75" stroke="none">terminology items</text>
          </g>
         </g>
      </g>
 

in Feature Requests by

1 Answer

0 votes

Unfortunately, I do not think it is possible to use SVG-specific techniques like text-anchor="middle" when exporting a diagram to SVG.

Conceptually, yEd's SVG exports works by redirecting the drawing commands that are used to display a diagram on screen to a converter that creates corresponding SVG elements. The set of drawing commands for rendering on screen does not include a center command - all it offers is a command to draw text at a specific location. As a result, the converter cannot know that the original intention was to "center text".

For completeness sake, I would like to add that the converter mentioned above is the Apache Batik SVG toolkit - i.e. this is a third-party library.

by [yWorks] (161k points)
Legal Disclosure | Privacy Policy
...