All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lightgraph.painters.SvgPainter Maven / Gradle / Ivy

The newest version!
package lightgraph.painters;

import lightgraph.LGFont;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * New imagej plugin that ...
 * User: mbs207
 * Date: 1/6/12
 * Time: 10:27 AM
 */
public class SvgPainter implements GraphPainter{
    StringBuilder OUTPUT;
    Color COLOR;
    boolean CLIPPING = false;
    double LINE_WIDTH=1;
    float[] DASHES;
    Rectangle clip;
    String FILL="none";
    Color BACKGROUND;
    FontMetrics metrics;
    boolean FINISHED=false;
    LGFont font = new LGFont("Arial", "Bold", 12);
    static final String DOCTYPE = "\n";
    static final String XML = "\n";
    static final String SVG_TAG = "\n";

    /**
     * Creates a new svg painter,which will build a large string containing an svg file to be
     * written to file when completed.
     *
     * @param height of output graph in px
     * @param width of output graph in px
     * @param background background color, primarily used for filling hollow strokes.
     */
    public SvgPainter(int height, int width, Color background){

        OUTPUT = new StringBuilder();
        OUTPUT.append(XML);
        OUTPUT.append(DOCTYPE);
        String dec = String.format(SVG_TAG,width*1f,height*1f,width,height);
        OUTPUT.append(dec);
        BACKGROUND=background;
        this.metrics = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB).getGraphics().getFontMetrics();
    }


    public static String svgColorString(Color c){
        String red = Integer.toString(c.getRed(),16);
        if(red.length()==1)
            red = "0" + red;

        String green = Integer.toString(c.getGreen(),16);
        if(green.length()==1)
            green = "0" + green;

        String blue = Integer.toString(c.getBlue(),16);
        if(blue.length()==1)
            blue = "0" + blue;
        return MessageFormat.format("#{0}{1}{2}", red, green, blue);
    }

    /**
     * For drawing arbitrary shapes, This will fill with the background color if
     * fill is set to true.
     *
     * @param s shape to be drawn.
     */
    public void drawPath(Shape s) {
        Rectangle r = s.getBounds();

        if(CLIPPING&&!(r.intersects(clip)||clip.contains(r.getX(), r.getY())))
            return;

        OUTPUT.append("\n",LINE_WIDTH));

    }

    public void setColor(Color c) {
        COLOR = c;
    }

    /**
     * For drawing plain lines. Creates a line element.
     *
     * @param x0
     * @param y0
     * @param x1
     * @param y1
     */
    public void drawLine(double x0, double y0, double x1, double y1) {
        if(CLIPPING){
            double x,w;
            if(x0\n",LINE_WIDTH));
    }

    /**
     * Filling an arbitrary shape with the forground color.
     *
     * @param s
     */
    public void fill(Shape s) {
        Rectangle r = s.getBounds();

        if(CLIPPING&&!r.intersects(clip))
            return;

        OUTPUT.append("\n");
    }
    public void setClip(int x, int y, int w, int h){

        CLIPPING = true;
        clip = new Rectangle(x,y,w,h);
        OUTPUT.append("\n");
        OUTPUT.append("    \n");
        OUTPUT.append(MessageFormat.format("    ", x, y, w, h));
        OUTPUT.append("    \n");
        OUTPUT.append("");
        OUTPUT.append("\n");

    }

    public void drawString(String s, double x, double y) {
        String tag = MessageFormat.format(
            "{2}\n",
            x, y, s, font.getName(), font.getSize(), font.getStyle(), svgColorString(COLOR)
        );
        OUTPUT.append(tag);
    }

    public void finish(File f){
        finish();

        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            bw.write(OUTPUT.toString());
            bw.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void finish(){
        if(FINISHED) return;

        FINISHED=true;
        if(CLIPPING)
            OUTPUT.append("\n");
        OUTPUT.append("");
    }

    public String getOutput(){
        if(!FINISHED) finish();
        return OUTPUT.toString();
    }

    public Color getColor(){
        return COLOR;
    }

    public void setLineWidth(double d){
        LINE_WIDTH=d;

    }

    public void restoreLineWidth(){
        LINE_WIDTH=1;
    }

    /**
     *
     *
     * @param dashes length/offest combo's for formatting line dashes. set to null for none.
     */
    public void setDashes(float[] dashes){
        DASHES = dashes;
    }

    public void setFill(boolean fill){
        if(fill){
            FILL=svgColorString(BACKGROUND);
        } else{
            FILL="none";
        }
    }

    public void startGroup() {
        OUTPUT.append("\n");
    }

    public void endGroup() {
        OUTPUT.append("\n");
    }

    @Override
    public void setFont(LGFont font) {
        this.font = font;
    }

    @Override
    public int getStringWidth(String label) {
        String stripped = stripSvgTags(label);
        return metrics.stringWidth(stripped);
    }

    String stripSvgTags(String tagged){
        return tagged.replaceAll("<[^>]*>", "");
    }

    @Override
    public void drawVerticalString(String s, double x, double y) {

        String tag = MessageFormat.format(
            "{2}\n",
            x, y, s, font.getName(), font.getSize(), font.getStyle(), svgColorString(COLOR)
        );
        OUTPUT.append(tag);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy