You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Phil Scadden <P....@gns.cri.nz> on 2021/10/12 03:19:57 UTC

Setting up style in defs not working.

If I manually create an svg
<svg…>
<defs id="genericDefs">
        <style>
            @import url("https://fonts.googleapis.com/css?family=Basic");
        </style>
</defs>
<g><g style=" font-weight:bold; font-family:'Basic'; image-rendering:optimizeQuality;"><text x="277" xml:space="preserve" y="24" style="stroke:none;">my text</text>…. Etc etc

Then all is well  - it renders with the google font.

However, trying to create this Batik is turning into a mission. I have tried a lot of variations on this:
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        String svgNS = "http://www.w3.org/2000/svg";
        Document document = domImpl.createDocument(svgNS, "svg", null);
        SVGGraphics2D svg = new SVGGraphics2D(document);
        Element root =  svg.getRoot();

          defs = root.getElementsByTagNameNS(svgNS, "defs").item(0);
        Element style = document.createElementNS
            (SVGSyntax.SVG_NAMESPACE_URI, SVGSyntax.SVG_STYLE_TAG);
        style.setAttributeNS(null, SVGSyntax.SVG_TYPE_ATTRIBUTE, "text/css");
        style.appendChild(document.createCDATASection("@import url(\"https://fonts.googleapis.com/css?family=Open+Sans\");"));
        defs.appendChild(style);

if I examine the structure in the debugger, it looks fine. However, if I continue and the SVGGraphic2D to create text etc, then the output svg (embedded in
an HTML page) does not contain the style with defs, and it doesn’t honour the
        Font f = new Font("Open Sans", Font.BOLD, 12);
        g2d.setFont(f);

Clearly something missing here? My desire is to render text with a google font.
____________________
Ngā mihi, Nā Phil Scadden
Te Raraunga me te Tātaritanga Mokowā Aronuku (Geospatial Data and Analysis)
GNS Science Te Pῡ Ao
764 Cumberland St, Private Bag 1930,
Dunedin, New Zealand Ph +64 3 4799663, 027 3463185

“Whāia te iti kahurangi ki te tūohu koe me he maunga teitei”

Notice: This email and any attachments are confidential and may not be used, published or redistributed without the prior written consent of the Institute of Geological and Nuclear Sciences Limited (GNS Science). If received in error please destroy and immediately notify GNS Science. Do not copy or disclose the contents.