Inkscape.org
Creating New Extensions Usage of inkex.TextElement.shape_box() method
  1. #1
    Pavel Serikov Pavel Serikov @jaih

    Official description of shape_box method looks like:

    Returns a horrible bounding box that just contains the coord points of the text without width or height (which is impossible to calculate)

    Why it's impossible to calculate width or height of text ?

  2. #2
    Polygon Polygon @Polygon🌶

    That is maybe because it dynamically changes with the use of descender+ascender etc if this makes sense.

  3. #3
    Pavel Serikov Pavel Serikov @jaih

    Is it possible to calculate width and height of text in Inkscape using other methods ?

    By the way, I saw that there is standart getBBox() method at SVG 2.0 spec, doing same that I need from Inkscape:

    <!DOCTYPE html>
    <html>
    <body>
    <svg>
      <text id="svg1" x="50" y="50" style="font: bold 24px sans-serif;">I love SVG!</text>
    </svg>
    
    <script type="text/javascript">
      textElement = document.getElementById("svg1");
      var bbox = textElement.getBBox();
      console.log(bbox);   // SVGRect { x: 50, y: 27, width: 148.78334045410156, height: 29 }
    </script>
    </body>

    If I change font size or style in example above - bbox var is changing and calculating fine.

    If I do same in Python - result is not ok

    #!/usr/bin/env python
    
    import inkex
    from pprint import pprint
    
    text = inkex.TextElement.new(x="50",y="50", style = {'font': 'bold 24px sans-serif'} );
    text.text = 'I love SVG!'
    print(text.shape_box())                  # BoundingBox((50.0, 50.0),(50.0, 50.0))

    Any chance to count bbox of text in Inkscape?

    My versions:

    $ inkscape --version
    Inkscape 1.1 (1:1.1+202105261517+ce6663b3b7)
    
    $ pip freeze | grep inkex
    inkex==1.0.1
    
    $ pip freeze | grep lxml
    lxml==4.5.0
    

     

  4. #4
    Polygon Polygon @Polygon🌶

    Maybe try your query in the Chat: https://chat.inkscape.org/channel/inkscape_extensions

    Good luck.

  5. #5
    Pavel Serikov Pavel Serikov @jaih

    Response from chat:

    > @pavelsr It's not possible to calculate the bounding box, because we don't have the paths for the text. All we know is that you have some text, we have no idea what it looks like, and therefore how big it is.

    But it doesn't work if I have paths for the text. If I use text.get_path().shape_box() instead of text.shape_box() I got   AttributeError: 'Path' object has no attribute 'shape_box'

    If I make text.get_path().bounding_box() it returns None

    How to get paths for text and then get bounding box for it ?

  6. #6
    Polygon Polygon @Polygon🌶
    jaih

    because we don't have the paths for the text.

    That is what I was trying to say. Text object is dynamically updating and has probably too many variables like myriads of font types for instance.

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Usage of inkex.TextElement.shape_box() method