84

Box size known. Text string length unknown. Fit text to box without ruining its aspect ratio.

enter image description here

After an evening of googling and reading the SVG spec, I'm pretty sure this isn't possible without JavaScript. The closest I could get was using the textLength and lengthAdjust text attributes, but that stretches the text along one axis only.

<svg width="436" height="180"
    style="border:solid 6px"
    xmlns="http://www.w3.org/2000/svg">
    <text y="50%" textLength="436" lengthAdjust="spacingAndGlyphs">UGLY TEXT</text>
</svg>

enter image description here

I am aware of SVG Scaling Text to fit container and fitting text into the box

6
  • 2
    I ended up making a loop in javascript that increases the font size until getBBox shows it would no longer fit. Ugly, so still hoping there would be some other way.
    – Bemmu
    Mar 19, 2013 at 3:03
  • I have also been trying to get this same functionality to work. The best method I have found was the same one you did. Loop through JS and change font until it fits. But even in fonts there is still some whitespace above and below so you can't seem to get it just right.
    – Chad
    Mar 6, 2014 at 23:35
  • This sucks, it seems like such a basic thing, but the spec is clear that it only stretches it in one direction. After playing around, I was able to get it close by modifying only the Y scale using transform, aka: transform="scale(0,5)" - jsfiddle.net/G5L8W Mar 22, 2014 at 0:07
  • I am trying to think if there was a way to calculate the amount of scale being applied to the text on the x axis and apply it to the y axis, but that would then also mean adding in checks to make sure you aren't over scaling the Y, and then you're back at using JS. I'll bet you could use transform/scale with JS better though. Mar 22, 2014 at 0:08
  • 1

5 Answers 5

53
+100

I didn't find a way to do it directly without Javascript, but I found a JS quite easy solution, without for loops and without modify the font-size and fits well in all dimensions, that is, the text grows until the limit of the shortest side.

Basically, I use the transform property, calculating the right proportion between the desired size and the current one.

This is the code:

<?xml version="1.0" encoding="UTF-8" ?>
<svg version="1.2" viewBox="0 0 1000 1000" width="1000" height="1000" xmlns="http://www.w3.org/2000/svg" >
 <text id="t1" y="50" >MY UGLY TEXT</text>
 <script type="application/ecmascript"> 

    var width=500, height=500;

    var textNode = document.getElementById("t1");
    var bb = textNode.getBBox();
    var widthTransform = width / bb.width;
    var heightTransform = height / bb.height;
    var value = widthTransform < heightTransform ? widthTransform : heightTransform;
    textNode.setAttribute("transform", "matrix("+value+", 0, 0, "+value+", 0,0)");

 </script>
</svg>

In the previous example the text grows until the width == 500, but if I use a box size of width = 500 and height = 30, then the text grows until height == 30.

9
  • 10
    Or simply... textNode.setAttribute("transform", "scale("+value+")"); Feb 27, 2015 at 19:55
  • 4
    Quick note- the scale attribute also affects the coordinate system of the current item, so if you want the element to be in the same position, will need to divide both x and y positions by the scalar to get the same relative position Jun 13, 2016 at 21:09
  • 2
    Or even more simply... set the text font-size to 1 (1em in our case); then once you have the scale factor (value in this answer) just set the font-size to it. See example below.
    – Izhaki
    Jun 4, 2018 at 21:42
  • I also wanted the text to be verticaly-centered, so I changed the end (after var value = ...) to textNode.style.transform = "translateY(50%) scale(" + value + ")"; textNode.style.transformOrigin = '0px 0px'; textNode.style.dominantBaseline = 'central';
    – jave.web
    Oct 3, 2019 at 18:12
  • 1
    @Krunal, I'd try using Inkscape CLI: stackoverflow.com/a/34725966/661140
    – Roberto
    Dec 12, 2019 at 17:18
19

first of all: just saw that the answer doesn't precisely address your need - it might still be an option, so here we go:

you are rightly observing that svg doesn't support word-wrapping directly. however, you might benefit from foreignObject elements serving as a wrapper for xhtml fragments where word-wrapping is available.

