55import java .awt .Font ;
66import java .awt .Graphics2D ;
77import java .awt .RenderingHints ;
8- import java .awt .Toolkit ;
98import java .awt .image .BufferedImage ;
109import java .awt .image .DataBufferInt ;
11- import java .io .FileNotFoundException ;
12- import java .io .IOException ;
1310import java .nio .ByteBuffer ;
1411import java .util .Arrays ;
1512
1613import org .apache .batik .transcoder .TranscoderException ;
17- import org .mozilla .javascript .Context ;
18- import org .mozilla .javascript .ImporterTopLevel ;
19- import org .mozilla .javascript .NativeArray ;
20- import org .mozilla .javascript .Scriptable ;
21- import org .mozilla .javascript .ScriptableObject ;
2214
2315public class Painter
2416{
@@ -28,10 +20,9 @@ public class Painter
2820 public static final Color COLOR = Color .WHITE ;
2921 public static final float MARGIN = 10f ;
3022
31- public Painter (String svg )
23+ public Painter (String svg , Dimension dimension , CoordinateText [] texts )
3224 {
3325 boolean rainbow = false ;
34- Dimension dimension = Toolkit .getDefaultToolkit ().getScreenSize ();
3526 try
3627 {
3728 this .image = new BufferedImageTranscoder (svg , (float )dimension .getWidth (), (float )dimension .getHeight ()).getBufferedImage ();
@@ -43,13 +34,17 @@ public Painter(String svg)
4334 }
4435
4536 g2d = image .createGraphics ();
37+ g2d .setColor (Color .BLACK );
38+ g2d .drawRect (0 , 0 , dimension .width , dimension .height );
4639 g2d .setColor (COLOR );
4740 g2d .setFont (FONT );
4841 g2d .setRenderingHint (RenderingHints .KEY_ANTIALIASING , RenderingHints .VALUE_ANTIALIAS_ON );
4942 g2d .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING , RenderingHints .VALUE_TEXT_ANTIALIAS_ON );
5043
5144 if (rainbow )
5245 drawRainbow ();
46+
47+ drawOverlay (texts );
5348 }
5449
5550 public void drawRainbow ()
@@ -82,16 +77,12 @@ public byte[] getImageData()
8277 {
8378 ByteBuffer buffer = ByteBuffer .allocate (image .getWidth () * image .getHeight () * 4 );
8479 Arrays .stream (((DataBufferInt ) image .getData ().getDataBuffer ()).getData ())
85- .forEach (i -> buffer .putInt (Integer .reverseBytes (i )));
80+ .forEach (i -> buffer .putInt (Integer .reverseBytes (i )));
8681 return buffer .array ();
8782 }
88-
89- public void drawOverlay (String js , String version ) throws FileNotFoundException , IOException
83+
84+ private void drawOverlay (CoordinateText [] texts )
9085 {
91- Context context = Context .enter ();
92- Scriptable scope = createScriptable (context , version );
93- CoordinateText [] texts = Arrays .stream (((NativeArray ) context .evaluateReader (scope , new java .io .FileReader (js ), "<cmd>" , 1 , null )).toArray ()).toArray (CoordinateText []::new );
94-
9586 for (CoordinateText text : texts )
9687 {
9788 float width = (float )FONT .getStringBounds (text .getText (), g2d .getFontRenderContext ()).getWidth ();
@@ -109,16 +100,4 @@ public void drawOverlay(String js, String version) throws FileNotFoundException,
109100 }
110101 }
111102 }
112-
113- private Scriptable createScriptable (Context context , String version )
114- {
115- Scriptable scope = new ImporterTopLevel (context );
116- ScriptableObject host = (ScriptableObject ) ScriptableObject .getObjectPrototype (scope );
117- ScriptableObject .putConstProperty (host , "version" , Context .javaToJS (version , scope ));
118- ScriptableObject .putConstProperty (host , "width" , Context .javaToJS (image .getWidth (), scope ));
119- ScriptableObject .putConstProperty (host , "height" , Context .javaToJS (image .getHeight (), scope ));
120- ScriptableObject .putConstProperty (scope , "SVGWall" , Context .javaToJS (host , scope ));
121-
122- return scope ;
123- }
124103}
0 commit comments