use of org.w3c.dom.svg.SVGSVGElement in project yamcs-studio by yamcs.
the class SimpleImageTranscoder method applyMatrix.
private Document applyMatrix(double[][] matrix) {
// creation of the SVG document
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
final Document newDocument = impl.createDocument(svgNS, "svg", null);
// get the root element (the 'svg' element).
Element svgRoot = newDocument.getDocumentElement();
// get the original document size
SVGSVGElement svgElmt = ((SVGOMDocument) originalDocument).getRootElement();
double width = 30;
double height = 30;
try {
width = svgElmt.getWidth().getBaseVal().getValue();
height = svgElmt.getHeight().getBaseVal().getValue();
} catch (NullPointerException e) {
// FIXME
// this is a dirty workaround for the RAP problem, which doesn't know how to
// transform between units and pixels. Here we assume that all units are inches
// and 96 dpi is used.
SVGLength length = svgElmt.getWidth().getBaseVal();
double value = length.getValueInSpecifiedUnits();
width = value * 25.4 / 0.26458333333333333333333333333333;
length = svgElmt.getHeight().getBaseVal();
value = length.getValueInSpecifiedUnits();
height = value * 25.4 / 0.26458333333333333333333333333333;
}
// current Transformation Matrix
double[][] CTM = { { matrix[0][0], matrix[0][1], 0 }, { matrix[1][0], matrix[1][1], 0 }, { 0, 0, 1 } };
// apply permutation to viewBox corner points
double[] a = transformP(0.0, 0.0, 1.0, CTM);
double[] b = transformP(width, 0.0, 1.0, CTM);
double[] c = transformP(width, height, 1.0, CTM);
double[] d = transformP(0.0, height, 1.0, CTM);
// find new points
double minX = findMin(a[0], b[0], c[0], d[0]);
double minY = findMin(a[1], b[1], c[1], d[1]);
double maxX = findMax(a[0], b[0], c[0], d[0]);
double maxY = findMax(a[1], b[1], c[1], d[1]);
double newWidth = maxX - minX;
double newHeight = maxY - minY;
// set the width and height attributes on the root 'svg' element.
svgRoot.setAttributeNS(null, "width", String.valueOf(newWidth));
svgRoot.setAttributeNS(null, "height", String.valueOf(newHeight));
String vbs = minX + " " + minY + " " + newWidth + " " + newHeight;
svgRoot.setAttributeNS(null, "viewBox", vbs);
svgRoot.setAttributeNS(null, "preserveAspectRatio", "none");
// Create the transform matrix
StringBuilder sb = new StringBuilder();
// a c e
// b d f
// 0 0 1
sb.append("matrix(");
sb.append(CTM[0][0] + ",");
sb.append(CTM[1][0] + ",");
sb.append(CTM[0][1] + ",");
sb.append(CTM[1][1] + ",");
sb.append(CTM[0][2] + ",");
sb.append(CTM[1][2] + ")");
Element graphic = newDocument.createElementNS(svgNS, "g");
graphic.setAttributeNS(null, "transform", sb.toString());
// Attach the transform to the root 'svg' element.
Node copiedRoot = newDocument.importNode(originalDocument.getDocumentElement(), true);
graphic.appendChild(copiedRoot);
svgRoot.appendChild(graphic);
// }
return newDocument;
}
use of org.w3c.dom.svg.SVGSVGElement in project yamcs-studio by yamcs.
the class SVGHandler method doRender.
protected void doRender() {
if (disposed) {
return;
}
updateMatrix();
changeColor(colorToChange, colorToApply);
gvtRoot = builder.build(bridgeContext, svgDocument);
// get the 'width' and 'height' attributes of the SVG document
float width = 400, height = 400;
float docWidth = (float) bridgeContext.getDocumentSize().getWidth();
float docHeight = (float) bridgeContext.getDocumentSize().getHeight();
if (canvasWidth > 0 && canvasHeight > 0) {
width = canvasWidth;
height = canvasHeight;
} else if (canvasHeight > 0) {
width = (docWidth * canvasHeight) / docHeight;
height = canvasHeight;
} else if (canvasWidth > 0) {
width = canvasWidth;
height = (docHeight * canvasWidth) / docWidth;
} else {
width = docWidth;
height = docHeight;
}
// compute the preserveAspectRatio matrix
AffineTransform renderingTransform = null;
AffineTransform Px = null;
SVGSVGElement root = svgDocument.getRootElement();
String viewBox = root.getAttributeNS(null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE);
if (viewBox != null && viewBox.length() != 0) {
String aspectRatio = root.getAttributeNS(null, SVGConstants.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE);
Px = ViewBox.getPreserveAspectRatioTransform(root, viewBox, aspectRatio, width, height, bridgeContext);
} else {
// no viewBox has been specified, create a scale transform
float xscale = width / docWidth;
float yscale = height / docHeight;
float scale = Math.min(xscale, yscale);
Px = AffineTransform.getScaleInstance(scale, scale);
}
Shape curAOI = new Rectangle2D.Float(0, 0, width, height);
CanvasGraphicsNode cgn = getCanvasGraphicsNode(gvtRoot);
if (cgn != null) {
cgn.setViewingTransform(Px);
renderingTransform = new AffineTransform();
} else {
renderingTransform = Px;
}
if (renderer != null) {
renderer.dispose();
renderer = null;
}
renderer = createImageRenderer();
int w = (int) (curAOI.getBounds().width + 0.5);
int h = (int) (curAOI.getBounds().height + 0.5);
renderer.updateOffScreen(w, h);
renderer.setTree(gvtRoot);
renderer.setTransform(renderingTransform);
renderer.setDoubleBuffered(false);
renderer.clearOffScreen();
renderer.repaint(curAOI);
if (updateManager != null) {
updateManager.setGVTRoot(gvtRoot);
}
needRender = false;
}
use of org.w3c.dom.svg.SVGSVGElement in project yamcs-studio by yamcs.
the class SimpleImageTranscoder method getDocumentSize.
public Dimension getDocumentSize() {
SVGSVGElement svgElmt = ((SVGOMDocument) document).getRootElement();
double width = svgElmt.getWidth().getBaseVal().getValue();
double height = svgElmt.getHeight().getBaseVal().getValue();
return new Dimension((int) Math.round(width), (int) Math.round(height));
}
Aggregations