Changeset 32906 in osm for applications/editors/josm/plugins/seachart/jicons/src
- Timestamp:
- 2016-09-03T16:18:15+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/jicons/src/jicons/Jicons.java
r32380 r32906 37 37 38 38 public class Jicons { 39 40 static int x = 0;41 static int y = 0;42 static int w = 0;43 static int h = 0;44 static double s = 0;45 46 public static void main(String[] args) throws IOException {47 Context context;48 S57map map = null;49 BufferedReader in;50 int line = 0;51 String format = "";52 String file = "";53 String k = "";54 String v = "";55 56 BufferedImage img;57 Graphics2D g2;58 boolean inIcons = false;59 boolean inIcon = false;60 61 if (args.length < 2) {62 System.err.println("Usage: java -jar jicons.jar icon_definition_file icons_directory");63 System.exit(-1);64 }65 in = new BufferedReader(new FileReader(args[0]));66 67 context = new Context();68 String ln;69 while ((ln = in.readLine()) != null) {70 line++;71 if (inIcons) {72 if (inIcon) {73 if (ln.contains("</icon")) {74 inIcon = false;75 map.tagsDone(0);76 // generate icon file77 switch (format) {78 case "PNG":79 img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);80 g2 = img.createGraphics();81 Renderer.reRender(g2, new Rectangle(x, y, w, h), 16, s / Renderer.symbolScale[16], map, context);82 try {83 ImageIO.write(img, "png", new File(args[1] + file + ".png"));84 } catch (Exception e) {85 System.err.println("Line " + line + ": PNG write Exception");86 }87 System.err.println(file + ".png");88 break;89 case "SVG":90 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();91 String svgNS = "http://www.w3.org/2000/svg";92 Document document = domImpl.createDocument(svgNS, "svg", null);93 SVGGraphics2D svgGenerator = new SVGGraphics2D(document);94 svgGenerator.setSVGCanvasSize(new Dimension(w, h));95 Renderer.reRender(svgGenerator, new Rectangle(x, y, w, h), 16, s / Renderer.symbolScale[16], map, context);96 boolean useCSS = true;97 Writer out = null;98 try {99 out = new OutputStreamWriter(new FileOutputStream(args[1] + file + ".svg"), "UTF-8");100 } catch (IOException e1) {101 System.err.println("Line " + line + ": SVG file Exception");102 }103 try {104 svgGenerator.stream(out, useCSS);105 } catch (SVGGraphics2DIOException e) {106 System.err.println("Line " + line + ": SVG write Exception");107 }108 System.err.println(file + ".svg");109 break;110 }111 } else if (ln.contains("<tag")) {112 k = v = "";113 String[] token = ln.split("k=");114 k = token[1].split("[\"\']")[1];115 token = token[1].split("v=");116 v = token[1].split("[\"\']")[1];117 if (k.isEmpty()) {118 System.err.println("Line " + line + ": No key in tag");119 System.exit(-1);120 }121 if (v.isEmpty()) {122 System.err.println("Line " + line + ": No value in tag");123 System.exit(-1);124 }125 map.addTag(k, v);126 }127 } else if (ln.contains("<icon")) {128 inIcon = true;129 h = w = x = y = -1;130 s = 0;131 file = format = "";132 map = new S57map(true);133 map.addNode(0, 0, 0);134 for (String token : ln.split("[ ]+")) {135 if (token.matches("^width=.+")) {136 w = Integer.parseInt(token.split("[\"\']")[1]);137 } else if (token.matches("^height=.+")) {138 h = Integer.parseInt(token.split("[\"\']")[1]);139 } else if (token.matches("^x=.+")) {140 x = Integer.parseInt(token.split("[\"\']")[1]);141 } else if (token.matches("^y=.+")) {142 y = Integer.parseInt(token.split("[\"\']")[1]);143 } else if (token.matches("^scale=.+")) {144 s = Double.parseDouble(token.split("[\"\']")[1]);145 } else if (token.matches("^file=.+")) {146 file = (token.split("[\"\']")[1]);147 } else if (token.matches("^format=.+")) {148 format = (token.split("[\"\']")[1]);149 }150 }151 if (file.isEmpty()) {152 System.err.println("Line " + line + ": No filename");153 System.exit(-1);154 }155 if (format.isEmpty()) {156 System.err.println("Line " + line + ": No format");157 System.exit(-1);158 }159 if ((h < 0) && (w < 0)) {160 System.err.println("Line " + line + ": No icon size");161 System.exit(-1);162 }163 if (w < 0) {164 w = h;165 }166 if (h < 0) {167 h = w;168 }169 if (x < 0) {170 x = w / 2;171 }172 if (y < 0) {173 y = h / 2;174 }175 if (s == 0) {176 s = 1;177 }178 } else if (ln.contains("</icons")) {179 inIcons = false;180 break;181 }182 } else if (ln.contains("<icons")) {183 inIcons = true;184 }185 }186 in.close();187 System.err.println("Finished");188 System.exit(0);189 }190 191 static class Context implements ChartContext {192 193 public Point2D getPoint(Snode coord) {194 return new Point2D.Double(x, y);195 }196 197 public double mile(Feature feature) {198 return Math.min(w, h);199 }200 201 public boolean clip() {202 return false;203 }204 205 public Color background(S57map map) {206 return new Color(0, true);207 }208 209 public RuleSet ruleset() {210 return RuleSet.ALL;211 }212 }39 40 static int x = 0; 41 static int y = 0; 42 static int w = 0; 43 static int h = 0; 44 static double s = 0; 45 46 public static void main(String[] args) throws IOException { 47 Context context; 48 S57map map = null; 49 BufferedReader in; 50 int line = 0; 51 String format = ""; 52 String file = ""; 53 String k = ""; 54 String v = ""; 55 56 BufferedImage img; 57 Graphics2D g2; 58 boolean inIcons = false; 59 boolean inIcon = false; 60 61 if (args.length < 2) { 62 System.err.println("Usage: java -jar jicons.jar icon_definition_file icons_directory"); 63 System.exit(-1); 64 } 65 in = new BufferedReader(new FileReader(args[0])); 66 67 context = new Context(); 68 String ln; 69 while ((ln = in.readLine()) != null) { 70 line++; 71 if (inIcons) { 72 if (inIcon) { 73 if (ln.contains("</icon")) { 74 inIcon = false; 75 map.tagsDone(0); 76 // generate icon file 77 switch (format) { 78 case "PNG": 79 img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 80 g2 = img.createGraphics(); 81 Renderer.reRender(g2, new Rectangle(x, y, w, h), 16, s / Renderer.symbolScale[16], map, context); 82 try { 83 ImageIO.write(img, "png", new File(args[1] + file + ".png")); 84 } catch (Exception e) { 85 System.err.println("Line " + line + ": PNG write Exception"); 86 } 87 System.err.println(file + ".png"); 88 break; 89 case "SVG": 90 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); 91 String svgNS = "http://www.w3.org/2000/svg"; 92 Document document = domImpl.createDocument(svgNS, "svg", null); 93 SVGGraphics2D svgGenerator = new SVGGraphics2D(document); 94 svgGenerator.setSVGCanvasSize(new Dimension(w, h)); 95 Renderer.reRender(svgGenerator, new Rectangle(x, y, w, h), 16, s / Renderer.symbolScale[16], map, context); 96 boolean useCSS = true; 97 Writer out = null; 98 try { 99 out = new OutputStreamWriter(new FileOutputStream(args[1] + file + ".svg"), "UTF-8"); 100 } catch (IOException e1) { 101 System.err.println("Line " + line + ": SVG file Exception"); 102 } 103 try { 104 svgGenerator.stream(out, useCSS); 105 } catch (SVGGraphics2DIOException e) { 106 System.err.println("Line " + line + ": SVG write Exception"); 107 } 108 System.err.println(file + ".svg"); 109 break; 110 } 111 } else if (ln.contains("<tag")) { 112 k = v = ""; 113 String[] token = ln.split("k="); 114 k = token[1].split("[\"\']")[1]; 115 token = token[1].split("v="); 116 v = token[1].split("[\"\']")[1]; 117 if (k.isEmpty()) { 118 System.err.println("Line " + line + ": No key in tag"); 119 System.exit(-1); 120 } 121 if (v.isEmpty()) { 122 System.err.println("Line " + line + ": No value in tag"); 123 System.exit(-1); 124 } 125 map.addTag(k, v); 126 } 127 } else if (ln.contains("<icon")) { 128 inIcon = true; 129 h = w = x = y = -1; 130 s = 0; 131 file = format = ""; 132 map = new S57map(true); 133 map.addNode(0, 0, 0); 134 for (String token : ln.split("[ ]+")) { 135 if (token.matches("^width=.+")) { 136 w = Integer.parseInt(token.split("[\"\']")[1]); 137 } else if (token.matches("^height=.+")) { 138 h = Integer.parseInt(token.split("[\"\']")[1]); 139 } else if (token.matches("^x=.+")) { 140 x = Integer.parseInt(token.split("[\"\']")[1]); 141 } else if (token.matches("^y=.+")) { 142 y = Integer.parseInt(token.split("[\"\']")[1]); 143 } else if (token.matches("^scale=.+")) { 144 s = Double.parseDouble(token.split("[\"\']")[1]); 145 } else if (token.matches("^file=.+")) { 146 file = (token.split("[\"\']")[1]); 147 } else if (token.matches("^format=.+")) { 148 format = (token.split("[\"\']")[1]); 149 } 150 } 151 if (file.isEmpty()) { 152 System.err.println("Line " + line + ": No filename"); 153 System.exit(-1); 154 } 155 if (format.isEmpty()) { 156 System.err.println("Line " + line + ": No format"); 157 System.exit(-1); 158 } 159 if ((h < 0) && (w < 0)) { 160 System.err.println("Line " + line + ": No icon size"); 161 System.exit(-1); 162 } 163 if (w < 0) { 164 w = h; 165 } 166 if (h < 0) { 167 h = w; 168 } 169 if (x < 0) { 170 x = w / 2; 171 } 172 if (y < 0) { 173 y = h / 2; 174 } 175 if (s == 0) { 176 s = 1; 177 } 178 } else if (ln.contains("</icons")) { 179 inIcons = false; 180 break; 181 } 182 } else if (ln.contains("<icons")) { 183 inIcons = true; 184 } 185 } 186 in.close(); 187 System.err.println("Finished"); 188 System.exit(0); 189 } 190 191 static class Context implements ChartContext { 192 193 public Point2D getPoint(Snode coord) { 194 return new Point2D.Double(x, y); 195 } 196 197 public double mile(Feature feature) { 198 return Math.min(w, h); 199 } 200 201 public boolean clip() { 202 return false; 203 } 204 205 public Color background(S57map map) { 206 return new Color(0, true); 207 } 208 209 public RuleSet ruleset() { 210 return RuleSet.ALL; 211 } 212 } 213 213 }
Note:
See TracChangeset
for help on using the changeset viewer.
