| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.util; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.AffineTransform; |
| 26 |
import java.awt.geom.Rectangle2D; |
| 27 |
import java.awt.image.BufferedImage; |
| 28 |
import java.util.function.BiConsumer; |
| 29 |
import java.util.function.Consumer; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.NotNull; |
| 32 |
import org.jetbrains.annotations.Nullable; |
| 33 |
|
| 34 |
import com.github.weisj.jsvg.attributes.UnitType; |
| 35 |
import com.github.weisj.jsvg.geometry.util.GeometryUtil; |
| 36 |
import com.github.weisj.jsvg.nodes.SVGNode; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.Instantiator; |
| 38 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 39 |
import com.github.weisj.jsvg.renderer.impl.NodeRenderer; |
| 40 |
import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor; |
| 41 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 42 |
import com.github.weisj.jsvg.renderer.output.impl.Graphics2DOutput; |
| 43 |
import com.github.weisj.jsvg.renderer.output.impl.GraphicsUtil; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public final class BlittableImage { |
| 51 |
|
| 52 |
|
| 53 |
@FunctionalInterface |
| 54 |
public interface BufferSurfaceSupplier { |
| 55 |
@NotNull |
| 56 |
BufferedImage createBufferSurface(@Nullable AffineTransform at, double width, double height); |
| 57 |
} |
| 58 |
|
| 59 |
private final @NotNull BufferedImage image; |
| 60 |
private final @NotNull RenderContext context; |
| 61 |
private final @NotNull Rectangle2D boundsInDeviceSpace; |
| 62 |
private final @NotNull Rectangle2D clippedUserBounds; |
| 63 |
|
| 64 |
private BlittableImage(@NotNull BufferedImage image, @NotNull RenderContext context, |
| 65 |
@NotNull Rectangle2D boundsInDeviceSpace, @NotNull Rectangle2D clippedUserBounds) { |
| 66 |
this.image = image; |
| 67 |
this.context = context; |
| 68 |
this.boundsInDeviceSpace = boundsInDeviceSpace; |
| 69 |
this.clippedUserBounds = clippedUserBounds; |
| 70 |
} |
| 71 |
|
| 72 |
public static @Nullable BlittableImage create(@NotNull BufferSurfaceSupplier bufferSurfaceSupplier, |
| 73 |
@NotNull RenderContext context, @Nullable Rectangle2D clipBounds, |
| 74 |
@NotNull Rectangle2D bounds, @NotNull Rectangle2D objectBounds, @NotNull UnitType contentUnits) { |
| 75 |
RenderContextAccessor.Accessor accessor = RenderContextAccessor.instance(); |
| 76 |
RenderContext imageContext = accessor.createInitial(accessor.currentColor(context), context.platformSupport(), |
| 77 |
contentUnits.deriveMeasure(context.measureContext())); |
| 78 |
return create(bufferSurfaceSupplier, context, clipBounds, bounds, objectBounds, contentUnits, imageContext); |
| 79 |
} |
| 80 |
|
| 81 |
public static @Nullable BlittableImage create(@NotNull BufferSurfaceSupplier bufferSurfaceSupplier, |
| 82 |
@NotNull RenderContext context, @Nullable Rectangle2D clipBounds, |
| 83 |
@NotNull Rectangle2D bounds, @NotNull Rectangle2D objectBounds, @NotNull UnitType contentUnits, |
| 84 |
@NotNull RenderContext imageContext) { |
| 85 |
Rectangle2D boundsInDeviceSpace = GeometryUtil.userBoundsToDeviceBounds(context, bounds); |
| 86 |
|
| 87 |
if (clipBounds != null) { |
| 88 |
Rectangle2D clipBoundsInDeviceSpace = GeometryUtil.userBoundsToDeviceBounds(context, clipBounds); |
| 89 |
Rectangle2D.intersect(clipBoundsInDeviceSpace, boundsInDeviceSpace, boundsInDeviceSpace); |
| 90 |
} |
| 91 |
|
| 92 |
if (ShapeUtil.isInvalidArea(boundsInDeviceSpace)) return null; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
GeometryUtil.adjustForAliasing(boundsInDeviceSpace); |
| 97 |
|
| 98 |
|
| 99 |
Rectangle2D adjustedBoundsInUserSpace = GeometryUtil.convertBounds(context, boundsInDeviceSpace, |
| 100 |
GeometryUtil.Space.DEVICE, GeometryUtil.Space.USER); |
| 101 |
|
| 102 |
BufferedImage img = bufferSurfaceSupplier.createBufferSurface(null, |
| 103 |
boundsInDeviceSpace.getWidth(), |
| 104 |
boundsInDeviceSpace.getHeight()); |
| 105 |
|
| 106 |
AffineTransform rootTransform = new AffineTransform(); |
| 107 |
rootTransform.translate(-boundsInDeviceSpace.getX(), -boundsInDeviceSpace.getY()); |
| 108 |
rootTransform.concatenate(context.rootTransform()); |
| 109 |
|
| 110 |
AffineTransform userSpaceTransform = new AffineTransform(context.userSpaceTransform()); |
| 111 |
if (contentUnits == UnitType.ObjectBoundingBox) { |
| 112 |
userSpaceTransform = new AffineTransform(userSpaceTransform); |
| 113 |
userSpaceTransform.translate(objectBounds.getX(), objectBounds.getY()); |
| 114 |
userSpaceTransform.scale(objectBounds.getWidth(), objectBounds.getHeight()); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
RenderContextAccessor.instance().setRootTransform(imageContext, rootTransform, userSpaceTransform); |
| 120 |
|
| 121 |
return new BlittableImage(img, imageContext, boundsInDeviceSpace, adjustedBoundsInUserSpace); |
| 122 |
} |
| 123 |
|
| 124 |
public @NotNull RenderContext context() { |
| 125 |
return context; |
| 126 |
} |
| 127 |
|
| 128 |
public @NotNull Rectangle2D imageBoundsInDeviceSpace() { |
| 129 |
return boundsInDeviceSpace; |
| 130 |
} |
| 131 |
|
| 132 |
public @NotNull Rectangle2D clippedUserBounds() { |
| 133 |
return clippedUserBounds; |
| 134 |
} |
| 135 |
|
| 136 |
public @NotNull BufferedImage image() { |
| 137 |
return image; |
| 138 |
} |
| 139 |
|
| 140 |
public @NotNull Graphics2D createGraphics() { |
| 141 |
Graphics2D g = GraphicsUtil.createGraphics(image); |
| 142 |
g.transform(context.rootTransform()); |
| 143 |
g.transform(context.userSpaceTransform()); |
| 144 |
return g; |
| 145 |
} |
| 146 |
|
| 147 |
public void renderNode(@NotNull Output parentOutput, @NotNull SVGNode node, |
| 148 |
@NotNull Instantiator instantiator) { |
| 149 |
render(parentOutput, (out, ctx) -> NodeRenderer.renderNode(node, ctx, out, instantiator)); |
| 150 |
} |
| 151 |
|
| 152 |
public void clearBackground(@NotNull Color color) { |
| 153 |
Graphics2D g = image.createGraphics(); |
| 154 |
g.setColor(color); |
| 155 |
g.fillRect(0, 0, image.getWidth(), image.getHeight()); |
| 156 |
g.dispose(); |
| 157 |
} |
| 158 |
|
| 159 |
public void render(@NotNull Output output, @NotNull Consumer<Graphics2D> painter) { |
| 160 |
Graphics2D imgGraphics = createGraphics(); |
| 161 |
imgGraphics.setRenderingHints(output.renderingHints()); |
| 162 |
painter.accept(imgGraphics); |
| 163 |
imgGraphics.dispose(); |
| 164 |
} |
| 165 |
|
| 166 |
public void render(@NotNull Output output, @NotNull BiConsumer<Output, RenderContext> painter) { |
| 167 |
Graphics2D imgGraphics = createGraphics(); |
| 168 |
imgGraphics.setRenderingHints(output.renderingHints()); |
| 169 |
painter.accept(new Graphics2DOutput(imgGraphics), context); |
| 170 |
imgGraphics.dispose(); |
| 171 |
} |
| 172 |
|
| 173 |
public void prepareForBlitting(@NotNull Output output) { |
| 174 |
output.setTransform(AffineTransform.getTranslateInstance( |
| 175 |
boundsInDeviceSpace.getX(), boundsInDeviceSpace.getY())); |
| 176 |
} |
| 177 |
|
| 178 |
public void blitTo(@NotNull Output output) { |
| 179 |
Output out = output.createChild(); |
| 180 |
out.setTransform(AffineTransform.getTranslateInstance( |
| 181 |
boundsInDeviceSpace.getX(), boundsInDeviceSpace.getY())); |
| 182 |
out.drawImage(image); |
| 183 |
out.dispose(); |
| 184 |
} |
| 185 |
|
| 186 |
public void debug(@NotNull Output output) { |
| 187 |
debug(output, true); |
| 188 |
} |
| 189 |
|
| 190 |
public void debug(@NotNull Output output, boolean drawImage) { |
| 191 |
output.debugPaint(g -> { |
| 192 |
g.setComposite(AlphaComposite.SrcOver.derive(0.5f)); |
| 193 |
g.setTransform(AffineTransform.getTranslateInstance( |
| 194 |
boundsInDeviceSpace.getX(), boundsInDeviceSpace.getY())); |
| 195 |
if (drawImage) { |
| 196 |
g.drawImage(image, 0, 0, null); |
| 197 |
} |
| 198 |
g.setColor(Color.MAGENTA); |
| 199 |
g.drawRect(0, 0, image.getWidth(), image.getHeight()); |
| 200 |
}); |
| 201 |
} |
| 202 |
} |