📄 svgmodeldigester.java
字号:
IFlowletBind sourceFlowletBind =
(IFlowletBind) flowletIdToFlowletBindMapping.get(sourceFlowletRef);
net.orthanc.flow4j.base.Point sourceOffset =
AnchorRegistry.getSourceAnchorOffset(
sourceFlowletBind.getType(),
source.getAnchor());
Point p1 =
new Point(
sourceFlowletBind.getX() + sourceOffset.x,
sourceFlowletBind.getY() + sourceOffset.y);
String targetFlowletRef = target.getType() + "_" + target.getIndex();
IFlowletBind targetFlowletBind =
(IFlowletBind) flowletIdToFlowletBindMapping.get(targetFlowletRef);
net.orthanc.flow4j.base.Point targetOffset =
AnchorRegistry.getTargetAnchorOffset(
targetFlowletBind.getType(),
target.getAnchor());
Point p2 =
new Point(
targetFlowletBind.getX() + targetOffset.x,
targetFlowletBind.getY() + targetOffset.y);
List transitionPoints = new ArrayList();
transitionPoints.add(p1);
// digest the bendpoints
digest(transitionBind.getBendpoints(), transitionPoints);
transitionPoints.add(p2);
drawTransition(svgGenerator, transitionPoints);
return transitionPoints;
}
/**
*
* @param bendpointsBind
* @param transitionPoints
*/
private void digest(List bendpoints, List transitionPoints) {
if (bendpoints == null)
return;
for (Iterator iter = bendpoints.iterator(); iter.hasNext();) {
BendpointBind bendpointBind = (BendpointBind) iter.next();
Point point = new Point(bendpointBind.getX(), bendpointBind.getY());
adjustCanvasSize(point);
transitionPoints.add(point);
}
}
/**
* Draws the transition with it's bendpoints, if there are any.
* @param transitionPoints
*/
private void drawTransition(SVGGraphics2D g, List transitionPoints) {
int countPoints = transitionPoints.size();
int[] x = new int[countPoints];
int[] y = new int[countPoints];
int counter = 0;
for (Iterator iter = transitionPoints.iterator();
iter.hasNext();
counter++) {
Point point = (Point) iter.next();
x[counter] = point.x;
y[counter] = point.y;
}
// draw polyLine
g.setStroke(SINGLE_STROKE);
g.setPaint(Color.BLACK);
g.drawPolyline(x, y, transitionPoints.size());
// draw arrow decoration
drawArrowDecorationPolygon(
g,
(Point) transitionPoints.get(countPoints - 2),
(Point) transitionPoints.get(countPoints - 1));
}
/**
* Draws the Arrow decoration.
* @param g
* @param p1
* @param p2
*/
private void drawArrowDecorationPolygon(
SVGGraphics2D g,
Point p1,
Point p2) {
double Dx = p2.getX() - p1.getX();
double Dy = p2.getY() - p1.getY();
// length of line
double L = Math.sqrt(Dx * Dx + Dy * Dy);
// 10 pt from end
int pt_from_end = 8;
double dx = Dx * pt_from_end / L;
double dy = Dy * pt_from_end / L;
// begin arrow
double pB_x = p2.getX() - dx;
double pB_y = p2.getY() - dy;
// perpendicular
double pDx = Dy;
double pDy = -Dx;
int pt_perp = 3;
double pdx = pDx * pt_perp / L;
double pdy = pDy * pt_perp / L;
// points of the arrow
Point p3 = new Point((int) (pB_x + pdx), (int) (pB_y + pdy));
Point p4 = new Point((int) (pB_x - pdx), (int) (pB_y - pdy));
Polygon poly = new Polygon();
poly.addPoint((int) p2.getX(), (int) p2.getY());
poly.addPoint((int) p3.getX(), (int) p3.getY());
poly.addPoint((int) p4.getX(), (int) p4.getY());
g.fillPolygon(poly);
}
/**
*
* TODO
* @param label
*/
private void drawLabel(SVGGraphics2D g, FlowletLabelBind label) {
int x = label.getX();
int y = label.getY();
String text = label.getText();
drawLabel(g, text, x, y, Color.BLACK);
}
/**
*
* @param g
* @param text
* @param x
* @param y
* @param color
*/
private void drawLabel(
SVGGraphics2D g,
String text,
int x,
int y,
Color color) {
g.setPaint(color);
// y is text's baseline
int pt_y_corr = -5;
y += g.getFontMetrics().getHeight() + pt_y_corr;
((AbstractGraphics2D) g).drawString(text, x, y);
Point bottomRight =
new Point(x + g.getFontMetrics().stringWidth(text), y);
adjustCanvasSize(bottomRight);
}
/**
*
* TODO
* @param useCSS
* @return
*/
public String getSVGXML(boolean useCSS)
throws SVGGraphics2DIOException, IOException {
//Writer out = new OutputStreamWriter(System.out, "UTF-8");
StringWriter writer = new StringWriter();
writeSVGXML(writer, useCSS);
writer.close();
return writer.toString();
}
/**
* TODO
* @param useCSS
* @return
* @throws SVGGraphics2DIOException
*/
public InputStream getSVGXMLStream(boolean useCSS)
throws SVGGraphics2DIOException, IOException {
return new ByteArrayInputStream(getSVGXMLBytes(useCSS));
}
/**
*
* TODO
* @param useCSS
* @return
* @throws SVGGraphics2DIOException
* @throws IOException
*/
public byte[] getSVGXMLBytes(boolean useCSS)
throws SVGGraphics2DIOException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writeSVGXML(writer, useCSS);
writer.close();
return out.toByteArray();
}
/**
* Writes the SVG XML code to the writer.
* Does not close the stream.
* @param writer
* @param useCSS
* @throws SVGGraphics2DIOException
*/
private void writeSVGXML(Writer writer, boolean useCSS)
throws SVGGraphics2DIOException {
svgGenerator.setSVGCanvasSize(
new Dimension(canvasSize.width + 5, canvasSize.height + 5));
svgGenerator.stream(writer, useCSS);
}
/**
*
* TODO
* @param useCSS
* @param transcoderInput
* @return
* @throws SVGGraphics2DIOException
* @throws TranscoderException
*/
private InputStream getSVGTranscodedStream(
boolean useCSS,
Transcoder transcoder,
TranscoderInput transcoderInput)
throws SVGGraphics2DIOException, TranscoderException, IOException {
// create the transcoder output
ByteArrayOutputStream out = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(out);
// save
transcoder.transcode(transcoderInput, output);
// flush and close the stream then exit
out.flush();
out.close();
return new ByteArrayInputStream(out.toByteArray());
}
/**
*
* TODO
* @param useCSS
* @return
* @throws SVGGraphics2DIOException
* @throws TranscoderException
* @throws IOException
*/
public InputStream getSVGJPEGStream(boolean useCSS)
throws SVGGraphics2DIOException, TranscoderException, IOException {
return getSVGJPEGStream(getSVGXMLStream(useCSS), useCSS);
}
/**
*
* Closes the xml stream.
* @param SVGXMLStream
* @param useCSS
* @return
* @throws SVGGraphics2DIOException
* @throws TranscoderException
* @throws IOException
*/
public InputStream getSVGJPEGStream(
InputStream SVGXMLStream,
boolean useCSS)
throws SVGGraphics2DIOException, TranscoderException, IOException {
// create a JPEG transcoder
JPEGTranscoder t = new JPEGTranscoder();
// set the transcoding hints
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));
//t.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME, saxParser.getClass().getName());
try {
// get XML parser from jaxp
String xmlReaderClassName = SAXParserFactory.newInstance().newSAXParser().getXMLReader().getClass().getName();
t.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME, xmlReaderClassName);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// t.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, new Float(1000.0));
// create the transcoder input
TranscoderInput input = new TranscoderInput(SVGXMLStream);
SVGXMLStream.close();
return getSVGTranscodedStream(useCSS, t, input);
}
/**
*
* TODO
* @param useCSS
* @return
* @throws SVGGraphics2DIOException
* @throws TranscoderException
* @throws IOException
*/
public InputStream getSVGPDFStream(boolean useCSS)
throws SVGGraphics2DIOException, TranscoderException, IOException {
return getSVGPDFStream(getSVGXMLStream(useCSS), useCSS);
}
/**
*
* Closes the xml stream.
* @param useCSS
* @return
* @throws SVGGraphics2DIOException
* @throws TranscoderException
* @throws IOException
*/
public InputStream getSVGPDFStream(
InputStream SVGXMLStream,
boolean useCSS)
throws SVGGraphics2DIOException, TranscoderException, IOException {
// create a PDF transcoder
PDFTranscoder t = new PDFTranscoder();
// set the transcoding hints
// t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(.8));
// create the transcoder input
TranscoderInput input = new TranscoderInput(SVGXMLStream);
SVGXMLStream.close();
return getSVGTranscodedStream(useCSS, t, input);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -