Skip to content

Introduces support for AWT components #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions jsvg/src/main/java/com/github/weisj/jsvg/SVGDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.github.weisj.jsvg.geometry.size.MeasureContext;
import com.github.weisj.jsvg.nodes.SVG;
import com.github.weisj.jsvg.renderer.*;
import com.github.weisj.jsvg.renderer.awt.JComponentPlatformSupport;
import com.github.weisj.jsvg.renderer.awt.AwtComponentPlatformSupport;
import com.github.weisj.jsvg.renderer.awt.NullPlatformSupport;
import com.github.weisj.jsvg.renderer.awt.PlatformSupport;

Expand Down Expand Up @@ -73,21 +73,31 @@ public void render(@Nullable JComponent component, @NotNull Graphics2D g) {
render(component, g, null);
}

@Deprecated
public void render(@Nullable JComponent component, @NotNull Graphics2D graphics2D, @Nullable ViewBox bounds) {
render((Component) component, graphics2D, bounds);
}

public void render(@Nullable Component component, @NotNull Graphics2D graphics2D, @Nullable ViewBox bounds) {
PlatformSupport platformSupport = component != null
? new JComponentPlatformSupport(component)
? new AwtComponentPlatformSupport(component)
: new NullPlatformSupport();
renderWithPlatform(platformSupport, graphics2D, bounds);
}

private float computePlatformFontSize(@NotNull PlatformSupport platformSupport, @NotNull Output output) {
return output.contextFontSize().orElseGet(platformSupport::fontSize);
}

public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Graphics2D graphics2D,
@Nullable ViewBox bounds) {
Graphics2D g = (Graphics2D) graphics2D.create();
setupSVGRenderingHints(g);
Output output = new Graphics2DOutput(g);
renderWithPlatform(platformSupport, output, bounds);
output.dispose();
}

private float computePlatformFontSize(@NotNull PlatformSupport platformSupport, @NotNull Output output) {
return output.contextFontSize().orElseGet(platformSupport::fontSize);
}

public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Output output,
@Nullable ViewBox bounds) {
RenderContext context = prepareRenderContext(platformSupport, output, bounds);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* MIT License
*
* Copyright (c) 2024 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.github.weisj.jsvg.renderer.awt;

import java.awt.*;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

import org.jetbrains.annotations.NotNull;

public class AwtComponentPlatformSupport implements PlatformSupport {
protected final @NotNull Component component;

public AwtComponentPlatformSupport(@NotNull Component component) {
this.component = component;
}

@Override
public float fontSize() {
Font font = component.getFont();
if (font != null) return font.getSize2D();
return PlatformSupport.super.fontSize();
}

@Override
public @NotNull TargetSurface targetSurface() {
return component::repaint;
}

@Override
public boolean isLongLived() {
return true;
}

@Override
public @NotNull ImageObserver imageObserver() {
return component;
}

@Override
public @NotNull Image createImage(@NotNull ImageProducer imageProducer) {
return component.createImage(imageProducer);
}

@Override
public String toString() {
return "AwtComponentSupport{" +
"component=" + component +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,18 @@
*/
package com.github.weisj.jsvg.renderer.awt;

import java.awt.*;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

import javax.swing.*;

import org.jetbrains.annotations.NotNull;

public final class JComponentPlatformSupport implements PlatformSupport {

private final @NotNull JComponent component;
/**
* @deprecated Use {@link AwtComponentPlatformSupport} instead.
*/
@Deprecated
public final class JComponentPlatformSupport extends AwtComponentPlatformSupport {

public JComponentPlatformSupport(@NotNull JComponent component) {
this.component = component;
}

@Override
public float fontSize() {
Font font = component.getFont();
if (font != null) return font.getSize2D();
return PlatformSupport.super.fontSize();
}

@Override
public @NotNull TargetSurface targetSurface() {
return component::repaint;
}

@Override
public boolean isLongLived() {
return true;
}

@Override
public @NotNull ImageObserver imageObserver() {
return component;
}

@Override
public @NotNull Image createImage(@NotNull ImageProducer imageProducer) {
return component.createImage(imageProducer);
super(component);
}

@Override
Expand Down
Loading