Apache Batik

repository·main·Indexed 42 days ago

https://github.com/apache/xmlgraphics-batik

A modular Java toolkit for working with Scalable Vector Graphics (SVG), enabling developers to parse, generate, view, and convert SVG images in client-side and server-side applications. Includes a suite of CLI tools such as an SVG Browser, a rasterizer for converting SVG to JPEG, PNG, PDF, and TIFF, a pretty-printer (svgpp), and a slideshow application.

Tokens
5.8K
Snippets
12
Records
33
Agent score
53%

What's inside Batik

  1. Use Batik Extensions in applications

    main

    Batik Extensions are provided via specific JAR files that include the extensions on the classpath. When you use these JAR files to start a Batik application, the extensions will be available and functional.

    Warning: Batik Extensions are not part of the SVG standard. Content created using these extensions is non-conformant SVG. Other SVG renderers will not be able to render these documents. Use these extensions only for content intended for closed systems.

  2. Overview of Apache Batik

    main

    Apache Batik is a Java-based toolkit designed for handling Scalable Vector Graphics (SVG) images. It provides modular building blocks that developers can use to view, generate, parse, or manipulate SVG content within Java applications.

    Key capabilities include:

    • SVG Viewing: Includes a Swing component for adding SVG viewing capabilities to Java desktop applications.
    • SVG Generation: Tools to generate SVG content on both client-side and server-side.
    • SVG Parsing: Modules for parsing SVG documents.
    • Format Conversion: Ability to convert SVG content into raster formats like JPEG or PNG.
    • Extensibility: Highly extensible architecture that allows for handling custom SVG tags.
  3. Use the Rasterizer task for Ant to convert SVG to raster

    main

    The Rasterizer task is an Ant task designed to convert SVG files into raster formats. It is provided as a contribution to the Batik project.

    Prerequisites:

    • JDK: At least JDK 1.4 is required for both building and executing the task.
    • Batik: You must compile Batik before attempting to compile this task.
    • Ant Version Compatibility: To avoid unexpected behavior caused by internal changes in Ant, ensure that the version of Ant used to build the task is the same version used to execute it.
  4. Access contributed fonts for Batik

    main

    The contrib/fonts/gladiator directory contains font assets contributed to the Apache Batik project. These assets are organized into two formats:

    • SVG fonts: Located in the svg/ subdirectory.
    • TrueType fonts (TTF): Located in the ttf/ subdirectory.

    These can be used as resources when working with Batik to ensure specific font rendering in SVG documents.

  5. Define URI generation strategies

    main

    The <uri> element defines how a generated file's URI is created from its source URI. The type attribute determines the strategy:

    1. append: Appends the generated page's URI to the end of the source URI.
      • Example: src="index.html" with src-prefix="documents/" and dest="build/dest/" results in build/dest/documents/index.html.
    2. replace: Completely ignores the source URI and uses the dest URI instead. This only works for "single" pages and not when links are followed.
    3. insert: Inserts the generated page's URI into the destination URI at the position marked with a *.

    If the dest attribute is omitted, the global <dest-dir> value is used.

    <!-- append example -->
    <uri type="append" src-prefix="documents/" src="index.html" dest="build/dest/"/>
    
    <!-- replace example -->
    <uri type="replace" src-prefix="documents/" src="index.html" dest="build/dest/docs.html"/>
    
    <!-- insert example -->
    <uri type="insert" src-prefix="documents/" src="index.html" dest="zip://*.zip/page.html"/>
  6. Include or exclude URIs during link following

    main

    When follow-links is enabled, you can control which URIs Cocoon follows using <include-links> and <exclude> patterns.

    Exclusion Rules:

    • A URI must match an include pattern (if provided) AND NOT match any exclude pattern to be followed.
    • Patterns use wildcard matching (e.g., **/, api/**, javadoc/**).
    • Multiple <exclude> nodes can be used.
    • Currently, matching is performed against the complete source URI (including prefix).
  7. Configure optional scripting languages in Batik

    main

    By default, Batik includes a scripting engine for ECMAScript. If your application requires support for other scripting languages, you can add support for:

    • Python
    • TCL

    Detailed instructions for adding these components can be found in the official installation documentation.

  8. Configure SVG Scripting and Security

    main

    The rasterizer allows controlling how scripts are handled during conversion:

    • -scripts <type>: Specify the allowed script types.
    • -anyScriptOrigin: If present, scripts are not constrained to the same origin as the document.
    • -scriptSecurityOff: Disables secure execution of scripts entirely.
    • -allowExternalResources: Enables the loading of external resources.

    Security Warning: Using -scriptSecurityOff or -anyScriptOrigin reduces the security sandbox applied to the SVG during conversion.