Inkscape.org
Creating New Extensions how to get font-size
  1. #1
    JorgeInkscape JorgeInkscape @JorgeInkscape
    *
    Hello group.
    I'm making an inkscape extension that gives me a json file, but I can't get the font.size property of a text

    -------------------------- Code ---------------------------------------------------

    import inkex
    import json
    import os
    from lastDirectoryAndFile import getRuta

    class GetJson(inkex.EffectExtension):
        def add_arguments(self, pars):
            pass # We don't need arguments for this extension

        def effect(self):
            data = [] # Lista de diccionarios
            for elem in self.svg.selection:
                if elem.typename == "TextElement":
                    json_data = {
                        "id": elem.get("id"),
                        "type": elem.typename,
                        "text": elem.get_text(),
                        "width": elem.get("width"),
                        "height": elem.get("height"),
                        "x": elem.get("x"),
                        "y": elem.get("y"),
                        "font-size":elem.get("style")
                    } 
                else:
                    href=getRuta(elem.get("xlink:href"))
                    json_data = {
                        "id": elem.get("id"),
                        "type": elem.typename,
                        "width": elem.get("width"),
                        "height": elem.get("height"),
                        "x": elem.get("x"),
                        "y": elem.get("y"),
                        "href":href
                    }

                data.append(json_data)

            json_string = json.dumps(data, indent=4)

            #file_path = os.path.join(os.getcwd(), "myJson.json")
            with open("miruta/myJson.json", "w") as file:
                file.write(json_string)

    if __name__ == '__main__':
        GetJson().run()

    ------------------------------- End Code -------------------------------

    "font-size":elem.get("style")["font-size"] it does not work

     

    I want to show you the Json I am getting

    ------------------------------------ JSON---------------------------------------

    [
        {
            "id": "tarjetas",
            "type": "Image",
            "width": "135.46666",
            "height": "135.46666",
            "x": "26.346313",
            "y": "-23.362434",
            "href": "Downloads/2118338.png"
        },
        {
            "id": "texto1",
            "type": "TextElement",
            "text": "Hola esta es una prueba para mirar \ncomo funciona este texto de \nverdad",
            "width": null,
            "height": null,
            "x": "22.099817",
            "y": "93.789467",
            "font-size": "font-weight:bold;font-size:6.35px;line-height:1.25;font-family:Goldman;-inkscape-font-specification:'Goldman Bold';letter-spacing:0px;word-spacing:0px;white-space:pre;inline-size:123.644;display:inline;stroke-width:0.264583"
        },
        {
            "id": "cerebro",
            "type": "Image",
            "width": "69.528175",
            "height": "59.186085",
            "x": "95.512871",
            "y": "62.81852",
            "href": "Downloads/1787077.png"
        }
    ]

    ------------------------------------ End JSON -----------------------------------

     

  2. #2
    inklinea inklinea @inklinea⛰️

    True font size is a combination of the svg font-size, the element transform and all of the composed transforms going back to the svg root.

    It can also be set via class or attribute.

    However if you want to get a font-size just in the inline style ( which should override class/attribute )

    if selected_item.TAG == 'text':
    font_size = selected_item.style.get('font-size')

    'get' will return None if there is no inline style entry.

    If you want to detected font-size from class or attribute ( where there is no inline style ) you can use:

    specified_font_size = selected_item.specified_style().get('font-size')

     

  3. #3
    JorgeInkscape JorgeInkscape @JorgeInkscape

    Perfect, thank you very much