Skip to content

Commit cb7ffa8

Browse files
committed
Introduces support for AWT's component
Fixes #76
1 parent 1bbe103 commit cb7ffa8

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

jsvg/src/main/java/com/github/weisj/jsvg/SVGDocument.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import javax.swing.*;
2929

30+
import com.github.weisj.jsvg.renderer.awt.AwtComponentPlatformSupport;
3031
import org.jetbrains.annotations.NotNull;
3132
import org.jetbrains.annotations.Nullable;
3233

@@ -74,20 +75,29 @@ public void render(@Nullable JComponent component, @NotNull Graphics2D g) {
7475
}
7576

7677
public void render(@Nullable JComponent component, @NotNull Graphics2D graphics2D, @Nullable ViewBox bounds) {
78+
render(component, graphics2D, bounds);
79+
}
80+
81+
public void render(@Nullable Component component, @NotNull Graphics2D graphics2D, @Nullable ViewBox bounds) {
7782
PlatformSupport platformSupport = component != null
78-
? new JComponentPlatformSupport(component)
83+
? new AwtComponentPlatformSupport(component)
7984
: new NullPlatformSupport();
85+
renderWithPlatform(platformSupport, graphics2D, bounds);
86+
}
87+
88+
private float computePlatformFontSize(@NotNull PlatformSupport platformSupport, @NotNull Output output) {
89+
return output.contextFontSize().orElseGet(platformSupport::fontSize);
90+
}
91+
92+
public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Graphics2D graphics2D,
93+
@Nullable ViewBox bounds) {
8094
Graphics2D g = (Graphics2D) graphics2D.create();
8195
setupSVGRenderingHints(g);
8296
Output output = new Graphics2DOutput(g);
8397
renderWithPlatform(platformSupport, output, bounds);
8498
output.dispose();
8599
}
86100

87-
private float computePlatformFontSize(@NotNull PlatformSupport platformSupport, @NotNull Output output) {
88-
return output.contextFontSize().orElseGet(platformSupport::fontSize);
89-
}
90-
91101
public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Output output,
92102
@Nullable ViewBox bounds) {
93103
RenderContext context = prepareRenderContext(platformSupport, output, bounds);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.weisj.jsvg.renderer.awt;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.awt.*;
6+
import java.awt.image.ImageObserver;
7+
import java.awt.image.ImageProducer;
8+
9+
public class AwtComponentPlatformSupport implements PlatformSupport {
10+
protected final @NotNull Component component;
11+
12+
public AwtComponentPlatformSupport(@NotNull Component component) {
13+
this.component = component;
14+
}
15+
16+
@Override
17+
public float fontSize() {
18+
Font font = component.getFont();
19+
if (font != null) return font.getSize2D();
20+
return PlatformSupport.super.fontSize();
21+
}
22+
23+
@Override
24+
public @NotNull TargetSurface targetSurface() {
25+
return component::repaint;
26+
}
27+
28+
@Override
29+
public boolean isLongLived() {
30+
return true;
31+
}
32+
33+
@Override
34+
public @NotNull ImageObserver imageObserver() {
35+
return component;
36+
}
37+
38+
@Override
39+
public @NotNull Image createImage(@NotNull ImageProducer imageProducer) {
40+
return component.createImage(imageProducer);
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "AwtComponentSupport{" +
46+
"component=" + component +
47+
'}';
48+
}
49+
}

jsvg/src/main/java/com/github/weisj/jsvg/renderer/awt/JComponentPlatformSupport.java

+2-31
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,10 @@
2929

3030
import org.jetbrains.annotations.NotNull;
3131

32-
public final class JComponentPlatformSupport implements PlatformSupport {
33-
34-
private final @NotNull JComponent component;
32+
public final class JComponentPlatformSupport extends AwtComponentPlatformSupport {
3533

3634
public JComponentPlatformSupport(@NotNull JComponent component) {
37-
this.component = component;
38-
}
39-
40-
@Override
41-
public float fontSize() {
42-
Font font = component.getFont();
43-
if (font != null) return font.getSize2D();
44-
return PlatformSupport.super.fontSize();
45-
}
46-
47-
@Override
48-
public @NotNull TargetSurface targetSurface() {
49-
return component::repaint;
50-
}
51-
52-
@Override
53-
public boolean isLongLived() {
54-
return true;
55-
}
56-
57-
@Override
58-
public @NotNull ImageObserver imageObserver() {
59-
return component;
60-
}
61-
62-
@Override
63-
public @NotNull Image createImage(@NotNull ImageProducer imageProducer) {
64-
return component.createImage(imageProducer);
35+
super(component);
6536
}
6637

6738
@Override

0 commit comments

Comments
 (0)