本文整理汇总了Java中org.apache.batik.svggen.SVGGeneratorContext类的典型用法代码示例。如果您正苦于以下问题:Java SVGGeneratorContext类的具体用法?Java SVGGeneratorContext怎么用?Java SVGGeneratorContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SVGGeneratorContext类属于org.apache.batik.svggen包,在下文中一共展示了SVGGeneratorContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeSVG
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
private void writeSVG(OutputStream stream) throws IOException {
DOMImplementation impl = GenericDOMImplementation
.getDOMImplementation();
String svgNS = "http://www.w3.org/2000/svg";
Document myFactory = impl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(myFactory);
ctx.setEmbeddedFontsOn(Options.getBoolean("EMBEDDED_SVG_FONTS", true));
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, Options.getBoolean(
"EMBEDDED_SVG_FONTS", true));
svgGenerator.setSVGCanvasSize(size);
paint(svgGenerator, 0, 0);
boolean useCSS = true;
Writer out = new OutputStreamWriter(stream, "UTF-8");
svgGenerator.stream(out, useCSS);
}
示例2: generateSVG
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
/**
* Create a SVG image. The image handler will write all images files to
* "res/images".
*
* @param p the drawable component.
* @param outStream the the output stream.
* @throws UnsupportedEncodingException error on unsupported encoding.
* @throws SVGGraphics2DIOException error on error to conversion.
*/
private void generateSVG(Drawable p, OutputStream outStream) throws UnsupportedEncodingException, SVGGraphics2DIOException {
DOMImplementation domImpl
= GenericDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document myFactory = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx
= SVGGeneratorContext.createDefault(myFactory);
GenericImageHandler ihandler = new CachedImageHandlerPNGEncoder("res/images", null);
ctx.setGenericImageHandler(ihandler);
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
TurtleState ts = new TurtleState();
p.draw(svgGenerator, ts);
// Create the SVG DOM tree.
Writer out = new OutputStreamWriter(outStream, "UTF-8");
svgGenerator.stream(out, true);
}
示例3: createSvgGraphics2D
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
public SVGGraphics2D createSvgGraphics2D() {
DOMImplementation impl = GenericDOMImplementation
.getDOMImplementation();
String namespaceURI = SVGConstants.SVG_NAMESPACE_URI;
Document domFactory = impl.createDocument(namespaceURI, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory);
ctx.setEmbeddedFontsOn(true);
GraphicContextDefaults defaults = new GraphicContextDefaults();
defaults.setFont(new Font("Arial", Font.PLAIN, 12));
ctx.setGraphicContextDefaults(defaults);
ctx.setPrecision(12);
SVGGraphics2D g2d = new SVGGraphics2D(ctx, false);
// This prevents the
// "null incompatible with text-specific antialiasing enable key" error
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_DEFAULT);
return g2d;
}
示例4: saveToSVG
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
/**
* Parses a mathematical formula like "(a+b)/c" to a pretty image and saves
* it as an SVG file.
*
* @param formula A raw formula input String.
* @param file The SVG file to save to.
* @throws ParseException When parsing the LaTeX formula failed.
* @throws IOException When writing the file failed.
* @throws DetailedParseCancellationException When parsing the raw formula to
* LaTeX failed.
*/
public static void saveToSVG(String formula, File file)
throws ParseException, IOException, DetailedParseCancellationException {
String latexFormula = FormulaParser.parseToLatex(formula);
TeXIcon icon = FormulaParser.getTeXIcon(latexFormula);
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
Document document =domImpl.createDocument(
SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
SVGGraphics2D g2 = new SVGGraphics2D(ctx, true);
g2.setSVGCanvasSize(new Dimension(icon.getIconWidth(),icon.getIconHeight()));
icon.paintIcon(null, g2, 0, 0);
try (FileOutputStream svgs = new FileOutputStream(file)) {
Writer out = new OutputStreamWriter(svgs, "UTF-8");
g2.stream(out, false);
svgs.flush();
}
}
示例5: processSvg
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
private void processSvg(Collection<Image> images, OutputStream stream, DrawLayout layout) throws OutputException {
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext context = SVGGeneratorContext.createDefault(document);
context.setGraphicContextDefaults(new SVGGeneratorContext.GraphicContextDefaults());
// set default font
context.getGraphicContextDefaults().setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
// set antialiasing
Map<Key, Object> map = new HashMap<>();
map.put(RenderingHints.KEY_ANTIALIASING, Boolean.TRUE);
map.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
context.getGraphicContextDefaults().setRenderingHints(new RenderingHints(map));
SVGGraphics2D g2d = new SVGGraphics2D(context, false);
List<Dimension> sizes = this.getSizes(images, g2d);
Dimension size = this.getTotalSize(sizes, layout);
g2d.setSVGCanvasSize(size);
this.drawImages(sizes, images, g2d, layout);
// write to ouput - do not use css style
boolean useCSS = false;
try {
Writer out = new OutputStreamWriter(stream, "UTF-8");
g2d.stream(out, useCSS);
} catch (IOException e) {
throw new OutputException(e.getMessage(), e);
}
}
示例6: writeSVG
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
/**
* Writes the specified diagram editor as SVG.
* @param editor a DiagramEditor
* @param file the file to write to
* @throws IOException if error occurred
*/
public void writeSVG(DiagramEditor editor, File file) throws IOException {
FileOutputStream out = null;
OutputStreamWriter writer = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
Document doc =
domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
ctx.setComment("Generated by TinyUML with Batik SVG Generator");
ctx.setEmbeddedFontsOn(true);
SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);
g2d.setSVGCanvasSize(editor.getTotalCanvasSize());
editor.paintComponentNonScreen(g2d);
// write the file
File theFile = getFileWithExtension(file);
if (canWrite(editor, theFile)) {
out = new FileOutputStream(theFile);
writer = new OutputStreamWriter(out, "UTF-8");
g2d.stream(writer);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (writer != null) { writer.close(); }
if (out != null) { out.close(); }
}
}
示例7: export
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
public void export(JComponent component, File file) {
if (component == null || file == null)
throw new IllegalArgumentException();
String fileName = file.getAbsolutePath();
if (!fileName.endsWith(".svg"))
fileName += ".svg";
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation
.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document doc = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
ctx.setComment("Generated by MuLaViTo with Batik SVG");
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
// Prepare export: Exchange background color.
Color c = component.getBackground();
component.setBackground(Color.WHITE);
component.paint(svgGenerator);
component.setBackground(c);
// Modify export.
svgGenerator.setSVGCanvasSize(component.getSize());
// Export.
try {
svgGenerator.stream(fileName);
} catch (SVGGraphics2DIOException e) {
e.printStackTrace();
}
}
示例8: export
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
public void export(JComponent component, File file) {
if (component == null || file == null)
throw new IllegalArgumentException();
String fileName = file.getAbsolutePath();
if (!fileName.endsWith(".svg"))
fileName += ".svg";
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation
.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document doc = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
ctx.setComment("Generated by MuLaViTo with Batik SVG");
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
// Prepare export: Exchange background color.
Color c = component.getBackground();
component.setBackground(Color.WHITE);
component.paint(svgGenerator);
component.setBackground(c);
// Modify export.
svgGenerator.setSVGCanvasSize(component.getSize());
// Export.
try {
svgGenerator.stream(fileName);
} catch (SVGGraphics2DIOException e) {
e.printStackTrace();
}
}
示例9: setStyle
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
@Override
public void setStyle(Element element, Map styleMap, SVGGeneratorContext generatorContext)
{
super.setStyle(element, styleMap, generatorContext);
if ("text".equals(element.getNodeName()))
{
element.setAttribute("class", "svg_word");
Element parentG = (Element)element.getParentNode();
parentG.setAttribute("class", "svg_g");
parentG.setAttribute("onmousedown", "selectElement(evt)");
parentG.setAttribute("oncontextmenu", "showContextMenu(evt, this)");
parentG.setAttribute("id", "g_" + id++);
}
}
示例10: createSVG
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
public static byte[] createSVG(WordCloudRenderer renderer, SVGTextStyleHandler styleHandler)
{
// Create an instance of org.w3c.dom.Document.
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
// Configure the SVGGraphics2D
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
ctx.setStyleHandler(styleHandler);
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
// rendering the cloud
renderer.render(svgGenerator);
SVGSVGElement root = (SVGSVGElement)svgGenerator.getRoot();
styleHandler.postRenderAction(root);
try
{
Writer writer = new StringWriter();
svgGenerator.stream(root, writer);
writer.close();
return writer.toString().getBytes("UTF-8");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
示例11: SVGGraphics
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
/**
* Create a new SVGGraphics.
*/
public SVGGraphics() {
// create a DOM document
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder buidler;
try {
buidler = factory.newDocumentBuilder();
} catch (final ParserConfigurationException e) {
LOGGER.error("Unable to create a DocumentBuilder {}", e);
throw new RuntimeException("Unable to create a Document Builder");
}
final DOMImplementation dom = buidler.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
final String svgNS = "http://www.w3.org/2000/svg";
final Document document = dom.createDocument(svgNS, "svg", null);
// Reuse our embedded base64-encoded image data.
final SVGGeneratorContext context = SVGGeneratorContext.createDefault(document);
final GenericImageHandler ihandler = new CachedImageHandlerBase64Encoder() {
@Override
public void encodeImage(final BufferedImage buf, final OutputStream os) throws IOException {
final Base64EncoderStream b64Encoder = new Base64EncoderStream(os);
ImageIO.write(buf, "PNG", b64Encoder);
b64Encoder.close();
}
};
context.setGenericImageHandler(ihandler);
// Create an instance of the SVG Generator.
svg2d = new SVGGraphics2D(context, false);
driver = new Java2DDriver(svg2d);
}
示例12: convert
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
public void convert(Node node, LayoutContext ctx, OutputStream out) throws IOException
{
Document document;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e1) {
throw new IOException(e1);
}
document.appendChild(document.createElementNS("http://www.w3.org/2000/svg", "svg"));
SVGGeneratorContext svgCtx = SVGGeneratorContext.createDefault(document);
svgCtx.setComment("Converted from MathML using JEuclid");
SVGGraphics2D graphics = new SVGGraphics2D(svgCtx, true);
JEuclidView view = new JEuclidView(node, ctx, graphics);
int ascentHeight = (int) Math.ceil((double) view.getAscentHeight());
int descentHeight = (int) Math.ceil((double) view.getDescentHeight());
int height = ascentHeight + descentHeight;
int width = (int) Math.ceil((double) view.getWidth());
graphics.setSVGCanvasSize(new Dimension(width, height));
view.draw(graphics, 0.0F, (float) ascentHeight);
document.replaceChild(graphics.getRoot(), document.getFirstChild());
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
} catch (TransformerException e) {
throw new IOException(e);
}
}
示例13: convert
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
/** {@inheritDoc} */
public Image convert(Image src, Map hints) throws ImageException {
checkSourceFlavor(src);
ImageGraphics2D g2dImage = (ImageGraphics2D)src;
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
Document document = domImpl.createDocument(
SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
Element root = document.getDocumentElement();
// Create an SVGGeneratorContext to customize SVG generation
SVGGeneratorContext genCtx = SVGGeneratorContext.createDefault(document);
genCtx.setComment("Generated by Apache Batik's SVGGraphics2D");
genCtx.setEmbeddedFontsOn(true);
// Create an instance of the SVG Generator
SVGGraphics2D g2d = new SVGGraphics2D(genCtx, true);
ImageSize size = src.getSize();
Dimension dim = size.getDimensionMpt();
g2d.setSVGCanvasSize(dim);
//SVGGraphics2D doesn't generate the viewBox by itself
root.setAttribute("viewBox", "0 0 " + dim.width + " " + dim.height);
g2dImage.getGraphics2DImagePainter().paint(g2d,
new Rectangle2D.Float(0, 0, dim.width, dim.height));
//Populate the document root with the generated SVG content.
g2d.getRoot(root);
//Return the generated SVG image
ImageXMLDOM svgImage = new ImageXMLDOM(src.getInfo(), document, BatikImageFlavors.SVG_DOM);
g2d.dispose();
return svgImage;
}
示例14: renderPage
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
/** {@inheritDoc} */
public void renderPage(PageViewport pageViewport) throws IOException {
log.debug("Rendering page: " + pageViewport.getPageNumberString());
// Get a DOMImplementation
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
this.document = domImpl.createDocument(null, "svg", null);
// Create an SVGGeneratorContext to customize SVG generation
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(this.document);
ctx.setComment("Generated by " + userAgent.getProducer() + " with Batik SVG Generator");
ctx.setEmbeddedFontsOn(true);
// Create an instance of the SVG Generator
this.svgGenerator = new SVGGraphics2D(ctx, true);
Rectangle2D viewArea = pageViewport.getViewArea();
Dimension dim = new Dimension();
dim.setSize(viewArea.getWidth() / 1000, viewArea.getHeight() / 1000);
this.svgGenerator.setSVGCanvasSize(dim);
AffineTransform at = this.svgGenerator.getTransform();
this.state = new Java2DGraphicsState(this.svgGenerator, this.fontInfo, at);
try {
//super.renderPage(pageViewport);
renderPageAreas(pageViewport.getPage());
} finally {
this.state = null;
}
writeSVGFile(pageViewport.getPageIndex());
this.svgGenerator = null;
this.document = null;
}
示例15: toSVGColor
import org.apache.batik.svggen.SVGGeneratorContext; //导入依赖的package包/类
public static String toSVGColor(Document document, Color color) {
java.awt.Color awtColor = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
SVGGeneratorContext svgContext = SVGGeneratorContext.createDefault(document);
SVGPaintDescriptor paint = SVGColor.toSVG(awtColor, svgContext);
return paint.getPaintValue();
}