Java源码示例:org.apache.batik.anim.dom.SVGDOMImplementation
示例1
public static void writeToSVG(JComponent panel, File fileName) throws IOException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getWidth();
logger.info(
() -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
// Get a DOMImplementation
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
svgGenerator.setSVGCanvasSize(new Dimension(width, height));
panel.print(svgGenerator);
boolean useCSS = true; // we want to use CSS style attribute
try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
svgGenerator.stream(out, useCSS);
}
}
示例2
private boolean clip(Matrix matrix, PDImageXObject pdImageXObject) throws IOException {
if (clipTypes.isEmpty() || (clipTypes.size() == 1 && clipTypes.get(0).equals("rect")
&& Math.abs(matrix.getScaleX() / matrix.getScalingFactorY()
- (clipArea[2] - clipArea[0]) / (clipArea[3] - clipArea[1])) <= 0.01D))
return false;
SVGGraphics2D svgGraphics2D = new SVGGraphics2D(GenericDOMImplementation.getDOMImplementation()
.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null));
if (pdImageXObject != null) {
int w = (int) matrix.getScalingFactorX();
int h = (int) matrix.getScalingFactorY();
svgGraphics2D.clip(getPath(clipTypes, clipPoints, clipArea));
svgGraphics2D.drawImage(pdImageXObject.getImage().getScaledInstance(w, h, Image.SCALE_SMOOTH),
(int) (matrix.getTranslateX() - clipArea[0]), (int) (matrix.getTranslateY() - clipArea[1]), w, h, null);
}
save(svgGraphics2D, clipArea[0], clipArea[1], (clipArea[2] - clipArea[0]), (clipArea[3] - clipArea[1]));
return true;
}
示例3
public static void writeToSVG(JComponent panel, File fileName) throws IOException {
// print the panel to pdf
int width = panel.getWidth();
int height = panel.getWidth();
logger.info(
() -> MessageFormat.format("Exporting panel to SVG file (width x height; {0} x {1}): {2}",
width, height, fileName.getAbsolutePath()));
// Get a DOMImplementation
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
svgGenerator.setSVGCanvasSize(new Dimension(width, height));
panel.print(svgGenerator);
boolean useCSS = true; // we want to use CSS style attribute
try (Writer out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8")) {
svgGenerator.stream(out, useCSS);
}
}
示例4
public static Object _loadSvg( final Context cx, final Scriptable thisObj,
final Object[] args, final Function funObj ) {
final String file = args[ 0 ].toString();
try {
final BaseScope scope = (BaseScope) thisObj;
final String parser = "org.apache.xerces.parsers.SAXParser"; //XMLResourceDescriptor.getXMLParserClassName();
final SAXSVGDocumentFactory f = new SAXSVGDocumentFactory( parser );
final String uri = "file:" + scope.basePath + "/" + file;
final SVGOMDocument doc = (SVGOMDocument) f.createDocument( uri );
// Initialize the CSS Engine for the document
final SVGDOMImplementation impl = (SVGDOMImplementation) SVGDOMImplementation.getDOMImplementation();
final UserAgent userAgent = new UserAgentAdapter();
final BridgeContext ctx = new BridgeContext( userAgent, new DocumentLoader( userAgent ) );
doc.setCSSEngine( impl.createCSSEngine( doc, ctx ) );
return Context.javaToJS( doc, scope );
} catch ( Exception e ) {
e.printStackTrace();
logger.error( e );
return Context.getUndefinedValue();
}
}
示例5
private void parseGeometry(ReaderContext readerContext, XSLFSimpleShape xslfSimpleShape, JSONObject shape) throws IOException {
SVGGraphics2D svgGraphics2D = new SVGGraphics2D(GenericDOMImplementation.getDOMImplementation()
.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null));
Rectangle2D rectangle2D = xslfSimpleShape.getAnchor();
xslfSimpleShape.draw(svgGraphics2D, new Rectangle2D.Double(0.0D, 0.0D, rectangle2D.getWidth(), rectangle2D.getHeight()));
Element root = svgGraphics2D.getRoot();
double[] viewBox = getViewBox(xslfSimpleShape);
root.setAttribute("viewBox", "0 0 " + (viewBox == null ? (rectangle2D.getWidth() + " " + rectangle2D.getHeight())
: (viewBox[0] + " " + viewBox[1])));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
svgGraphics2D.stream(root, new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), false, false);
svgGraphics2D.dispose();
outputStream.flush();
outputStream.close();
String svg = outputStream.toString().trim()
.replaceAll("\\s+", " ")
.replaceAll(" >", ">")
.replaceAll("> <", "><")
.replaceAll("<text [^>]+>[^<]*</text>", "")
.replaceAll("<g [^>]+></g>", "");
if (!svg.contains("<path "))
return;
ByteArrayInputStream inputStream = new ByteArrayInputStream(svg.getBytes());
String geometry = readerContext.getMediaWriter().write(MediaType.Svg, "geometry.svg", inputStream);
inputStream.close();
shape.put("geometry", geometry);
}
示例6
public static Document inkValueToSvg(Integer width, Integer height, byte[]... inkValues) throws Exception {
if (inkValues != null && inkValues.length > 0) {
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = impl.createDocument(svgNS, "svg", null);
// Get the root element (the 'svg' element).
Element svgRoot = doc.getDocumentElement();
// Set the width and height attributes on the root 'svg' element.
svgRoot.setAttributeNS(null, "width", Integer.toString(width)); //defaults to 400
svgRoot.setAttributeNS(null, "height", Integer.toString(height)); //defaults to 400
for (int i = 0; i < inkValues.length; i++) {
String inkJson = inkValueToString(inkValues[i]);
if (inkJson != null) {
JsonElement strokes = new JsonParser().parse(inkJson);
Iterator<JsonElement> strokesIt = strokes.getAsJsonArray().iterator();
while (strokesIt.hasNext()) {
JsonObject stroke = strokesIt.next().getAsJsonObject();
// Create the rectangle.
Element path = doc.createElementNS(svgNS, "path");
StringBuilder d = new StringBuilder();
getSvgPath(stroke.get("path"), d);
if (d.length() > 0) {
path.setAttributeNS(null, "d", d.toString());
stroke.remove("path");
Iterator<Entry<String, JsonElement>> strokeAttributesIt = stroke.entrySet().iterator();
while (strokeAttributesIt.hasNext()) {
Entry<String, JsonElement> strokeAttribute = strokeAttributesIt.next();
if (strokeAttribute.getValue().isJsonPrimitive()) {
path.setAttributeNS(null, strokeAttribute.getKey(), strokeAttribute.getValue().getAsString());
}
}
// rectangle.setAttributeNS(null, "y", "20");
// rectangle.setAttributeNS(null, "width", "100");
// rectangle.setAttributeNS(null, "height", "50");
// rectangle.setAttributeNS(null, "fill", "red");
// Attach the rectangle to the root 'svg' element.
svgRoot.appendChild(path);
}
}
}
}
return doc;
}
return null;
}