Debian Codegraph @

src/tools/org/h2/build/doc/RailroadImages.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.build.doc;
7
8 import java.awt.BasicStroke;
9 import java.awt.Color;
10 import java.awt.Graphics2D;
11 import java.awt.RenderingHints;
12 import java.awt.image.BufferedImage;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.nio.file.Paths;
18
19 import javax.imageio.ImageIO;
20
21 /**
22 * Create the images used in the railroad diagrams.
23 */
24 public class RailroadImages {
25
26 private static final int SIZE = 64;
27 private static final int LINE_REPEAT = 32;
28 private static final int DIV = 2;
29 private static final int STROKE = 6;
30
31 private Path outDir;
32
33 /**
34 * This method is called when executing this application from the command
35 * line.
36 *
37 * @param args the command line parameters
38 * @throws IOException on I/O exception
39 */
40 public static void main(String... args) throws IOException {
41 new RailroadImages().run(Paths.get("docs/html/images"));
42 }
43
44 /**
45 * Create the images.
46 *
47 * @param out the target directory
48 * @throws IOException on I/O exception
49 */
50 void run(Path out) throws IOException {
51 this.outDir = out;
52 Files.createDirectories(outDir);
53 BufferedImage img;
54 Graphics2D g;
55
56 img = new BufferedImage(SIZE * 64, SIZE * LINE_REPEAT,
57 BufferedImage.TYPE_INT_ARGB);
58 g = img.createGraphics();
59 for (int i = 0; i < 2; i++) {
60 setStroke(g, i);
61 g.drawLine(0, SIZE / 2, SIZE * 64, SIZE / 2);
62 }
63 g.dispose();
64 savePng(img, "div-d.png");
65 img = null;
66
67 img = new BufferedImage(SIZE, SIZE * LINE_REPEAT, BufferedImage.TYPE_INT_ARGB);
68 g = img.createGraphics();
69 for (int i = 0; i < 2; i++) {
70 setStroke(g, i);
71 g.drawLine(0, SIZE / 2, SIZE, SIZE / 2);
72 g.drawLine(SIZE / 2, SIZE, SIZE / 2, SIZE * LINE_REPEAT);
73 // g.drawLine(0, SIZE / 2, SIZE / 2, SIZE);
74 g.drawArc(-SIZE / 2, SIZE / 2, SIZE, SIZE, 0, 90);
75 }
76 g.dispose();
77 savePng(img, "div-ts.png");
78 savePng(flipHorizontal(img), "div-te.png");
79 img = null;
80
81 img = new BufferedImage(SIZE, SIZE * LINE_REPEAT, BufferedImage.TYPE_INT_ARGB);
82 g = img.createGraphics();
83 for (int i = 0; i < 2; i++) {
84 setStroke(g, i);
85 g.drawArc(SIZE / 2, -SIZE / 2, SIZE, SIZE, 180, 270);
86 // g.drawLine(SIZE / 2, 0, SIZE, SIZE / 2);
87 }
88 savePng(img, "div-ls.png");
89 savePng(flipHorizontal(img), "div-le.png");
90 for (int i = 0; i < 2; i++) {
91 setStroke(g, i);
92 g.drawArc(SIZE / 2, -SIZE / 2, SIZE, SIZE, 180, 270);
93 g.drawLine(SIZE / 2, 0, SIZE / 2, SIZE * LINE_REPEAT);
94 }
95 g.dispose();
96 savePng(img, "div-ks.png");
97 savePng(flipHorizontal(img), "div-ke.png");
98 img = null;
99 }
100
101 private static void setStroke(Graphics2D g, int i) {
102 if (i == 0) {
103 g.setColor(Color.WHITE);
104 g.setStroke(new BasicStroke(STROKE * 3));
105 } else {
106 g.setColor(Color.BLACK);
107 g.setStroke(new BasicStroke(STROKE));
108 }
109 }
110
111 private void savePng(BufferedImage img, String fileName) {
112 int w = img.getWidth();
113 int h = img.getHeight();
114 BufferedImage smaller = new BufferedImage(w / DIV, h / DIV, img.getType());
115 Graphics2D g = smaller.createGraphics();
116 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
117 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
118 g.drawImage(img, 0, 0, w / DIV, h / DIV, 0, 0, w, h, null);
119 g.dispose();
120 try (OutputStream out = Files.newOutputStream(outDir.resolve(fileName))) {
121 ImageIO.write(smaller, "png", out);
122 } catch (IOException e) {
123 throw new RuntimeException(e);
124 }
125 }
126
127 private static BufferedImage flipHorizontal(BufferedImage img) {
128 int w = img.getWidth(), h = img.getHeight();
129 BufferedImage copy = new BufferedImage(w, h, img.getType());
130 Graphics2D g = copy.createGraphics();
131 g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);
132 g.dispose();
133 return copy;
134 }
135
136 }