1// BEGIN LICENSE BLOCK
2// Version: CMPL 1.1
3//
4// The contents of this file are subject to the Cisco-style Mozilla Public
5// License Version 1.1 (the "License"); you may not use this file except
6// in compliance with the License.  You may obtain a copy of the License
7// at www.eclipse-clp.org/license.
8//
9// Software distributed under the License is distributed on an "AS IS"
10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11// the License for the specific language governing rights and limitations
12// under the License.
13//
14// The Original Code is  CPViz Constraint Visualization System
15// The Initial Developer of the Original Code is  Helmut Simonis
16// Portions created by the Initial Developer are
17// Copyright (C) 2009-2010 Helmut Simonis
18//
19// Contributor(s): 	Helmut Simonis, 4C, Univerity College Cork, Cork
20//
21//
22// END LICENSE BLOCK
23// ----------------------------------------------------------------------
24package ie.ucc.cccc.viz;
25
26import java.io.File;
27import java.io.FileWriter;
28import java.io.IOException;
29import java.io.PrintWriter;
30
31
32/**
33 * Utility class to describe the space needed for the coordinate system of an SVG file.
34 * Based on x,y,w,h integer values
35 * @author hsimonis
36 *
37 */
38public class Box {
39	private static int scaleSVG=100;
40	private int x,y,width,height;
41
42	public String toString() {
43		return "<Box X"+x+" Y "+y+" Width "+width+" Height "+height+">";
44	}
45	public Box(int x,int y, int width, int height){
46		this.x = x;
47		this.y = y;
48		this.width = width;
49		this.height = height;
50	}
51
52	public int getX() {
53		return x;
54	}
55	public int getY() {
56		return y;
57	}
58	public int getWidth() {
59		return width;
60	}
61	public int getHeight() {
62		return height;
63	}
64
65	/**
66	 * Expand the current box to also include Box box.
67	 * @param box Box
68	 */
69	public void expandBox(Box box) {
70		int x1 = Math.min(x,box.x);
71		int y1 = Math.min(y,box.y);
72		int x2 = Math.max(x+width,box.x+box.width);
73		int y2 = Math.max(y+height,box.y+box.height);
74		width = x2-x1;
75		height = y2-y1;
76		x = x1;
77		y = y1;
78	}
79
80	/**
81	 * code to generate the XML preamble for SVG files
82	 * @param tool Tool, describes the settings for the output
83	 * @param name String, the constructed filename for output
84	 * @return the file descriptor to write the SVG content
85	 */
86	public PrintWriter svgPrefix(Tool tool,String name) {
87		try{
88			File f = new File(tool.getDirectory(),name);
89			PrintWriter out = new PrintWriter(new FileWriter(f));
90			out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
91			out.println("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"");
92			out.println(" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
93			out.println("<svg");
94			out.println("   xmlns:svg=\"http://www.w3.org/2000/svg\"");
95			out.println("   xmlns=\"http://www.w3.org/2000/svg\"");
96			out.println("   version=\"1.1\"");
97			out.println("   width=\""+tool.getWidth()+"px\"");
98			out.println("   height=\""+tool.getHeight()+"px\"");
99			int width1 = width;
100			int height1 = height;
101			out.println("   viewBox=\""+scaleSVG*x+" "+scaleSVG*y+
102					" "+scaleSVG*width1+" "+scaleSVG*height1+"\">");
103			return out;
104		} catch (IOException e) {
105			System.out.println("no luck");
106			return null;
107		}
108
109	}
110
111	/**
112	 * Code to generate the final closing element of an SVG file and close
113	 * the file descriptor. This is placed here for symmetry with the svgPrefix method,
114	 * there is no other connection to the Box class
115	 * @param out file descriptor
116	 */
117	public void svgPostfix(PrintWriter out){
118		out.println("</svg>");
119		out.close();
120	}
121}
122