Inkscape.org
Creating New Extensions Getting the bounding box of text
  1. #1
    random_text random_text @random_text

    I am trying to make an extension and the text positioning is driving me mad (especially when the sodipodi:role is set). Things would be so much easier if there was a bounding box function for text, but according to the documentation the only one available is horrible and cannot determine the true limits. I confirmed this.

    Is there really no good way to get text's bounding box programmatically?

  2. #2
    random_text random_text @random_text
    *

    Well, I found a way. It's not pretty though: it requires a command line call.

        import sys, copy     
        sys.path.append('/usr/share/inkscape/extensions')
        import subprocess
        
        tProc = subprocess.run( 'inkscape --query-all "%s"' % self.options.input_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        tFStR = tProc.stdout  # List of all SVG objects in tFile
        tErrM = tProc.stderr
        # inkex.utils.debug(tFStR)
        tBBLi = tFStR.splitlines()
        x=[[float(x.strip('\'')) for x in str(d).split(',')[1:]] for d in tBBLi]
        bbs=dict();
        for d in tBBLi:
            key = str(d).split(',')[0][2:];
            data = [float(x.strip('\'')) for x in str(d).split(',')[1:]]
            bbs[key] = data;
        return bbs


    But now I'm wondering if there's a way to calculate the true x and y of a text object from its bounding box.

  3. #3
    random_text random_text @random_text

    In particular, this strategy is really limiting because (as far as I can tell), the command-line version will only ever get the bounding box from before the extension was called. It doesn't update it.

  4. #4
    Martin Owens Martin Owens @doctormo🌹🧀

    Running inkscape manually can result in API problems as inkscape's interface changes. There's a command module to help you use these calls, see `inkex.command.inkscape` and some examples in the core repository.