Skip to content

Commit 0088c5d

Browse files
committed
set headless mode per default
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 9c2b599 commit 0088c5d

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/engine/AbstractJsGraphvizEngine.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public abstract class AbstractJsGraphvizEngine extends AbstractGraphvizEngine {
3131
private static final Pattern FONT_NAME_PATTERN = Pattern.compile("\"?fontname\"?\\s*=\\s*\"?(.*?)[\",;\\]]");
3232
private static final Map<Class<?>, ThreadLocal<EngineState>> ENGINES = new HashMap<>();
3333
private final Supplier<JavascriptEngine> engineSupplier;
34-
private final FontMeasurer fontMeasurer = new FontMeasurer();
3534

3635
protected AbstractJsGraphvizEngine(boolean sync, Supplier<JavascriptEngine> engineSupplier) {
3736
super(sync);
@@ -113,7 +112,7 @@ private void measureFonts(String src) {
113112
final Matcher matcher = FONT_NAME_PATTERN.matcher(src);
114113
while (matcher.find()) {
115114
final String font = matcher.group(1).trim();
116-
final double[] widths = fontMeasurer.measureFont(font);
115+
final double[] widths = FontMeasurer.measureFont(font);
117116
if (widths.length > 0) {
118117
final String widthsString = Arrays.stream(widths).mapToObj(Double::toString).collect(joining(","));
119118
engine().executeJavascript("initViz().setFontWidth('" + font + "',[" + widthsString + "])");

graphviz-java/src/main/java/guru/nidi/graphviz/engine/FontMeasurer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
import java.util.HashSet;
2222
import java.util.Set;
2323

24-
class FontMeasurer {
24+
final class FontMeasurer {
2525
private static final FontRenderContext FONT_RENDER_CONTEXT =
2626
new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR).createGraphics().getFontRenderContext();
2727
private static final double COURIER_WIDTH = .5999;
2828
private static final Font COURIER = new Font("Courier", Font.PLAIN, 10);
2929
private static final double COURIER_SPACE_WIDTH = charWidth(COURIER, ' ');
3030
private static final double COURIER_BORDER_WIDTH = borderWidth(COURIER);
3131
private static final double[] COURIER_WIDTHS = courierWidths();
32-
private final Set<String> fonts = new HashSet<>();
32+
private static final Set<String> FONTS = new HashSet<>();
33+
34+
private FontMeasurer() {
35+
}
3336

3437
private static double[] courierWidths() {
3538
double[] w = new double[256];
@@ -47,11 +50,11 @@ private static double borderWidth(Font font) {
4750
return font.createGlyphVector(FONT_RENDER_CONTEXT, new char[]{56, 56}).getVisualBounds().getWidth();
4851
}
4952

50-
double[] measureFont(String name) {
51-
if (fonts.contains(name)) {
53+
static double[] measureFont(String name) {
54+
if (FONTS.contains(name)) {
5255
return new double[0];
5356
}
54-
fonts.add(name);
57+
FONTS.add(name);
5558
final Font font = new Font(name, Font.PLAIN, 10);
5659
final double spaceWidth = charWidth(font, ' ');
5760
final double borderWidth = borderWidth(font);

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Graphviz.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
import static java.util.regex.Pattern.CASE_INSENSITIVE;
3838

3939
public final class Graphviz {
40+
static {
41+
if (System.getProperty("java.awt.headless") == null) {
42+
System.setProperty("java.awt.headless", "true");
43+
}
44+
}
45+
4046
private static final Logger LOG = LoggerFactory.getLogger(Graphviz.class);
4147

4248
private static final Pattern DPI_PATTERN = Pattern.compile("\"?dpi\"?\\s*=\\s*\"?([0-9.]+)\"?", CASE_INSENSITIVE);
@@ -184,6 +190,10 @@ private static void doReleaseEngine(GraphvizEngine engine) {
184190
}
185191
}
186192

193+
public static void noHeadless() {
194+
System.setProperty("java.awt.headless", "false");
195+
}
196+
187197
public static Graphviz fromFile(File src) throws IOException {
188198
try (final InputStream in = new FileInputStream(src)) {
189199
return fromString(readAsString(in)).basedir(src.getAbsoluteFile().getParentFile());

0 commit comments

Comments
 (0)