Skip to content

Commit bb2f0a6

Browse files
committed
- tmp font file created from font stream.
1 parent 78de46f commit bb2f0a6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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;
@@ -80,6 +82,8 @@ public String getArtifactFilename( final String aspectName, final Locale languag
8082
static final String GET_ELEMENT_NAME_FUNC = "urn:samm:org.eclipse.esmf.samm:function:2.0.0#getElementName";
8183
static final String GET_NAMESPACE_FUNC = "urn:samm:org.eclipse.esmf.samm:function:2.0.0#getNamespace";
8284

85+
private static final String TMP_FONT_FILE = "/tmp/Font-RobotoCondensed-Regular.tmp";
86+
8387
private final Query boxmodelToDotQuery;
8488
private final BoxModel boxModelNamespace;
8589

@@ -168,8 +172,9 @@ String getInputStreamAsString( final String resource ) {
168172
private void generatePng( final String dotInput, final OutputStream output ) throws IOException {
169173
// To make the font available during PNG generation, it needs to be registered
170174
// in Java Runtime's graphics environment
171-
try ( final InputStream fontStream = getInputStream( FONT_FILE ) ) {
172-
final Font f = Font.createFont( Font.TRUETYPE_FONT, fontStream );
175+
try{
176+
File tmpFontFile = gnererateTmpFontFile();
177+
final Font f = Font.createFont( Font.TRUETYPE_FONT, tmpFontFile );
173178
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
174179
ge.registerFont( f );
175180
} catch ( final FontFormatException e ) {
@@ -180,7 +185,18 @@ private void generatePng( final String dotInput, final OutputStream output ) thr
180185
final Graphviz graphviz = Graphviz.fromGraph( g );
181186
graphviz.render( guru.nidi.graphviz.engine.Format.PNG ).toOutputStream( output );
182187
}
183-
188+
private File gnererateTmpFontFile() throws IOException {
189+
File tempFontFile = new File(TMP_FONT_FILE);
190+
if (!tempFontFile.exists()){
191+
try (InputStream fontStream = getInputStream(FONT_FILE)){
192+
try (OutputStream output = new FileOutputStream(tempFontFile, false)){
193+
fontStream.transferTo(output);
194+
}
195+
}
196+
}
197+
tempFontFile.deleteOnExit();
198+
return tempFontFile;
199+
}
184200
private String base64EncodeInputStream( final InputStream in ) throws IOException {
185201
try ( final ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
186202
final byte[] buffer = new byte[1024];

0 commit comments

Comments
 (0)