17
17
import java .awt .FontFormatException ;
18
18
import java .awt .GraphicsEnvironment ;
19
19
import java .io .ByteArrayOutputStream ;
20
+ import java .io .File ;
21
+ import java .io .FileOutputStream ;
20
22
import java .io .IOException ;
21
23
import java .io .InputStream ;
22
24
import java .io .OutputStream ;
23
25
import java .io .UncheckedIOException ;
24
26
import java .nio .charset .StandardCharsets ;
27
+ import java .nio .file .Files ;
25
28
import java .util .Base64 ;
26
29
import java .util .EnumMap ;
27
30
import java .util .List ;
@@ -168,8 +171,9 @@ String getInputStreamAsString( final String resource ) {
168
171
private void generatePng ( final String dotInput , final OutputStream output ) throws IOException {
169
172
// To make the font available during PNG generation, it needs to be registered
170
173
// 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 );
173
177
final GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment ();
174
178
ge .registerFont ( f );
175
179
} catch ( final FontFormatException e ) {
@@ -180,7 +184,17 @@ private void generatePng( final String dotInput, final OutputStream output ) thr
180
184
final Graphviz graphviz = Graphviz .fromGraph ( g );
181
185
graphviz .render ( guru .nidi .graphviz .engine .Format .PNG ).toOutputStream ( output );
182
186
}
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
+ }
184
198
private String base64EncodeInputStream ( final InputStream in ) throws IOException {
185
199
try ( final ByteArrayOutputStream os = new ByteArrayOutputStream () ) {
186
200
final byte [] buffer = new byte [1024 ];
0 commit comments