Get svg text size in python

I am creating an SVG image in python (clean, no external libs yet). I want to know what the size of the text element will be before I put it correctly. Any good idea how to do this? I checked the pysvg library, but I did not see anything like getTextSize ()

+5
source share
2 answers

It can be quite complicated. First you need to read the chapter in the text of the SVG specification. Assuming you want to get the width of simple elements text, not elements textpath, at least you need:

  • Disassemble the properties of the font selection , properties distance and read the attribute xml:spaceand property writing-mode(also can be top down, rather than left to right and from right to left).
  • Based on the foregoing, open the correct font and read the glyph data and extract the width and height of the glyphs in the text line. Finding a font unambiguously can be a big task, seeing several places where font files can be hidden.
  • (optional) Look at the line for possible ligatures (depending on the language) and replace them with the correct glyph if it exists in the font.
  • Add a width for all characters and spaces, the latter depending on the properties of the interval and (optionally) possible kerning pairs.

pango. python py-gtk. , , python . Layout.

- SVG- . , , SVG- Firefox .

, TeX , (pdf) ( ) ( ).

+1

, . , ( ), , svg, Inkscape, , png. png ( ), temp svg png , , .

, DPI 90, , , , , svgwrite . -D - , , .. .

os.cmd(/cygdrive/c/Program\ Files\ \(x86\)/Inkscape/inkscape.exe -f work_temp.svg -e work_temp.png -d 90 -D)

png, , , python3 ( python2)

def is_png(data):
    return (data[:8] == b'\x89PNG\r\n\x1a\n'and (data[12:16] == b'IHDR'))

def get_image_info(data):
    if is_png(data):
        w, h = struct.unpack('>LL', data[16:24])
        width = int(w)
        height = int(h)
    else:
        raise Exception('not a png image')
    return width, height

if __name__ == '__main__':
    with open('foo.png', 'rb') as f:
        data = f.read()

    print is_png(data)
    print get_image_info(data)

,

+1

All Articles