Skip to content

Commit 74f2412

Browse files
authored
Merge pull request #377 from Cofinity-X/feature/CXCS-422-create-only-one-font-tmp-file
Write temporary font file when generating diagrams. Fixes #371 .
2 parents ac9f524 + b42b784 commit 74f2412

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/diagram/AspectModelDiagramGenerator.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import java.awt.FontFormatException;
1818
import java.awt.GraphicsEnvironment;
1919
import java.io.ByteArrayOutputStream;
20+
import java.io.File;
21+
import java.io.FileOutputStream;
2022
import java.io.IOException;
2123
import java.io.InputStream;
2224
import java.io.OutputStream;
2325
import java.io.UncheckedIOException;
2426
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
2528
import java.util.Base64;
2629
import java.util.EnumMap;
2730
import java.util.List;
@@ -168,8 +171,9 @@ String getInputStreamAsString( final String resource ) {
168171
private void generatePng( final String dotInput, final OutputStream output ) throws IOException {
169172
// To make the font available during PNG generation, it needs to be registered
170173
// in Java Runtime's graphics environment
171-
try ( final InputStream fontStream = getInputStream( FONT_FILE ) ) {
172-
final Font f = Font.createFont( Font.TRUETYPE_FONT, fontStream );
174+
try{
175+
final File tmpFontFile = generateTmpFontFile();
176+
final Font f = Font.createFont( Font.TRUETYPE_FONT, tmpFontFile );
173177
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
174178
ge.registerFont( f );
175179
} catch ( final FontFormatException e ) {
@@ -180,7 +184,17 @@ private void generatePng( final String dotInput, final OutputStream output ) thr
180184
final Graphviz graphviz = Graphviz.fromGraph( g );
181185
graphviz.render( guru.nidi.graphviz.engine.Format.PNG ).toOutputStream( output );
182186
}
183-
187+
private File generateTmpFontFile() throws IOException {
188+
File tempFontFile = new File( System.getProperty( "java.io.tmpdir" ) + File.separator + "aspect-model-diagram.tmp" );
189+
if ( !tempFontFile.exists() ){
190+
try ( final InputStream fontStream = getInputStream( FONT_FILE );
191+
final OutputStream output = new FileOutputStream( tempFontFile, false ) ){
192+
fontStream.transferTo( output );
193+
}
194+
}
195+
tempFontFile.deleteOnExit();
196+
return tempFontFile;
197+
}
184198
private String base64EncodeInputStream( final InputStream in ) throws IOException {
185199
try ( final ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
186200
final byte[] buffer = new byte[1024];

0 commit comments

Comments
 (0)