have a look at this self-contained demo (available online):

<?xml version="1.0" encoding="utf-8"?>
<!-- SO: http://stackoverflow.com/questions/15430189/pure-svg-way-to-fit-text-to-a-box  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
   xmlns:xhtml="http://www.w3.org/1999/xhtml"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   version="1.1"
   width="20cm" height="20cm"
   viewBox="0 0 500 500"
   preserveAspectRatio="xMinYMin"
   style="background-color:white; border: solid 1px black;"
>
  <title>simulated wrapping in svg</title>
  <desc>A foreignObject container</desc>

   <!-- Text-Elemente -->
   <foreignObject
      x="100" y="100" width="200" height="150"
      transform="translate(0,0)"
   >
      <xhtml:div style="display: table; height: 150px; overflow: hidden;">
         <xhtml:div style="display: table-cell; vertical-align: middle;">
            <xhtml:div style="color:black; text-align:center;">Demo test that is supposed to be word-wrapped somewhere along the line to show that it is indeed possible to simulate ordinary text containers in svg.</xhtml:div>
         </xhtml:div>
      </xhtml:div>
   </foreignObject>

  <rect x="100" y="100" width="200" height="150" fill="transparent" stroke="red" stroke-width="3"/>
</svg>

3
  • 3
    Thanks, but was not interested in word wrapping.
    – Bemmu
    Mar 24, 2014 at 3:23
  • 5
    @collapsar, your demo link is broken
    – shanabus
    Aug 12, 2019 at 13:17
  • If I add more text, it will hide, and the font size of the text will not shrink. Feb 23, 2022 at 17:51
6

I've developed @Roberto answer, but instead of transforming (scaling) the textNode, we simply:

  • give it font-size of 1em to begin with
  • calculate the scale based on getBBox
  • set the font-size to that scale

(You can also use 1px etc.)

Here's the React HOC that does this:

import React from 'react';
import TextBox from './TextBox';

const AutoFitTextBox = TextBoxComponent =>
  class extends React.Component {
    constructor(props) {
      super(props);
      this.svgTextNode = React.createRef();
      this.state = { scale: 1 };
    }

    componentDidMount() {
      const { width, height } = this.props;
      const textBBox = this.getTextBBox();
      const widthScale = width / textBBox.width;
      const heightScale = height / textBBox.height;
      const scale = Math.min(widthScale, heightScale);

      this.setState({ scale });
    }

    getTextBBox() {
      const svgTextNode = this.svgTextNode.current;
      return svgTextNode.getBBox();
    }

    render() {
      const { scale } = this.state;
      return (
        <TextBoxComponent
          forwardRef={this.svgTextNode}
          fontSize={`${scale}em`}
          {...this.props}
        />
      );
    }
  };

export default AutoFitTextBox(TextBox);
3

I don't think its the solution for what you want to do but you can use textLength with percentage ="100%" for full width.

<svg width="436" height="180"
    style="border:solid 6px"
    xmlns="http://www.w3.org/2000/svg">
    <text x="0%" y="50%" textLength="100%">blabla</text>
</svg>

you can also add text-anchor="middle" and change the x position to center perfectly your text

this will not change the fontsize and you will have weird space letterspacing...

JSFIDDLE DEMO

2
  • 1
    It yust expands the letter spacing. :(
    – MGM
    Apr 15, 2018 at 17:24
  • Yep, and just to be clear on the text centering - the attribute is "text-anchor" and you would need also x=50% like <text x="50%" y="50%" textLength="100%" text-anchor="middle">AB</text>
    – jave.web
    Oct 3, 2019 at 17:03
2

This is still an issue in 2022. There is no way to define bounds and get text to scale in a pure scalable vector graphic. Adjusting the font size manually is still the only solution it seems, and the examples given are quite buggy. Has anybody figured out a clean solution that works? Judging by the svg spec it looks like a pure solution doesn't exist.

And to provide some sort of answer myself, this resource is the best I've found, is hacky, but works much more robustly: fitrsvgtext - storybook | fitrsvgtext - GitHub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.