Skip to content

Commit fd6ef02

Browse files
authored
JavaScript code refactor
1 parent 01ec405 commit fd6ef02

File tree

6 files changed

+68
-52
lines changed

6 files changed

+68
-52
lines changed

assets/svgwall.appdata.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
<internet>always</internet>
3939
</supports>
4040
<releases>
41+
<release version="20240625.1" date="2024-06-25">
42+
<description>
43+
<p>JavaScipt code refactor</p>
44+
</description>
45+
</release>
4146
<release version="20240624.1" date="2024-06-24">
4247
<description>
4348
<p>Mozilla Rhino included for text overlay scripting</p>

assets/svgwall.desktop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ Terminal=true
88
MimeType=image/svg+xml
99
Comment=SVG Wallpaper Utility for Xorg
1010
X-AppImage-Name=SVGWall
11-
X-AppImage-Version=20240624.1
11+
X-AppImage-Version=20240625.1
1212
X-AppImage-Arch=x86_64

overlay.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
importPackage(Packages.com.grimpirate);
2-
importPackage(java.lang);
3-
importPackage(java.time);
4-
importPackage(java.time.format);
52

63
const MARGIN = 10;
74
[
@@ -12,17 +9,17 @@ const MARGIN = 10;
129
{
1310
x: MARGIN,
1411
y: 0,
15-
t: `${s('java.runtime.name')} ${s('java.runtime.version')}`,
12+
t: SVGWall.jvm,
1613
a: Anchor.LEFT
1714
},{
1815
x: MARGIN,
1916
y: -18,
20-
t: s('os.version'),
17+
t: SVGWall.os_ver,
2118
a: Anchor.LEFT
2219
},{
2320
x: MARGIN,
2421
y: - 2 * 18,
25-
t: `${s('os.name')} ${s('os.arch')}`,
22+
t: SVGWall.os_arch,
2623
a: Anchor.LEFT
2724
},{
2825
x: SVGWall.width * 0.5,
@@ -32,7 +29,7 @@ const MARGIN = 10;
3229
},{
3330
x: SVGWall.width - MARGIN,
3431
y: 0,
35-
t: t(),
32+
t: SVGWall.time,
3633
a: Anchor.RIGHT
3734
},{
3835
x: SVGWall.width - MARGIN,
@@ -46,13 +43,3 @@ const MARGIN = 10;
4643
//////////////////////////////
4744
]
4845
.map(a => 'a' in a ? new CoordinateText(a.x, SVGWall.height - MARGIN + a.y, a.t, a.a) : new CoordinateText(SVGWall.height - MARGIN + a.x, a.y, a.t));
49-
50-
function s(p)
51-
{
52-
return System.getProperty(p);
53-
}
54-
55-
function t()
56-
{
57-
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
58-
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.grimpirate;
2+
3+
import java.awt.Dimension;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.time.LocalDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.util.Arrays;
9+
10+
import org.mozilla.javascript.Context;
11+
import org.mozilla.javascript.ImporterTopLevel;
12+
import org.mozilla.javascript.NativeArray;
13+
import org.mozilla.javascript.Scriptable;
14+
import org.mozilla.javascript.ScriptableObject;
15+
16+
public class JavaScript
17+
{
18+
private CoordinateText[] texts;
19+
20+
public JavaScript(String js, String version, Dimension dimension) throws FileNotFoundException, IOException
21+
{
22+
Context context = Context.enter();
23+
Scriptable scope = new ImporterTopLevel(context);
24+
ScriptableObject host = (ScriptableObject) ScriptableObject.getObjectPrototype(scope);
25+
ScriptableObject.putConstProperty(host, "version", Context.javaToJS(version, scope));
26+
ScriptableObject.putConstProperty(host, "width", Context.javaToJS(dimension.getWidth(), scope));
27+
ScriptableObject.putConstProperty(host, "height", Context.javaToJS(dimension.getHeight(), scope));
28+
ScriptableObject.putConstProperty(host, "jvm", Context.javaToJS(System.getProperty("java.runtime.name") + " " + System.getProperty("java.runtime.version"), scope));
29+
ScriptableObject.putConstProperty(host, "os_ver", Context.javaToJS(System.getProperty("os.version"), scope));
30+
ScriptableObject.putConstProperty(host, "os_arch", Context.javaToJS(System.getProperty("os.name") + " " + System.getProperty("os.arch"), scope));
31+
ScriptableObject.putConstProperty(host, "time", Context.javaToJS(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), scope));
32+
ScriptableObject.putConstProperty(scope, "SVGWall", Context.javaToJS(host, scope));
33+
CoordinateText[] texts = Arrays.stream(((NativeArray) context.evaluateReader(scope, new java.io.FileReader(js), "<cmd>", 1, null)).toArray()).toArray(CoordinateText[]::new);
34+
Context.exit();
35+
this.texts = texts;
36+
}
37+
38+
public CoordinateText[] getText()
39+
{
40+
return texts;
41+
}
42+
}

src_java/com/grimpirate/Painter.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@
55
import java.awt.Font;
66
import java.awt.Graphics2D;
77
import java.awt.RenderingHints;
8-
import java.awt.Toolkit;
98
import java.awt.image.BufferedImage;
109
import java.awt.image.DataBufferInt;
11-
import java.io.FileNotFoundException;
12-
import java.io.IOException;
1310
import java.nio.ByteBuffer;
1411
import java.util.Arrays;
1512

1613
import 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

2315
public 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
}

src_java/com/grimpirate/SVGWall.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.grimpirate;
22

3+
import java.awt.Dimension;
4+
import java.awt.Toolkit;
35
import java.io.IOException;
46

57
import javax.xml.parsers.ParserConfigurationException;
@@ -25,12 +27,13 @@ public static void main(String... args) throws PicocliException, SAXException, I
2527
ParseResult result = (new Shell(args)).getParseResult();
2628

2729
String svg = result.matchedOptionValue("--svg", "");
28-
String js = result.matchedOptionValue("--overlay", null);
30+
String js = result.matchedOptionValue("--overlay", "");
31+
32+
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
33+
34+
JavaScript script = new JavaScript(js, result.commandSpec().version()[0], dimension);
2935

30-
Painter painter = new Painter(svg);
31-
32-
if(null != js)
33-
painter.drawOverlay(js, result.commandSpec().version()[0]);
36+
Painter painter = new Painter(svg, dimension, script.getText());
3437

3538
apply(painter.getImageData());
3639
}

0 commit comments

Comments
 (0)