Skip to content

develar/jsvg

 
 

Repository files navigation

Quality Gate Status Code Style CI Maven Central

JSVG - A Java SVG implementation

The SVG logo rendered by JSVG
The SVG logo rendered using JSVG

JSVG is an SVG user agent using AWT graphics. Its aim is to provide a small and fast implementation. This library is under active development and doesn't yet support all features of the SVG specification, some of which it decidedly won't support at all. This implementation only tries to be a static user agent meaning it won't support any scripting languages or interaction. Animations aren't currently implemented but are planned to be supported.

This library aims to be as lightweight as possible. Generally JSVG uses ~50% less memory than svgSalamander and ~98% less than Batik.

JSVG is used by the Jetbrains IDEA IDE suite for rendering their interface icons.

How to use

The library is available on maven central:

dependencies {
    implementation("com.github.weisj:jsvg:1.2.0")
}

Also, nightly snapshot builds will be released to maven:

repositories {
    maven {
        url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
    }
}

// Optional:
configurations.all {
    resolutionStrategy.cacheChangingModulesFor(0, "seconds")
}

dependencies {
    implementation("com.github.weisj:jsvg:latest.integration")
}

Loading

To load an svg icon you can use the SVGLoader class. It will produce an SVGDocument

SVGLoader loader = new SVGLoader();
URL svgUrl = MyClass.class.getResource("mySvgFile.svg");
SVGDocument svgDocument = loader.load(svgUrl);

Note that SVGLoader is not guaranteed to be thread safe hence shouldn't be used across multiple threads.

Rendering

An SVGDocument can be rendered to any Graphics2D object you like e.g. a BufferedImage

FloatSize size = svgDocument.size();
BufferedImage image = new BufferedImage((int) size.width,(int) size.height);
Graphics2D g = image.createGraphics();
svgDocument.render(null,g);
g.dispose();

or a swing component

class MyComponent extends JComponent {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        svgDocument.render(this, (Graphics2D) g, new ViewBox(0, 0, getWidth(), getHeight()));
    }
}

Supported features

For supported elements most of the attributes which apply to them are implemented.

  • ✅: The element is supported. Note that this doesn't mean that every attribute is supported.
  • (:white_check_mark:): The element is supported, but won't have any effect (e.g. it's currently not possible to query the content of a <desc> element)
  • ☑️: The element is partially implemented and might not support most basic features of the element.
  • ❌: The element is currently not supported
  • ⚠️: The element is deprecated in the spec and has a low priority of getting implemented.
  • 🧪: The element is an experimental part of the svg 2.* spec. It may not fully behave as expected.

Shape and container elements

Element Status
a
circle
clipPath
defs
ellipse
foreignObject
g
image
line
marker
mask
path
polygon
polyline
rect
svg
symbol
use
view (:white_check_mark:)

Paint server elements

Element Status
linearGradient
🧪meshgradient
🧪meshrow
🧪meshpatch
pattern
radialGradient
solidColor
stop

Text elements

Element Status
text
textPath
⚠️tref
tspan

Animation elements

Element Status
animate
⚠️animateColor
animateMotion
animateTransform
mpath
set
switch

Filter elements

Element Status
feBlend
feColorMatrix
feComponentTransfer
feComposite
feConvolveMatrix
feDiffuseLighting
feDisplacementMap
feDistantLight
feFlood
feFuncA
feFuncB
feFuncG
feFuncR
feGaussianBlur
feImage
feMerge
feMergeNode
feMorphology
feOffset
fePointLight
feSpecularLighting
feSpotLight
feTile
feTurbulence
filter ☑️

Font elements

Element Status
⚠️altGlyph
⚠️altGlyphDef
⚠️altGlyphItem
⚠️font
⚠️font-face
⚠️font-face-format
⚠️font-face-name
⚠️font-face-src
⚠️font-face-uri
⚠️glyph
⚠️glyphRef
⚠️hkern
⚠️missing-glyph
⚠️vkern

Other elements

Element Status
desc (:white_check_mark:)
title (:white_check_mark:)
metadata (:white_check_mark:)
color-profile
⚠️cursor
script
style ☑️

About

Java SVG renderer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 99.8%
  • Kotlin 0.2%